From 0a419a1f9f369c9e8f22204d7270ce725858b0a3 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 25 Sep 2024 10:23:52 -0400 Subject: [PATCH 1/2] add force_insert_update tests to CI --- .github/workflows/test-python.yml | 1 + django_mongodb/features.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index dd449bb13..d8fc02ea9 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -82,6 +82,7 @@ jobs: expressions_case defer defer_regress + force_insert_update from_db_value generic_relations generic_relations_regress diff --git a/django_mongodb/features.py b/django_mongodb/features.py index c001de119..21cd64884 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -126,6 +126,10 @@ class DatabaseFeatures(BaseDatabaseFeatures): # subclasses of BaseDatabaseIntrospection may require a get_constraints() method "migrations.test_operations.OperationTests.test_add_func_unique_constraint", "migrations.test_operations.OperationTests.test_remove_func_unique_constraint", + # MongoDB's "duplicate key error" must be raised as IntegrityError, not + # DatabaseError. + "force_insert_update.tests.ForceInsertInheritanceTests.test_force_insert_diamond_mti", + "force_insert_update.tests.ForceTests.test_force_update", } # $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3. _django_test_expected_failures_bitwise = { From 5b92640be3bd197fddcd8734146643cfef8cc2ce Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 25 Sep 2024 10:33:48 -0400 Subject: [PATCH 2/2] raise "duplicate key error" as IntegrityError --- django_mongodb/features.py | 4 ---- django_mongodb/query.py | 6 +++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 21cd64884..c001de119 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -126,10 +126,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): # subclasses of BaseDatabaseIntrospection may require a get_constraints() method "migrations.test_operations.OperationTests.test_add_func_unique_constraint", "migrations.test_operations.OperationTests.test_remove_func_unique_constraint", - # MongoDB's "duplicate key error" must be raised as IntegrityError, not - # DatabaseError. - "force_insert_update.tests.ForceInsertInheritanceTests.test_force_insert_diamond_mti", - "force_insert_update.tests.ForceTests.test_force_update", } # $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3. _django_test_expected_failures_bitwise = { diff --git a/django_mongodb/query.py b/django_mongodb/query.py index df2d0d19c..4c8b4639d 100644 --- a/django_mongodb/query.py +++ b/django_mongodb/query.py @@ -9,7 +9,7 @@ from django.db.models.sql.constants import INNER from django.db.models.sql.datastructures import Join from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode -from pymongo.errors import DuplicateKeyError, PyMongoError +from pymongo.errors import BulkWriteError, DuplicateKeyError, PyMongoError def wrap_database_errors(func): @@ -17,6 +17,10 @@ def wrap_database_errors(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) + except BulkWriteError as e: + if "E11000 duplicate key error" in str(e): + raise IntegrityError from e + raise except DuplicateKeyError as e: raise IntegrityError from e except PyMongoError as e: