summaryrefslogtreecommitdiff
path: root/tests/bulk_create
diff options
context:
space:
mode:
authorNatalia <124304+nessita@users.noreply.github.com>2025-10-24 09:50:01 -0300
committernessita <124304+nessita@users.noreply.github.com>2025-10-24 11:33:40 -0300
commit6fcbbe0b855d8701a4da1b65772ccf326f996b9e (patch)
tree6409768621d93e4aa89e9d65611bbdc6a9ec93cf /tests/bulk_create
parent74239181252ca73bebb84789856f5d8937d421b4 (diff)
Fixed IntegrityError in bulk_create.tests.BulkCreateTransactionTests due to duplicate primary keys.
Some tests in BulkCreateTransactionTests were inserting Country objects with hardcoded primary keys, which could conflict with existing rows (if the sequence value wasn't bumped by another test). Updated the tests to dynamically select an unused primary key instead. Thanks to Simon Charette for the exhaustive and enlightening review.
Diffstat (limited to 'tests/bulk_create')
-rw-r--r--tests/bulk_create/tests.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py
index 8ab918fff5..7b77ffed3d 100644
--- a/tests/bulk_create/tests.py
+++ b/tests/bulk_create/tests.py
@@ -889,19 +889,27 @@ class BulkCreateTests(TestCase):
class BulkCreateTransactionTests(TransactionTestCase):
available_apps = ["bulk_create"]
+ def get_unused_country_id(self):
+ # Find a serial ID that hasn't been used already and has enough of a
+ # buffer for the following `bulk_create` call without an explicit pk
+ # not to conflict.
+ return getattr(Country.objects.last(), "id", 10) + 100
+
def test_no_unnecessary_transaction(self):
+ unused_id = self.get_unused_country_id()
with self.assertNumQueries(1):
Country.objects.bulk_create(
- [Country(id=1, name="France", iso_two_letter="FR")]
+ [Country(id=unused_id, name="France", iso_two_letter="FR")]
)
with self.assertNumQueries(1):
Country.objects.bulk_create([Country(name="Canada", iso_two_letter="CA")])
def test_objs_with_and_without_pk(self):
+ unused_id = self.get_unused_country_id()
with self.assertNumQueries(4):
Country.objects.bulk_create(
[
- Country(id=10, name="France", iso_two_letter="FR"),
+ Country(id=unused_id, name="France", iso_two_letter="FR"),
Country(name="Canada", iso_two_letter="CA"),
]
)