8000 Addressing corner cases for datastore.Key.__eq__. · googleapis/google-cloud-python@b61ba2e · GitHub
[go: up one dir, main page]

Skip to content

Commit b61ba2e

Browse files
committed
Addressing corner cases for datastore.Key.__eq__.
These corners are a direct result of #274.
1 parent 3d4ce5d commit b61ba2e

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

gcloud/datastore/key.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,24 @@ def __eq__(self, other):
256256
if self is other:
257257
return True
258258

259-
return (self._dataset_id == other._dataset_id and
260-
self.namespace() == other.namespace() and
261-
self.path() == other.path())
259+
if not isinstance(other, self.__class__):
260+
return False
261+
262+
# Check that paths match.
263+
if self.path() != other.path():
264+
8000 return False
265+
266+
# Check that datasets match.
267+
if not (self._dataset_id == other._dataset_id or
268+
self._dataset_id is None or other._dataset_id is None):
269+
return False
270+
271+
# Check that namespaces match.
272+
if not (self._namespace == other._namespace or
273+
self._namespace is None or other._namespace is None):
274+
return False
275+
276+
return True
262277

263278
def __ne__(self, other):
264279
return not self.__eq__(other)

gcloud/datastore/test_key.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,21 @@ def test_key___eq__(self):
332332
self.assertEqual(key1, key1)
333333
key3 = self._getTargetClass().from_path('abc', 'ghi')
334334
self.assertNotEqual(key1, key3)
335+
336+
def test_key___eq___wrong_type(self):
337+
key = self._getTargetClass().from_path('abc', 'def')
338+
self.assertNotEqual(key, 10)
339+
340+
def test_key___eq___dataset_id(self):
341+
key1 = self._getTargetClass().from_path('abc', 'def')
342+
key2 = self._getTargetClass().from_path('abc', 'def', dataset_id='foo')
343+
self.assertEqual(key1, key2)
344+
key3 = self._getTargetClass().from_path('abc', 'def', dataset_id='bar')
345+
self.assertNotEqual(key2, key3)
346+
347+
def test_key___eq___namespace(self):
348+
key1 = self._getTargetClass().from_path('abc', 'def')
349+
key2 = self._getTargetClass().from_path('abc', 'def', namespace='foo')
350+
self.assertEqual(key1, key2)
351+
key3 = self._getTargetClass().from_path('abc', 'def', namespace='bar')
352+
self.assertNotEqual(key2, key3)

0 commit comments

Comments
 (0)
0