Skip to content

Commit 9f64dad

Browse files
authored
PYTHON-5473 - Better test assertions for booleans (#2450)
1 parent 9514a67 commit 9f64dad

File tree

6 files changed

+46
-70
lines changed

6 files changed

+46
-70
lines changed

test/asynchronous/test_database.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ async def test_list_collection_names(self):
189189
filter={"name": {"$regex": r"^(?!system\.)"}}
190190
)
191191
for coll in no_system_collections:
192-
self.assertTrue(not coll.startswith("system."))
192+
self.assertFalse(coll.startswith("system."))
193193
self.assertIn("systemcoll.test", no_system_collections)
194194

195195
# Force more than one batch.
@@ -265,19 +265,13 @@ async def test_list_collections(self):
265265
try:
266266
# Found duplicate.
267267
coll_cnt[coll] += 1
268-
self.assertTrue(False)
268+
self.fail("Found duplicate")
269269
except KeyError:
270270
coll_cnt[coll] = 1
271271
coll_cnt: dict = {}
272272

273-
# Checking if is there any collection which don't exists.
274-
if (
275-
len(set(colls) - {"test", "test.mike"}) == 0
276-
or len(set(colls) - {"test", "test.mike", "system.indexes"}) == 0
277-
):
278-
self.assertTrue(True)
279-
else:
280-
self.assertTrue(False)
273+
# Check if there are any collections which don't exist.
274+
self.assertLessEqual(set(colls), {"test", "test.mike", "system.indexes"})
281275

282276
colls = await (await db.list_collections(filter={"name": {"$regex": "^test$"}})).to_list()
283277
self.assertEqual(1, len(colls))
@@ -307,16 +301,13 @@ async def test_list_collections(self):
307301
try:
308302
# Found duplicate.
309303
coll_cnt[coll] += 1
310-
self.assertTrue(False)
304+
self.fail("Found duplicate")
311305
except KeyError:
312306
coll_cnt[coll] = 1
313307
coll_cnt = {}
314308

315-
# Checking if is there any collection which don't exists.
316-
if len(set(colls) - {"test"}) == 0 or len(set(colls) - {"test", "system.indexes"}) == 0:
317-
self.assertTrue(True)
318-
else:
319-
self.assertTrue(False)
309+
# Check if there are any collections which don't exist.
310+
self.assertLessEqual(set(colls), {"test", "system.indexes"})
320311

321312
await self.client.drop_database("pymongo_test")
322313

test/asynchronous/test_gridfs_bucket.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,16 @@ async def test_upload_ensures_index(self):
164164
await files.drop()
165165
await self.fs.upload_from_stream("filename", b"junk")
166166

167-
self.assertTrue(
168-
any(
169-
info.get("key") == [("files_id", 1), ("n", 1)]
170-
for info in (await chunks.index_information()).values()
171-
)
167+
self.assertIn(
168+
[("files_id", 1), ("n", 1)],
169+
[info.get("key") for info in (await chunks.index_information()).values()],
170+
"Missing required index on chunks collection: {files_id: 1, n: 1}",
172171
)
173-
self.assertTrue(
174-
any(
175-
info.get("key") == [("filename", 1), ("uploadDate", 1)]
176-
for info in (await files.index_information()).values()
177-
)
172+
173+
self.assertIn(
174+
[("filename", 1), ("uploadDate", 1)],
175+
[info.get("key") for info in (await files.index_information()).values()],
176+
"Missing required index on files collection: {filename: 1, uploadDate: 1}",
178177
)
179178

180179
async def test_ensure_index_shell_compat(self):
@@ -192,11 +191,10 @@ async def test_ensure_index_shell_compat(self):
192191
# No error.
193192
await self.fs.upload_from_stream("filename", b"data")
194193

195-
self.assertTrue(
196-
any(
197-
info.get("key") == [("filename", 1), ("uploadDate", 1)]
198-
for info in (await files.index_information()).values()
199-
)
194+
self.assertIn(
195+
[("filename", 1), ("uploadDate", 1)],
196+
[info.get("key") for info in (await files.index_information()).values()],
197+
"Missing required index on files collection: {filename: 1, uploadDate: 1}",
200198
)
201199
await files.drop()
202200

test/asynchronous/test_monitoring.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,8 @@ async def test_kill_cursors(self):
512512
self.assertEqual(cursor.address, succeeded.connection_id)
513513
# There could be more than one cursor_id here depending on
514514
# when the thread last ran.
515-
self.assertTrue(
516-
cursor_id in succeeded.reply["cursorsUnknown"]
517-
or cursor_id in succeeded.reply["cursorsKilled"]
515+
self.assertIn(
516+
cursor_id, succeeded.reply["cursorsUnknown"] + succeeded.reply["cursorsKilled"]
518517
)
519518

520519
async def test_non_bulk_writes(self):
@@ -1066,7 +1065,7 @@ async def test_write_errors(self):
10661065
self.assertEqual(2, len(errors))
10671066
fields = {"index", "code", "errmsg"}
10681067
for error in errors:
1069-
self.assertTrue(fields.issubset(set(error)))
1068+
self.assertLessEqual(fields, set(error))
10701069

10711070
async def test_first_batch_helper(self):
10721071
# Regardless of server version and use of helpers._first_batch

test/test_database.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_list_collection_names(self):
188188
filter={"name": {"$regex": r"^(?!system\.)"}}
189189
)
190190
for coll in no_system_collections:
191-
self.assertTrue(not coll.startswith("system."))
191+
self.assertFalse(coll.startswith("system."))
192192
self.assertIn("systemcoll.test", no_system_collections)
193193

194194
# Force more than one batch.
@@ -264,19 +264,13 @@ def test_list_collections(self):
264264
try:
265265
# Found duplicate.
266266
coll_cnt[coll] += 1
267-
self.assertTrue(False)
267+
self.fail("Found duplicate")
268268
except KeyError:
269269
coll_cnt[coll] = 1
270270
coll_cnt: dict = {}
271271

272-
# Checking if is there any collection which don't exists.
273-
if (
274-
len(set(colls) - {"test", "test.mike"}) == 0
275-
or len(set(colls) - {"test", "test.mike", "system.indexes"}) == 0
276-
):
277-
self.assertTrue(True)
278-
else:
279-
self.assertTrue(False)
272+
# Check if there are any collections which don't exist.
273+
self.assertLessEqual(set(colls), {"test", "test.mike", "system.indexes"})
280274

281275
colls = (db.list_collections(filter={"name": {"$regex": "^test$"}})).to_list()
282276
self.assertEqual(1, len(colls))
@@ -304,16 +298,13 @@ def test_list_collections(self):
304298
try:
305299
# Found duplicate.
306300
coll_cnt[coll] += 1
307-
self.assertTrue(False)
301+
self.fail("Found duplicate")
308302
except KeyError:
309303
coll_cnt[coll] = 1
310304
coll_cnt = {}
311305

312-
# Checking if is there any collection which don't exists.
313-
if len(set(colls) - {"test"}) == 0 or len(set(colls) - {"test", "system.indexes"}) == 0:
314-
self.assertTrue(True)
315-
else:
316-
self.assertTrue(False)
306+
# Check if there are any collections which don't exist.
307+
self.assertLessEqual(set(colls), {"test", "system.indexes"})
317308

318309
self.client.drop_database("pymongo_test")
319310

test/test_gridfs_bucket.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,16 @@ def test_upload_ensures_index(self):
162162
files.drop()
163163
self.fs.upload_from_stream("filename", b"junk")
164164

165-
self.assertTrue(
166-
any(
167-
info.get("key") == [("files_id", 1), ("n", 1)]
168-
for info in (chunks.index_information()).values()
169-
)
165+
self.assertIn(
166+
[("files_id", 1), ("n", 1)],
167+
[info.get("key") for info in (chunks.index_information()).values()],
168+
"Missing required index on chunks collection: {files_id: 1, n: 1}",
170169
)
171-
self.assertTrue(
172-
any(
173-
info.get("key") == [("filename", 1), ("uploadDate", 1)]
174-
for info in (files.index_information()).values()
175-
)
170+
171+
self.assertIn(
172+
[("filename", 1), ("uploadDate", 1)],
173+
[info.get("key") for info in (files.index_information()).values()],
174+
"Missing required index on files collection: {filename: 1, uploadDate: 1}",
176175
)
177176

178177
def test_ensure_index_shell_compat(self):
@@ -190,11 +189,10 @@ def test_ensure_index_shell_compat(self):
190189
# No error.
191190
self.fs.upload_from_stream("filename", b"data")
192191

193-
self.assertTrue(
194-
any(
195-
info.get("key") == [("filename", 1), ("uploadDate", 1)]
196-
for info in (files.index_information()).values()
197-
)
192+
self.assertIn(
193+
[("filename", 1), ("uploadDate", 1)],
194+
[info.get("key") for info in (files.index_information()).values()],
195+
"Missing required index on files collection: {filename: 1, uploadDate: 1}",
198196
)
199197
files.drop()
200198

test/test_monitoring.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,8 @@ def test_kill_cursors(self):
510510
self.assertEqual(cursor.address, succeeded.connection_id)
511511
# There could be more than one cursor_id here depending on
512512
# when the thread last ran.
513-
self.assertTrue(
514-
cursor_id in succeeded.reply["cursorsUnknown"]
515-
or cursor_id in succeeded.reply["cursorsKilled"]
513+
self.assertIn(
514+
cursor_id, succeeded.reply["cursorsUnknown"] + succeeded.reply["cursorsKilled"]
516515
)
517516

518517
def test_non_bulk_writes(self):
@@ -1064,7 +1063,7 @@ def test_write_errors(self):
10641063
self.assertEqual(2, len(errors))
10651064
fields = {"index", "code", "errmsg"}
10661065
for error in errors:
1067-
self.assertTrue(fields.issubset(set(error)))
1066+
self.assertLessEqual(fields, set(error))
10681067

10691068
def test_first_batch_helper(self):
10701069
# Regardless of server version and use of helpers._first_batch

0 commit comments

Comments
 (0)