Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unordered parameter on BulkWriter #948

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
7 changes: 5 additions & 2 deletions beanie/odm/bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ class Config:


class BulkWriter:
def __init__(self, session: Optional[ClientSession] = None):
def __init__(
self, session: Optional[ClientSession] = None, ordered: bool = True
):
self.operations: List[Operation] = []
self.session = session
self.ordered = ordered

async def __aenter__(self):
return self
Expand Down Expand Up @@ -78,7 +81,7 @@ async def commit(self) -> Optional[BulkWriteResult]:
requests.append(query)

return await obj_class.get_motor_collection().bulk_write( # type: ignore
requests, session=self.session
requests, session=self.session, ordered=self.ordered
)
return None

Expand Down
22 changes: 22 additions & 0 deletions tests/odm/documents/test_bulk_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ async def test_update(documents, document_not_inserted):
)


async def test_unordered_update(documents, document):
await documents(5)
doc = await DocumentTestModel.find_one(DocumentTestModel.test_int == 0)
doc.test_int = 100
with pytest.raises(BulkWriteError):
async with BulkWriter(ordered=False) as bulk_writer:
await DocumentTestModel.insert_one(
document, bulk_writer=bulk_writer
)
await doc.save_changes(bulk_writer=bulk_writer)

assert len(await DocumentTestModel.find_all().to_list()) == 6
assert (
len(
await DocumentTestModel.find(
DocumentTestModel.test_int == 100
).to_list()
)
== 1
)


async def test_delete(documents, document_not_inserted):
await documents(5)
doc = await DocumentTestModel.find_one(DocumentTestModel.test_int == 0)
Expand Down
Loading