|
| 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