Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#280 Fix inforequests view mine #416

Merged
merged 5 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions chcemvediet/apps/accounts/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
# vim: expandtab
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.test.utils import override_settings


class AccountsTestCaseMixin(TestCase):

def _pre_setup(self):
super(AccountsTestCaseMixin, self)._pre_setup()
self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

def _post_teardown(self):
self.settings_override.disable()
super(AccountsTestCaseMixin, self)._post_teardown()
10 changes: 3 additions & 7 deletions chcemvediet/apps/accounts/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# vim: expandtab
# -*- coding: utf-8 -*-
import os

import lxml.html
from django.contrib.auth.models import User
from django.test import TestCase

from poleno.utils.urls import reverse

from . import AccountsTestCaseMixin


class LoginFormTest(AccountsTestCaseMixin, TestCase):
class LoginFormTest(TestCase):
u"""
Tests ``allauth`` ``account_login`` view using ``LoginForm`` form registered as
"account_login". Does not check ``account_login`` functionality, only checks functionality
Expand Down Expand Up @@ -51,7 +47,7 @@ def test_recaptcha_field_is_required(self):
response = self.client.post(reverse(u'account_login'), data, follow=True)
self.assertFormError(response, u'form', u'recaptcha', u'This field is required.')

class SignupFormTest(AccountsTestCaseMixin, TestCase):
class SignupFormTest(TestCase):
u"""
Tests ``allauth`` ``account_signup`` view using ``SignupForm`` form registered as
"account_signup". Does not check ``account_signup`` functionality, only checks functionality
Expand Down Expand Up @@ -207,7 +203,7 @@ def test_recaptcha_field_is_required(self):
response = self.client.post(reverse(u'account_signup'), data, follow=True)
self.assertFormError(response, u'form', u'recaptcha', u'This field is required.')

class ResetPasswordFormTest(AccountsTestCaseMixin, TestCase):
class ResetPasswordFormTest(TestCase):
u"""
Tests ``allauth`` ``password_reset`` view using ``ResetPasswordForm`` form registered as
"password_reset". Does not check ``password_reset`` functionality, only checks functionality
Expand Down
3 changes: 1 addition & 2 deletions chcemvediet/apps/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
from django.contrib.auth.models import User
from django.test import TestCase

from . import AccountsTestCaseMixin
from ..models import Profile
from ..signals import create_profile_on_user_post_save


class ProfileModelTest(AccountsTestCaseMixin, TestCase):
class ProfileModelTest(TestCase):
u"""
Tests ``Profile`` model.
"""
Expand Down
7 changes: 1 addition & 6 deletions chcemvediet/apps/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# vim: expandtab
# -*- coding: utf-8 -*-
import functools
import mock

from django.contrib.auth.models import User
from django.test import TestCase

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

from . import AccountsTestCaseMixin


class ProfileViewTest(AccountsTestCaseMixin, ViewTestCaseMixin, TestCase):
class ProfileViewTest(ViewTestCaseMixin, TestCase):
u"""
Tests ``profile()`` view registered as "accounts:profile".
"""
Expand Down
16 changes: 5 additions & 11 deletions chcemvediet/apps/inforequests/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
viliambalaz marked this conversation as resolved.
Show resolved Hide resolved
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):
martinmacko47 marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -70,16 +71,9 @@ def _pre_setup(self):
self.settings_override = override_settings(
MEDIA_ROOT=self.tempdir.path,
EMAIL_BACKEND=u'poleno.mail.backend.EmailBackend',
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

self.user1 = self._create_user()
self.user2 = self._create_user()
self.obligee1 = self._create_obligee()
self.obligee2 = self._create_obligee()
self.obligee3 = self._create_obligee()

def _post_teardown(self):
self.settings_override.disable()
self.tempdir.cleanup()
Expand Down
70 changes: 0 additions & 70 deletions chcemvediet/apps/inforequests/tests/test_views/skip_test_mine.py

This file was deleted.

155 changes: 155 additions & 0 deletions chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# vim: expandtab
# -*- 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

from .. import InforequestsTestCaseMixin
from ...models import Action


class MineViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase):
u"""
Tests ``mine()`` view registered as "inforequests:mine".
"""

def _create_pending_inforequest(self, applicant):
return self._create_inforequest(applicant=applicant)

def _create_successful_inforequest(self, applicant):
martinmacko47 marked this conversation as resolved.
Show resolved Hide resolved
inforequest, _, _ = self._create_inforequest_scenario(applicant, {u'closed': True},
(u'disclosure', dict(disclosure_level=Action.DISCLOSURE_LEVELS.PARTIAL)))
return inforequest

def _create_unsuccessful_inforequest(self, applicant):
return self._create_inforequest(applicant=applicant, closed=True)

def test_allowed_http_methods(self):
allowed = [u'HEAD', u'GET']
self.assert_allowed_http_methods(allowed, reverse(u'inforequests:mine'))

def test_anonymous_user_is_redirected(self):
self.assert_anonymous_user_is_redirected(reverse(u'inforequests:mine'))

def test_authenticated_user_gets_inforequest_mine(self):
self._login_user(self.user1)
martinmacko47 marked this conversation as resolved.
Show resolved Hide resolved
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, u'inforequests/mine/mine.html')

def test_user_gets_only_his_inforequests_and_drafts(self):
drafts1 = [self._create_inforequest_draft(applicant=self.user1) for i in range(5)]
drafts2 = [self._create_inforequest_draft(applicant=self.user2) for i in range(3)]
pending_inforequests1 = [self._create_pending_inforequest(applicant=self.user1) for i in range(4)]
pending_inforequests2 = [self._create_pending_inforequest(applicant=self.user2) for i in range(5)]
successful_inforequests1 = [self._create_successful_inforequest(applicant=self.user1) for i in range(3)]
successful_inforequests2 = [self._create_successful_inforequest(applicant=self.user2) for i in range(3)]
unsuccessful_inforequests1 = [self._create_unsuccessful_inforequest(applicant=self.user1) for i in range(3)]
unsuccessful_inforequests2 = [self._create_unsuccessful_inforequest(applicant=self.user2) for i in range(3)]

self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
self.assertItemsEqual(response.context[u'pending_inforequests'], pending_inforequests1)
self.assertItemsEqual(response.context[u'drafts'], drafts1)
self.assertItemsEqual(response.context[u'successful_inforequests'], successful_inforequests1)
self.assertItemsEqual(response.context[u'unsuccessful_inforequests'], unsuccessful_inforequests1)

def test_with_user_with_no_his_inforequests_nor_drafts(self):
drafts2 = [self._create_inforequest_draft(applicant=self.user2) for i in range(3)]
pending_inforequests2 = [self._create_pending_inforequest(applicant=self.user2) for i in range(5)]
successful_inforequests2 = [self._create_successful_inforequest(applicant=self.user2) for i in range(3)]
unsuccessful_inforequests2 = [self._create_unsuccessful_inforequest(applicant=self.user2) for i in range(3)]

self._login_user(self.user1)
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
self.assertItemsEqual(response.context[u'pending_inforequests'], [])
self.assertItemsEqual(response.context[u'drafts'], [])
self.assertItemsEqual(response.context[u'successful_inforequests'], [])
self.assertItemsEqual(response.context[u'unsuccessful_inforequests'], [])

def test_related_models_are_prefetched_before_render(self):
drafts1 = [self._create_inforequest_draft(applicant=self.user1) for i in range(5)]
pending_inforequests1 = [self._create_pending_inforequest(applicant=self.user1) for i in range(4)]
successful_inforequests1 = [self._create_successful_inforequest(applicant=self.user1) for i in range(3)]
unsuccessful_inforequests1 = [self._create_unsuccessful_inforequest(applicant=self.user1) for i in range(3)]

# Force view querysets to evaluate before calling render
def pre_mock_render(request, template, context):
list(context[u'pending_inforequests'])
viliambalaz marked this conversation as resolved.
Show resolved Hide resolved
list(context[u'drafts'])
list(context[u'successful_inforequests'])
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"""
FROM "inforequests_action"
INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \)
WHERE "inforequests_branch"."inforequest_id" = %s
ORDER BY "inforequests_action"."created" DESC, "inforequests_action"."id" DESC
LIMIT 1
""")
] * 4
patterns_successful_inforequests = [
squeeze(u"""
FROM "inforequests_action" WHERE "inforequests_action"."branch_id" = %s
ORDER BY "inforequests_action"."created" ASC, "inforequests_action"."id" ASC
"""),
squeeze(u"""
FROM "inforequests_branch" WHERE "inforequests_branch"."inforequest_id" = %s
ORDER BY "inforequests_branch"."id" ASC"""),
squeeze(u"""
FROM "inforequests_action"
INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \)
WHERE "inforequests_branch"."inforequest_id" = %s
ORDER BY "inforequests_action"."created" DESC, "inforequests_action"."id" DESC
LIMIT 1
"""),
squeeze(u"""
SELECT MAX\("inforequests_action"."disclosure_level"\) AS "disclosure_level__max"
FROM "inforequests_action"
INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \)
WHERE "inforequests_branch"."inforequest_id" = %s
"""),
] * 3
patterns_unsuccessful_inforequests = [
squeeze(u"""
FROM "inforequests_action"
INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \)
WHERE "inforequests_branch"."inforequest_id" = %s
ORDER BY "inforequests_action"."created" DESC, "inforequests_action"."id" DESC
LIMIT 1
"""),
squeeze(u"""
SELECT MAX\("inforequests_action"."disclosure_level"\) AS "disclosure_level__max"
FROM "inforequests_action"
INNER JOIN "inforequests_branch" ON \( "inforequests_action"."branch_id" = "inforequests_branch"."id" \)
WHERE "inforequests_branch"."inforequest_id" = %s
"""),
] * 3
patterns = list(
patterns_single_column +
patterns_pending_inforequests +
patterns_successful_inforequests +
patterns_unsuccessful_inforequests
)
with self.assertQueriesDuringRender(patterns, pre_mock_render=pre_mock_render):
response = self.client.get(reverse(u'inforequests:mine'))
self.assertEqual(response.status_code, 200)
Loading