Skip to content

Commit bad07db

Browse files
committed
Added __eq__ to conf class to prevent equals errors
1 parent 80cae6b commit bad07db

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pyms/config/confile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def normalize_keys(self, key):
5050
key = key.replace("-", "_")
5151
return key
5252

53+
def __eq__(self, other):
54+
if not isinstance(other, ConfFile) and not isinstance(other, dict):
55+
return False
56+
return dict(self) == dict(other)
57+
5358
def __getattr__(self, name, *args, **kwargs):
5459
try:
5560
keys = self.normalize_keys(name).split(".")

tests/tests_config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ def test_dictionary_recursive_dict_normal_key_dinamyc(self):
4848
config = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}, "test_2": "c"})
4949
self.assertEqual(getattr(config, "test_1.test_1_2"), "b")
5050

51+
def test_equal_instances_error(self):
52+
config1 = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}, "test_2": "c"})
53+
config2 = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}})
54+
self.assertNotEqual(config1, config2)
55+
56+
def test_equal_instances_error2(self):
57+
config1 = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}})
58+
config2 = {"test-1": {"test-1-1": "a", "test-1-2": "b"}}
59+
self.assertNotEqual(config1, config2)
60+
61+
def test_equal_instances_ok(self):
62+
config1 = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}})
63+
config2 = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}})
64+
self.assertEqual(config1, config2)
65+
66+
def test_equal_instances_ok2(self):
67+
config1 = ConfFile(config={"test-1": {"test-1-1": "a", "test_1-2": "b"}})
68+
config2 = {"test_1": {"test_1_1": "a", "test_1_2": "b"}}
69+
self.assertEqual(config1, config2)
70+
5171
def test_dictionary_attribute_not_exists(self):
5272
config = ConfFile(config={"test-1": "a"})
5373
with self.assertRaises(AttrDoesNotExistException):
@@ -98,3 +118,6 @@ def test_empty_conf_two_levels(self):
98118
def test_empty_conf_three_levels(self):
99119
config = ConfFile(empty_init=True)
100120
self.assertEqual(config.my_ms.level_two.level_three, {})
121+
122+
if __name__ == '__main__':
123+
unittest.main()

0 commit comments

Comments
 (0)