From 760ad00ac7f9ca7330884cbd924a21b6a5d557e4 Mon Sep 17 00:00:00 2001 From: Anders <6058745+ddabble@users.noreply.github.com> Date: Sat, 24 Aug 2024 19:52:36 +0200 Subject: [PATCH] test num queries --- simple_history/tests/tests/test_utils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index 7db701d9..66cafdaa 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -5,7 +5,7 @@ import django from django.contrib.auth import get_user_model -from django.db import IntegrityError, transaction +from django.db import IntegrityError, connection, transaction from django.test import TestCase, TransactionTestCase, override_settings from django.utils import timezone @@ -155,6 +155,22 @@ def test_bulk_create_history_with_disabled_setting(self): self.assertEqual(Poll.objects.count(), 5) self.assertEqual(Poll.history.count(), 0) + def test_bulk_create_history_without_pks(self): + for poll in self.data: + poll.pk = None + + # See https://docs.djangoproject.com/en/stable/ref/models/querysets/#bulk-create + db_supports_returning_autofield_pks = connection.vendor in { + "postgresql", + "sqlite", + } + # An extra query must be made on some DBs to retrieve the auto-generated PKs + with self.assertNumQueries(2 if db_supports_returning_autofield_pks else 3): + bulk_create_with_history(self.data, Poll) + + self.assertEqual(Poll.objects.count(), 5) + self.assertEqual(Poll.history.count(), 5) + def test_bulk_create_history_alternative_manager(self): bulk_create_with_history( self.data,