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