lsst.afw  16.0-19-g2da375352+1
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 
23 import lsst.utils.tests
24 import difflib
25 from .schema import Schema
26 
27 __all__ = ["assertSchemasEqual", "diffSchemas", "joinWords"]
28 
29 
30 def joinWords(items):
31  """Join a sequence of words into a comma-separated, 'and'-finalized
32  string with correct English syntax.
33  """
34  if len(items) == 1:
35  result = items[0]
36  elif len(items) == 2:
37  result = "%s and %s" % tuple(items)
38  else:
39  result = "%s, and %s" % (", ".join(items[:-1]), items[-1])
40  return result
41 
42 
43 def diffSchemas(schema1, schema2, flags=Schema.IDENTICAL):
44  """Return a string diff of two schemas.
45 
46  Parameters
47  ----------
48  schema1 : :py:class:`lsst.afw.table.Schema`
49  First schema to diff. Items appearing only in this schema
50  will be prefixed with "-" in the diff.
51  schema2 : :py:class:`lsst.afw.table.Schema`
52  Second schema to diff. Items appearing only in this schema
53  will be prefixed with "-" in the diff.
54  flags : `int`
55  A bitwise OR of :py:class:`lsst.afw.table.Schema.ComparisonFlags`
56  indicating which features of schema items to compare. The returned
57  diff will always show all differences, but no diff will be shown if
58  the only differences are not included in the flags. Default is
59  `lsst.afw.table.Schema.IDENTICAL`, which checks everything.
60 
61  Returns
62  -------
63  diff : `str`
64  A "unified diff" string representation of the difference between the
65  schemas, or an empty string if there is no difference.
66  """
67  result = schema1.compare(schema2, flags)
68  if result == flags:
69  return ""
70  components = []
71  if not result & Schema.EQUAL_KEYS:
72  components.append("keys")
73  if not result & Schema.EQUAL_NAMES:
74  components.append("names")
75  if not result & Schema.EQUAL_DOCS:
76  components.append("docs")
77  if not result & Schema.EQUAL_UNITS:
78  components.append("units")
79  if not result & Schema.EQUAL_ALIASES:
80  components.append("aliases")
81  diff = "\n".join(difflib.unified_diff(
82  str(schema1).split("\n"), str(schema2).split("\n")))
83  return "%s differ:\n%s" % (joinWords(components).capitalize(), diff)
84 
85 
86 @lsst.utils.tests.inTestCase
87 def assertSchemasEqual(testCase, schema1, schema2, flags=Schema.IDENTICAL):
88  """Assert that two Schemas are equal.
89 
90  Generates a message from the difference between the schemas; see
91  :py:func:`diffSchemas` for more information.
92  """
93  msg = diffSchemas(schema1, schema2, flags=flags)
94  if msg:
95  testCase.fail(msg)
def diffSchemas(schema1, schema2, flags=Schema.IDENTICAL)
Definition: testUtils.py:43
def assertSchemasEqual(testCase, schema1, schema2, flags=Schema.IDENTICAL)
Definition: testUtils.py:87