PYTHON-5473 - Better test assertions for booleans (#2450)

This commit is contained in:
Noah Stapp 2025-07-31 08:57:00 -04:00 committed by GitHub
parent 9514a67270
commit 9f64dad687
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 46 additions and 70 deletions

View File

@ -189,7 +189,7 @@ class TestDatabase(AsyncIntegrationTest):
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 @@ class TestDatabase(AsyncIntegrationTest):
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 @@ class TestDatabase(AsyncIntegrationTest):
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")

View File

@ -164,17 +164,16 @@ class TestGridfs(AsyncIntegrationTest):
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 @@ class TestGridfs(AsyncIntegrationTest):
# 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()

View File

@ -512,9 +512,8 @@ class AsyncTestCommandMonitoring(AsyncIntegrationTest):
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 @@ class AsyncTestCommandMonitoring(AsyncIntegrationTest):
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

View File

@ -188,7 +188,7 @@ class TestDatabase(IntegrationTest):
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 @@ class TestDatabase(IntegrationTest):
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 @@ class TestDatabase(IntegrationTest):
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")

View File

@ -162,17 +162,16 @@ class TestGridfs(IntegrationTest):
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 @@ class TestGridfs(IntegrationTest):
# 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()

View File

@ -510,9 +510,8 @@ class TestCommandMonitoring(IntegrationTest):
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 @@ class TestCommandMonitoring(IntegrationTest):
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