Skip to content

Commit

Permalink
#280 Add review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Oct 30, 2021
1 parent fa3606f commit cf88a97
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
7 changes: 0 additions & 7 deletions chcemvediet/apps/inforequests/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,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
73 changes: 45 additions & 28 deletions chcemvediet/apps/inforequests/tests/test_views/test_mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ class MineViewTest(InforequestsTestCaseMixin, ViewTestCaseMixin, TestCase):
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):
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']
Expand All @@ -37,12 +42,12 @@ def test_authenticated_user_gets_inforequest_mine(self):
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_inforequest(applicant=self.user1) for i in range(4)]
pending_inforequests2 = [self._create_inforequest(applicant=self.user2) for i in range(5)]
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_inforequest(applicant=self.user1, closed=True) for i in range(3)]
unsuccessful_inforequests2 = [self._create_inforequest(applicant=self.user2, closed=True) 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'))
Expand All @@ -54,9 +59,9 @@ def test_user_gets_only_his_inforequests_and_drafts(self):

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_inforequest(applicant=self.user2) for i in range(5)]
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_inforequest(applicant=self.user2, closed=True) 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'))
Expand All @@ -68,9 +73,9 @@ def test_with_user_with_no_his_inforequests_nor_drafts(self):

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_inforequest(applicant=self.user1) for i in range(4)]
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_inforequest(applicant=self.user1, closed=True) 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):
Expand All @@ -80,59 +85,71 @@ def pre_mock_render(request, template, context):
list(context[u'unsuccessful_inforequests'])

self._login_user(self.user1)

# main/base/single_column.html
patterns = [
u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s',
patterns_single_column = [
u'FROM "accounts_profile" WHERE "accounts_profile"."user_id" = %s LIMIT 21',
squeeze(u"""
FROM "mail_message"
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',
u'FROM "invitations_invitationsupply" WHERE "invitations_invitationsupply"."user_id" = %s LIMIT 21',
]

# pending_inforequests
patterns.extend([
# 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
""") 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',
]
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)

# unsuccessful_inforequests
patterns.extend([
] * 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)
] * 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)
11 changes: 11 additions & 0 deletions chcemvediet/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.template import Context, Template
from django.test import TestCase
from django.test.runner import DiscoverRunner
from django.test.utils import override_settings

from poleno.attachments.models import Attachment
from poleno.mail.models import Message, Recipient
Expand All @@ -35,9 +36,14 @@ def setup_test_environment(self, **kwargs):
super(CustomTestRunner, self).setup_test_environment(**kwargs)
settings.LANGUAGE_CODE = u'en'
os.environ[u'RECAPTCHA_TESTING'] = u'True'
self.settings_override = override_settings(
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()

def teardown_test_environment(self, **kwargs):
del os.environ[u'RECAPTCHA_TESTING']
self.settings_override.disable()
super(CustomTestRunner, self).teardown_test_environment(**kwargs)

def run_tests(self, *args, **kwargs):
Expand All @@ -62,6 +68,11 @@ def _pre_setup(self):
self.inforequest = self._create_inforequest()
self.branch = self._create_branch()
self.action = self._create_action()
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 _call_with_defaults(self, func, kwargs, defaults):
Expand Down

0 comments on commit cf88a97

Please sign in to comment.