test_utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from math import nan
  2. from unittest import TestCase
  3. from jsonschema._utils import equal
  4. class TestEqual(TestCase):
  5. def test_none(self):
  6. self.assertTrue(equal(None, None))
  7. def test_nan(self):
  8. self.assertTrue(equal(nan, nan))
  9. class TestDictEqual(TestCase):
  10. def test_equal_dictionaries(self):
  11. dict_1 = {"a": "b", "c": "d"}
  12. dict_2 = {"c": "d", "a": "b"}
  13. self.assertTrue(equal(dict_1, dict_2))
  14. def test_equal_dictionaries_with_nan(self):
  15. dict_1 = {"a": nan, "c": "d"}
  16. dict_2 = {"c": "d", "a": nan}
  17. self.assertTrue(equal(dict_1, dict_2))
  18. def test_missing_key(self):
  19. dict_1 = {"a": "b", "c": "d"}
  20. dict_2 = {"c": "d", "x": "b"}
  21. self.assertFalse(equal(dict_1, dict_2))
  22. def test_additional_key(self):
  23. dict_1 = {"a": "b", "c": "d"}
  24. dict_2 = {"c": "d", "a": "b", "x": "x"}
  25. self.assertFalse(equal(dict_1, dict_2))
  26. def test_missing_value(self):
  27. dict_1 = {"a": "b", "c": "d"}
  28. dict_2 = {"c": "d", "a": "x"}
  29. self.assertFalse(equal(dict_1, dict_2))
  30. def test_empty_dictionaries(self):
  31. dict_1 = {}
  32. dict_2 = {}
  33. self.assertTrue(equal(dict_1, dict_2))
  34. def test_one_none(self):
  35. dict_1 = None
  36. dict_2 = {"a": "b", "c": "d"}
  37. self.assertFalse(equal(dict_1, dict_2))
  38. def test_same_item(self):
  39. dict_1 = {"a": "b", "c": "d"}
  40. self.assertTrue(equal(dict_1, dict_1))
  41. def test_nested_equal(self):
  42. dict_1 = {"a": {"a": "b", "c": "d"}, "c": "d"}
  43. dict_2 = {"c": "d", "a": {"a": "b", "c": "d"}}
  44. self.assertTrue(equal(dict_1, dict_2))
  45. def test_nested_dict_unequal(self):
  46. dict_1 = {"a": {"a": "b", "c": "d"}, "c": "d"}
  47. dict_2 = {"c": "d", "a": {"a": "b", "c": "x"}}
  48. self.assertFalse(equal(dict_1, dict_2))
  49. def test_mixed_nested_equal(self):
  50. dict_1 = {"a": ["a", "b", "c", "d"], "c": "d"}
  51. dict_2 = {"c": "d", "a": ["a", "b", "c", "d"]}
  52. self.assertTrue(equal(dict_1, dict_2))
  53. def test_nested_list_unequal(self):
  54. dict_1 = {"a": ["a", "b", "c", "d"], "c": "d"}
  55. dict_2 = {"c": "d", "a": ["b", "c", "d", "a"]}
  56. self.assertFalse(equal(dict_1, dict_2))
  57. class TestListEqual(TestCase):
  58. def test_equal_lists(self):
  59. list_1 = ["a", "b", "c"]
  60. list_2 = ["a", "b", "c"]
  61. self.assertTrue(equal(list_1, list_2))
  62. def test_equal_lists_with_nan(self):
  63. list_1 = ["a", nan, "c"]
  64. list_2 = ["a", nan, "c"]
  65. self.assertTrue(equal(list_1, list_2))
  66. def test_unsorted_lists(self):
  67. list_1 = ["a", "b", "c"]
  68. list_2 = ["b", "b", "a"]
  69. self.assertFalse(equal(list_1, list_2))
  70. def test_first_list_larger(self):
  71. list_1 = ["a", "b", "c"]
  72. list_2 = ["a", "b"]
  73. self.assertFalse(equal(list_1, list_2))
  74. def test_second_list_larger(self):
  75. list_1 = ["a", "b"]
  76. list_2 = ["a", "b", "c"]
  77. self.assertFalse(equal(list_1, list_2))
  78. def test_list_with_none_unequal(self):
  79. list_1 = ["a", "b", None]
  80. list_2 = ["a", "b", "c"]
  81. self.assertFalse(equal(list_1, list_2))
  82. list_1 = ["a", "b", None]
  83. list_2 = [None, "b", "c"]
  84. self.assertFalse(equal(list_1, list_2))
  85. def test_list_with_none_equal(self):
  86. list_1 = ["a", None, "c"]
  87. list_2 = ["a", None, "c"]
  88. self.assertTrue(equal(list_1, list_2))
  89. def test_empty_list(self):
  90. list_1 = []
  91. list_2 = []
  92. self.assertTrue(equal(list_1, list_2))
  93. def test_one_none(self):
  94. list_1 = None
  95. list_2 = []
  96. self.assertFalse(equal(list_1, list_2))
  97. def test_same_list(self):
  98. list_1 = ["a", "b", "c"]
  99. self.assertTrue(equal(list_1, list_1))
  100. def test_equal_nested_lists(self):
  101. list_1 = ["a", ["b", "c"], "d"]
  102. list_2 = ["a", ["b", "c"], "d"]
  103. self.assertTrue(equal(list_1, list_2))
  104. def test_unequal_nested_lists(self):
  105. list_1 = ["a", ["b", "c"], "d"]
  106. list_2 = ["a", [], "c"]
  107. self.assertFalse(equal(list_1, list_2))