From 0e407351a4630c995d7adad960047c81cc22a153 Mon Sep 17 00:00:00 2001 From: Iris <58442094+sleepyStick@users.noreply.github.com> Date: Fri, 27 Jun 2025 14:06:00 -0700 Subject: [PATCH] PYTHON-5392 Better test assertions for comparisons (#2350) Co-authored-by: Noah Stapp --- test/asynchronous/test_bulk.py | 8 ++++---- test/asynchronous/test_client.py | 2 +- test/asynchronous/test_collection.py | 2 +- test/asynchronous/test_database.py | 2 +- test/asynchronous/test_pooling.py | 9 +++++---- test/test_bson.py | 2 ++ test/test_bulk.py | 8 ++++---- test/test_client.py | 2 +- test/test_collection.py | 2 +- test/test_database.py | 2 +- test/test_objectid.py | 2 +- test/test_pooling.py | 9 +++++---- 12 files changed, 27 insertions(+), 23 deletions(-) diff --git a/test/asynchronous/test_bulk.py b/test/asynchronous/test_bulk.py index b6dedb497..02958e6f0 100644 --- a/test/asynchronous/test_bulk.py +++ b/test/asynchronous/test_bulk.py @@ -994,7 +994,7 @@ class AsyncTestBulkWriteConcern(AsyncBulkTestBase): # When talking to legacy servers there will be a # write concern error for each operation. - self.assertTrue(len(details["writeConcernErrors"]) > 0) + self.assertGreater(len(details["writeConcernErrors"]), 0) failed = details["writeConcernErrors"][0] self.assertEqual(64, failed["code"]) @@ -1035,7 +1035,7 @@ class AsyncTestBulkWriteConcern(AsyncBulkTestBase): details, ) - self.assertTrue(len(details["writeConcernErrors"]) > 1) + self.assertGreater(len(details["writeConcernErrors"]), 1) failed = details["writeErrors"][0] self.assertIn("duplicate", failed["errmsg"]) @@ -1073,7 +1073,7 @@ class AsyncTestBulkWriteConcern(AsyncBulkTestBase): self.assertEqual(0, len(details["writeErrors"])) # When talking to legacy servers there will be a # write concern error for each operation. - self.assertTrue(len(details["writeConcernErrors"]) > 1) + self.assertGreater(len(details["writeConcernErrors"]), 1) await self.coll.delete_many({}) await self.coll.create_index("a", unique=True) @@ -1100,7 +1100,7 @@ class AsyncTestBulkWriteConcern(AsyncBulkTestBase): self.assertEqual(1, len(details["writeErrors"])) # When talking to legacy servers there will be a # write concern error for each operation. - self.assertTrue(len(details["writeConcernErrors"]) > 1) + self.assertGreater(len(details["writeConcernErrors"]), 1) failed = details["writeErrors"][0] self.assertEqual(2, failed["index"]) diff --git a/test/asynchronous/test_client.py b/test/asynchronous/test_client.py index aaa7e7d56..52f16e0bc 100644 --- a/test/asynchronous/test_client.py +++ b/test/asynchronous/test_client.py @@ -1005,7 +1005,7 @@ class TestClient(AsyncIntegrationTest): cursor = await self.client.list_databases() self.assertIsInstance(cursor, AsyncCommandCursor) helper_docs = await cursor.to_list() - self.assertTrue(len(helper_docs) > 0) + self.assertGreater(len(helper_docs), 0) self.assertEqual(len(helper_docs), len(cmd_docs)) # PYTHON-3529 Some fields may change between calls, just compare names. for helper_doc, cmd_doc in zip(helper_docs, cmd_docs): diff --git a/test/asynchronous/test_collection.py b/test/asynchronous/test_collection.py index b6f96cc99..cda8452d1 100644 --- a/test/asynchronous/test_collection.py +++ b/test/asynchronous/test_collection.py @@ -500,7 +500,7 @@ class AsyncTestCollection(AsyncIntegrationTest): # Sort by 'score' field. cursor.sort([("score", {"$meta": "textScore"})]) results = await cursor.to_list() - self.assertTrue(results[0]["score"] >= results[1]["score"]) + self.assertGreaterEqual(results[0]["score"], results[1]["score"]) await db.test.drop_indexes() diff --git a/test/asynchronous/test_database.py b/test/asynchronous/test_database.py index 3195c7498..e6f0c6a53 100644 --- a/test/asynchronous/test_database.py +++ b/test/asynchronous/test_database.py @@ -239,7 +239,7 @@ class TestDatabase(AsyncIntegrationTest): listener.reset() await db.drop_collection("unique") await db.create_collection("unique", check_exists=False) - self.assertTrue(len(listener.started_events) > 0) + self.assertGreater(len(listener.started_events), 0) self.assertNotIn("listCollections", listener.started_command_names()) async def test_list_collections(self): diff --git a/test/asynchronous/test_pooling.py b/test/asynchronous/test_pooling.py index 64c5738db..66edf0177 100644 --- a/test/asynchronous/test_pooling.py +++ b/test/asynchronous/test_pooling.py @@ -331,8 +331,9 @@ class TestPooling(_TestPoolingBase): pass duration = time.time() - start - self.assertTrue( - abs(wait_queue_timeout - duration) < 1, + self.assertLess( + abs(wait_queue_timeout - duration), + 1, f"Waited {duration:.2f} seconds for a socket, expected {wait_queue_timeout:f}", ) @@ -547,7 +548,7 @@ class TestPoolMaxSize(_TestPoolingBase): await async_joinall(tasks) self.assertEqual(ntasks, self.n_passed) - self.assertTrue(len(cx_pool.conns) > 1) + self.assertGreater(len(cx_pool.conns), 1) self.assertEqual(0, cx_pool.requests) async def test_max_pool_size_none(self): @@ -578,7 +579,7 @@ class TestPoolMaxSize(_TestPoolingBase): await async_joinall(tasks) self.assertEqual(ntasks, self.n_passed) - self.assertTrue(len(cx_pool.conns) > 1) + self.assertGreater(len(cx_pool.conns), 1) self.assertEqual(cx_pool.max_pool_size, float("inf")) async def test_max_pool_size_zero(self): diff --git a/test/test_bson.py b/test/test_bson.py index 23e0a29c4..e9a1dd1ca 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -1045,6 +1045,8 @@ class TestBSON(unittest.TestCase): def test_minkey_maxkey_comparison(self): # MinKey's <, <=, >, >=, !=, and ==. + # These tests should be kept as assertTrue as opposed to using unittest's built-in comparison assertions because + # MinKey and MaxKey define their own __ge__, __le__, and other comparison attributes, and we want to explicitly test that. self.assertTrue(MinKey() < None) self.assertTrue(MinKey() < 1) self.assertTrue(MinKey() <= 1) diff --git a/test/test_bulk.py b/test/test_bulk.py index ac6f760d3..1de406fca 100644 --- a/test/test_bulk.py +++ b/test/test_bulk.py @@ -992,7 +992,7 @@ class TestBulkWriteConcern(BulkTestBase): # When talking to legacy servers there will be a # write concern error for each operation. - self.assertTrue(len(details["writeConcernErrors"]) > 0) + self.assertGreater(len(details["writeConcernErrors"]), 0) failed = details["writeConcernErrors"][0] self.assertEqual(64, failed["code"]) @@ -1033,7 +1033,7 @@ class TestBulkWriteConcern(BulkTestBase): details, ) - self.assertTrue(len(details["writeConcernErrors"]) > 1) + self.assertGreater(len(details["writeConcernErrors"]), 1) failed = details["writeErrors"][0] self.assertIn("duplicate", failed["errmsg"]) @@ -1069,7 +1069,7 @@ class TestBulkWriteConcern(BulkTestBase): self.assertEqual(0, len(details["writeErrors"])) # When talking to legacy servers there will be a # write concern error for each operation. - self.assertTrue(len(details["writeConcernErrors"]) > 1) + self.assertGreater(len(details["writeConcernErrors"]), 1) self.coll.delete_many({}) self.coll.create_index("a", unique=True) @@ -1096,7 +1096,7 @@ class TestBulkWriteConcern(BulkTestBase): self.assertEqual(1, len(details["writeErrors"])) # When talking to legacy servers there will be a # write concern error for each operation. - self.assertTrue(len(details["writeConcernErrors"]) > 1) + self.assertGreater(len(details["writeConcernErrors"]), 1) failed = details["writeErrors"][0] self.assertEqual(2, failed["index"]) diff --git a/test/test_client.py b/test/test_client.py index 18624f892..dd1bf94cf 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -978,7 +978,7 @@ class TestClient(IntegrationTest): cursor = self.client.list_databases() self.assertIsInstance(cursor, CommandCursor) helper_docs = cursor.to_list() - self.assertTrue(len(helper_docs) > 0) + self.assertGreater(len(helper_docs), 0) self.assertEqual(len(helper_docs), len(cmd_docs)) # PYTHON-3529 Some fields may change between calls, just compare names. for helper_doc, cmd_doc in zip(helper_docs, cmd_docs): diff --git a/test/test_collection.py b/test/test_collection.py index 5643a6202..ccace72be 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -490,7 +490,7 @@ class TestCollection(IntegrationTest): # Sort by 'score' field. cursor.sort([("score", {"$meta": "textScore"})]) results = cursor.to_list() - self.assertTrue(results[0]["score"] >= results[1]["score"]) + self.assertGreaterEqual(results[0]["score"], results[1]["score"]) db.test.drop_indexes() diff --git a/test/test_database.py b/test/test_database.py index 0fe6c01a9..56691383b 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -238,7 +238,7 @@ class TestDatabase(IntegrationTest): listener.reset() db.drop_collection("unique") db.create_collection("unique", check_exists=False) - self.assertTrue(len(listener.started_events) > 0) + self.assertGreater(len(listener.started_events), 0) self.assertNotIn("listCollections", listener.started_command_names()) def test_list_collections(self): diff --git a/test/test_objectid.py b/test/test_objectid.py index d7db7229e..dbc61951d 100644 --- a/test/test_objectid.py +++ b/test/test_objectid.py @@ -92,7 +92,7 @@ class TestObjectId(unittest.TestCase): self.assertEqual(utc, d2.tzinfo) d2 = d2.replace(tzinfo=None) - self.assertTrue(d2 - d1 < datetime.timedelta(seconds=2)) + self.assertLess(d2 - d1, datetime.timedelta(seconds=2)) def test_from_datetime(self): d = datetime.datetime.now(tz=datetime.timezone.utc).replace(tzinfo=None) diff --git a/test/test_pooling.py b/test/test_pooling.py index 05513afe1..b995c467c 100644 --- a/test/test_pooling.py +++ b/test/test_pooling.py @@ -331,8 +331,9 @@ class TestPooling(_TestPoolingBase): pass duration = time.time() - start - self.assertTrue( - abs(wait_queue_timeout - duration) < 1, + self.assertLess( + abs(wait_queue_timeout - duration), + 1, f"Waited {duration:.2f} seconds for a socket, expected {wait_queue_timeout:f}", ) @@ -545,7 +546,7 @@ class TestPoolMaxSize(_TestPoolingBase): joinall(tasks) self.assertEqual(ntasks, self.n_passed) - self.assertTrue(len(cx_pool.conns) > 1) + self.assertGreater(len(cx_pool.conns), 1) self.assertEqual(0, cx_pool.requests) def test_max_pool_size_none(self): @@ -576,7 +577,7 @@ class TestPoolMaxSize(_TestPoolingBase): joinall(tasks) self.assertEqual(ntasks, self.n_passed) - self.assertTrue(len(cx_pool.conns) > 1) + self.assertGreater(len(cx_pool.conns), 1) self.assertEqual(cx_pool.max_pool_size, float("inf")) def test_max_pool_size_zero(self):