lsst.afw  14.0-25-g0e8fc950e+7
testUtils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2016 AURA/LSST.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <https://www.lsstcorp.org/LegalNotices/>.
21 #
22 from __future__ import absolute_import, division, print_function
23 
24 import lsst.utils.tests
25 import difflib
26 from .schema import Schema
27 
28 __all__ = ["assertSchemasEqual", "diffSchemas", "joinWords"]
29 
30 
31 def joinWords(items):
32  """Join a sequence of words into a comma-separated, 'and'-finalized
33  string with correct English syntax.
34  """
35  if len(items) == 1:
36  result = items[0]
37  elif len(items) == 2:
38  result = "%s and %s" % tuple(items)
39  else:
40  result = "%s, and %s" % (", ".join(items[:-1]), items[-1])
41  return result
42 
43 
44 def diffSchemas(schema1, schema2, flags=Schema.IDENTICAL):
45  """Return a string diff of two schemas.
46 
47  Parameters
48  ----------
49  schema1 : :py:class:`lsst.afw.table.Schema`
50  First schema to diff. Items appearing only in this schema
51  will be prefixed with "-" in the diff.
52  schema2 : :py:class:`lsst.afw.table.Schema`
53  Second schema to diff. Items appearing only in this schema
54  will be prefixed with "-" in the diff.
55  flags : `int`
56  A bitwise OR of :py:class:`lsst.afw.table.Schema.ComparisonFlags`
57  indicating which features of schema items to compare. The returned
58  diff will always show all differences, but no diff will be shown if
59  the only differences are not included in the flags. Default is
60  `lsst.afw.table.Schema.IDENTICAL`, which checks everything.
61 
62  Returns
63  -------
64  diff : `str`
65  A "unified diff" string representation of the difference between the
66  schemas, or an empty string if there is no difference.
67  """
68  result = schema1.compare(schema2, flags)
69  if result == flags:
70  return ""
71  components = []
72  if not result & Schema.EQUAL_KEYS:
73  components.append("keys")
74  if not result & Schema.EQUAL_NAMES:
75  components.append("names")
76  if not result & Schema.EQUAL_DOCS:
77  components.append("docs")
78  if not result & Schema.EQUAL_UNITS:
79  components.append("units")
80  if not result & Schema.EQUAL_ALIASES:
81  components.append("aliases")
82  diff = "\n".join(difflib.unified_diff(
83  str(schema1).split("\n"), str(schema2).split("\n")))
84  return "%s differ:\n%s" % (joinWords(components).capitalize(), diff)
85 
86 
87 @lsst.utils.tests.inTestCase
88 def assertSchemasEqual(testCase, schema1, schema2, flags=Schema.IDENTICAL):
89  """Assert that two Schemas are equal.
90 
91  Generates a message from the difference between the schemas; see
92  :py:func:`diffSchemas` for more information.
93  """
94  msg = diffSchemas(schema1, schema2, flags=flags)
95  if msg:
96  testCase.fail(msg)
def diffSchemas(schema1, schema2, flags=Schema.IDENTICAL)
Definition: testUtils.py:44
def assertSchemasEqual(testCase, schema1, schema2, flags=Schema.IDENTICAL)
Definition: testUtils.py:88