From b31e5a0ccf9274315ca5c8fc86f43fa301b3d7fa Mon Sep 17 00:00:00 2001 From: Sophia Yang Date: Fri, 5 Jun 2026 09:34:12 -0400 Subject: [PATCH 1/4] inital refactor --- test/asynchronous/test_change_stream.py | 2 +- test/asynchronous/test_collection.py | 8 +++---- test/asynchronous/test_cursor.py | 18 +++++++++------- test/asynchronous/test_index_management.py | 21 +++++++------------ .../asynchronous/test_sdam_monitoring_spec.py | 3 ++- test/asynchronous/test_session.py | 4 ++-- test/test_change_stream.py | 2 +- test/test_collection.py | 8 +++---- test/test_cursor.py | 18 +++++++++------- test/test_index_management.py | 21 +++++++------------ test/test_sdam_monitoring_spec.py | 3 ++- test/test_session.py | 4 ++-- 12 files changed, 52 insertions(+), 60 deletions(-) diff --git a/test/asynchronous/test_change_stream.py b/test/asynchronous/test_change_stream.py index 2e1f83884b..e4128b2499 100644 --- a/test/asynchronous/test_change_stream.py +++ b/test/asynchronous/test_change_stream.py @@ -926,7 +926,7 @@ async def asyncSetUp(self): await super().asyncSetUp() # Use a new collection for each test. await self.watched_collection().drop() - await self.watched_collection().insert_one({}) + await self.db.create_collection(self.watched_collection().name) async def change_stream_with_client(self, client, *args, **kwargs): return ( diff --git a/test/asynchronous/test_collection.py b/test/asynchronous/test_collection.py index 3232650487..704d407491 100644 --- a/test/asynchronous/test_collection.py +++ b/test/asynchronous/test_collection.py @@ -229,7 +229,7 @@ async def test_create_indexes(self): self.assertRaises(ValueError, IndexModel, []) await db.test.drop_indexes() - await db.test.insert_one({}) + await db.create_collection("test") self.assertEqual(len(await db.test.index_information()), 1) await db.test.create_indexes([IndexModel("hello")]) @@ -292,7 +292,7 @@ async def test_create_index(self): await db.test.create_index([]) await db.test.drop_indexes() - await db.test.insert_one({}) + await db.create_collection("test") self.assertEqual(len(await db.test.index_information()), 1) await db.test.create_index("hello") @@ -392,7 +392,7 @@ async def test_index_management_max_time_ms(self): async def test_list_indexes(self): db = self.db await db.test.drop() - await db.test.insert_one({}) # create collection + await db.create_collection("test") def map_indexes(indexes): return {index["name"]: index for index in indexes} @@ -426,7 +426,7 @@ def map_indexes(indexes): async def test_index_info(self): db = self.db await db.test.drop() - await db.test.insert_one({}) # create collection + await db.create_collection("test") self.assertEqual(len(await db.test.index_information()), 1) self.assertIn("_id_", await db.test.index_information()) diff --git a/test/asynchronous/test_cursor.py b/test/asynchronous/test_cursor.py index 95604f65df..bc82d07e64 100644 --- a/test/asynchronous/test_cursor.py +++ b/test/asynchronous/test_cursor.py @@ -1513,9 +1513,17 @@ async def test_command_cursor_to_list_csot_applied(self): class TestRawBatchCursor(AsyncIntegrationTest): + async def asyncSetUp(self): + await super().asyncSetUp() + await self.db.test.drop() + await self.db.create_collection("test") + + async def asyncTearDown(self): + await self.db.test.drop() + await super().asyncTearDown() + async def test_find_raw(self): c = self.db.test - await c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] await c.insert_many(docs) batches = await c.find_raw_batches().sort("_id").to_list() @@ -1525,7 +1533,6 @@ async def test_find_raw(self): @async_client_context.require_transactions async def test_find_raw_transaction(self): c = self.db.test - await c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] await c.insert_many(docs) @@ -1555,7 +1562,6 @@ async def test_find_raw_transaction(self): @async_client_context.require_failCommand_fail_point async def test_find_raw_retryable_reads(self): c = self.db.test - await c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] await c.insert_many(docs) @@ -1576,7 +1582,6 @@ async def test_find_raw_retryable_reads(self): @async_client_context.require_no_standalone async def test_find_raw_snapshot_reads(self): c = self.db.get_collection("test", write_concern=WriteConcern(w="majority")) - await c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] await c.insert_many(docs) @@ -1595,12 +1600,10 @@ async def test_find_raw_snapshot_reads(self): async def test_explain(self): c = self.db.test - await c.insert_one({}) explanation = await c.find_raw_batches().explain() self.assertIsInstance(explanation, dict) async def test_empty(self): - await self.db.test.drop() cursor = self.db.test.find_raw_batches() with self.assertRaises(StopAsyncIteration): await anext(cursor) @@ -1615,7 +1618,6 @@ async def test_clone(self): @async_client_context.require_no_mongos async def test_exhaust(self): c = self.db.test - await c.drop() await c.insert_many({"_id": i} for i in range(200)) result = b"".join(await c.find_raw_batches(cursor_type=CursorType.EXHAUST).to_list()) self.assertEqual([{"_id": i} for i in range(200)], decode_all(result)) @@ -1632,6 +1634,7 @@ async def test_get_item(self): self.db.test.find_raw_batches()[0] async def test_collation(self): + await self.db.test.insert_one({}) await anext(self.db.test.find_raw_batches(collation=Collation("en_US"))) async def test_read_concern(self): @@ -1645,7 +1648,6 @@ async def test_monitoring(self): listener = OvertCommandListener() client = await self.async_rs_or_single_client(event_listeners=[listener]) c = client.pymongo_test.test - await c.drop() await c.insert_many([{"_id": i} for i in range(10)]) listener.reset() diff --git a/test/asynchronous/test_index_management.py b/test/asynchronous/test_index_management.py index ac096ec099..141ed118ee 100644 --- a/test/asynchronous/test_index_management.py +++ b/test/asynchronous/test_index_management.py @@ -118,8 +118,7 @@ class TestSearchIndexIntegration(SearchIndexIntegrationBase): async def test_comment_field(self): # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - await coll0.insert_one({}) + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0`` that implicitly passes its type. search_definition = {"mappings": {"dynamic": False}} @@ -144,11 +143,10 @@ async def test_case_1(self): """Driver can successfully create and list search indexes.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0`` with the ``createSearchIndex`` helper. Use the following definition: model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - await coll0.insert_one({}) resp = await coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. @@ -166,8 +164,7 @@ async def test_case_2(self): """Driver can successfully create multiple indexes in batch.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - await coll0.insert_one({}) + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Create two new search indexes on ``coll0`` with the ``createSearchIndexes`` helper. name1 = "test-search-index-1" @@ -202,8 +199,7 @@ async def test_case_3(self): """Driver can successfully drop search indexes.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - await coll0.insert_one({}) + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} @@ -232,8 +228,7 @@ async def test_case_3(self): async def test_case_4(self): """Driver can update a search index.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - await coll0.insert_one({}) + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} @@ -273,8 +268,7 @@ async def test_case_5(self): async def test_case_6(self): """Driver can successfully create and list search indexes with non-default readConcern and writeConcern.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - await coll0.insert_one({}) + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Apply a write concern ``WriteConcern(w=1)`` and a read concern with ``ReadConcern(level="majority")`` to ``coll0``. coll0 = coll0.with_options( @@ -301,8 +295,7 @@ async def test_case_7(self): """Driver handles index types.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - await coll0.insert_one({}) + coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") # Use these search and vector search definitions for indexes. search_definition = {"mappings": {"dynamic": False}} diff --git a/test/asynchronous/test_sdam_monitoring_spec.py b/test/asynchronous/test_sdam_monitoring_spec.py index 71ec6c6b46..27cc50a697 100644 --- a/test/asynchronous/test_sdam_monitoring_spec.py +++ b/test/asynchronous/test_sdam_monitoring_spec.py @@ -296,10 +296,11 @@ async def asyncSetUp(self): event_listeners=[self.listener], retryWrites=retry_writes ) self.coll = self.test_client[self.client.db.name].test - await self.coll.insert_one({}) + await self.coll.database.create_collection(self.coll.name) self.listener.reset() async def asyncTearDown(self): + await self.coll.drop() await super().asyncTearDown() async def _test_app_error(self, fail_command_opts, expected_error): diff --git a/test/asynchronous/test_session.py b/test/asynchronous/test_session.py index 13ce578671..338cef509a 100644 --- a/test/asynchronous/test_session.py +++ b/test/asynchronous/test_session.py @@ -675,7 +675,7 @@ async def test_aggregate_error(self): coll = client.pymongo_test.collection # 3.6.0 mongos only validates the aggregate pipeline when the # database exists. - await coll.insert_one({}) + await coll.database.create_collection(coll.name) listener.reset() with self.assertRaises(OperationFailure): @@ -827,7 +827,7 @@ async def _test_unacknowledged_ops(self, client, *ops): async def test_unacknowledged_writes(self): # Ensure the collection exists. - await self.client.pymongo_test.test_unacked_writes.insert_one({}) + await self.client.pymongo_test.create_collection("test_unacked_writes") client = await self.async_rs_or_single_client(w=0, event_listeners=[self.listener]) db = client.pymongo_test coll = db.test_unacked_writes diff --git a/test/test_change_stream.py b/test/test_change_stream.py index 792b39cc29..717d7d3bd7 100644 --- a/test/test_change_stream.py +++ b/test/test_change_stream.py @@ -908,7 +908,7 @@ def setUp(self): super().setUp() # Use a new collection for each test. self.watched_collection().drop() - self.watched_collection().insert_one({}) + self.db.create_collection(self.watched_collection().name) def change_stream_with_client(self, client, *args, **kwargs): return ( diff --git a/test/test_collection.py b/test/test_collection.py index ac469782e9..a7912100b0 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -229,7 +229,7 @@ def test_create_indexes(self): self.assertRaises(ValueError, IndexModel, []) db.test.drop_indexes() - db.test.insert_one({}) + db.create_collection("test") self.assertEqual(len(db.test.index_information()), 1) db.test.create_indexes([IndexModel("hello")]) @@ -292,7 +292,7 @@ def test_create_index(self): db.test.create_index([]) db.test.drop_indexes() - db.test.insert_one({}) + db.create_collection("test") self.assertEqual(len(db.test.index_information()), 1) db.test.create_index("hello") @@ -386,7 +386,7 @@ def test_index_management_max_time_ms(self): def test_list_indexes(self): db = self.db db.test.drop() - db.test.insert_one({}) # create collection + db.create_collection("test") def map_indexes(indexes): return {index["name"]: index for index in indexes} @@ -420,7 +420,7 @@ def map_indexes(indexes): def test_index_info(self): db = self.db db.test.drop() - db.test.insert_one({}) # create collection + db.create_collection("test") self.assertEqual(len(db.test.index_information()), 1) self.assertIn("_id_", db.test.index_information()) diff --git a/test/test_cursor.py b/test/test_cursor.py index cd13e9bfff..f37db6c14b 100644 --- a/test/test_cursor.py +++ b/test/test_cursor.py @@ -1504,9 +1504,17 @@ def test_command_cursor_to_list_csot_applied(self): class TestRawBatchCursor(IntegrationTest): + def setUp(self): + super().setUp() + self.db.test.drop() + self.db.create_collection("test") + + def tearDown(self): + self.db.test.drop() + super().tearDown() + def test_find_raw(self): c = self.db.test - c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] c.insert_many(docs) batches = c.find_raw_batches().sort("_id").to_list() @@ -1516,7 +1524,6 @@ def test_find_raw(self): @client_context.require_transactions def test_find_raw_transaction(self): c = self.db.test - c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] c.insert_many(docs) @@ -1546,7 +1553,6 @@ def test_find_raw_transaction(self): @client_context.require_failCommand_fail_point def test_find_raw_retryable_reads(self): c = self.db.test - c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] c.insert_many(docs) @@ -1567,7 +1573,6 @@ def test_find_raw_retryable_reads(self): @client_context.require_no_standalone def test_find_raw_snapshot_reads(self): c = self.db.get_collection("test", write_concern=WriteConcern(w="majority")) - c.drop() docs = [{"_id": i, "x": 3.0 * i} for i in range(10)] c.insert_many(docs) @@ -1586,12 +1591,10 @@ def test_find_raw_snapshot_reads(self): def test_explain(self): c = self.db.test - c.insert_one({}) explanation = c.find_raw_batches().explain() self.assertIsInstance(explanation, dict) def test_empty(self): - self.db.test.drop() cursor = self.db.test.find_raw_batches() with self.assertRaises(StopIteration): next(cursor) @@ -1606,7 +1609,6 @@ def test_clone(self): @client_context.require_no_mongos def test_exhaust(self): c = self.db.test - c.drop() c.insert_many({"_id": i} for i in range(200)) result = b"".join(c.find_raw_batches(cursor_type=CursorType.EXHAUST).to_list()) self.assertEqual([{"_id": i} for i in range(200)], decode_all(result)) @@ -1623,6 +1625,7 @@ def test_get_item(self): self.db.test.find_raw_batches()[0] def test_collation(self): + self.db.test.insert_one({}) next(self.db.test.find_raw_batches(collation=Collation("en_US"))) def test_read_concern(self): @@ -1634,7 +1637,6 @@ def test_monitoring(self): listener = OvertCommandListener() client = self.rs_or_single_client(event_listeners=[listener]) c = client.pymongo_test.test - c.drop() c.insert_many([{"_id": i} for i in range(10)]) listener.reset() diff --git a/test/test_index_management.py b/test/test_index_management.py index 2d723bb4a3..e380c851c2 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -118,8 +118,7 @@ class TestSearchIndexIntegration(SearchIndexIntegrationBase): def test_comment_field(self): # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - coll0.insert_one({}) + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0`` that implicitly passes its type. search_definition = {"mappings": {"dynamic": False}} @@ -144,11 +143,10 @@ def test_case_1(self): """Driver can successfully create and list search indexes.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0`` with the ``createSearchIndex`` helper. Use the following definition: model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - coll0.insert_one({}) resp = coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. @@ -166,8 +164,7 @@ def test_case_2(self): """Driver can successfully create multiple indexes in batch.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - coll0.insert_one({}) + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Create two new search indexes on ``coll0`` with the ``createSearchIndexes`` helper. name1 = "test-search-index-1" @@ -202,8 +199,7 @@ def test_case_3(self): """Driver can successfully drop search indexes.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - coll0.insert_one({}) + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} @@ -232,8 +228,7 @@ def test_case_3(self): def test_case_4(self): """Driver can update a search index.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - coll0.insert_one({}) + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Create a new search index on ``coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} @@ -273,8 +268,7 @@ def test_case_5(self): def test_case_6(self): """Driver can successfully create and list search indexes with non-default readConcern and writeConcern.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - coll0.insert_one({}) + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Apply a write concern ``WriteConcern(w=1)`` and a read concern with ``ReadConcern(level="majority")`` to ``coll0``. coll0 = coll0.with_options( @@ -301,8 +295,7 @@ def test_case_7(self): """Driver handles index types.""" # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db[f"col{uuid.uuid4()}"] - coll0.insert_one({}) + coll0 = self.db.create_collection(f"col{uuid.uuid4()}") # Use these search and vector search definitions for indexes. search_definition = {"mappings": {"dynamic": False}} diff --git a/test/test_sdam_monitoring_spec.py b/test/test_sdam_monitoring_spec.py index 2167e561cf..1c9706a975 100644 --- a/test/test_sdam_monitoring_spec.py +++ b/test/test_sdam_monitoring_spec.py @@ -296,10 +296,11 @@ def setUp(self): event_listeners=[self.listener], retryWrites=retry_writes ) self.coll = self.test_client[self.client.db.name].test - self.coll.insert_one({}) + self.coll.database.create_collection(self.coll.name) self.listener.reset() def tearDown(self): + self.coll.drop() super().tearDown() def _test_app_error(self, fail_command_opts, expected_error): diff --git a/test/test_session.py b/test/test_session.py index cf071df49a..d2a3a034e5 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -675,7 +675,7 @@ def test_aggregate_error(self): coll = client.pymongo_test.collection # 3.6.0 mongos only validates the aggregate pipeline when the # database exists. - coll.insert_one({}) + coll.database.create_collection(coll.name) listener.reset() with self.assertRaises(OperationFailure): @@ -827,7 +827,7 @@ def _test_unacknowledged_ops(self, client, *ops): def test_unacknowledged_writes(self): # Ensure the collection exists. - self.client.pymongo_test.test_unacked_writes.insert_one({}) + self.client.pymongo_test.create_collection("test_unacked_writes") client = self.rs_or_single_client(w=0, event_listeners=[self.listener]) db = client.pymongo_test coll = db.test_unacked_writes From 32524523514a78ebeb8408f5028c25a3006dd4f7 Mon Sep 17 00:00:00 2001 From: Sophia Yang Date: Fri, 5 Jun 2026 10:25:20 -0400 Subject: [PATCH 2/4] error fix --- test/asynchronous/test_sdam_monitoring_spec.py | 1 + test/test_sdam_monitoring_spec.py | 1 + 2 files changed, 2 insertions(+) diff --git a/test/asynchronous/test_sdam_monitoring_spec.py b/test/asynchronous/test_sdam_monitoring_spec.py index 27cc50a697..2fe560ab48 100644 --- a/test/asynchronous/test_sdam_monitoring_spec.py +++ b/test/asynchronous/test_sdam_monitoring_spec.py @@ -296,6 +296,7 @@ async def asyncSetUp(self): event_listeners=[self.listener], retryWrites=retry_writes ) self.coll = self.test_client[self.client.db.name].test + await self.coll.drop() # necessary for first test run await self.coll.database.create_collection(self.coll.name) self.listener.reset() diff --git a/test/test_sdam_monitoring_spec.py b/test/test_sdam_monitoring_spec.py index 1c9706a975..9b09c59f83 100644 --- a/test/test_sdam_monitoring_spec.py +++ b/test/test_sdam_monitoring_spec.py @@ -296,6 +296,7 @@ def setUp(self): event_listeners=[self.listener], retryWrites=retry_writes ) self.coll = self.test_client[self.client.db.name].test + self.coll.drop() # necessary for first test run self.coll.database.create_collection(self.coll.name) self.listener.reset() From adb6b972aef08c20280765407feeb373d40b6ec5 Mon Sep 17 00:00:00 2001 From: Sophia Yang Date: Fri, 5 Jun 2026 11:32:05 -0400 Subject: [PATCH 3/4] cleanup --- test/asynchronous/test_cursor.py | 5 - test/asynchronous/test_index_management.py | 106 +++++++++------------ test/asynchronous/test_session.py | 11 ++- test/test_cursor.py | 5 - test/test_index_management.py | 104 +++++++++----------- test/test_session.py | 11 ++- 6 files changed, 100 insertions(+), 142 deletions(-) diff --git a/test/asynchronous/test_cursor.py b/test/asynchronous/test_cursor.py index bc82d07e64..8b6d888f33 100644 --- a/test/asynchronous/test_cursor.py +++ b/test/asynchronous/test_cursor.py @@ -1516,11 +1516,6 @@ class TestRawBatchCursor(AsyncIntegrationTest): async def asyncSetUp(self): await super().asyncSetUp() await self.db.test.drop() - await self.db.create_collection("test") - - async def asyncTearDown(self): - await self.db.test.drop() - await super().asyncTearDown() async def test_find_raw(self): c = self.db.test diff --git a/test/asynchronous/test_index_management.py b/test/asynchronous/test_index_management.py index 613eed87d9..999637a06c 100644 --- a/test/asynchronous/test_index_management.py +++ b/test/asynchronous/test_index_management.py @@ -96,6 +96,7 @@ async def asyncSetUp(self) -> None: ) await self.client.drop_database(_NAME) self.db = self.client[self.db_name] + self.coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") async def asyncTearDown(self): await self.client.drop_database(_NAME) @@ -117,13 +118,10 @@ class TestSearchIndexIntegration(SearchIndexIntegrationBase): db_name = "test_search_index" async def test_comment_field(self): - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - - # Create a new search index on ``coll0`` that implicitly passes its type. + # Create a new search index on ``self.coll0`` that implicitly passes its type. search_definition = {"mappings": {"dynamic": False}} self.listener.reset() - implicit_search_resp = await coll0.create_search_index( + implicit_search_resp = await self.coll0.create_search_index( model={"name": _NAME + "-implicit", "definition": search_definition}, comment="foo" ) event = self.listener.events[0] @@ -131,7 +129,9 @@ async def test_comment_field(self): # Get the index definition. self.listener.reset() - await (await coll0.list_search_indexes(name=implicit_search_resp, comment="foo")).next() + await ( + await self.coll0.list_search_indexes(name=implicit_search_resp, comment="foo") + ).next() event = self.listener.events[0] self.assertEqual(event.command["comment"], "foo") @@ -142,19 +142,16 @@ class TestSearchIndexProse(SearchIndexIntegrationBase): async def test_case_1(self): """Driver can successfully create and list search indexes.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - - # Create a new search index on ``coll0`` with the ``createSearchIndex`` helper. Use the following definition: + # Create a new search index on ``self.coll0`` with the ``createSearchIndex`` helper. Use the following definition: model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - resp = await coll0.create_search_index(model) + resp = await self.coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. self.assertEqual(resp, _NAME) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied and store the value in a variable ``index``: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied and store the value in a variable ``index``: # An index with the ``name`` of ``test-search-index`` is present and the index has a field ``queryable`` with a value of ``true``. - index = await self.wait_for_ready(coll0) + index = await self.wait_for_ready(self.coll0) # . Assert that ``index`` has a property ``latestDefinition`` whose value is ``{ 'mappings': { 'dynamic': false } }`` self.assertIn("latestDefinition", index) @@ -163,10 +160,7 @@ async def test_case_1(self): async def test_case_2(self): """Driver can successfully create multiple indexes in batch.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - - # Create two new search indexes on ``coll0`` with the ``createSearchIndexes`` helper. + # Create two new search indexes on ``self.coll0`` with the ``createSearchIndexes`` helper. name1 = "test-search-index-1" name2 = "test-search-index-2" definition = {"mappings": {"dynamic": False}} @@ -174,21 +168,21 @@ async def test_case_2(self): {"name": name1, "definition": definition}, {"name": name2, "definition": definition}, ] - await coll0.create_search_indexes( + await self.coll0.create_search_indexes( [SearchIndexModel(i["definition"], i["name"]) for i in index_definitions] ) # .Assert that the command returns an array containing the new indexes' names: ``["test-search-index-1", "test-search-index-2"]``. - indices = await (await coll0.list_search_indexes()).to_list() + indices = await (await self.coll0.list_search_indexes()).to_list() names = [i["name"] for i in indices] self.assertIn(name1, names) self.assertIn(name2, names) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied. + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied. # An index with the ``name`` of ``test-search-index-1`` is present and index has a field ``queryable`` with the value of ``true``. Store result in ``index1``. # An index with the ``name`` of ``test-search-index-2`` is present and index has a field ``queryable`` with the value of ``true``. Store result in ``index2``. - index1 = await self.wait_for_ready(coll0, name1) - index2 = await self.wait_for_ready(coll0, name2) + index1 = await self.wait_for_ready(self.coll0, name1) + index2 = await self.wait_for_ready(self.coll0, name2) # Assert that ``index1`` and ``index2`` have the property ``latestDefinition`` whose value is ``{ "mappings" : { "dynamic" : false } }`` for index in [index1, index2]: @@ -198,27 +192,24 @@ async def test_case_2(self): async def test_case_3(self): """Driver can successfully drop search indexes.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - - # Create a new search index on ``coll0``. + # Create a new search index on ``self.coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - resp = await coll0.create_search_index(model) + resp = await self.coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. self.assertEqual(resp, "test-search-index") - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: # An index with the ``name`` of ``test-search-index`` is present and index has a field ``queryable`` with the value of ``true``. - await self.wait_for_ready(coll0) + await self.wait_for_ready(self.coll0) - # Run a ``dropSearchIndex`` on ``coll0``, using ``test-search-index`` for the name. - await coll0.drop_search_index(_NAME) + # Run a ``dropSearchIndex`` on ``self.coll0``, using ``test-search-index`` for the name. + await self.coll0.drop_search_index(_NAME) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until ``listSearchIndexes`` returns an empty array. + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until ``listSearchIndexes`` returns an empty array. t0 = time.time() while True: - indices = await (await coll0.list_search_indexes()).to_list() + indices = await (await self.coll0.list_search_indexes()).to_list() if not indices: break if (time.time() - t0) / 60 > 5: @@ -227,33 +218,31 @@ async def test_case_3(self): async def test_case_4(self): """Driver can update a search index.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - # Create a new search index on ``coll0``. + # Create a new search index on ``self.coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - resp = await coll0.create_search_index(model) + resp = await self.coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. self.assertEqual(resp, _NAME) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: # An index with the ``name`` of ``test-search-index`` is present and index has a field ``queryable`` with the value of ``true``. - await self.wait_for_ready(coll0) + await self.wait_for_ready(self.coll0) - # Run a ``updateSearchIndex`` on ``coll0``. + # Run a ``updateSearchIndex`` on ``self.coll0``. # Assert that the command does not error and the server responds with a success. model2: dict[str, Any] = {"name": _NAME, "definition": {"mappings": {"dynamic": True}}} - await coll0.update_search_index(_NAME, model2["definition"]) + await self.coll0.update_search_index(_NAME, model2["definition"]) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: # An index with the ``name`` of ``test-search-index`` is present. This index is referred to as ``index``. # The index has a field ``queryable`` with a value of ``true`` and has a field ``status`` with the value of ``READY``. predicate = lambda index: index.get("queryable") is True and index.get("status") == "READY" - await self.wait_for_ready(coll0, predicate=predicate) + await self.wait_for_ready(self.coll0, predicate=predicate) # Assert that an index is present with the name ``test-search-index`` and the definition has a property ``latestDefinition`` whose value is ``{ 'mappings': { 'dynamic': true } }``. - index = (await (await coll0.list_search_indexes(_NAME)).to_list())[0] + index = (await (await self.coll0.list_search_indexes(_NAME)).to_list())[0] self.assertIn("latestDefinition", index) self.assertEqual(index["latestDefinition"], model2["definition"]) @@ -267,11 +256,9 @@ async def test_case_5(self): async def test_case_6(self): """Driver can successfully create and list search indexes with non-default readConcern and writeConcern.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - # Apply a write concern ``WriteConcern(w=1)`` and a read concern with ``ReadConcern(level="majority")`` to ``coll0``. - coll0 = coll0.with_options( + # Apply a write concern ``WriteConcern(w=1)`` and a read concern with ``ReadConcern(level="majority")`` to ``self.coll0``. + coll0 = self.coll0.with_options( write_concern=WriteConcern(w="1"), read_concern=ReadConcern(level="majority") ) @@ -294,9 +281,6 @@ async def test_case_6(self): async def test_case_7(self): """Driver handles index types.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - # Use these search and vector search definitions for indexes. search_definition = {"mappings": {"dynamic": False}} vector_search_definition = { @@ -310,30 +294,30 @@ async def test_case_7(self): ] } - # Create a new search index on ``coll0`` that implicitly passes its type. - implicit_search_resp = await coll0.create_search_index( + # Create a new search index on ``self.coll0`` that implicitly passes its type. + implicit_search_resp = await self.coll0.create_search_index( model={"name": _NAME + "-implicit", "definition": search_definition} ) # Get the index definition. - resp = await (await coll0.list_search_indexes(name=implicit_search_resp)).next() + resp = await (await self.coll0.list_search_indexes(name=implicit_search_resp)).next() # Assert that the index model contains the correct index type: ``"search"``. self.assertEqual(resp["type"], "search") - # Create a new search index on ``coll0`` that explicitly passes its type. - explicit_search_resp = await coll0.create_search_index( + # Create a new search index on ``self.coll0`` that explicitly passes its type. + explicit_search_resp = await self.coll0.create_search_index( model={"name": _NAME + "-explicit", "type": "search", "definition": search_definition} ) # Get the index definition. - resp = await (await coll0.list_search_indexes(name=explicit_search_resp)).next() + resp = await (await self.coll0.list_search_indexes(name=explicit_search_resp)).next() # Assert that the index model contains the correct index type: ``"search"``. self.assertEqual(resp["type"], "search") - # Create a new vector search index on ``coll0`` that explicitly passes its type. - explicit_vector_resp = await coll0.create_search_index( + # Create a new vector search index on ``self.coll0`` that explicitly passes its type. + explicit_vector_resp = await self.coll0.create_search_index( model={ "name": _NAME + "-vector", "type": "vectorSearch", @@ -342,14 +326,14 @@ async def test_case_7(self): ) # Get the index definition. - resp = await (await coll0.list_search_indexes(name=explicit_vector_resp)).next() + resp = await (await self.coll0.list_search_indexes(name=explicit_vector_resp)).next() # Assert that the index model contains the correct index type: ``"vectorSearch"``. self.assertEqual(resp["type"], "vectorSearch") # Catch the error raised when trying to create a vector search index without specifying the type with self.assertRaises(OperationFailure) as e: - await coll0.create_search_index( + await self.coll0.create_search_index( model={"name": _NAME + "-error", "definition": vector_search_definition} ) self.assertIn("Attribute mappings missing.", e.exception.details["errmsg"]) diff --git a/test/asynchronous/test_session.py b/test/asynchronous/test_session.py index a76eefad07..a0b76dec41 100644 --- a/test/asynchronous/test_session.py +++ b/test/asynchronous/test_session.py @@ -978,6 +978,12 @@ async def asyncSetUp(self): await super().asyncSetUp() self.listener = SessionTestListener() self.client = await self.async_rs_or_single_client(event_listeners=[self.listener]) + await self.client.pymongo_test.drop_collection("test") + await self.client.pymongo_test.create_collection("test") + + async def asyncTearDown(self): + await self.client.pymongo_test.drop_collection("test") + await super().asyncTearDown() @async_client_context.require_no_standalone async def test_core(self): @@ -1048,9 +1054,6 @@ async def _test_reads(self, op, exception=None): @async_client_context.require_no_standalone async def test_reads(self): - # Make sure the collection exists. - await self.client.pymongo_test.test.insert_one({}) - async def aggregate(coll, session): return await (await coll.aggregate([], session=session)).to_list() @@ -1206,7 +1209,6 @@ async def test_read_concern(self): @async_client_context.require_no_standalone async def test_cluster_time_with_server_support(self): - await self.client.pymongo_test.test.insert_one({}) self.listener.reset() await self.client.pymongo_test.test.find_one({}) after_cluster_time = self.listener.started_events[0].command.get("$clusterTime") @@ -1214,7 +1216,6 @@ async def test_cluster_time_with_server_support(self): @async_client_context.require_standalone async def test_cluster_time_no_server_support(self): - await self.client.pymongo_test.test.insert_one({}) self.listener.reset() await self.client.pymongo_test.test.find_one({}) after_cluster_time = self.listener.started_events[0].command.get("$clusterTime") diff --git a/test/test_cursor.py b/test/test_cursor.py index f37db6c14b..b6f9c4d508 100644 --- a/test/test_cursor.py +++ b/test/test_cursor.py @@ -1507,11 +1507,6 @@ class TestRawBatchCursor(IntegrationTest): def setUp(self): super().setUp() self.db.test.drop() - self.db.create_collection("test") - - def tearDown(self): - self.db.test.drop() - super().tearDown() def test_find_raw(self): c = self.db.test diff --git a/test/test_index_management.py b/test/test_index_management.py index fa9789bc01..f9c1b43f7c 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -96,6 +96,7 @@ def setUp(self) -> None: ) self.client.drop_database(_NAME) self.db = self.client[self.db_name] + self.coll0 = self.db.create_collection(f"col{uuid.uuid4()}") def tearDown(self): self.client.drop_database(_NAME) @@ -117,13 +118,10 @@ class TestSearchIndexIntegration(SearchIndexIntegrationBase): db_name = "test_search_index" def test_comment_field(self): - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - - # Create a new search index on ``coll0`` that implicitly passes its type. + # Create a new search index on ``self.coll0`` that implicitly passes its type. search_definition = {"mappings": {"dynamic": False}} self.listener.reset() - implicit_search_resp = coll0.create_search_index( + implicit_search_resp = self.coll0.create_search_index( model={"name": _NAME + "-implicit", "definition": search_definition}, comment="foo" ) event = self.listener.events[0] @@ -131,7 +129,7 @@ def test_comment_field(self): # Get the index definition. self.listener.reset() - (coll0.list_search_indexes(name=implicit_search_resp, comment="foo")).next() + (self.coll0.list_search_indexes(name=implicit_search_resp, comment="foo")).next() event = self.listener.events[0] self.assertEqual(event.command["comment"], "foo") @@ -142,19 +140,16 @@ class TestSearchIndexProse(SearchIndexIntegrationBase): def test_case_1(self): """Driver can successfully create and list search indexes.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - - # Create a new search index on ``coll0`` with the ``createSearchIndex`` helper. Use the following definition: + # Create a new search index on ``self.coll0`` with the ``createSearchIndex`` helper. Use the following definition: model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - resp = coll0.create_search_index(model) + resp = self.coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. self.assertEqual(resp, _NAME) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied and store the value in a variable ``index``: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied and store the value in a variable ``index``: # An index with the ``name`` of ``test-search-index`` is present and the index has a field ``queryable`` with a value of ``true``. - index = self.wait_for_ready(coll0) + index = self.wait_for_ready(self.coll0) # . Assert that ``index`` has a property ``latestDefinition`` whose value is ``{ 'mappings': { 'dynamic': false } }`` self.assertIn("latestDefinition", index) @@ -163,10 +158,7 @@ def test_case_1(self): def test_case_2(self): """Driver can successfully create multiple indexes in batch.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - - # Create two new search indexes on ``coll0`` with the ``createSearchIndexes`` helper. + # Create two new search indexes on ``self.coll0`` with the ``createSearchIndexes`` helper. name1 = "test-search-index-1" name2 = "test-search-index-2" definition = {"mappings": {"dynamic": False}} @@ -174,21 +166,21 @@ def test_case_2(self): {"name": name1, "definition": definition}, {"name": name2, "definition": definition}, ] - coll0.create_search_indexes( + self.coll0.create_search_indexes( [SearchIndexModel(i["definition"], i["name"]) for i in index_definitions] ) # .Assert that the command returns an array containing the new indexes' names: ``["test-search-index-1", "test-search-index-2"]``. - indices = (coll0.list_search_indexes()).to_list() + indices = (self.coll0.list_search_indexes()).to_list() names = [i["name"] for i in indices] self.assertIn(name1, names) self.assertIn(name2, names) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied. + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied. # An index with the ``name`` of ``test-search-index-1`` is present and index has a field ``queryable`` with the value of ``true``. Store result in ``index1``. # An index with the ``name`` of ``test-search-index-2`` is present and index has a field ``queryable`` with the value of ``true``. Store result in ``index2``. - index1 = self.wait_for_ready(coll0, name1) - index2 = self.wait_for_ready(coll0, name2) + index1 = self.wait_for_ready(self.coll0, name1) + index2 = self.wait_for_ready(self.coll0, name2) # Assert that ``index1`` and ``index2`` have the property ``latestDefinition`` whose value is ``{ "mappings" : { "dynamic" : false } }`` for index in [index1, index2]: @@ -198,27 +190,24 @@ def test_case_2(self): def test_case_3(self): """Driver can successfully drop search indexes.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - - # Create a new search index on ``coll0``. + # Create a new search index on ``self.coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - resp = coll0.create_search_index(model) + resp = self.coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. self.assertEqual(resp, "test-search-index") - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: # An index with the ``name`` of ``test-search-index`` is present and index has a field ``queryable`` with the value of ``true``. - self.wait_for_ready(coll0) + self.wait_for_ready(self.coll0) - # Run a ``dropSearchIndex`` on ``coll0``, using ``test-search-index`` for the name. - coll0.drop_search_index(_NAME) + # Run a ``dropSearchIndex`` on ``self.coll0``, using ``test-search-index`` for the name. + self.coll0.drop_search_index(_NAME) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until ``listSearchIndexes`` returns an empty array. + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until ``listSearchIndexes`` returns an empty array. t0 = time.time() while True: - indices = (coll0.list_search_indexes()).to_list() + indices = (self.coll0.list_search_indexes()).to_list() if not indices: break if (time.time() - t0) / 60 > 5: @@ -227,33 +216,31 @@ def test_case_3(self): def test_case_4(self): """Driver can update a search index.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - # Create a new search index on ``coll0``. + # Create a new search index on ``self.coll0``. model = {"name": _NAME, "definition": {"mappings": {"dynamic": False}}} - resp = coll0.create_search_index(model) + resp = self.coll0.create_search_index(model) # Assert that the command returns the name of the index: ``"test-search-index"``. self.assertEqual(resp, _NAME) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: # An index with the ``name`` of ``test-search-index`` is present and index has a field ``queryable`` with the value of ``true``. - self.wait_for_ready(coll0) + self.wait_for_ready(self.coll0) - # Run a ``updateSearchIndex`` on ``coll0``. + # Run a ``updateSearchIndex`` on ``self.coll0``. # Assert that the command does not error and the server responds with a success. model2: dict[str, Any] = {"name": _NAME, "definition": {"mappings": {"dynamic": True}}} - coll0.update_search_index(_NAME, model2["definition"]) + self.coll0.update_search_index(_NAME, model2["definition"]) - # Run ``coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: + # Run ``self.coll0.listSearchIndexes()`` repeatedly every 5 seconds until the following condition is satisfied: # An index with the ``name`` of ``test-search-index`` is present. This index is referred to as ``index``. # The index has a field ``queryable`` with a value of ``true`` and has a field ``status`` with the value of ``READY``. predicate = lambda index: index.get("queryable") is True and index.get("status") == "READY" - self.wait_for_ready(coll0, predicate=predicate) + self.wait_for_ready(self.coll0, predicate=predicate) # Assert that an index is present with the name ``test-search-index`` and the definition has a property ``latestDefinition`` whose value is ``{ 'mappings': { 'dynamic': true } }``. - index = ((coll0.list_search_indexes(_NAME)).to_list())[0] + index = ((self.coll0.list_search_indexes(_NAME)).to_list())[0] self.assertIn("latestDefinition", index) self.assertEqual(index["latestDefinition"], model2["definition"]) @@ -267,11 +254,9 @@ def test_case_5(self): def test_case_6(self): """Driver can successfully create and list search indexes with non-default readConcern and writeConcern.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - # Apply a write concern ``WriteConcern(w=1)`` and a read concern with ``ReadConcern(level="majority")`` to ``coll0``. - coll0 = coll0.with_options( + # Apply a write concern ``WriteConcern(w=1)`` and a read concern with ``ReadConcern(level="majority")`` to ``self.coll0``. + coll0 = self.coll0.with_options( write_concern=WriteConcern(w="1"), read_concern=ReadConcern(level="majority") ) @@ -294,9 +279,6 @@ def test_case_6(self): def test_case_7(self): """Driver handles index types.""" - # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). - coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - # Use these search and vector search definitions for indexes. search_definition = {"mappings": {"dynamic": False}} vector_search_definition = { @@ -310,30 +292,30 @@ def test_case_7(self): ] } - # Create a new search index on ``coll0`` that implicitly passes its type. - implicit_search_resp = coll0.create_search_index( + # Create a new search index on ``self.coll0`` that implicitly passes its type. + implicit_search_resp = self.coll0.create_search_index( model={"name": _NAME + "-implicit", "definition": search_definition} ) # Get the index definition. - resp = (coll0.list_search_indexes(name=implicit_search_resp)).next() + resp = (self.coll0.list_search_indexes(name=implicit_search_resp)).next() # Assert that the index model contains the correct index type: ``"search"``. self.assertEqual(resp["type"], "search") - # Create a new search index on ``coll0`` that explicitly passes its type. - explicit_search_resp = coll0.create_search_index( + # Create a new search index on ``self.coll0`` that explicitly passes its type. + explicit_search_resp = self.coll0.create_search_index( model={"name": _NAME + "-explicit", "type": "search", "definition": search_definition} ) # Get the index definition. - resp = (coll0.list_search_indexes(name=explicit_search_resp)).next() + resp = (self.coll0.list_search_indexes(name=explicit_search_resp)).next() # Assert that the index model contains the correct index type: ``"search"``. self.assertEqual(resp["type"], "search") - # Create a new vector search index on ``coll0`` that explicitly passes its type. - explicit_vector_resp = coll0.create_search_index( + # Create a new vector search index on ``self.coll0`` that explicitly passes its type. + explicit_vector_resp = self.coll0.create_search_index( model={ "name": _NAME + "-vector", "type": "vectorSearch", @@ -342,14 +324,14 @@ def test_case_7(self): ) # Get the index definition. - resp = (coll0.list_search_indexes(name=explicit_vector_resp)).next() + resp = (self.coll0.list_search_indexes(name=explicit_vector_resp)).next() # Assert that the index model contains the correct index type: ``"vectorSearch"``. self.assertEqual(resp["type"], "vectorSearch") # Catch the error raised when trying to create a vector search index without specifying the type with self.assertRaises(OperationFailure) as e: - coll0.create_search_index( + self.coll0.create_search_index( model={"name": _NAME + "-error", "definition": vector_search_definition} ) self.assertIn("Attribute mappings missing.", e.exception.details["errmsg"]) diff --git a/test/test_session.py b/test/test_session.py index 65783b55df..072e311fc3 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -978,6 +978,12 @@ def setUp(self): super().setUp() self.listener = SessionTestListener() self.client = self.rs_or_single_client(event_listeners=[self.listener]) + self.client.pymongo_test.drop_collection("test") + self.client.pymongo_test.create_collection("test") + + def tearDown(self): + self.client.pymongo_test.drop_collection("test") + super().tearDown() @client_context.require_no_standalone def test_core(self): @@ -1048,9 +1054,6 @@ def _test_reads(self, op, exception=None): @client_context.require_no_standalone def test_reads(self): - # Make sure the collection exists. - self.client.pymongo_test.test.insert_one({}) - def aggregate(coll, session): return (coll.aggregate([], session=session)).to_list() @@ -1200,7 +1203,6 @@ def test_read_concern(self): @client_context.require_no_standalone def test_cluster_time_with_server_support(self): - self.client.pymongo_test.test.insert_one({}) self.listener.reset() self.client.pymongo_test.test.find_one({}) after_cluster_time = self.listener.started_events[0].command.get("$clusterTime") @@ -1208,7 +1210,6 @@ def test_cluster_time_with_server_support(self): @client_context.require_standalone def test_cluster_time_no_server_support(self): - self.client.pymongo_test.test.insert_one({}) self.listener.reset() self.client.pymongo_test.test.find_one({}) after_cluster_time = self.listener.started_events[0].command.get("$clusterTime") From 50d76517883d0a637e9a606dd9e5e3e141edff1a Mon Sep 17 00:00:00 2001 From: Sophia Yang Date: Fri, 5 Jun 2026 17:07:02 -0400 Subject: [PATCH 4/4] cleanup2 --- test/asynchronous/test_index_management.py | 3 --- test/asynchronous/test_session.py | 4 ---- test/test_index_management.py | 3 --- test/test_session.py | 4 ---- 4 files changed, 14 deletions(-) diff --git a/test/asynchronous/test_index_management.py b/test/asynchronous/test_index_management.py index 999637a06c..cd20b27986 100644 --- a/test/asynchronous/test_index_management.py +++ b/test/asynchronous/test_index_management.py @@ -98,9 +98,6 @@ async def asyncSetUp(self) -> None: self.db = self.client[self.db_name] self.coll0 = await self.db.create_collection(f"col{uuid.uuid4()}") - async def asyncTearDown(self): - await self.client.drop_database(_NAME) - async def wait_for_ready(self, coll, name=_NAME, predicate=None): """Wait for a search index to be ready.""" indices: list[Mapping[str, Any]] = [] diff --git a/test/asynchronous/test_session.py b/test/asynchronous/test_session.py index a0b76dec41..65476c8687 100644 --- a/test/asynchronous/test_session.py +++ b/test/asynchronous/test_session.py @@ -981,10 +981,6 @@ async def asyncSetUp(self): await self.client.pymongo_test.drop_collection("test") await self.client.pymongo_test.create_collection("test") - async def asyncTearDown(self): - await self.client.pymongo_test.drop_collection("test") - await super().asyncTearDown() - @async_client_context.require_no_standalone async def test_core(self): async with self.client.start_session() as sess: diff --git a/test/test_index_management.py b/test/test_index_management.py index f9c1b43f7c..425e6e47c7 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -98,9 +98,6 @@ def setUp(self) -> None: self.db = self.client[self.db_name] self.coll0 = self.db.create_collection(f"col{uuid.uuid4()}") - def tearDown(self): - self.client.drop_database(_NAME) - def wait_for_ready(self, coll, name=_NAME, predicate=None): """Wait for a search index to be ready.""" indices: list[Mapping[str, Any]] = [] diff --git a/test/test_session.py b/test/test_session.py index 072e311fc3..0ed27fa95f 100644 --- a/test/test_session.py +++ b/test/test_session.py @@ -981,10 +981,6 @@ def setUp(self): self.client.pymongo_test.drop_collection("test") self.client.pymongo_test.create_collection("test") - def tearDown(self): - self.client.pymongo_test.drop_collection("test") - super().tearDown() - @client_context.require_no_standalone def test_core(self): with self.client.start_session() as sess: