diff --git a/reversion_compare_tests/test_factory_car_reverse_models.py b/reversion_compare_tests/test_factory_car_reverse_models.py index 87af1f9..86e4230 100644 --- a/reversion_compare_tests/test_factory_car_reverse_models.py +++ b/reversion_compare_tests/test_factory_car_reverse_models.py @@ -11,15 +11,22 @@ * models.OneToOneField() * models.IntegerField() - :copyleft: 2012-2016 by the django-reversion-compare team, see AUTHORS for more details. + :copyleft: 2012-2017 by the django-reversion-compare team, see AUTHORS for more details. :license: GNU GPL v3 or above, see LICENSE for more details. """ from __future__ import absolute_import, division, print_function -from reversion import is_registered -from reversion import unregister, revisions -from reversion.models import Version, Revision +from django.db import connection +from django.test.utils import CaptureQueriesContext + +from reversion import is_registered, revisions, unregister +from reversion.models import Revision, Version + +from .models import Car, Factory, Person +from .test_utils.db_queries import print_db_queries +from .test_utils.test_cases import BaseTestCase +from .test_utils.test_data import TestData try: import django_tools @@ -31,9 +38,6 @@ ) % err raise ImportError(msg) -from .models import Factory, Car, Person -from .test_utils.test_cases import BaseTestCase -from .test_utils.test_data import TestData class FactoryCarReverseRelationModelTest(BaseTestCase): @@ -81,11 +85,7 @@ def test_select_compare(self): '' % self.version_ids[2], ) - def test_diff1(self): - response = self.client.get( - "/admin/reversion_compare_tests/factory/%s/history/compare/" % self.factory.pk, - data={"version_id2": self.version_ids[1], "version_id1": self.version_ids[2]} - ) + def assert_diff1(self, response): # debug_response(response) # from django-tools self.assertContainsHtml( response, @@ -103,3 +103,25 @@ def test_diff1(self): ''', '
version 2: discontinued car-three, add car-four, add Bob the worker
', # edit comment ) + + def test_diff1(self): + response = self.client.get( + "/admin/reversion_compare_tests/factory/%s/history/compare/" % self.factory.pk, + data={"version_id2": self.version_ids[1], "version_id1": self.version_ids[2]} + ) + self.assert_diff1(response) + + def test_select_compare1_queries(self): + with CaptureQueriesContext(connection) as queries: + response = self.client.get( + "/admin/reversion_compare_tests/factory/%s/history/compare/" % self.factory.pk, + data={"version_id2": self.version_ids[1], "version_id1": self.version_ids[2]} + ) + self.assert_diff1(response) + + # print_db_queries(queries.captured_queries) + # total queries....: 37 + # unique queries...: 28 + # duplicate queries: 9 + + self.assertLess(len(queries.captured_queries), 37+2) # real+buffer diff --git a/reversion_compare_tests/test_utils/db_queries.py b/reversion_compare_tests/test_utils/db_queries.py new file mode 100644 index 0000000..a3aaade --- /dev/null +++ b/reversion_compare_tests/test_utils/db_queries.py @@ -0,0 +1,32 @@ +# coding: utf-8 + +""" + django-reversion-compare unittests + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyleft: 2017 by the django-reversion-compare team, see AUTHORS for more details. + :created: 2017 by Jens Diemer + :license: GNU GPL v3 or above, see LICENSE for more details. +""" + +from __future__ import absolute_import, division, print_function + +import re + + +def print_db_queries(queries): + queries_data={} + for query in queries: + sql = query["sql"] + queries_data.setdefault(sql, 0) + queries_data[sql] += 1 + duplicates = sum([count - 1 for count in list(queries_data.values())]) + print("-"*79) + print("total queries....: %i" % len(queries)) + print("unique queries...: %i" % len(queries_data)) + print("duplicate queries: %i" % duplicates) + print() + for query, count in sorted(queries_data.items()): + query = re.sub(r'["\'`]', "", query) + print("%s x %s" % (count, query)) + print("-"*79) diff --git a/reversion_compare_tests/test_view.py b/reversion_compare_tests/test_view.py index 7a30ca8..156f507 100644 --- a/reversion_compare_tests/test_view.py +++ b/reversion_compare_tests/test_view.py @@ -6,16 +6,24 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Test history compare CBV - - :copyleft: 2012-2016 by the django-reversion-compare team, see AUTHORS for more details. + + :copyleft: 2012-2017 by the django-reversion-compare team, see AUTHORS for more details. :license: GNU GPL v3 or above, see LICENSE for more details. """ from __future__ import absolute_import, division, print_function +from django.db import connection +from django.test.utils import CaptureQueriesContext + from reversion import is_registered from reversion.models import Version +from .models import SimpleModel +from .test_utils.db_queries import print_db_queries +from .test_utils.test_cases import BaseTestCase +from .test_utils.test_data import TestData + try: import django_tools except ImportError as err: @@ -27,12 +35,6 @@ raise ImportError(msg) - -from .test_utils.test_cases import BaseTestCase -from .models import SimpleModel -from .test_utils.test_data import TestData - - class CBViewTest(BaseTestCase): """ unittests for testing reversion_compare.views.HistoryCompareDetailView @@ -46,7 +48,7 @@ def setUp(self): queryset = Version.objects.get_for_object(self.item1) self.version_ids1 = queryset.values_list("pk", flat=True) - + queryset = Version.objects.get_for_object(self.item2) self.version_ids2 = queryset.values_list("pk", flat=True) @@ -58,12 +60,11 @@ def test_initial_state(self): self.assertEqual(Version.objects.get_for_object(self.item1).count(), 2) self.assertEqual(list(self.version_ids1), [2, 1]) - + self.assertEqual(list(self.version_ids1), [2, 1]) self.assertEqual(list(self.version_ids2), [7, 6, 5, 4, 3]) - - def test_select_compare1(self): - response = self.client.get("/test_view/%s" % self.item1.pk) + + def assert_select_compare1(self, response): self.assertContainsHtml( response, '', @@ -72,7 +73,23 @@ def test_select_compare1(self): '' % self.version_ids1[1], '' % self.version_ids1[1], ) - + + def test_select_compare1(self): + response = self.client.get("/test_view/%s" % self.item1.pk) + self.assert_select_compare1(response) + + def test_select_compare1_queries(self): + with CaptureQueriesContext(connection) as queries: + response = self.client.get("/test_view/%s" % self.item1.pk) + self.assert_select_compare1(response) + + # print_db_queries(queries.captured_queries) + # total queries....: 7 + # unique queries...: 4 + # duplicate queries: 3 + + self.assertLess(len(queries.captured_queries), 7+2) # real+buffer + def test_select_compare2(self): response = self.client.get("/test_view/%s" % self.item2.pk) for i in range(4): @@ -86,12 +103,8 @@ def test_select_compare2(self): "%s" % comment, '', ) - - def test_select_compare_and_diff(self): - response = self.client.get("/test_view/%s" % self.item1.pk, data={ - "version_id2": self.version_ids1[0], - "version_id1": self.version_ids1[1] - }) + + def assert_select_compare_and_diff(self, response): self.assertContainsHtml( response, '', @@ -106,7 +119,28 @@ def test_select_compare_and_diff(self): '+ version two', '
simply change the CharField text.
', # edit comment ) - + + def test_select_compare_and_diff(self): + response = self.client.get("/test_view/%s" % self.item1.pk, data={ + "version_id2": self.version_ids1[0], + "version_id1": self.version_ids1[1] + }) + self.assert_select_compare_and_diff(response) + + def test_select_compare_and_diff_queries(self): + with CaptureQueriesContext(connection) as queries: + response = self.client.get("/test_view/%s" % self.item1.pk, data={ + "version_id2": self.version_ids1[0], + "version_id1": self.version_ids1[1] + }) + self.assert_select_compare_and_diff(response) + + # print_db_queries(queries.captured_queries) + # total queries....: 15 + # unique queries...: 9 + # duplicate queries: 6 + self.assertLess(len(queries.captured_queries), 15+2) # real+buffer + def test_prev_next_buttons(self): base_url = "/test_view/%s" % self.item2.pk for i in range(4):