From 63976aea46ed719e78c6b85f7b1f00cb3db0a02d Mon Sep 17 00:00:00 2001 From: viliambalaz Date: Wed, 15 Dec 2021 14:51:40 +0100 Subject: [PATCH] #280 Fix test_detail view --- .../apps/inforequests/tests/__init__.py | 4 +- .../tests/test_models/test_inforequest.py | 8 +- .../{skip_test_detail.py => test_detail.py} | 113 +++++++++--------- .../tests/test_views/test_mine.py | 14 +-- chcemvediet/tests/__init__.py | 16 +++ 5 files changed, 77 insertions(+), 78 deletions(-) rename chcemvediet/apps/inforequests/tests/test_views/{skip_test_detail.py => test_detail.py} (69%) diff --git a/chcemvediet/apps/inforequests/tests/__init__.py b/chcemvediet/apps/inforequests/tests/__init__.py index e7cea54f5..f837805a3 100644 --- a/chcemvediet/apps/inforequests/tests/__init__.py +++ b/chcemvediet/apps/inforequests/tests/__init__.py @@ -166,8 +166,8 @@ def _create_inforequest_scenario(self, *args): ``_create_message()`` and 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) diff --git a/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py b/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py index be3c80865..e8c9b4d0f 100644 --- a/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py +++ b/chcemvediet/apps/inforequests/tests/test_models/test_inforequest.py @@ -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 @@ -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): @@ -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) @@ -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) diff --git a/chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py b/chcemvediet/apps/inforequests/tests/test_views/test_detail.py similarity index 69% rename from chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py rename to chcemvediet/apps/inforequests/tests/test_views/test_detail.py index b4934a92a..bf7ccf4fd 100644 --- a/chcemvediet/apps/inforequests/tests/test_views/skip_test_detail.py +++ b/chcemvediet/apps/inforequests/tests/test_views/test_detail.py @@ -1,7 +1,5 @@ # vim: expandtab # -*- coding: utf-8 -*- -from collections import defaultdict - from django.test import TestCase from poleno.utils.test import ViewTestCaseMixin @@ -9,6 +7,7 @@ from .. import InforequestsTestCaseMixin + class DetailViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase): u""" Tests ``detail()`` view registered as "inforequests:detail". @@ -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( @@ -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: @@ -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( @@ -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() @@ -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() @@ -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)) @@ -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)) @@ -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,))) diff --git a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py index 863114f2a..209456a9a 100644 --- a/chcemvediet/apps/inforequests/tests/test_views/test_mine.py +++ b/chcemvediet/apps/inforequests/tests/test_views/test_mine.py @@ -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""" @@ -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 diff --git a/chcemvediet/tests/__init__.py b/chcemvediet/tests/__init__.py index 1c399e149..29bc69f4f 100644 --- a/chcemvediet/tests/__init__.py +++ b/chcemvediet/tests/__init__.py @@ -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 @@ -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()