Skip to content

Commit

Permalink
test num queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ddabble committed Sep 2, 2024
1 parent d8cf0b8 commit a5099e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions simple_history/tests/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
get_m2m_reverse_field_name,
update_change_reason,
)
from .utils import db_supports_returning_autofield_pks

User = get_user_model()

Expand Down Expand Up @@ -155,6 +156,28 @@ 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

# 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_without_some_pks(self):
self.data[1].pk = None
self.data[3].pk = None

# An extra query must be made on some DBs to retrieve the auto-generated PKs
with self.assertNumQueries(3 if db_supports_returning_autofield_pks() else 4):
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,
Expand Down
12 changes: 12 additions & 0 deletions simple_history/tests/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from enum import Enum
from typing import Type
from functools import cache

from django.conf import settings
from django.db.models import Model
from django.db import connection
from django.test import TestCase

request_middleware = "simple_history.middleware.HistoryRequestMiddleware"
Expand Down Expand Up @@ -35,6 +37,16 @@ def assertRecordValues(self, record, klass: Type[Model], values_dict: dict):
self.assertEqual(getattr(record.history_object, key), value)


@cache
def db_supports_returning_autofield_pks() -> bool:
# See https://docs.djangoproject.com/en/stable/ref/models/querysets/#bulk-create
return connection.display_name.lower() in {
"postgresql",
"mariadb",
"sqlite",
}


class TestDbRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == "external":
Expand Down

0 comments on commit a5099e5

Please sign in to comment.