Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/asynchronous/test_change_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 4 additions & 4 deletions test/asynchronous/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")])
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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())

Expand Down
13 changes: 5 additions & 8 deletions test/asynchronous/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,9 +1513,12 @@ 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()

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()
Expand All @@ -1525,7 +1528,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)

Expand Down Expand Up @@ -1555,7 +1557,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)

Expand All @@ -1576,7 +1577,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)

Expand All @@ -1595,12 +1595,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)
Expand All @@ -1615,7 +1613,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))
Expand All @@ -1632,6 +1629,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):
Expand All @@ -1645,7 +1643,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()
Expand Down
116 changes: 45 additions & 71 deletions test/asynchronous/test_index_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ async def asyncSetUp(self) -> None:
)
await self.client.drop_database(_NAME)
self.db = self.client[self.db_name]

async def asyncTearDown(self):
await self.client.drop_database(_NAME)
self.coll0 = await self.db.create_collection(f"col{uuid.uuid4()}")

async def wait_for_ready(self, coll, name=_NAME, predicate=None):
"""Wait for a search index to be ready."""
Expand All @@ -117,22 +115,20 @@ 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 = self.db[f"col{uuid.uuid4()}"]
await coll0.insert_one({})

# 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]
self.assertEqual(event.command["comment"], "foo")

# 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")

Expand All @@ -143,20 +139,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``).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, i see its been moved to the setup,, can we either this comment up with the line of code maybe?

coll0 = self.db[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}}}
await coll0.insert_one({})
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)
Expand All @@ -165,33 +157,29 @@ 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 = self.db[f"col{uuid.uuid4()}"]
await coll0.insert_one({})

# 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}}
index_definitions: list[dict[str, Any]] = [
{"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]:
Expand All @@ -201,28 +189,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 = self.db[f"col{uuid.uuid4()}"]
await coll0.insert_one({})

# 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:
Expand All @@ -231,34 +215,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 = self.db[f"col{uuid.uuid4()}"]
await coll0.insert_one({})

# 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"])

Expand All @@ -272,12 +253,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 = self.db[f"col{uuid.uuid4()}"]
await coll0.insert_one({})

# 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")
)

Expand All @@ -300,10 +278,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 = self.db[f"col{uuid.uuid4()}"]
await coll0.insert_one({})

# Use these search and vector search definitions for indexes.
search_definition = {"mappings": {"dynamic": False}}
vector_search_definition = {
Expand All @@ -317,30 +291,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",
Expand All @@ -349,14 +323,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"])
Expand Down
Loading
Loading