Skip to content

Commit

Permalink
#280 Set correct path to mocked render and render_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Nov 23, 2021
1 parent c926fb6 commit 525eea5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
11 changes: 6 additions & 5 deletions chcemvediet/apps/inforequests/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
56 changes: 55 additions & 1 deletion chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
4 changes: 0 additions & 4 deletions chcemvediet/apps/inforequests/views/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 525eea5

Please sign in to comment.