diff --git a/simple_history/tests/tests/test_manager.py b/simple_history/tests/tests/test_manager.py index 30e5ec11..19df1821 100644 --- a/simple_history/tests/tests/test_manager.py +++ b/simple_history/tests/tests/test_manager.py @@ -3,7 +3,7 @@ import django from django.contrib.auth import get_user_model -from django.db import IntegrityError +from django.db import IntegrityError, connection from django.test import TestCase, override_settings, skipUnlessDBFeature from simple_history.manager import SIMPLE_HISTORY_REVERSE_ATTR_NAME @@ -151,6 +151,9 @@ def test_historical_query_set(self): """ Demonstrates how the HistoricalQuerySet works to provide as_of functionality. """ + backend = connection.vendor + is_mysql = backend == "mysql" + document1 = RankedDocument.objects.create(rank=42) document2 = RankedDocument.objects.create(rank=84) document2.rank = 51 @@ -158,26 +161,30 @@ def test_historical_query_set(self): document1.delete() t2 = datetime.now() - # look for historical records, get back a queryset + # Look for historical records, get back a queryset with self.assertNumQueries(1): - queryset = RankedDocument.history.filter(history_date__lte=t2) + queryset = RankedDocument.history.filter(history_date__lte=t1) self.assertEqual(queryset.count(), 4) - # only want the most recend records (provided by HistoricalQuerySet) - self.assertEqual(queryset.latest_of_each().count(), 2) + # Only want the most recent records (provided by HistoricalQuerySet) + with self.assertNumQueries(2 if is_mysql else 1): + self.assertEqual(queryset.latest_of_each().count(), 2) - # want to see the instances as of that time? - self.assertEqual(queryset.latest_of_each().as_instances().count(), 1) + # Want to see the instances as of that time? + with self.assertNumQueries(2 if is_mysql else 1): + self.assertEqual(queryset.latest_of_each().as_instances().count(), 1) - # these new methods are idempotent - self.assertEqual( - queryset.latest_of_each() - .latest_of_each() - .as_instances() - .as_instances() - .count(), - 1, - ) + # These new methods are idempotent + # (One extra query per call to `latest_of_each()` when using MySQL) + with self.assertNumQueries(3 if is_mysql else 1): + self.assertEqual( + queryset.latest_of_each() + .latest_of_each() + .as_instances() + .as_instances() + .count(), + 1, + ) # that was all the same as calling as_of! self.assertEqual(