Skip to content

Commit

Permalink
#280 Fix test_detail view
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Dec 15, 2021
1 parent 5f4315e commit 63976ae
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 78 deletions.
4 changes: 2 additions & 2 deletions chcemvediet/apps/inforequests/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ def _create_inforequest_scenario(self, *args):
``_create_message()`` and <recipient_args> arguments for ``_create_recipient()``.
"""
args = list(args)
applicant = args.pop(0) if args and isinstance(args[0], User) else self.user1
obligee = args.pop(0) if args and isinstance(args[0], Obligee) else self.obligee1
applicant = args.pop(0) if args and isinstance(args[0], User) else self.user
obligee = args.pop(0) if args and isinstance(args[0], Obligee) else self.obligee
extra = args.pop(0) if args and isinstance(args[0], dict) else {}
inforequest = Inforequest.objects.create(applicant=applicant, **extra)
branch, actions = self._create_inforequest_scenario__branch(inforequest, obligee, None, u'request', args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def test_slug_property_with_empty_subject(self):
self.assertEqual(inforequest.slug, ugettext(u'inforequests:Inforequest:fallback_slug'))

def test_prefetch_branches_staticmethod(self):
inforequest, branch1, actions = self._create_inforequest_scenario(u'advancement')
inforequest, branch1, actions = self._create_inforequest_scenario(self.user1, u'advancement')
_, (_, ((branch2, _),)) = actions

# Without arguments
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_branches_property(self):
self.assertEqual(inforequest.branches, [branch1, branch2])

def test_prefetch_main_branch_staticmethod(self):
inforequest, branch, _ = self._create_inforequest_scenario(u'advancement')
inforequest, branch, _ = self._create_inforequest_scenario(self.user1, u'advancement')

# Without arguments
with self.assertNumQueries(2):
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_undecided_emails_set_property_is_not_ordered(self):
self.assertFalse(inforequest.undecided_emails_set.all().ordered)

def test_prefetch_undecided_emails_staticmethod(self):
inforequest, _, _ = self._create_inforequest_scenario()
inforequest, _, _ = self._create_inforequest_scenario(self.user1)
_, rel1 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNKNOWN)
_, rel2 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNRELATED)
_, rel3 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNDECIDED)
Expand Down Expand Up @@ -661,7 +661,7 @@ def test_oldest_undecided_email_property_with_no_undecided_emails(self):
self.assertIsNone(inforequest.oldest_undecided_email)

def test_prefetch_newest_undecided_email_staticmethod(self):
inforequest, _, _ = self._create_inforequest_scenario()
inforequest, _, _ = self._create_inforequest_scenario(self.user1)
_, rel1 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNKNOWN)
_, rel2 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNRELATED)
_, rel3 = self._create_inforequest_email(inforequest=inforequest, reltype=InforequestEmail.TYPES.UNDECIDED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# vim: expandtab
# -*- coding: utf-8 -*-
from collections import defaultdict

from django.test import TestCase

from poleno.utils.test import ViewTestCaseMixin
from poleno.utils.urls import reverse

from .. import InforequestsTestCaseMixin


class DetailViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase):
u"""
Tests ``detail()`` view registered as "inforequests:detail".
Expand All @@ -22,44 +21,53 @@ def assertTemplateUsedCount(self, response, template_name, count):
def test_allowed_http_methods(self):
inforequest, _, _ = self._create_inforequest_scenario()
allowed = [u'HEAD', u'GET']
self.assert_allowed_http_methods(allowed, reverse(u'inforequests:detail', args=(inforequest.pk,)))

def test_anonymous_user_is_redirected(self):
inforequest, _, _ = self._create_inforequest_scenario()
self.assert_anonymous_user_is_redirected(reverse(u'inforequests:detail', args=(inforequest.pk,)))
self.assert_allowed_http_methods(allowed, reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

def test_authenticated_user_gets_inforequest_detail(self):
inforequest, _, _ = self._create_inforequest_scenario()
def test_owner_gets_inforequest_detail(self):
inforequest, _, _ = self._create_inforequest_scenario({u'published': False})
self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, u'inforequests/detail.html')
self.assertTemplateUsed(response, u'inforequests/detail/detail.html')

def test_invalid_inforequest_returns_404_not_found(self):
self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(47,)))
self.assertEqual(response.status_code, 404)

def test_inforequest_owned_by_another_user_returns_404_not_found(self):
inforequest, _, _ = self._create_inforequest_scenario(self.user2)
def test_published_inforequest_owned_by_another_user_gets_inforequest_detail(self):
inforequest, _, _ = self._create_inforequest_scenario(self.user, {u'published': True})
self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, u'inforequests/detail/detail.html')

def test_non_published_inforequest_owned_by_another_user_returns_404_not_found(self):
inforequest, _, _ = self._create_inforequest_scenario(self.user, {u'published': False})
self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))
self.assertEqual(response.status_code, 404)

def test_invalid_inforequest_slug_is_redirected_to_inforequest_detail(self):
inforequest, _, _ = self._create_inforequest_scenario({u'published': True})
response1 = self.client.get(reverse(u'inforequests:detail', args=(u'invalid', inforequest.pk,)))
response2 = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
self.assertRedirects(response1, reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))
self.assertRedirects(response2, reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

def test_inforequest_with_single_branch(self):
inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'extension')
inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'reversion')
self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

# Three actions in single branch
self.assertTemplateUsedCount(response, u'inforequests/detail_branch.html', 1)
self.assertTemplateUsedCount(response, u'inforequests/detail_action.html', 3)
self.assertTemplateUsedCount(response, u'inforequests/detail/branch/main.html', 1)
self.assertTemplateUsedCount(response, u'inforequests/detail/action/main.html', 3)

def test_inforequest_with_single_branch_related_models_are_prefetched_before_render(self):
inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'extension')
inforequest, _, _ = self._create_inforequest_scenario(u'confirmation', u'reversion')
self._login_user()
with self.assertQueriesDuringRender([]):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
with self.assertQueriesDuringRender(self.patterns_single_column):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

def test_inforequest_with_multiple_branches(self):
inforequest, _, _ = self._create_inforequest_scenario(
Expand All @@ -70,7 +78,7 @@ def test_inforequest_with_multiple_branches(self):
[u'confirmation', (u'advancement', [u'refusal'])]),
)
self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

# Total 12 actions in 5 branches.
# -- Main Branch:
Expand All @@ -90,8 +98,9 @@ def test_inforequest_with_multiple_branches(self):
# -- branch:
# -- advanced_request
# -- refusal
self.assertTemplateUsedCount(response, u'inforequests/detail_branch.html', 5)
self.assertTemplateUsedCount(response, u'inforequests/detail_action.html', 12)
self.assertTemplateUsedCount(response, u'inforequests/detail/branch/main.html', 1)
self.assertTemplateUsedCount(response, u'inforequests/detail/branch/sub.html', 4)
self.assertTemplateUsedCount(response, u'inforequests/detail/action/main.html', 12)

def test_inforequest_with_multiple_branches_related_models_are_prefetched_before_render(self):
inforequest, _, _ = self._create_inforequest_scenario(
Expand All @@ -102,8 +111,8 @@ def test_inforequest_with_multiple_branches_related_models_are_prefetched_before
[u'confirmation', (u'advancement', [u'refusal'])]),
)
self._login_user()
with self.assertQueriesDuringRender([]):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
with self.assertQueriesDuringRender(self.patterns_single_column):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

def test_inforequest_with_undecided_email(self):
inforequest, _, _ = self._create_inforequest_scenario()
Expand All @@ -112,20 +121,12 @@ def test_inforequest_with_undecided_email(self):
self._create_inforequest_email(inforequest=inforequest)

self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

# User can decide
self.assertTemplateUsed(response, u'inforequests/detail_decide.html')
self.assertTemplateUsed(response, u'inforequests/detail_undecided.html')
self.assertTemplateUsed(response, u'inforequests/detail/texts/add_email.html')
# User may not act
self.assertTemplateNotUsed(response, u'inforequests/detail_add_smail.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_new_action.html')

templates = defaultdict(list)
for template in response.templates:
templates[template.name].append(template)

self.assertTemplateUsedCount(response, u'inforequests/detail_email.html', 3)
self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_smail.html')

def test_inforequest_with_undecided_email_related_models_are_prefetched_before_render(self):
inforequest, _, _ = self._create_inforequest_scenario()
Expand All @@ -134,26 +135,24 @@ def test_inforequest_with_undecided_email_related_models_are_prefetched_before_r
self._create_inforequest_email(inforequest=inforequest)

self._login_user()
with self.assertQueriesDuringRender([]):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
with self.assertQueriesDuringRender(self.patterns_single_column):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

def test_inforequest_without_undecided_email(self):
inforequest, _, _ = self._create_inforequest_scenario()
self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

# There is nothing to decide
self.assertTemplateNotUsed(response, u'inforequests/detail_decide.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_undecided.html')
self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_email.html')
# User may act
self.assertTemplateUsed(response, u'inforequests/detail_add_smail.html')
self.assertTemplateUsed(response, u'inforequests/detail_new_action.html')
self.assertTemplateUsed(response, u'inforequests/detail/texts/add_smail.html')

def test_inforequest_without_undecided_email_related_models_are_prefetched_before_render(self):
inforequest, _, _ = self._create_inforequest_scenario()
self._login_user()
with self.assertQueriesDuringRender([]):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
with self.assertQueriesDuringRender(self.patterns_single_column):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

def test_closed_inforequest_with_undecided_email(self):
inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True))
Expand All @@ -162,24 +161,20 @@ def test_closed_inforequest_with_undecided_email(self):
self._create_inforequest_email(inforequest=inforequest)

self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

# Inforequest is closed, user may do nothing
self.assertTemplateNotUsed(response, u'inforequests/detail_decide.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_undecided.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_add_smail.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_new_action.html')
self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_email.html')
self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_smail.html')

def test_closed_inforequest_without_undecided_email(self):
inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True))
self._login_user()
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))

# Inforequest is closed, user may do nothing
self.assertTemplateNotUsed(response, u'inforequests/detail_decide.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_undecided.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_add_smail.html')
self.assertTemplateNotUsed(response, u'inforequests/detail_new_action.html')
self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_email.html')
self.assertTemplateNotUsed(response, u'inforequests/detail/texts/add_smail.html')

def test_closed_inforequest_related_models_are_prefetched_before_render(self):
inforequest, _, _ = self._create_inforequest_scenario(dict(closed=True))
Expand All @@ -188,5 +183,5 @@ def test_closed_inforequest_related_models_are_prefetched_before_render(self):
self._create_inforequest_email(inforequest=inforequest)

self._login_user()
with self.assertQueriesDuringRender([]):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.pk,)))
with self.assertQueriesDuringRender(self.patterns_single_column):
response = self.client.get(reverse(u'inforequests:detail', args=(inforequest.slug, inforequest.pk,)))
14 changes: 1 addition & 13 deletions chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,6 @@ def pre_mock_render(request, template, context):
list(context[u'unsuccessful_inforequests'])

self._login_user(self.user1)
patterns_single_column = [
u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s LIMIT 21',
squeeze(u"""
SELECT COUNT\(\*\) 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 LIMIT 21',
]
# TODO: Optimize N+1 queries in #419
patterns_pending_inforequests = [
squeeze(u"""
Expand Down Expand Up @@ -145,7 +133,7 @@ def pre_mock_render(request, template, context):
"""),
] * 3
patterns = list(
patterns_single_column +
self.patterns_single_column +
patterns_pending_inforequests +
patterns_successful_inforequests +
patterns_unsuccessful_inforequests
Expand Down
16 changes: 16 additions & 0 deletions chcemvediet/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from poleno.attachments.models import Attachment
from poleno.mail.models import Message, Recipient
from poleno.utils.date import local_today, utc_now
from poleno.utils.misc import squeeze

from ..apps.geounits.models import Region, District, Municipality, Neighbourhood
from ..apps.inforequests.models import Branch, Inforequest, InforequestEmail, InforequestDraft, Action
Expand Down Expand Up @@ -48,6 +49,21 @@ def run_tests(self, *args, **kwargs):

class ChcemvedietTestCaseMixin(TestCase):

def __init__(self, methodName):
super(ChcemvedietTestCaseMixin, self).__init__(methodName)
self.patterns_single_column = [
u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s LIMIT 21',
squeeze(u"""
SELECT COUNT\(\*\) 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 LIMIT 21',
]

def _pre_setup(self):
super(ChcemvedietTestCaseMixin, self)._pre_setup()
self.counter = itertools.count()
Expand Down

0 comments on commit 63976ae

Please sign in to comment.