Skip to content

Commit b761930

Browse files
committed
Adding comparison method for Records
1 parent b76e09d commit b761930

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

pynetbox/core/response.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,19 @@ def __getstate__(self):
213213
def __setstate__(self, d):
214214
self.__dict__.update(d)
215215

216-
def __hash__(self):
216+
def __key__(self):
217217
if hasattr(self, "id"):
218-
return hash((self.endpoint.name, self.id))
218+
return (self.endpoint.name, self.id)
219219
else:
220-
return hash(self.endpoint.name)
220+
return (self.endpoint.name)
221+
222+
def __hash__(self):
223+
return hash(self.__key__())
224+
225+
def __eq__(self, other):
226+
if isinstance(other, Record):
227+
return self.__key__() == other.__key__()
228+
return NotImplemented
221229

222230
def _add_cache(self, item):
223231
key, value = item

tests/unit/test_response.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,14 @@ def test_hash_diff(self):
128128
test2 = Record({}, None, endpoint2)
129129
test2.id = 2
130130
self.assertNotEqual(hash(test1), hash(test2))
131+
132+
def test_compare(self):
133+
endpoint1 = Mock()
134+
endpoint1.name = "test-endpoint"
135+
endpoint2 = Mock()
136+
endpoint2.name = "test-endpoint"
137+
test1 = Record({}, None, endpoint1)
138+
test1.id = 1
139+
test2 = Record({}, None, endpoint2)
140+
test2.id = 1
141+
self.assertEqual(test1, test2)

0 commit comments

Comments
 (0)