From 330ffc539bc7401ec241914a4a22d2e46f773c2c Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 22 Oct 2014 18:10:54 -0700 Subject: [PATCH] Addressing corner cases for datastore.Key.__eq__. These corners are a direct result of #274. --- gcloud/datastore/key.py | 21 ++++++++++++++++++--- gcloud/datastore/test_key.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/gcloud/datastore/key.py b/gcloud/datastore/key.py index fba77bff396c..9ccbccd58adf 100644 --- a/gcloud/datastore/key.py +++ b/gcloud/datastore/key.py @@ -224,9 +224,24 @@ def __eq__(self, other): if self is other: return True - return (self._dataset_id == other._dataset_id and - self.namespace() == other.namespace() and - self.path() == other.path()) + if not isinstance(other, self.__class__): + return False + + # Check that paths match. + if self.path() != other.path(): + return False + + # Check that datasets match. + if not (self._dataset_id == other._dataset_id or + self._dataset_id is None or other._dataset_id is None): + return False + + # Check that namespaces match. + if not (self._namespace == other._namespace or + self._namespace is None or other._namespace is None): + return False + + return True def __ne__(self, other): return not self.__eq__(other) diff --git a/gcloud/datastore/test_key.py b/gcloud/datastore/test_key.py index c242caa96c12..57eb82b517c5 100644 --- a/gcloud/datastore/test_key.py +++ b/gcloud/datastore/test_key.py @@ -284,3 +284,21 @@ def test_key___eq__(self): self.assertEqual(key1, key1) key3 = self._getTargetClass().from_path('abc', 'ghi') self.assertNotEqual(key1, key3) + + def test_key___eq___wrong_type(self): + key = self._getTargetClass().from_path('abc', 'def') + self.assertNotEqual(key, 10) + + def test_key___eq___dataset_id(self): + key1 = self._getTargetClass().from_path('abc', 'def') + key2 = self._getTargetClass().from_path('abc', 'def', dataset_id='foo') + self.assertEqual(key1, key2) + key3 = self._getTargetClass().from_path('abc', 'def', dataset_id='bar') + self.assertNotEqual(key2, key3) + + def test_key___eq___namespace(self): + key1 = self._getTargetClass().from_path('abc', 'def') + key2 = self._getTargetClass().from_path('abc', 'def', namespace='foo') + self.assertEqual(key1, key2) + key3 = self._getTargetClass().from_path('abc', 'def', namespace='bar') + self.assertNotEqual(key2, key3)