Skip to content

Commit c9a8961

Browse files
committed
<FIX>: async_insert - write concern
1 parent 6ec069b commit c9a8961

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

mongoengine/queryset/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pymongo.collection import ReturnDocument
1212
from pymongo.common import validate_read_preference
1313
from pymongo.read_concern import ReadConcern
14+
from pymongo.write_concern import WriteConcern
1415

1516
from mongoengine import signals
1617
from mongoengine.async_utils import (
@@ -2461,6 +2462,12 @@ async def async_insert(
24612462

24622463
# Apply write concern if provided
24632464
if write_concern:
2465+
# Convert dict to WriteConcern object if needed
2466+
if isinstance(write_concern, dict):
2467+
# Merge with existing write concern
2468+
combined_concerns = dict(collection.write_concern.document.items())
2469+
combined_concerns.update(write_concern)
2470+
write_concern = WriteConcern(**combined_concerns)
24642471
collection = collection.with_options(write_concern=write_concern)
24652472

24662473
session = await _get_async_session()

test_async_insert_write_concern.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python
2+
"""Test write_concern handling in async_insert method"""
3+
4+
import asyncio
5+
6+
from mongoengine import (
7+
Document,
8+
StringField,
9+
connect_async,
10+
disconnect_async,
11+
)
12+
13+
14+
class TestDocument(Document):
15+
name = StringField(required=True)
16+
meta = {"collection": "test_write_concern", "db_alias": "test_async"}
17+
18+
19+
async def test_async_insert_with_write_concern():
20+
"""Test that async_insert properly handles write_concern as dict"""
21+
22+
# Connect to MongoDB
23+
await connect_async("test_db", alias="test_async")
24+
25+
try:
26+
# Clear existing data
27+
await TestDocument.objects.async_delete()
28+
29+
# Test 1: Insert with write_concern as dict (should now work)
30+
print("Test 1: Insert with write_concern as dict")
31+
doc1 = TestDocument(name="Test1")
32+
write_concern_dict = {"w": 1, "j": False}
33+
34+
try:
35+
result = await TestDocument.objects.async_insert(
36+
doc1, write_concern=write_concern_dict
37+
)
38+
print(f" ✓ Insert with dict write_concern succeeded: {result.name}")
39+
except TypeError as e:
40+
print(f" ✗ Error with dict write_concern: {e}")
41+
42+
# Test 2: Insert without write_concern (default)
43+
print("\nTest 2: Insert without write_concern")
44+
doc2 = TestDocument(name="Test2")
45+
result = await TestDocument.objects.async_insert(doc2)
46+
print(f" ✓ Insert without write_concern succeeded: {result.name}")
47+
48+
# Test 3: Bulk insert with write_concern
49+
print("\nTest 3: Bulk insert with write_concern")
50+
docs = [TestDocument(name="Test3"), TestDocument(name="Test4")]
51+
results = await TestDocument.objects.async_insert(docs, write_concern={"w": 1})
52+
print(f" ✓ Bulk insert with write_concern succeeded: {len(results)} docs")
53+
54+
# Verify all documents were inserted
55+
count = await TestDocument.objects.async_count()
56+
print(f"\nTotal documents inserted: {count}")
57+
58+
# Also test with WriteConcern object directly
59+
from pymongo.write_concern import WriteConcern
60+
61+
print("\nTest 4: Insert with WriteConcern object")
62+
doc5 = TestDocument(name="Test5")
63+
wc = WriteConcern(w=1, j=True)
64+
result = await TestDocument.objects.async_insert(doc5, write_concern=wc)
65+
print(f" ✓ Insert with WriteConcern object succeeded: {result.name}")
66+
67+
print("\nAll tests passed successfully!")
68+
69+
except Exception as e:
70+
print(f"Error during testing: {e}")
71+
import traceback
72+
73+
traceback.print_exc()
74+
75+
finally:
76+
# Clean up
77+
await TestDocument.objects.async_delete()
78+
await disconnect_async("test_async")
79+
80+
81+
if __name__ == "__main__":
82+
asyncio.run(test_async_insert_with_write_concern())

0 commit comments

Comments
 (0)