diff options
author | Julian Berman <Julian@GrayVines.com> | 2021-07-09 15:24:59 +0200 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2021-07-09 17:53:32 +0200 |
commit | be4287cbce85d9089decee3eeb8744fc4d3a79fd (patch) | |
tree | 0f24a75d8a381bdc699527fcd7575e9be84543c6 | |
parent | 4398265201fe2375dfeba773fbe5c13fe33397ff (diff) | |
download | jsonschema-be4287cbce85d9089decee3eeb8744fc4d3a79fd.tar.gz |
Test only through _util.equal.
All of this is private, so the unit tests are for our own
benefit -- we may as well stop before getting too deep into
the helper function stack.
-rw-r--r-- | jsonschema/_utils.py | 32 | ||||
-rw-r--r-- | jsonschema/tests/test_utils.py | 73 |
2 files changed, 45 insertions, 60 deletions
diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py index f026c33..6c29dcf 100644 --- a/jsonschema/_utils.py +++ b/jsonschema/_utils.py @@ -158,9 +158,9 @@ def ensure_list(thing): return thing -def dict_equal(one, two): +def _mapping_equal(one, two): """ - Check if two dicts are the same using `equal` + Check if two mappings are equal using the semantics of `equal`. """ if len(one.keys()) != len(two.keys()): return False @@ -174,9 +174,9 @@ def dict_equal(one, two): return True -def list_equal(one, two): +def _sequence_equal(one, two): """ - Check if two lists are the same using `equal` + Check if two sequences are equal using the semantics of `equal`. """ if len(one) != len(two): return False @@ -188,23 +188,19 @@ def list_equal(one, two): return True -def is_sequence(instance): - """ - Checks if an instance is a sequence but not a string - """ - return isinstance(instance, Sequence) and not isinstance(instance, str) - - def equal(one, two): """ - Check if two things are equal, but evade booleans and ints being equal. - """ - if is_sequence(one) and is_sequence(two): - return list_equal(one, two) + Check if two things are equal evading some Python type hierarchy semantics. + Specifically in JSON Schema, evade `bool` inheriting from `int`, + recursing into sequences to do the same. + """ + if isinstance(one, str) or isinstance(two, str): + return one == two + if isinstance(one, Sequence) and isinstance(two, Sequence): + return _sequence_equal(one, two) if isinstance(one, Mapping) and isinstance(two, Mapping): - return dict_equal(one, two) - + return _mapping_equal(one, two) return unbool(one) == unbool(two) @@ -232,7 +228,7 @@ def uniq(container): sliced = itertools.islice(sort, 1, None) for i, j in zip(sort, sliced): - return not list_equal(i, j) + return not _sequence_equal(i, j) except (NotImplementedError, TypeError): seen = [] diff --git a/jsonschema/tests/test_utils.py b/jsonschema/tests/test_utils.py index 9efadd6..4e542b9 100644 --- a/jsonschema/tests/test_utils.py +++ b/jsonschema/tests/test_utils.py @@ -1,135 +1,124 @@ from unittest import TestCase -from jsonschema._utils import dict_equal, list_equal +from jsonschema._utils import equal -class TestDictEqual(TestCase): +class TestEqual(TestCase): + def test_none(self): + self.assertTrue(equal(None, None)) + +class TestDictEqual(TestCase): def test_equal_dictionaries(self): dict_1 = {"a": "b", "c": "d"} dict_2 = {"c": "d", "a": "b"} - self.assertTrue(dict_equal(dict_1, dict_2)) + self.assertTrue(equal(dict_1, dict_2)) def test_missing_key(self): dict_1 = {"a": "b", "c": "d"} dict_2 = {"c": "d", "x": "b"} - self.assertFalse(dict_equal(dict_1, dict_2)) + self.assertFalse(equal(dict_1, dict_2)) def test_additional_key(self): dict_1 = {"a": "b", "c": "d"} dict_2 = {"c": "d", "a": "b", "x": "x"} - self.assertFalse(dict_equal(dict_1, dict_2)) + self.assertFalse(equal(dict_1, dict_2)) def test_missing_value(self): dict_1 = {"a": "b", "c": "d"} dict_2 = {"c": "d", "a": "x"} - self.assertFalse(dict_equal(dict_1, dict_2)) + self.assertFalse(equal(dict_1, dict_2)) def test_empty_dictionaries(self): dict_1 = {} dict_2 = {} - self.assertTrue(dict_equal(dict_1, dict_2)) + self.assertTrue(equal(dict_1, dict_2)) def test_one_none(self): dict_1 = None dict_2 = {"a": "b", "c": "d"} - with self.assertRaises(AttributeError): - self.assertFalse(dict_equal(dict_1, dict_2)) - with self.assertRaises(AttributeError): - self.assertFalse(dict_equal(dict_1, dict_2)) - - def test_both_none(self): - with self.assertRaises(AttributeError): - self.assertFalse(dict_equal(None, None)) + self.assertFalse(equal(dict_1, dict_2)) def test_same_item(self): dict_1 = {"a": "b", "c": "d"} - self.assertTrue(dict_equal(dict_1, dict_1)) + self.assertTrue(equal(dict_1, dict_1)) - def test_nested_dict_equal(self): + def test_nested_equal(self): dict_1 = {"a": {"a": "b", "c": "d"}, "c": "d"} dict_2 = {"c": "d", "a": {"a": "b", "c": "d"}} - self.assertTrue(dict_equal(dict_1, dict_2)) + self.assertTrue(equal(dict_1, dict_2)) def test_nested_dict_unequal(self): dict_1 = {"a": {"a": "b", "c": "d"}, "c": "d"} dict_2 = {"c": "d", "a": {"a": "b", "c": "x"}} - self.assertFalse(dict_equal(dict_1, dict_2)) + self.assertFalse(equal(dict_1, dict_2)) - def test_nested_list_equal(self): + def test_mixed_nested_equal(self): dict_1 = {"a": ["a", "b", "c", "d"], "c": "d"} dict_2 = {"c": "d", "a": ["a", "b", "c", "d"]} - self.assertTrue(dict_equal(dict_1, dict_2)) + self.assertTrue(equal(dict_1, dict_2)) def test_nested_list_unequal(self): dict_1 = {"a": ["a", "b", "c", "d"], "c": "d"} dict_2 = {"c": "d", "a": ["b", "c", "d", "a"]} - self.assertFalse(dict_equal(dict_1, dict_2)) + self.assertFalse(equal(dict_1, dict_2)) class TestListEqual(TestCase): - def test_equal_lists(self): list_1 = ["a", "b", "c"] list_2 = ["a", "b", "c"] - self.assertTrue(list_equal(list_1, list_2)) + self.assertTrue(equal(list_1, list_2)) def test_unsorted_lists(self): list_1 = ["a", "b", "c"] list_2 = ["b", "b", "a"] - self.assertFalse(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) def test_first_list_larger(self): list_1 = ["a", "b", "c"] list_2 = ["a", "b"] - self.assertFalse(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) def test_second_list_larger(self): list_1 = ["a", "b"] list_2 = ["a", "b", "c"] - self.assertFalse(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) def test_list_with_none_unequal(self): list_1 = ["a", "b", None] list_2 = ["a", "b", "c"] - self.assertFalse(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) list_1 = ["a", "b", None] list_2 = [None, "b", "c"] - self.assertFalse(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) def test_list_with_none_equal(self): list_1 = ["a", None, "c"] list_2 = ["a", None, "c"] - self.assertTrue(list_equal(list_1, list_2)) + self.assertTrue(equal(list_1, list_2)) def test_empty_list(self): list_1 = [] list_2 = [] - self.assertTrue(list_equal(list_1, list_2)) + self.assertTrue(equal(list_1, list_2)) def test_one_none(self): list_1 = None list_2 = [] - with self.assertRaises(TypeError): - self.assertTrue(list_equal(list_1, list_2)) - - def test_both_none(self): - list_1 = None - list_2 = None - with self.assertRaises(TypeError): - self.assertTrue(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) def test_same_list(self): list_1 = ["a", "b", "c"] - self.assertTrue(list_equal(list_1, list_1)) + self.assertTrue(equal(list_1, list_1)) def test_equal_nested_lists(self): list_1 = ["a", ["b", "c"], "d"] list_2 = ["a", ["b", "c"], "d"] - self.assertTrue(list_equal(list_1, list_2)) + self.assertTrue(equal(list_1, list_2)) def test_unequal_nested_lists(self): list_1 = ["a", ["b", "c"], "d"] list_2 = ["a", [], "c"] - self.assertFalse(list_equal(list_1, list_2)) + self.assertFalse(equal(list_1, list_2)) |