From 76b7f1ba2b40a6467f0425f79dab9d868e91c6ff Mon Sep 17 00:00:00 2001 From: Noah Stapp Date: Wed, 30 Jul 2025 16:45:13 -0400 Subject: [PATCH] PYTHON-5473 - Better test assertions for booleans --- test/asynchronous/test_database.py | 23 +++++++------------- test/asynchronous/test_gridfs_bucket.py | 28 ++++++++++++------------- test/asynchronous/test_monitoring.py | 7 +++---- test/test_database.py | 23 +++++++------------- test/test_gridfs_bucket.py | 28 ++++++++++++------------- test/test_monitoring.py | 7 +++---- 6 files changed, 46 insertions(+), 70 deletions(-) diff --git a/test/asynchronous/test_database.py b/test/asynchronous/test_database.py index e6f0c6a532..3b77330c0e 100644 --- a/test/asynchronous/test_database.py +++ b/test/asynchronous/test_database.py @@ -189,7 +189,7 @@ async def test_list_collection_names(self): filter={"name": {"$regex": r"^(?!system\.)"}} ) for coll in no_system_collections: - self.assertTrue(not coll.startswith("system.")) + self.assertFalse(coll.startswith("system.")) self.assertIn("systemcoll.test", no_system_collections) # Force more than one batch. @@ -265,19 +265,13 @@ async def test_list_collections(self): try: # Found duplicate. coll_cnt[coll] += 1 - self.assertTrue(False) + self.fail("Found duplicate") except KeyError: coll_cnt[coll] = 1 coll_cnt: dict = {} - # Checking if is there any collection which don't exists. - if ( - len(set(colls) - {"test", "test.mike"}) == 0 - or len(set(colls) - {"test", "test.mike", "system.indexes"}) == 0 - ): - self.assertTrue(True) - else: - self.assertTrue(False) + # Check if there are any collections which don't exist. + self.assertLessEqual(set(colls), {"test", "test.mike", "system.indexes"}) colls = await (await db.list_collections(filter={"name": {"$regex": "^test$"}})).to_list() self.assertEqual(1, len(colls)) @@ -307,16 +301,13 @@ async def test_list_collections(self): try: # Found duplicate. coll_cnt[coll] += 1 - self.assertTrue(False) + self.fail("Found duplicate") except KeyError: coll_cnt[coll] = 1 coll_cnt = {} - # Checking if is there any collection which don't exists. - if len(set(colls) - {"test"}) == 0 or len(set(colls) - {"test", "system.indexes"}) == 0: - self.assertTrue(True) - else: - self.assertTrue(False) + # Check if there are any collections which don't exist. + self.assertLessEqual(set(colls), {"test", "system.indexes"}) await self.client.drop_database("pymongo_test") diff --git a/test/asynchronous/test_gridfs_bucket.py b/test/asynchronous/test_gridfs_bucket.py index 4640507e94..fd9b9883bf 100644 --- a/test/asynchronous/test_gridfs_bucket.py +++ b/test/asynchronous/test_gridfs_bucket.py @@ -164,17 +164,16 @@ async def test_upload_ensures_index(self): await files.drop() await self.fs.upload_from_stream("filename", b"junk") - self.assertTrue( - any( - info.get("key") == [("files_id", 1), ("n", 1)] - for info in (await chunks.index_information()).values() - ) + self.assertIn( + [("files_id", 1), ("n", 1)], + [info.get("key") for info in (await chunks.index_information()).values()], + "Missing required index on chunks collection: {files_id: 1, n: 1}", ) - self.assertTrue( - any( - info.get("key") == [("filename", 1), ("uploadDate", 1)] - for info in (await files.index_information()).values() - ) + + self.assertIn( + [("filename", 1), ("uploadDate", 1)], + [info.get("key") for info in (await files.index_information()).values()], + "Missing required index on files collection: {filename: 1, uploadDate: 1}", ) async def test_ensure_index_shell_compat(self): @@ -192,11 +191,10 @@ async def test_ensure_index_shell_compat(self): # No error. await self.fs.upload_from_stream("filename", b"data") - self.assertTrue( - any( - info.get("key") == [("filename", 1), ("uploadDate", 1)] - for info in (await files.index_information()).values() - ) + self.assertIn( + [("filename", 1), ("uploadDate", 1)], + [info.get("key") for info in (await files.index_information()).values()], + "Missing required index on files collection: {filename: 1, uploadDate: 1}", ) await files.drop() diff --git a/test/asynchronous/test_monitoring.py b/test/asynchronous/test_monitoring.py index a44223a725..9b2a3691eb 100644 --- a/test/asynchronous/test_monitoring.py +++ b/test/asynchronous/test_monitoring.py @@ -512,9 +512,8 @@ async def test_kill_cursors(self): self.assertEqual(cursor.address, succeeded.connection_id) # There could be more than one cursor_id here depending on # when the thread last ran. - self.assertTrue( - cursor_id in succeeded.reply["cursorsUnknown"] - or cursor_id in succeeded.reply["cursorsKilled"] + self.assertIn( + cursor_id, succeeded.reply["cursorsUnknown"] + succeeded.reply["cursorsKilled"] ) async def test_non_bulk_writes(self): @@ -1066,7 +1065,7 @@ async def test_write_errors(self): self.assertEqual(2, len(errors)) fields = {"index", "code", "errmsg"} for error in errors: - self.assertTrue(fields.issubset(set(error))) + self.assertLessEqual(fields, set(error)) async def test_first_batch_helper(self): # Regardless of server version and use of helpers._first_batch diff --git a/test/test_database.py b/test/test_database.py index 56691383b2..c50e09b6e1 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -188,7 +188,7 @@ def test_list_collection_names(self): filter={"name": {"$regex": r"^(?!system\.)"}} ) for coll in no_system_collections: - self.assertTrue(not coll.startswith("system.")) + self.assertFalse(coll.startswith("system.")) self.assertIn("systemcoll.test", no_system_collections) # Force more than one batch. @@ -264,19 +264,13 @@ def test_list_collections(self): try: # Found duplicate. coll_cnt[coll] += 1 - self.assertTrue(False) + self.fail("Found duplicate") except KeyError: coll_cnt[coll] = 1 coll_cnt: dict = {} - # Checking if is there any collection which don't exists. - if ( - len(set(colls) - {"test", "test.mike"}) == 0 - or len(set(colls) - {"test", "test.mike", "system.indexes"}) == 0 - ): - self.assertTrue(True) - else: - self.assertTrue(False) + # Check if there are any collections which don't exist. + self.assertLessEqual(set(colls), {"test", "test.mike", "system.indexes"}) colls = (db.list_collections(filter={"name": {"$regex": "^test$"}})).to_list() self.assertEqual(1, len(colls)) @@ -304,16 +298,13 @@ def test_list_collections(self): try: # Found duplicate. coll_cnt[coll] += 1 - self.assertTrue(False) + self.fail("Found duplicate") except KeyError: coll_cnt[coll] = 1 coll_cnt = {} - # Checking if is there any collection which don't exists. - if len(set(colls) - {"test"}) == 0 or len(set(colls) - {"test", "system.indexes"}) == 0: - self.assertTrue(True) - else: - self.assertTrue(False) + # Check if there are any collections which don't exist. + self.assertLessEqual(set(colls), {"test", "system.indexes"}) self.client.drop_database("pymongo_test") diff --git a/test/test_gridfs_bucket.py b/test/test_gridfs_bucket.py index 33902c9cfc..9dbb082ee9 100644 --- a/test/test_gridfs_bucket.py +++ b/test/test_gridfs_bucket.py @@ -162,17 +162,16 @@ def test_upload_ensures_index(self): files.drop() self.fs.upload_from_stream("filename", b"junk") - self.assertTrue( - any( - info.get("key") == [("files_id", 1), ("n", 1)] - for info in (chunks.index_information()).values() - ) + self.assertIn( + [("files_id", 1), ("n", 1)], + [info.get("key") for info in (chunks.index_information()).values()], + "Missing required index on chunks collection: {files_id: 1, n: 1}", ) - self.assertTrue( - any( - info.get("key") == [("filename", 1), ("uploadDate", 1)] - for info in (files.index_information()).values() - ) + + self.assertIn( + [("filename", 1), ("uploadDate", 1)], + [info.get("key") for info in (files.index_information()).values()], + "Missing required index on files collection: {filename: 1, uploadDate: 1}", ) def test_ensure_index_shell_compat(self): @@ -190,11 +189,10 @@ def test_ensure_index_shell_compat(self): # No error. self.fs.upload_from_stream("filename", b"data") - self.assertTrue( - any( - info.get("key") == [("filename", 1), ("uploadDate", 1)] - for info in (files.index_information()).values() - ) + self.assertIn( + [("filename", 1), ("uploadDate", 1)], + [info.get("key") for info in (files.index_information()).values()], + "Missing required index on files collection: {filename: 1, uploadDate: 1}", ) files.drop() diff --git a/test/test_monitoring.py b/test/test_monitoring.py index 8b54793a36..7cb93adf81 100644 --- a/test/test_monitoring.py +++ b/test/test_monitoring.py @@ -510,9 +510,8 @@ def test_kill_cursors(self): self.assertEqual(cursor.address, succeeded.connection_id) # There could be more than one cursor_id here depending on # when the thread last ran. - self.assertTrue( - cursor_id in succeeded.reply["cursorsUnknown"] - or cursor_id in succeeded.reply["cursorsKilled"] + self.assertIn( + cursor_id, succeeded.reply["cursorsUnknown"] + succeeded.reply["cursorsKilled"] ) def test_non_bulk_writes(self): @@ -1064,7 +1063,7 @@ def test_write_errors(self): self.assertEqual(2, len(errors)) fields = {"index", "code", "errmsg"} for error in errors: - self.assertTrue(fields.issubset(set(error))) + self.assertLessEqual(fields, set(error)) def test_first_batch_helper(self): # Regardless of server version and use of helpers._first_batch