diff --git a/chcemvediet/apps/inforequests/tests/__init__.py b/chcemvediet/apps/inforequests/tests/__init__.py index f12989ed..a7f303bb 100644 --- a/chcemvediet/apps/inforequests/tests/__init__.py +++ b/chcemvediet/apps/inforequests/tests/__init__.py @@ -21,7 +21,7 @@ class InforequestsTestCaseMixin(ChcemvedietTestCaseMixin): @contextlib.contextmanager - def assertQueriesDuringRender(self, patterns, **kwargs): + def assertQueriesDuringRender(self, *patterns, **kwargs): u""" Use to assert that views prefetch all related models before rendering their templates. Views should prefetch their related models to prevent templates from making database @@ -39,15 +39,16 @@ def mock_render(*args, **kwargs): return res def mock_render_to_string(*args, **kwargs): if pre_mock_render_to_string: # pragma: no cover - self.pre_mock_render_to_string(*args, **kwargs) + pre_mock_render_to_string(*args, **kwargs) with CaptureQueriesContext(connection) as captured: res = render_to_string(*args, **kwargs) queries.append(captured) return res - with mock.patch(u'chcemvediet.apps.inforequests.views.render', mock_render): - with mock.patch(u'chcemvediet.apps.inforequests.views.render_to_string', mock_render_to_string): - yield + with mock.patch(u'chcemvediet.apps.inforequests.views.inforequest.render', mock_render): + with mock.patch(u'chcemvediet.apps.inforequests.views.shortcuts.render', mock_render): + with mock.patch(u'chcemvediet.apps.inforequests.views.shortcuts.render_to_string', mock_render_to_string): + yield self.assertEqual(len(queries), len(patterns), u'%d renders executed, %d expected' % (len(queries), len(patterns))) for render_queries, render_patterns in zip(queries, patterns): diff --git a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py index fd43dc55..c1532ae3 100644 --- a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py +++ b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- from django.test import TestCase +from poleno.utils.misc import squeeze from poleno.utils.test import ViewTestCaseMixin from poleno.utils.urls import reverse @@ -79,6 +80,59 @@ def pre_mock_render(request, template, context): list(context[u'unsuccessful_inforequests']) self._login_user(self.user1) - with self.assertQueriesDuringRender([], pre_mock_render=pre_mock_render): + + # main/base/single_column.html + patterns = [ + u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s', + squeeze(u""" + FROM "mail_message" + INNER JOIN "inforequests_inforequestemail" ON \( "mail_message"."id" = "inforequests_inforequestemail"."email_id" \) + INNER JOIN "inforequests_inforequest" ON \( "inforequests_inforequestemail"."inforequest_id" = "inforequests_inforequest"."id" \) + WHERE \("inforequests_inforequestemail"."type" = %s + AND "inforequests_inforequest"."applicant_id" = %s + AND "inforequests_inforequest"."closed" = %s\) + """), + u'FROM "invitations_invitationsupply" WHERE "invitations_invitationsupply"."user_id" = %s', + ] + + # pending_inforequests + patterns.extend([ + squeeze(u""" + FROM "inforequests_action" + INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \) + WHERE "inforequests_branch"."inforequest_id" = %s + """) for _ in range(4) + ]) + + # successful_inforequests + patterns.extend([ + u'FROM "inforequests_action" WHERE "inforequests_action"."branch_id" = %s', + u'FROM "inforequests_branch" WHERE "inforequests_branch"."inforequest_id" = %s', + squeeze(u""" + FROM "inforequests_action" + INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \) + WHERE "inforequests_branch"."inforequest_id" = %s + """), + squeeze(u""" + FROM "inforequests_action" + INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \) + WHERE "inforequests_branch"."inforequest_id" = %s + """), + ] * 3) + + # unsuccessful_inforequests + patterns.extend([ + squeeze(u""" + FROM "inforequests_action" + INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \) + WHERE "inforequests_branch"."inforequest_id" = %s + """), + squeeze(u""" + FROM "inforequests_action" + INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \) + WHERE "inforequests_branch"."inforequest_id" = %s + """), + ] * 3) + with self.assertQueriesDuringRender(patterns, pre_mock_render=pre_mock_render): response = self.client.get(reverse(u'inforequests:mine')) self.assertEqual(response.status_code, 200) diff --git a/chcemvediet/apps/inforequests/views/__init__.py b/chcemvediet/apps/inforequests/views/__init__.py index 5b0b3dca..299bd1d6 100644 --- a/chcemvediet/apps/inforequests/views/__init__.py +++ b/chcemvediet/apps/inforequests/views/__init__.py @@ -1,10 +1,6 @@ # vim: expandtab # -*- coding: utf-8 -*- -from django.shortcuts import render # To let tests mock this function. - -from poleno.utils.template import render_to_string # To let tests mock this function. - from .inforequest import inforequest_index from .inforequest import inforequest_mine from .inforequest import inforequest_create