PYTHON-5387 Better test assertions for membership (#2348)

This commit is contained in:
Iris 2025-05-23 09:04:32 -07:00 committed by GitHub
parent 65089ead4c
commit b8460b6001
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 352 additions and 340 deletions

View File

@ -94,7 +94,7 @@ class AsyncBulkTestBase(AsyncIntegrationTest):
self.assertEqual(expected["index"], actual["index"])
if expected["_id"] == "...":
# Unspecified value.
self.assertTrue("_id" in actual)
self.assertIn("_id", actual)
else:
self.assertEqual(expected["_id"], actual["_id"])
@ -107,7 +107,7 @@ class AsyncBulkTestBase(AsyncIntegrationTest):
self.assertEqual(expected["code"], actual["code"])
if expected["errmsg"] == "...":
# Unspecified value.
self.assertTrue("errmsg" in actual)
self.assertIn("errmsg", actual)
else:
self.assertEqual(expected["errmsg"], actual["errmsg"])
@ -115,7 +115,7 @@ class AsyncBulkTestBase(AsyncIntegrationTest):
actual_op = actual["op"].copy()
if expected_op.get("_id") == "...":
# Unspecified _id.
self.assertTrue("_id" in actual_op)
self.assertIn("_id", actual_op)
actual_op.pop("_id")
expected_op.pop("_id")
@ -160,7 +160,7 @@ class AsyncTestBulk(AsyncBulkTestBase):
result = await self.coll.bulk_write([UpdateMany({}, update)])
self.assertEqualResponse(expected, result.bulk_api_result)
self.assertEqual(2, result.matched_count)
self.assertTrue(result.modified_count in (2, None))
self.assertIn(result.modified_count, (2, None))
async def test_update_many(self):
await self._test_update_many({"$set": {"foo": "bar"}})
@ -201,7 +201,7 @@ class AsyncTestBulk(AsyncBulkTestBase):
result = await self.coll.bulk_write([UpdateOne({}, update)])
self.assertEqualResponse(expected, result.bulk_api_result)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (1, None))
self.assertIn(result.modified_count, (1, None))
async def test_update_one(self):
await self._test_update_one({"$set": {"foo": "bar"}})
@ -227,7 +227,7 @@ class AsyncTestBulk(AsyncBulkTestBase):
result = await self.coll.bulk_write([ReplaceOne({}, {"foo": "bar"})])
self.assertEqualResponse(expected, result.bulk_api_result)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (1, None))
self.assertIn(result.modified_count, (1, None))
async def test_remove(self):
# Test removing all documents, ordered.
@ -1037,7 +1037,7 @@ class AsyncTestBulkWriteConcern(AsyncBulkTestBase):
self.assertTrue(len(details["writeConcernErrors"]) > 1)
failed = details["writeErrors"][0]
self.assertTrue("duplicate" in failed["errmsg"])
self.assertIn("duplicate", failed["errmsg"])
@async_client_context.require_version_max(7, 1) # PYTHON-4560
@async_client_context.require_replica_set

View File

@ -674,7 +674,7 @@ class TestClient(AsyncIntegrationTest):
async with server._pool.checkout() as conn:
pass
self.assertEqual(1, len(server._pool.conns))
self.assertTrue(conn in server._pool.conns)
self.assertIn(conn, server._pool.conns)
async def test_max_idle_time_reaper_removes_stale_minPoolSize(self):
with client_knobs(kill_cursor_frequency=0.1):
@ -752,7 +752,7 @@ class TestClient(AsyncIntegrationTest):
lambda: len(server._pool.conns) == 10,
"a closed socket gets replaced from the pool",
)
self.assertFalse(conn in server._pool.conns)
self.assertNotIn(conn, server._pool.conns)
async def test_max_idle_time_checkout(self):
# Use high frequency to test _get_socket_no_auth.
@ -769,8 +769,8 @@ class TestClient(AsyncIntegrationTest):
async with server._pool.checkout() as new_con:
self.assertNotEqual(conn, new_con)
self.assertEqual(1, len(server._pool.conns))
self.assertFalse(conn in server._pool.conns)
self.assertTrue(new_con in server._pool.conns)
self.assertNotIn(conn, server._pool.conns)
self.assertIn(new_con, server._pool.conns)
# Test that connections are reused if maxIdleTimeMS is not set.
client = await self.async_rs_or_single_client()
@ -1032,8 +1032,8 @@ class TestClient(AsyncIntegrationTest):
cmd_names = [doc["name"] for doc in cmd_docs]
db_names = await self.client.list_database_names()
self.assertTrue("pymongo_test" in db_names)
self.assertTrue("pymongo_test_mike" in db_names)
self.assertIn("pymongo_test", db_names)
self.assertIn("pymongo_test_mike", db_names)
self.assertEqual(db_names, cmd_names)
async def test_drop_database(self):
@ -1257,9 +1257,9 @@ class TestClient(AsyncIntegrationTest):
client = await self.async_rs_or_single_client(uri)
await client.pymongo_test.test.insert_one({"dummy": "object"})
dbs = await client.list_database_names()
self.assertTrue("pymongo_test" in dbs)
self.assertIn("pymongo_test", dbs)
self.assertTrue(mongodb_socket in repr(client))
self.assertIn(mongodb_socket, repr(client))
# Confirm it fails with a missing socket.
with self.assertRaises(ConnectionFailure):
@ -1431,8 +1431,8 @@ class TestClient(AsyncIntegrationTest):
await client.pymongo_test_bernie.test.insert_one({"dummy": "object"})
dbs = await client.list_database_names()
self.assertTrue("pymongo_test" in dbs)
self.assertTrue("pymongo_test_bernie" in dbs)
self.assertIn("pymongo_test", dbs)
self.assertIn("pymongo_test_bernie", dbs)
async def test_contextlib(self):
client = await self.async_rs_or_single_client()

View File

@ -212,7 +212,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
async def test_drop_nonexistent_collection(self):
await self.db.drop_collection("test")
self.assertFalse("test" in await self.db.list_collection_names())
self.assertNotIn("test", await self.db.list_collection_names())
# No exception
await self.db.drop_collection("test")
@ -248,7 +248,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
await db.test.drop_indexes()
self.assertEqual(len(await db.test.index_information()), 1)
await db.test.create_indexes([IndexModel("hello")])
self.assertTrue("hello_1" in await db.test.index_information())
self.assertIn("hello_1", await db.test.index_information())
await db.test.drop_indexes()
self.assertEqual(len(await db.test.index_information()), 1)
@ -257,7 +257,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
)
info = await db.test.index_information()
for name in names:
self.assertTrue(name in info)
self.assertIn(name, info)
await db.test.drop()
await db.test.insert_one({"a": 1})
@ -311,16 +311,16 @@ class AsyncTestCollection(AsyncIntegrationTest):
await db.test.drop_indexes()
self.assertEqual(len(await db.test.index_information()), 1)
await db.test.create_index("hello")
self.assertTrue("hello_1" in await db.test.index_information())
self.assertIn("hello_1", await db.test.index_information())
await db.test.drop_indexes()
self.assertEqual(len(await db.test.index_information()), 1)
await db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)])
self.assertTrue("hello_-1_world_1" in await db.test.index_information())
self.assertIn("hello_-1_world_1", await db.test.index_information())
await db.test.drop_indexes()
await db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)], name=None)
self.assertTrue("hello_-1_world_1" in await db.test.index_information())
self.assertIn("hello_-1_world_1", await db.test.index_information())
await db.test.drop()
await db.test.insert_one({"a": 1})
@ -349,7 +349,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
with self.assertRaises(OperationFailure):
await db.test.drop_index(name)
self.assertEqual(len(await db.test.index_information()), 2)
self.assertTrue("hello_1" in await db.test.index_information())
self.assertIn("hello_1", await db.test.index_information())
await db.test.drop_indexes()
await db.test.create_index("hello")
@ -359,7 +359,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
self.assertEqual(name, "goodbye_1")
await db.test.drop_index([("goodbye", ASCENDING)])
self.assertEqual(len(await db.test.index_information()), 2)
self.assertTrue("hello_1" in await db.test.index_information())
self.assertIn("hello_1", await db.test.index_information())
with self.write_concern_collection() as coll:
await coll.drop_index("hello_1")
@ -395,7 +395,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
indexes = await (await db.test.list_indexes()).to_list()
self.assertEqual(len(indexes), 1)
self.assertTrue("_id_" in map_indexes(indexes))
self.assertIn("_id_", map_indexes(indexes))
await db.test.create_index("hello")
indexes = await (await db.test.list_indexes()).to_list()
@ -424,7 +424,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
await db.test.drop()
await db.test.insert_one({}) # create collection
self.assertEqual(len(await db.test.index_information()), 1)
self.assertTrue("_id_" in await db.test.index_information())
self.assertIn("_id_", await db.test.index_information())
await db.test.create_index("hello")
self.assertEqual(len(await db.test.index_information()), 2)
@ -488,7 +488,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
await db.test.drop_indexes()
self.assertEqual("t_text", await db.test.create_index([("t", TEXT)]))
index_info = (await db.test.index_information())["t_text"]
self.assertTrue("weights" in index_info)
self.assertIn("weights", index_info)
await db.test.insert_many(
[{"t": "spam eggs and spam"}, {"t": "spam"}, {"t": "egg sausage and bacon"}]
@ -549,7 +549,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
await db.test.create_index([("keya", ASCENDING)])
await db.test.create_index([("keyb", ASCENDING)], background=False)
await db.test.create_index([("keyc", ASCENDING)], background=True)
self.assertFalse("background" in (await db.test.index_information())["keya_1"])
self.assertNotIn("background", (await db.test.index_information())["keya_1"])
self.assertFalse((await db.test.index_information())["keyb_1"]["background"])
self.assertTrue((await db.test.index_information())["keyc_1"]["background"])
@ -702,7 +702,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
doc = await anext(db.test.find({}, {"_id": False}))
l = list(doc)
self.assertFalse("_id" in l)
self.assertNotIn("_id", l)
async def test_options(self):
db = self.db
@ -764,7 +764,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, ObjectId)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, await db.test.count_documents({"_id": _id}))
self.assertTrue(result.acknowledged)
@ -776,7 +776,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, int)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, await db.test.count_documents({"_id": _id}))
self.assertTrue(result.acknowledged)
@ -943,7 +943,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, int)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, await db.test.count_documents({"x": doc["x"]}))
self.assertTrue(result.acknowledged)
docs = [{"_id": i, "a": 200 - i} for i in range(100, 200)]
@ -953,7 +953,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, int)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, await db.test.count_documents({"a": doc["a"]}))
self.assertTrue(result.acknowledged)
@ -1131,23 +1131,23 @@ class AsyncTestCollection(AsyncIntegrationTest):
)
self.assertEqual(1, await db.test.count_documents({}))
doc = await anext(db.test.find({}))
self.assertTrue("x" in doc)
self.assertIn("x", doc)
doc = await anext(db.test.find({}))
self.assertTrue("mike" in doc)
self.assertIn("mike", doc)
doc = await anext(db.test.find({}))
self.assertTrue("extra thing" in doc)
self.assertIn("extra thing", doc)
doc = await anext(db.test.find({}, ["x", "mike"]))
self.assertTrue("x" in doc)
self.assertIn("x", doc)
doc = await anext(db.test.find({}, ["x", "mike"]))
self.assertTrue("mike" in doc)
self.assertIn("mike", doc)
doc = await anext(db.test.find({}, ["x", "mike"]))
self.assertFalse("extra thing" in doc)
self.assertNotIn("extra thing", doc)
doc = await anext(db.test.find({}, ["mike"]))
self.assertFalse("x" in doc)
self.assertNotIn("x", doc)
doc = await anext(db.test.find({}, ["mike"]))
self.assertTrue("mike" in doc)
self.assertIn("mike", doc)
doc = await anext(db.test.find({}, ["mike"]))
self.assertFalse("extra thing" in doc)
self.assertNotIn("extra thing", doc)
@no_type_check
async def test_fields_specifier_as_dict(self):
@ -1158,8 +1158,8 @@ class AsyncTestCollection(AsyncIntegrationTest):
self.assertEqual([1, 2, 3], (await db.test.find_one())["x"])
self.assertEqual([2, 3], (await db.test.find_one(projection={"x": {"$slice": -2}}))["x"])
self.assertTrue("x" not in await db.test.find_one(projection={"x": 0}))
self.assertTrue("mike" in await db.test.find_one(projection={"x": 0}))
self.assertNotIn("x", await db.test.find_one(projection={"x": 0}))
self.assertIn("mike", await db.test.find_one(projection={"x": 0}))
async def test_find_w_regex(self):
db = self.db
@ -1194,7 +1194,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
async for x in db.test.find():
self.assertEqual(x["hello"], "world")
self.assertTrue("_id" in x)
self.assertIn("_id", x)
async def test_unique_index(self):
db = self.db
@ -1314,7 +1314,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
try:
await self.db.test.update_many({}, {"$thismodifierdoesntexist": 1})
except OperationFailure as exc:
self.assertTrue(exc.code in (9, 10147, 16840, 17009))
self.assertIn(exc.code, (9, 10147, 16840, 17009))
# Just check that we set the error document. Fields
# vary by MongoDB version.
self.assertTrue(exc.details is not None)
@ -1348,7 +1348,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.replace_one({"x": 1}, {"y": 1})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(1, await db.test.count_documents({"y": 1}))
@ -1359,7 +1359,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.replace_one({"y": 1}, replacement, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(1, await db.test.count_documents({"z": 1}))
@ -1369,7 +1369,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.replace_one({"x": 2}, {"y": 2}, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(0, result.matched_count)
self.assertTrue(result.modified_count in (None, 0))
self.assertIn(result.modified_count, (None, 0))
self.assertIsInstance(result.upserted_id, ObjectId)
self.assertTrue(result.acknowledged)
self.assertEqual(1, await db.test.count_documents({"y": 2}))
@ -1393,7 +1393,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.update_one({}, {"$inc": {"x": 1}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual((await db.test.find_one(id1))["x"], 6) # type: ignore
@ -1402,7 +1402,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.update_one({"x": 6}, {"$inc": {"x": 1}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual((await db.test.find_one(id1))["x"], 7) # type: ignore
@ -1411,7 +1411,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.update_one({"x": 2}, {"$set": {"y": 1}}, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(0, result.matched_count)
self.assertTrue(result.modified_count in (None, 0))
self.assertIn(result.modified_count, (None, 0))
self.assertIsInstance(result.upserted_id, ObjectId)
self.assertTrue(result.acknowledged)
@ -1450,7 +1450,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.update_many({"x": 4}, {"$set": {"y": 5}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(2, result.matched_count)
self.assertTrue(result.modified_count in (None, 2))
self.assertIn(result.modified_count, (None, 2))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(3, await db.test.count_documents({"y": 5}))
@ -1458,7 +1458,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.update_many({"x": 5}, {"$set": {"y": 6}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(1, await db.test.count_documents({"y": 6}))
@ -1466,7 +1466,7 @@ class AsyncTestCollection(AsyncIntegrationTest):
result = await db.test.update_many({"x": 2}, {"$set": {"y": 1}}, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(0, result.matched_count)
self.assertTrue(result.modified_count in (None, 0))
self.assertIn(result.modified_count, (None, 0))
self.assertIsInstance(result.upserted_id, ObjectId)
self.assertTrue(result.acknowledged)
@ -1725,21 +1725,21 @@ class AsyncTestCollection(AsyncIntegrationTest):
self.assertEqual(await db.test.find_one({}), await db.test.find_one())
self.assertEqual(await db.test.find_one({"hello": "world"}), await db.test.find_one())
self.assertTrue("hello" in await db.test.find_one(projection=["hello"]))
self.assertTrue("hello" not in await db.test.find_one(projection=["foo"]))
self.assertIn("hello", await db.test.find_one(projection=["hello"]))
self.assertNotIn("hello", await db.test.find_one(projection=["foo"]))
self.assertTrue("hello" in await db.test.find_one(projection=("hello",)))
self.assertTrue("hello" not in await db.test.find_one(projection=("foo",)))
self.assertIn("hello", await db.test.find_one(projection=("hello",)))
self.assertNotIn("hello", await db.test.find_one(projection=("foo",)))
self.assertTrue("hello" in await db.test.find_one(projection={"hello"}))
self.assertTrue("hello" not in await db.test.find_one(projection={"foo"}))
self.assertIn("hello", await db.test.find_one(projection={"hello"}))
self.assertNotIn("hello", await db.test.find_one(projection={"foo"}))
self.assertTrue("hello" in await db.test.find_one(projection=frozenset(["hello"])))
self.assertTrue("hello" not in await db.test.find_one(projection=frozenset(["foo"])))
self.assertIn("hello", await db.test.find_one(projection=frozenset(["hello"])))
self.assertNotIn("hello", await db.test.find_one(projection=frozenset(["foo"])))
self.assertEqual(["_id"], list(await db.test.find_one(projection={"_id": True})))
self.assertTrue("hello" in list(await db.test.find_one(projection={})))
self.assertTrue("hello" in list(await db.test.find_one(projection=[])))
self.assertIn("hello", list(await db.test.find_one(projection={})))
self.assertIn("hello", list(await db.test.find_one(projection=[])))
self.assertEqual(None, await db.test.find_one({"hello": "foo"}))
self.assertEqual(None, await db.test.find_one(ObjectId()))

View File

@ -174,8 +174,8 @@ class TestCursor(AsyncIntegrationTest):
cursor = coll.find().max_time_ms(999)
c2 = cursor.clone()
self.assertEqual(999, c2._max_time_ms)
self.assertTrue("$maxTimeMS" in cursor._query_spec())
self.assertTrue("$maxTimeMS" in c2._query_spec())
self.assertIn("$maxTimeMS", cursor._query_spec())
self.assertIn("$maxTimeMS", c2._query_spec())
self.assertTrue(await coll.find_one(max_time_ms=1000))
@ -240,19 +240,19 @@ class TestCursor(AsyncIntegrationTest):
# Tailable_await defaults.
await coll.find(cursor_type=CursorType.TAILABLE_AWAIT).to_list()
# find
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
self.assertNotIn("maxTimeMS", listener.started_events[0].command)
# getMore
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
listener.reset()
# Tailable_await with max_await_time_ms set.
await coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
self.assertNotIn("maxTimeMS", listener.started_events[0].command)
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[1].command)
self.assertIn("maxTimeMS", listener.started_events[1].command)
self.assertEqual(99, listener.started_events[1].command["maxTimeMS"])
listener.reset()
@ -263,11 +263,11 @@ class TestCursor(AsyncIntegrationTest):
await coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
listener.reset()
# Tailable_await with both max_time_ms and max_await_time_ms
@ -279,11 +279,11 @@ class TestCursor(AsyncIntegrationTest):
)
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[1].command)
self.assertIn("maxTimeMS", listener.started_events[1].command)
self.assertEqual(99, listener.started_events[1].command["maxTimeMS"])
listener.reset()
@ -291,31 +291,31 @@ class TestCursor(AsyncIntegrationTest):
await coll.find(batch_size=1).max_await_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
self.assertNotIn("maxTimeMS", listener.started_events[0].command)
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
listener.reset()
# Non tailable_await with max_time_ms
await coll.find(batch_size=1).max_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
# Non tailable_await with both max_time_ms and max_await_time_ms
await coll.find(batch_size=1).max_time_ms(99).max_await_time_ms(88).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
@async_client_context.require_test_commands
@async_client_context.require_no_mongos
@ -933,16 +933,19 @@ class TestCursor(AsyncIntegrationTest):
# Shallow copies can so can mutate
cursor2 = copy.copy(cursor)
cursor2._projection["cursor2"] = False
self.assertTrue(cursor._projection and "cursor2" in cursor._projection)
self.assertIsNotNone(cursor._projection)
self.assertIn("cursor2", cursor._projection.keys())
# Deepcopies and shouldn't mutate
cursor3 = copy.deepcopy(cursor)
cursor3._projection["cursor3"] = False
self.assertFalse(cursor._projection and "cursor3" in cursor._projection)
self.assertIsNotNone(cursor._projection)
self.assertNotIn("cursor3", cursor._projection.keys())
cursor4 = cursor.clone()
cursor4._projection["cursor4"] = False
self.assertFalse(cursor._projection and "cursor4" in cursor._projection)
self.assertIsNotNone(cursor._projection)
self.assertNotIn("cursor4", cursor._projection.keys())
# Test memo when deepcopying queries
query = {"hello": "world"}

View File

@ -163,13 +163,13 @@ class TestDatabase(AsyncIntegrationTest):
await db.create_collection("coll..ection") # type: ignore[arg-type]
test = await db.create_collection("test")
self.assertTrue("test" in await db.list_collection_names())
self.assertIn("test", await db.list_collection_names())
await test.insert_one({"hello": "world"})
self.assertEqual((await db.test.find_one())["hello"], "world")
await db.drop_collection("test.foo")
await db.create_collection("test.foo")
self.assertTrue("test.foo" in await db.list_collection_names())
self.assertIn("test.foo", await db.list_collection_names())
with self.assertRaises(CollectionInvalid):
await db.create_collection("test.foo")
@ -179,10 +179,10 @@ class TestDatabase(AsyncIntegrationTest):
await db.test.mike.insert_one({"dummy": "object"})
colls = await db.list_collection_names()
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
self.assertIn("test", colls)
self.assertIn("test.mike", colls)
for coll in colls:
self.assertTrue("$" not in coll)
self.assertNotIn("$", coll)
await db.systemcoll.test.insert_one({})
no_system_collections = await db.list_collection_names(
@ -252,12 +252,12 @@ class TestDatabase(AsyncIntegrationTest):
colls = [result["name"] async for result in results]
# All the collections present.
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
self.assertIn("test", colls)
self.assertIn("test.mike", colls)
# No collection containing a '$'.
for coll in colls:
self.assertTrue("$" not in coll)
self.assertNotIn("$", coll)
# Duplicate check.
coll_cnt: dict = {}
@ -294,12 +294,12 @@ class TestDatabase(AsyncIntegrationTest):
colls = [result["name"] async for result in results]
# Checking only capped collections are present
self.assertTrue("test" in colls)
self.assertFalse("test.mike" in colls)
self.assertIn("test", colls)
self.assertNotIn("test.mike", colls)
# No collection containing a '$'.
for coll in colls:
self.assertTrue("$" not in coll)
self.assertNotIn("$", coll)
# Duplicate check.
coll_cnt = {}
@ -339,24 +339,24 @@ class TestDatabase(AsyncIntegrationTest):
await db.drop_collection(None) # type: ignore[arg-type]
await db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in await db.list_collection_names())
self.assertIn("test", await db.list_collection_names())
await db.drop_collection("test")
self.assertFalse("test" in await db.list_collection_names())
self.assertNotIn("test", await db.list_collection_names())
await db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in await db.list_collection_names())
self.assertIn("test", await db.list_collection_names())
await db.drop_collection("test")
self.assertFalse("test" in await db.list_collection_names())
self.assertNotIn("test", await db.list_collection_names())
await db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in await db.list_collection_names())
self.assertIn("test", await db.list_collection_names())
await db.drop_collection(db.test)
self.assertFalse("test" in await db.list_collection_names())
self.assertNotIn("test", await db.list_collection_names())
await db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in await db.list_collection_names())
self.assertIn("test", await db.list_collection_names())
await db.test.drop()
self.assertFalse("test" in await db.list_collection_names())
self.assertNotIn("test", await db.list_collection_names())
await db.test.drop()
await db.drop_collection(db.test.doesnotexist)

View File

@ -479,77 +479,77 @@ class TestSampleShellCommands(AsyncIntegrationTest):
# End Example 44
async for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertFalse("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertNotIn("instock", doc)
# Start Example 45
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "_id": 0})
# End Example 45
async for doc in cursor:
self.assertFalse("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertFalse("instock" in doc)
self.assertNotIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertNotIn("instock", doc)
# Start Example 46
cursor = db.inventory.find({"status": "A"}, {"status": 0, "instock": 0})
# End Example 46
async for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertFalse("status" in doc)
self.assertTrue("size" in doc)
self.assertFalse("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertNotIn("status", doc)
self.assertIn("size", doc)
self.assertNotIn("instock", doc)
# Start Example 47
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "size.uom": 1})
# End Example 47
async for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertTrue("size" in doc)
self.assertFalse("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertIn("size", doc)
self.assertNotIn("instock", doc)
size = doc["size"]
self.assertTrue("uom" in size)
self.assertFalse("h" in size)
self.assertFalse("w" in size)
self.assertIn("uom", size)
self.assertNotIn("h", size)
self.assertNotIn("w", size)
# Start Example 48
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
# End Example 48
async for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertTrue("size" in doc)
self.assertTrue("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertIn("size", doc)
self.assertIn("instock", doc)
size = doc["size"]
self.assertFalse("uom" in size)
self.assertTrue("h" in size)
self.assertTrue("w" in size)
self.assertNotIn("uom", size)
self.assertIn("h", size)
self.assertIn("w", size)
# Start Example 49
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "instock.qty": 1})
# End Example 49
async for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertTrue("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertIn("instock", doc)
for subdoc in doc["instock"]:
self.assertFalse("warehouse" in subdoc)
self.assertTrue("qty" in subdoc)
self.assertNotIn("warehouse", subdoc)
self.assertIn("qty", subdoc)
# Start Example 50
cursor = db.inventory.find(
@ -558,11 +558,11 @@ class TestSampleShellCommands(AsyncIntegrationTest):
# End Example 50
async for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertTrue("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertIn("instock", doc)
self.assertEqual(len(doc["instock"]), 1)
async def test_update_and_replace(self):
@ -645,7 +645,7 @@ class TestSampleShellCommands(AsyncIntegrationTest):
async for doc in db.inventory.find({"item": "paper"}):
self.assertEqual(doc["size"]["uom"], "cm")
self.assertEqual(doc["status"], "P")
self.assertTrue("lastModified" in doc)
self.assertIn("lastModified", doc)
# Start Example 53
await db.inventory.update_many(
@ -657,7 +657,7 @@ class TestSampleShellCommands(AsyncIntegrationTest):
async for doc in db.inventory.find({"qty": {"$lt": 50}}):
self.assertEqual(doc["size"]["uom"], "in")
self.assertEqual(doc["status"], "P")
self.assertTrue("lastModified" in doc)
self.assertIn("lastModified", doc)
# Start Example 54
await db.inventory.replace_one(
@ -671,8 +671,8 @@ class TestSampleShellCommands(AsyncIntegrationTest):
async for doc in db.inventory.find({"item": "paper"}, {"_id": 0}):
self.assertEqual(len(doc.keys()), 2)
self.assertTrue("item" in doc)
self.assertTrue("instock" in doc)
self.assertIn("item", doc)
self.assertIn("instock", doc)
self.assertEqual(len(doc["instock"]), 2)
async def test_delete(self):

View File

@ -25,4 +25,4 @@ class TestJsonUtilRoundtrip(AsyncIntegrationTest):
await db.test.insert_many(docs)
reloaded_docs = json_util.loads(json_util.dumps(await (db.test.find()).to_list()))
for doc in docs:
self.assertTrue(doc in reloaded_docs)
self.assertIn(doc, reloaded_docs)

View File

@ -1088,8 +1088,8 @@ class AsyncTestCommandMonitoring(AsyncIntegrationTest):
self.assertEqual(started.command_name, succeeded.command_name)
self.assertEqual(started.request_id, succeeded.request_id)
self.assertEqual(started.connection_id, succeeded.connection_id)
self.assertTrue("cursor" in succeeded.reply)
self.assertTrue("ok" in succeeded.reply)
self.assertIn("cursor", succeeded.reply)
self.assertIn("ok", succeeded.reply)
self.listener.reset()

View File

@ -134,8 +134,9 @@ class TestSession(AsyncIntegrationTest):
await f(*args, **kw)
self.assertGreaterEqual(len(listener.started_events), 1)
for event in listener.started_events:
self.assertTrue(
"lsid" in event.command,
self.assertIn(
"lsid",
event.command,
f"{f.__name__} sent no lsid with {event.command_name}",
)
@ -170,8 +171,9 @@ class TestSession(AsyncIntegrationTest):
self.assertGreaterEqual(len(listener.started_events), 1)
lsids = []
for event in listener.started_events:
self.assertTrue(
"lsid" in event.command,
self.assertIn(
"lsid",
event.command,
f"{f.__name__} sent no lsid with {event.command_name}",
)
@ -422,8 +424,9 @@ class TestSession(AsyncIntegrationTest):
await f(session=s)
self.assertGreaterEqual(len(listener.started_events), 1)
for event in listener.started_events:
self.assertTrue(
"lsid" in event.command,
self.assertIn(
"lsid",
event.command,
f"{name} sent no lsid with {event.command_name}",
)
@ -441,15 +444,13 @@ class TestSession(AsyncIntegrationTest):
listener.reset()
await f(session=None)
event0 = listener.first_command_started()
self.assertTrue(
"lsid" in event0.command, f"{name} sent no lsid with {event0.command_name}"
)
self.assertIn("lsid", event0.command, f"{name} sent no lsid with {event0.command_name}")
lsid = event0.command["lsid"]
for event in listener.started_events[1:]:
self.assertTrue(
"lsid" in event.command, f"{name} sent no lsid with {event.command_name}"
self.assertIn(
"lsid", event.command, f"{name} sent no lsid with {event.command_name}"
)
self.assertEqual(
@ -1201,15 +1202,17 @@ class TestClusterTime(AsyncIntegrationTest):
self.assertGreaterEqual(len(listener.started_events), 1)
for i, event in enumerate(listener.started_events):
self.assertTrue(
"$clusterTime" in event.command,
self.assertIn(
"$clusterTime",
event.command,
f"{f.__name__} sent no $clusterTime with {event.command_name}",
)
if i > 0:
succeeded = listener.succeeded_events[i - 1]
self.assertTrue(
"$clusterTime" in succeeded.reply,
self.assertIn(
"$clusterTime",
succeeded.reply,
f"{f.__name__} received no $clusterTime with {succeeded.command_name}",
)

View File

@ -94,7 +94,7 @@ class BulkTestBase(IntegrationTest):
self.assertEqual(expected["index"], actual["index"])
if expected["_id"] == "...":
# Unspecified value.
self.assertTrue("_id" in actual)
self.assertIn("_id", actual)
else:
self.assertEqual(expected["_id"], actual["_id"])
@ -107,7 +107,7 @@ class BulkTestBase(IntegrationTest):
self.assertEqual(expected["code"], actual["code"])
if expected["errmsg"] == "...":
# Unspecified value.
self.assertTrue("errmsg" in actual)
self.assertIn("errmsg", actual)
else:
self.assertEqual(expected["errmsg"], actual["errmsg"])
@ -115,7 +115,7 @@ class BulkTestBase(IntegrationTest):
actual_op = actual["op"].copy()
if expected_op.get("_id") == "...":
# Unspecified _id.
self.assertTrue("_id" in actual_op)
self.assertIn("_id", actual_op)
actual_op.pop("_id")
expected_op.pop("_id")
@ -160,7 +160,7 @@ class TestBulk(BulkTestBase):
result = self.coll.bulk_write([UpdateMany({}, update)])
self.assertEqualResponse(expected, result.bulk_api_result)
self.assertEqual(2, result.matched_count)
self.assertTrue(result.modified_count in (2, None))
self.assertIn(result.modified_count, (2, None))
def test_update_many(self):
self._test_update_many({"$set": {"foo": "bar"}})
@ -201,7 +201,7 @@ class TestBulk(BulkTestBase):
result = self.coll.bulk_write([UpdateOne({}, update)])
self.assertEqualResponse(expected, result.bulk_api_result)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (1, None))
self.assertIn(result.modified_count, (1, None))
def test_update_one(self):
self._test_update_one({"$set": {"foo": "bar"}})
@ -227,7 +227,7 @@ class TestBulk(BulkTestBase):
result = self.coll.bulk_write([ReplaceOne({}, {"foo": "bar"})])
self.assertEqualResponse(expected, result.bulk_api_result)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (1, None))
self.assertIn(result.modified_count, (1, None))
def test_remove(self):
# Test removing all documents, ordered.
@ -1035,7 +1035,7 @@ class TestBulkWriteConcern(BulkTestBase):
self.assertTrue(len(details["writeConcernErrors"]) > 1)
failed = details["writeErrors"][0]
self.assertTrue("duplicate" in failed["errmsg"])
self.assertIn("duplicate", failed["errmsg"])
@client_context.require_version_max(7, 1) # PYTHON-4560
@client_context.require_replica_set

View File

@ -665,7 +665,7 @@ class TestClient(IntegrationTest):
with server._pool.checkout() as conn:
pass
self.assertEqual(1, len(server._pool.conns))
self.assertTrue(conn in server._pool.conns)
self.assertIn(conn, server._pool.conns)
def test_max_idle_time_reaper_removes_stale_minPoolSize(self):
with client_knobs(kill_cursor_frequency=0.1):
@ -731,7 +731,7 @@ class TestClient(IntegrationTest):
lambda: len(server._pool.conns) == 10,
"a closed socket gets replaced from the pool",
)
self.assertFalse(conn in server._pool.conns)
self.assertNotIn(conn, server._pool.conns)
def test_max_idle_time_checkout(self):
# Use high frequency to test _get_socket_no_auth.
@ -746,8 +746,8 @@ class TestClient(IntegrationTest):
with server._pool.checkout() as new_con:
self.assertNotEqual(conn, new_con)
self.assertEqual(1, len(server._pool.conns))
self.assertFalse(conn in server._pool.conns)
self.assertTrue(new_con in server._pool.conns)
self.assertNotIn(conn, server._pool.conns)
self.assertIn(new_con, server._pool.conns)
# Test that connections are reused if maxIdleTimeMS is not set.
client = self.rs_or_single_client()
@ -1005,8 +1005,8 @@ class TestClient(IntegrationTest):
cmd_names = [doc["name"] for doc in cmd_docs]
db_names = self.client.list_database_names()
self.assertTrue("pymongo_test" in db_names)
self.assertTrue("pymongo_test_mike" in db_names)
self.assertIn("pymongo_test", db_names)
self.assertIn("pymongo_test_mike", db_names)
self.assertEqual(db_names, cmd_names)
def test_drop_database(self):
@ -1220,9 +1220,9 @@ class TestClient(IntegrationTest):
client = self.rs_or_single_client(uri)
client.pymongo_test.test.insert_one({"dummy": "object"})
dbs = client.list_database_names()
self.assertTrue("pymongo_test" in dbs)
self.assertIn("pymongo_test", dbs)
self.assertTrue(mongodb_socket in repr(client))
self.assertIn(mongodb_socket, repr(client))
# Confirm it fails with a missing socket.
with self.assertRaises(ConnectionFailure):
@ -1390,8 +1390,8 @@ class TestClient(IntegrationTest):
client.pymongo_test_bernie.test.insert_one({"dummy": "object"})
dbs = client.list_database_names()
self.assertTrue("pymongo_test" in dbs)
self.assertTrue("pymongo_test_bernie" in dbs)
self.assertIn("pymongo_test", dbs)
self.assertIn("pymongo_test_bernie", dbs)
def test_contextlib(self):
client = self.rs_or_single_client()

View File

@ -212,7 +212,7 @@ class TestCollection(IntegrationTest):
def test_drop_nonexistent_collection(self):
self.db.drop_collection("test")
self.assertFalse("test" in self.db.list_collection_names())
self.assertNotIn("test", self.db.list_collection_names())
# No exception
self.db.drop_collection("test")
@ -248,7 +248,7 @@ class TestCollection(IntegrationTest):
db.test.drop_indexes()
self.assertEqual(len(db.test.index_information()), 1)
db.test.create_indexes([IndexModel("hello")])
self.assertTrue("hello_1" in db.test.index_information())
self.assertIn("hello_1", db.test.index_information())
db.test.drop_indexes()
self.assertEqual(len(db.test.index_information()), 1)
@ -257,7 +257,7 @@ class TestCollection(IntegrationTest):
)
info = db.test.index_information()
for name in names:
self.assertTrue(name in info)
self.assertIn(name, info)
db.test.drop()
db.test.insert_one({"a": 1})
@ -309,16 +309,16 @@ class TestCollection(IntegrationTest):
db.test.drop_indexes()
self.assertEqual(len(db.test.index_information()), 1)
db.test.create_index("hello")
self.assertTrue("hello_1" in db.test.index_information())
self.assertIn("hello_1", db.test.index_information())
db.test.drop_indexes()
self.assertEqual(len(db.test.index_information()), 1)
db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)])
self.assertTrue("hello_-1_world_1" in db.test.index_information())
self.assertIn("hello_-1_world_1", db.test.index_information())
db.test.drop_indexes()
db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)], name=None)
self.assertTrue("hello_-1_world_1" in db.test.index_information())
self.assertIn("hello_-1_world_1", db.test.index_information())
db.test.drop()
db.test.insert_one({"a": 1})
@ -347,7 +347,7 @@ class TestCollection(IntegrationTest):
with self.assertRaises(OperationFailure):
db.test.drop_index(name)
self.assertEqual(len(db.test.index_information()), 2)
self.assertTrue("hello_1" in db.test.index_information())
self.assertIn("hello_1", db.test.index_information())
db.test.drop_indexes()
db.test.create_index("hello")
@ -357,7 +357,7 @@ class TestCollection(IntegrationTest):
self.assertEqual(name, "goodbye_1")
db.test.drop_index([("goodbye", ASCENDING)])
self.assertEqual(len(db.test.index_information()), 2)
self.assertTrue("hello_1" in db.test.index_information())
self.assertIn("hello_1", db.test.index_information())
with self.write_concern_collection() as coll:
coll.drop_index("hello_1")
@ -389,7 +389,7 @@ class TestCollection(IntegrationTest):
indexes = (db.test.list_indexes()).to_list()
self.assertEqual(len(indexes), 1)
self.assertTrue("_id_" in map_indexes(indexes))
self.assertIn("_id_", map_indexes(indexes))
db.test.create_index("hello")
indexes = (db.test.list_indexes()).to_list()
@ -418,7 +418,7 @@ class TestCollection(IntegrationTest):
db.test.drop()
db.test.insert_one({}) # create collection
self.assertEqual(len(db.test.index_information()), 1)
self.assertTrue("_id_" in db.test.index_information())
self.assertIn("_id_", db.test.index_information())
db.test.create_index("hello")
self.assertEqual(len(db.test.index_information()), 2)
@ -478,7 +478,7 @@ class TestCollection(IntegrationTest):
db.test.drop_indexes()
self.assertEqual("t_text", db.test.create_index([("t", TEXT)]))
index_info = (db.test.index_information())["t_text"]
self.assertTrue("weights" in index_info)
self.assertIn("weights", index_info)
db.test.insert_many(
[{"t": "spam eggs and spam"}, {"t": "spam"}, {"t": "egg sausage and bacon"}]
@ -539,7 +539,7 @@ class TestCollection(IntegrationTest):
db.test.create_index([("keya", ASCENDING)])
db.test.create_index([("keyb", ASCENDING)], background=False)
db.test.create_index([("keyc", ASCENDING)], background=True)
self.assertFalse("background" in (db.test.index_information())["keya_1"])
self.assertNotIn("background", (db.test.index_information())["keya_1"])
self.assertFalse((db.test.index_information())["keyb_1"]["background"])
self.assertTrue((db.test.index_information())["keyc_1"]["background"])
@ -690,7 +690,7 @@ class TestCollection(IntegrationTest):
doc = next(db.test.find({}, {"_id": False}))
l = list(doc)
self.assertFalse("_id" in l)
self.assertNotIn("_id", l)
def test_options(self):
db = self.db
@ -752,7 +752,7 @@ class TestCollection(IntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, ObjectId)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, db.test.count_documents({"_id": _id}))
self.assertTrue(result.acknowledged)
@ -764,7 +764,7 @@ class TestCollection(IntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, int)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, db.test.count_documents({"_id": _id}))
self.assertTrue(result.acknowledged)
@ -931,7 +931,7 @@ class TestCollection(IntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, int)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, db.test.count_documents({"x": doc["x"]}))
self.assertTrue(result.acknowledged)
docs = [{"_id": i, "a": 200 - i} for i in range(100, 200)]
@ -941,7 +941,7 @@ class TestCollection(IntegrationTest):
for doc in docs:
_id = doc["_id"]
self.assertIsInstance(_id, int)
self.assertTrue(_id in result.inserted_ids)
self.assertIn(_id, result.inserted_ids)
self.assertEqual(1, db.test.count_documents({"a": doc["a"]}))
self.assertTrue(result.acknowledged)
@ -1117,23 +1117,23 @@ class TestCollection(IntegrationTest):
db.test.insert_one({"x": 1, "mike": "awesome", "extra thing": "abcdefghijklmnopqrstuvwxyz"})
self.assertEqual(1, db.test.count_documents({}))
doc = next(db.test.find({}))
self.assertTrue("x" in doc)
self.assertIn("x", doc)
doc = next(db.test.find({}))
self.assertTrue("mike" in doc)
self.assertIn("mike", doc)
doc = next(db.test.find({}))
self.assertTrue("extra thing" in doc)
self.assertIn("extra thing", doc)
doc = next(db.test.find({}, ["x", "mike"]))
self.assertTrue("x" in doc)
self.assertIn("x", doc)
doc = next(db.test.find({}, ["x", "mike"]))
self.assertTrue("mike" in doc)
self.assertIn("mike", doc)
doc = next(db.test.find({}, ["x", "mike"]))
self.assertFalse("extra thing" in doc)
self.assertNotIn("extra thing", doc)
doc = next(db.test.find({}, ["mike"]))
self.assertFalse("x" in doc)
self.assertNotIn("x", doc)
doc = next(db.test.find({}, ["mike"]))
self.assertTrue("mike" in doc)
self.assertIn("mike", doc)
doc = next(db.test.find({}, ["mike"]))
self.assertFalse("extra thing" in doc)
self.assertNotIn("extra thing", doc)
@no_type_check
def test_fields_specifier_as_dict(self):
@ -1144,8 +1144,8 @@ class TestCollection(IntegrationTest):
self.assertEqual([1, 2, 3], (db.test.find_one())["x"])
self.assertEqual([2, 3], (db.test.find_one(projection={"x": {"$slice": -2}}))["x"])
self.assertTrue("x" not in db.test.find_one(projection={"x": 0}))
self.assertTrue("mike" in db.test.find_one(projection={"x": 0}))
self.assertNotIn("x", db.test.find_one(projection={"x": 0}))
self.assertIn("mike", db.test.find_one(projection={"x": 0}))
def test_find_w_regex(self):
db = self.db
@ -1180,7 +1180,7 @@ class TestCollection(IntegrationTest):
for x in db.test.find():
self.assertEqual(x["hello"], "world")
self.assertTrue("_id" in x)
self.assertIn("_id", x)
def test_unique_index(self):
db = self.db
@ -1300,7 +1300,7 @@ class TestCollection(IntegrationTest):
try:
self.db.test.update_many({}, {"$thismodifierdoesntexist": 1})
except OperationFailure as exc:
self.assertTrue(exc.code in (9, 10147, 16840, 17009))
self.assertIn(exc.code, (9, 10147, 16840, 17009))
# Just check that we set the error document. Fields
# vary by MongoDB version.
self.assertTrue(exc.details is not None)
@ -1334,7 +1334,7 @@ class TestCollection(IntegrationTest):
result = db.test.replace_one({"x": 1}, {"y": 1})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(1, db.test.count_documents({"y": 1}))
@ -1345,7 +1345,7 @@ class TestCollection(IntegrationTest):
result = db.test.replace_one({"y": 1}, replacement, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(1, db.test.count_documents({"z": 1}))
@ -1355,7 +1355,7 @@ class TestCollection(IntegrationTest):
result = db.test.replace_one({"x": 2}, {"y": 2}, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(0, result.matched_count)
self.assertTrue(result.modified_count in (None, 0))
self.assertIn(result.modified_count, (None, 0))
self.assertIsInstance(result.upserted_id, ObjectId)
self.assertTrue(result.acknowledged)
self.assertEqual(1, db.test.count_documents({"y": 2}))
@ -1379,7 +1379,7 @@ class TestCollection(IntegrationTest):
result = db.test.update_one({}, {"$inc": {"x": 1}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual((db.test.find_one(id1))["x"], 6) # type: ignore
@ -1388,7 +1388,7 @@ class TestCollection(IntegrationTest):
result = db.test.update_one({"x": 6}, {"$inc": {"x": 1}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual((db.test.find_one(id1))["x"], 7) # type: ignore
@ -1397,7 +1397,7 @@ class TestCollection(IntegrationTest):
result = db.test.update_one({"x": 2}, {"$set": {"y": 1}}, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(0, result.matched_count)
self.assertTrue(result.modified_count in (None, 0))
self.assertIn(result.modified_count, (None, 0))
self.assertIsInstance(result.upserted_id, ObjectId)
self.assertTrue(result.acknowledged)
@ -1436,7 +1436,7 @@ class TestCollection(IntegrationTest):
result = db.test.update_many({"x": 4}, {"$set": {"y": 5}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(2, result.matched_count)
self.assertTrue(result.modified_count in (None, 2))
self.assertIn(result.modified_count, (None, 2))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(3, db.test.count_documents({"y": 5}))
@ -1444,7 +1444,7 @@ class TestCollection(IntegrationTest):
result = db.test.update_many({"x": 5}, {"$set": {"y": 6}})
self.assertIsInstance(result, UpdateResult)
self.assertEqual(1, result.matched_count)
self.assertTrue(result.modified_count in (None, 1))
self.assertIn(result.modified_count, (None, 1))
self.assertIsNone(result.upserted_id)
self.assertTrue(result.acknowledged)
self.assertEqual(1, db.test.count_documents({"y": 6}))
@ -1452,7 +1452,7 @@ class TestCollection(IntegrationTest):
result = db.test.update_many({"x": 2}, {"$set": {"y": 1}}, True)
self.assertIsInstance(result, UpdateResult)
self.assertEqual(0, result.matched_count)
self.assertTrue(result.modified_count in (None, 0))
self.assertIn(result.modified_count, (None, 0))
self.assertIsInstance(result.upserted_id, ObjectId)
self.assertTrue(result.acknowledged)
@ -1707,21 +1707,21 @@ class TestCollection(IntegrationTest):
self.assertEqual(db.test.find_one({}), db.test.find_one())
self.assertEqual(db.test.find_one({"hello": "world"}), db.test.find_one())
self.assertTrue("hello" in db.test.find_one(projection=["hello"]))
self.assertTrue("hello" not in db.test.find_one(projection=["foo"]))
self.assertIn("hello", db.test.find_one(projection=["hello"]))
self.assertNotIn("hello", db.test.find_one(projection=["foo"]))
self.assertTrue("hello" in db.test.find_one(projection=("hello",)))
self.assertTrue("hello" not in db.test.find_one(projection=("foo",)))
self.assertIn("hello", db.test.find_one(projection=("hello",)))
self.assertNotIn("hello", db.test.find_one(projection=("foo",)))
self.assertTrue("hello" in db.test.find_one(projection={"hello"}))
self.assertTrue("hello" not in db.test.find_one(projection={"foo"}))
self.assertIn("hello", db.test.find_one(projection={"hello"}))
self.assertNotIn("hello", db.test.find_one(projection={"foo"}))
self.assertTrue("hello" in db.test.find_one(projection=frozenset(["hello"])))
self.assertTrue("hello" not in db.test.find_one(projection=frozenset(["foo"])))
self.assertIn("hello", db.test.find_one(projection=frozenset(["hello"])))
self.assertNotIn("hello", db.test.find_one(projection=frozenset(["foo"])))
self.assertEqual(["_id"], list(db.test.find_one(projection={"_id": True})))
self.assertTrue("hello" in list(db.test.find_one(projection={})))
self.assertTrue("hello" in list(db.test.find_one(projection=[])))
self.assertIn("hello", list(db.test.find_one(projection={})))
self.assertIn("hello", list(db.test.find_one(projection=[])))
self.assertEqual(None, db.test.find_one({"hello": "foo"}))
self.assertEqual(None, db.test.find_one(ObjectId()))

View File

@ -174,8 +174,8 @@ class TestCursor(IntegrationTest):
cursor = coll.find().max_time_ms(999)
c2 = cursor.clone()
self.assertEqual(999, c2._max_time_ms)
self.assertTrue("$maxTimeMS" in cursor._query_spec())
self.assertTrue("$maxTimeMS" in c2._query_spec())
self.assertIn("$maxTimeMS", cursor._query_spec())
self.assertIn("$maxTimeMS", c2._query_spec())
self.assertTrue(coll.find_one(max_time_ms=1000))
@ -236,19 +236,19 @@ class TestCursor(IntegrationTest):
# Tailable_defaults.
coll.find(cursor_type=CursorType.TAILABLE_AWAIT).to_list()
# find
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
self.assertNotIn("maxTimeMS", listener.started_events[0].command)
# getMore
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
listener.reset()
# Tailable_with max_await_time_ms set.
coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
self.assertNotIn("maxTimeMS", listener.started_events[0].command)
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[1].command)
self.assertIn("maxTimeMS", listener.started_events[1].command)
self.assertEqual(99, listener.started_events[1].command["maxTimeMS"])
listener.reset()
@ -259,11 +259,11 @@ class TestCursor(IntegrationTest):
coll.find(cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
listener.reset()
# Tailable_with both max_time_ms and max_await_time_ms
@ -275,11 +275,11 @@ class TestCursor(IntegrationTest):
)
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[1].command)
self.assertIn("maxTimeMS", listener.started_events[1].command)
self.assertEqual(99, listener.started_events[1].command["maxTimeMS"])
listener.reset()
@ -287,31 +287,31 @@ class TestCursor(IntegrationTest):
coll.find(batch_size=1).max_await_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[0].command)
self.assertNotIn("maxTimeMS", listener.started_events[0].command)
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
listener.reset()
# Non tailable_await with max_time_ms
coll.find(batch_size=1).max_time_ms(99).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
# Non tailable_await with both max_time_ms and max_await_time_ms
coll.find(batch_size=1).max_time_ms(99).max_await_time_ms(88).to_list()
# find
self.assertEqual("find", listener.started_events[0].command_name)
self.assertTrue("maxTimeMS" in listener.started_events[0].command)
self.assertIn("maxTimeMS", listener.started_events[0].command)
self.assertEqual(99, listener.started_events[0].command["maxTimeMS"])
# getMore
self.assertEqual("getMore", listener.started_events[1].command_name)
self.assertFalse("maxTimeMS" in listener.started_events[1].command)
self.assertNotIn("maxTimeMS", listener.started_events[1].command)
@client_context.require_test_commands
@client_context.require_no_mongos
@ -924,16 +924,19 @@ class TestCursor(IntegrationTest):
# Shallow copies can so can mutate
cursor2 = copy.copy(cursor)
cursor2._projection["cursor2"] = False
self.assertTrue(cursor._projection and "cursor2" in cursor._projection)
self.assertIsNotNone(cursor._projection)
self.assertIn("cursor2", cursor._projection.keys())
# Deepcopies and shouldn't mutate
cursor3 = copy.deepcopy(cursor)
cursor3._projection["cursor3"] = False
self.assertFalse(cursor._projection and "cursor3" in cursor._projection)
self.assertIsNotNone(cursor._projection)
self.assertNotIn("cursor3", cursor._projection.keys())
cursor4 = cursor.clone()
cursor4._projection["cursor4"] = False
self.assertFalse(cursor._projection and "cursor4" in cursor._projection)
self.assertIsNotNone(cursor._projection)
self.assertNotIn("cursor4", cursor._projection.keys())
# Test memo when deepcopying queries
query = {"hello": "world"}

View File

@ -162,13 +162,13 @@ class TestDatabase(IntegrationTest):
db.create_collection("coll..ection") # type: ignore[arg-type]
test = db.create_collection("test")
self.assertTrue("test" in db.list_collection_names())
self.assertIn("test", db.list_collection_names())
test.insert_one({"hello": "world"})
self.assertEqual((db.test.find_one())["hello"], "world")
db.drop_collection("test.foo")
db.create_collection("test.foo")
self.assertTrue("test.foo" in db.list_collection_names())
self.assertIn("test.foo", db.list_collection_names())
with self.assertRaises(CollectionInvalid):
db.create_collection("test.foo")
@ -178,10 +178,10 @@ class TestDatabase(IntegrationTest):
db.test.mike.insert_one({"dummy": "object"})
colls = db.list_collection_names()
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
self.assertIn("test", colls)
self.assertIn("test.mike", colls)
for coll in colls:
self.assertTrue("$" not in coll)
self.assertNotIn("$", coll)
db.systemcoll.test.insert_one({})
no_system_collections = db.list_collection_names(
@ -251,12 +251,12 @@ class TestDatabase(IntegrationTest):
colls = [result["name"] for result in results]
# All the collections present.
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
self.assertIn("test", colls)
self.assertIn("test.mike", colls)
# No collection containing a '$'.
for coll in colls:
self.assertTrue("$" not in coll)
self.assertNotIn("$", coll)
# Duplicate check.
coll_cnt: dict = {}
@ -291,12 +291,12 @@ class TestDatabase(IntegrationTest):
colls = [result["name"] for result in results]
# Checking only capped collections are present
self.assertTrue("test" in colls)
self.assertFalse("test.mike" in colls)
self.assertIn("test", colls)
self.assertNotIn("test.mike", colls)
# No collection containing a '$'.
for coll in colls:
self.assertTrue("$" not in coll)
self.assertNotIn("$", coll)
# Duplicate check.
coll_cnt = {}
@ -336,24 +336,24 @@ class TestDatabase(IntegrationTest):
db.drop_collection(None) # type: ignore[arg-type]
db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in db.list_collection_names())
self.assertIn("test", db.list_collection_names())
db.drop_collection("test")
self.assertFalse("test" in db.list_collection_names())
self.assertNotIn("test", db.list_collection_names())
db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in db.list_collection_names())
self.assertIn("test", db.list_collection_names())
db.drop_collection("test")
self.assertFalse("test" in db.list_collection_names())
self.assertNotIn("test", db.list_collection_names())
db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in db.list_collection_names())
self.assertIn("test", db.list_collection_names())
db.drop_collection(db.test)
self.assertFalse("test" in db.list_collection_names())
self.assertNotIn("test", db.list_collection_names())
db.test.insert_one({"dummy": "object"})
self.assertTrue("test" in db.list_collection_names())
self.assertIn("test", db.list_collection_names())
db.test.drop()
self.assertFalse("test" in db.list_collection_names())
self.assertNotIn("test", db.list_collection_names())
db.test.drop()
db.drop_collection(db.test.doesnotexist)

View File

@ -479,77 +479,77 @@ class TestSampleShellCommands(IntegrationTest):
# End Example 44
for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertFalse("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertNotIn("instock", doc)
# Start Example 45
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "_id": 0})
# End Example 45
for doc in cursor:
self.assertFalse("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertFalse("instock" in doc)
self.assertNotIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertNotIn("instock", doc)
# Start Example 46
cursor = db.inventory.find({"status": "A"}, {"status": 0, "instock": 0})
# End Example 46
for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertFalse("status" in doc)
self.assertTrue("size" in doc)
self.assertFalse("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertNotIn("status", doc)
self.assertIn("size", doc)
self.assertNotIn("instock", doc)
# Start Example 47
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "size.uom": 1})
# End Example 47
for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertTrue("size" in doc)
self.assertFalse("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertIn("size", doc)
self.assertNotIn("instock", doc)
size = doc["size"]
self.assertTrue("uom" in size)
self.assertFalse("h" in size)
self.assertFalse("w" in size)
self.assertIn("uom", size)
self.assertNotIn("h", size)
self.assertNotIn("w", size)
# Start Example 48
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})
# End Example 48
for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertTrue("size" in doc)
self.assertTrue("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertIn("size", doc)
self.assertIn("instock", doc)
size = doc["size"]
self.assertFalse("uom" in size)
self.assertTrue("h" in size)
self.assertTrue("w" in size)
self.assertNotIn("uom", size)
self.assertIn("h", size)
self.assertIn("w", size)
# Start Example 49
cursor = db.inventory.find({"status": "A"}, {"item": 1, "status": 1, "instock.qty": 1})
# End Example 49
for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertTrue("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertIn("instock", doc)
for subdoc in doc["instock"]:
self.assertFalse("warehouse" in subdoc)
self.assertTrue("qty" in subdoc)
self.assertNotIn("warehouse", subdoc)
self.assertIn("qty", subdoc)
# Start Example 50
cursor = db.inventory.find(
@ -558,11 +558,11 @@ class TestSampleShellCommands(IntegrationTest):
# End Example 50
for doc in cursor:
self.assertTrue("_id" in doc)
self.assertTrue("item" in doc)
self.assertTrue("status" in doc)
self.assertFalse("size" in doc)
self.assertTrue("instock" in doc)
self.assertIn("_id", doc)
self.assertIn("item", doc)
self.assertIn("status", doc)
self.assertNotIn("size", doc)
self.assertIn("instock", doc)
self.assertEqual(len(doc["instock"]), 1)
def test_update_and_replace(self):
@ -645,7 +645,7 @@ class TestSampleShellCommands(IntegrationTest):
for doc in db.inventory.find({"item": "paper"}):
self.assertEqual(doc["size"]["uom"], "cm")
self.assertEqual(doc["status"], "P")
self.assertTrue("lastModified" in doc)
self.assertIn("lastModified", doc)
# Start Example 53
db.inventory.update_many(
@ -657,7 +657,7 @@ class TestSampleShellCommands(IntegrationTest):
for doc in db.inventory.find({"qty": {"$lt": 50}}):
self.assertEqual(doc["size"]["uom"], "in")
self.assertEqual(doc["status"], "P")
self.assertTrue("lastModified" in doc)
self.assertIn("lastModified", doc)
# Start Example 54
db.inventory.replace_one(
@ -671,8 +671,8 @@ class TestSampleShellCommands(IntegrationTest):
for doc in db.inventory.find({"item": "paper"}, {"_id": 0}):
self.assertEqual(len(doc.keys()), 2)
self.assertTrue("item" in doc)
self.assertTrue("instock" in doc)
self.assertIn("item", doc)
self.assertIn("instock", doc)
self.assertEqual(len(doc["instock"]), 2)
def test_delete(self):

View File

@ -25,4 +25,4 @@ class TestJsonUtilRoundtrip(IntegrationTest):
db.test.insert_many(docs)
reloaded_docs = json_util.loads(json_util.dumps((db.test.find()).to_list()))
for doc in docs:
self.assertTrue(doc in reloaded_docs)
self.assertIn(doc, reloaded_docs)

View File

@ -1086,8 +1086,8 @@ class TestCommandMonitoring(IntegrationTest):
self.assertEqual(started.command_name, succeeded.command_name)
self.assertEqual(started.request_id, succeeded.request_id)
self.assertEqual(started.connection_id, succeeded.connection_id)
self.assertTrue("cursor" in succeeded.reply)
self.assertTrue("ok" in succeeded.reply)
self.assertIn("cursor", succeeded.reply)
self.assertIn("ok", succeeded.reply)
self.listener.reset()

View File

@ -134,8 +134,9 @@ class TestSession(IntegrationTest):
f(*args, **kw)
self.assertGreaterEqual(len(listener.started_events), 1)
for event in listener.started_events:
self.assertTrue(
"lsid" in event.command,
self.assertIn(
"lsid",
event.command,
f"{f.__name__} sent no lsid with {event.command_name}",
)
@ -170,8 +171,9 @@ class TestSession(IntegrationTest):
self.assertGreaterEqual(len(listener.started_events), 1)
lsids = []
for event in listener.started_events:
self.assertTrue(
"lsid" in event.command,
self.assertIn(
"lsid",
event.command,
f"{f.__name__} sent no lsid with {event.command_name}",
)
@ -422,8 +424,9 @@ class TestSession(IntegrationTest):
f(session=s)
self.assertGreaterEqual(len(listener.started_events), 1)
for event in listener.started_events:
self.assertTrue(
"lsid" in event.command,
self.assertIn(
"lsid",
event.command,
f"{name} sent no lsid with {event.command_name}",
)
@ -441,15 +444,13 @@ class TestSession(IntegrationTest):
listener.reset()
f(session=None)
event0 = listener.first_command_started()
self.assertTrue(
"lsid" in event0.command, f"{name} sent no lsid with {event0.command_name}"
)
self.assertIn("lsid", event0.command, f"{name} sent no lsid with {event0.command_name}")
lsid = event0.command["lsid"]
for event in listener.started_events[1:]:
self.assertTrue(
"lsid" in event.command, f"{name} sent no lsid with {event.command_name}"
self.assertIn(
"lsid", event.command, f"{name} sent no lsid with {event.command_name}"
)
self.assertEqual(
@ -1187,15 +1188,17 @@ class TestClusterTime(IntegrationTest):
self.assertGreaterEqual(len(listener.started_events), 1)
for i, event in enumerate(listener.started_events):
self.assertTrue(
"$clusterTime" in event.command,
self.assertIn(
"$clusterTime",
event.command,
f"{f.__name__} sent no $clusterTime with {event.command_name}",
)
if i > 0:
succeeded = listener.succeeded_events[i - 1]
self.assertTrue(
"$clusterTime" in succeeded.reply,
self.assertIn(
"$clusterTime",
succeeded.reply,
f"{f.__name__} received no $clusterTime with {succeeded.command_name}",
)

View File

@ -148,8 +148,8 @@ class TestSON(unittest.TestCase):
"""has_key and __contains__"""
test_son = SON([(1, 100), (2, 200), (3, 300)])
self.assertIn(1, test_son)
self.assertTrue(2 in test_son, "in failed")
self.assertFalse(22 in test_son, "in succeeded when it shouldn't")
self.assertIn(2, test_son, "in failed")
self.assertNotIn(22, test_son, "in succeeded when it shouldn't")
self.assertTrue(test_son.has_key(2), "has_key failed")
self.assertFalse(test_son.has_key(22), "has_key succeeded when it shouldn't")