From cabf7ed4411ddec4e142c81cdfb2f4e1ebe65b22 Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Thu, 17 Dec 2020 04:44:56 +0100 Subject: [PATCH] PYTHON-2466 Make pymongo client, database and collection objects hashable. (#533) (cherry picked from commit 733ab2527bfec62cdf7e570c0ffebe7efdf92096) --- pymongo/collection.py | 3 +++ pymongo/database.py | 3 +++ pymongo/mongo_client.py | 3 +++ test/test_client.py | 4 ++++ test/test_collection.py | 3 +++ test/test_database.py | 3 +++ 6 files changed, 19 insertions(+) diff --git a/pymongo/collection.py b/pymongo/collection.py index 7a3d0bd99..4adce0a38 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -303,6 +303,9 @@ class Collection(common.BaseObject): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.__database, self.__name)) + @property def full_name(self): """The full name of this :class:`Collection`. diff --git a/pymongo/database.py b/pymongo/database.py index 3f0a95363..a31e918ac 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -272,6 +272,9 @@ class Database(common.BaseObject): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.__client, self.__name)) + def __repr__(self): return "Database(%r, %r)" % (self.__client, self.__name) diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 4ed23855f..6e1e24481 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1509,6 +1509,9 @@ class MongoClient(common.BaseObject): def __ne__(self, other): return not self == other + def __hash__(self): + return hash(self.address) + def _repr_helper(self): def option_repr(option, value): """Fix options whose __repr__ isn't usable in a constructor.""" diff --git a/test/test_client.py b/test/test_client.py index af4605201..6f2d587ed 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -627,6 +627,10 @@ class TestClient(IntegrationTest): # Explicitly test inequality self.assertFalse(client_context.client != c) + def test_hashable(self): + c = connected(rs_or_single_client()) + self.assertIn(c, {client_context.client}) + def test_host_w_port(self): with self.assertRaises(ValueError): connected(MongoClient("%s:1234567" % (client_context.host,), diff --git a/test/test_collection.py b/test/test_collection.py index 220ddab3a..96a3e454a 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -154,6 +154,9 @@ class TestCollection(IntegrationTest): self.assertEqual(self.db.test.mike, self.db["test.mike"]) self.assertEqual(self.db.test["mike"], self.db["test.mike"]) + def test_hashable(self): + self.assertIn(self.db.test.mike, {self.db["test.mike"]}) + @client_context.require_version_min(3, 3, 9) def test_create(self): # No Exception. diff --git a/test/test_database.py b/test/test_database.py index 349f09005..cdaf31332 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -126,6 +126,9 @@ class TestDatabase(IntegrationTest): self.assertFalse(Database(self.client, "test") != Database(self.client, "test")) + def test_hashable(self): + self.assertIn(self.client.test, {Database(self.client, "test")}) + def test_get_coll(self): db = Database(self.client, "pymongo_test") self.assertEqual(db.test, db["test"])