Skip to content

Commit

Permalink
Add some "db query count" tests, see: #95
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Oct 2, 2017
1 parent caba7b9 commit b4cd768
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 33 deletions.
46 changes: 34 additions & 12 deletions reversion_compare_tests/test_factory_car_reverse_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -81,11 +85,7 @@ def test_select_compare(self):
'<input type="radio" name="version_id2" value="%i" />' % 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,
Expand All @@ -103,3 +103,25 @@ def test_diff1(self):
''',
'<blockquote>version 2: discontinued car-three, add car-four, add Bob the worker</blockquote>', # 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
32 changes: 32 additions & 0 deletions reversion_compare_tests/test_utils/db_queries.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
: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)
76 changes: 55 additions & 21 deletions reversion_compare_tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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,
'<input type="submit" value="compare">',
Expand All @@ -72,7 +73,23 @@ def test_select_compare1(self):
'<input type="radio" name="version_id1" value="%i" checked="checked" />' % self.version_ids1[1],
'<input type="radio" name="version_id2" value="%i" />' % 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):
Expand All @@ -86,12 +103,8 @@ def test_select_compare2(self):
"<td>%s</td>" % comment,
'<input type="submit" value="compare">',
)

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,
'<input type="submit" value="compare">',
Expand All @@ -106,7 +119,28 @@ def test_select_compare_and_diff(self):
'<ins>+ version two</ins>',
'<blockquote>simply change the CharField text.</blockquote>', # 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):
Expand Down

0 comments on commit b4cd768

Please sign in to comment.