UPD: more/better tests.

This commit is contained in:
2019-03-25 17:17:56 +01:00
parent 94402fb5d0
commit bbe1ffac08
8 changed files with 481 additions and 144 deletions

View File

@@ -3,6 +3,7 @@ import os
import sys
import urllib
from unittest import skip, SkipTest
from django.apps import apps
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import SimpleTestCase, TestCase, tag
from django.urls import reverse
@@ -46,50 +47,47 @@ class AppsTestCase(SimpleTestCase):
self.assertIsInstance(value, of)
class Url(object):
def __init__(self, location, name=None, func=None, **kwargs):
self.location = location
self.name = name
self.func = func
self.redirect = kwargs.get('redirect', False)
self.status_code = kwargs.get('status_code', 200)
self.follow = kwargs.get('follow', False)
class EmailTestCase(TestCase):
email_sender = 'Automatic Software Test <root@localhost>'
email_base_url = 'http://localhost'
email_subject_prefix = '[Test]'
def assertSender(self, mail):
self.assertEqual(mail.from_email, self.email_sender)
class UrlsTestCase(SimpleTestCase):
urls = ()
def assertRecipients(self, mail, recipients):
self.assertEqual(len(mail.recipients()), len(recipients))
for recipient in recipients:
expected_recipient = u'"%s" <%s>' % (recipient.get_full_name(), recipient.email)
recipients = mail.recipients()
self.assertIn(expected_recipient, recipients)
def test_locations(self):
for url in self.urls:
if url.location:
response = self.client.get(url.location, follow=url.follow)
def assertSubject(self, mail, subject):
expected_subject = u'{} {}'.format(self.email_subject_prefix, subject)
self.assertEqual(mail.subject, expected_subject)
if url.redirect:
self.assertRedirects(response, url.redirect)
else:
self.assertEqual(response.status_code, url.status_code,
'Getting \'{}\' is not OK'.format(url.location))
def assertBody(self, mail, body):
expected_lines = body.splitlines()
lines = mail.body.splitlines()
i = 0
for expected_line in expected_lines:
try:
line = lines[i]
except IndexError:
self.fail('line %d: no such line: %s' % (i, expected_line))
i += 1
try:
self.assertEqual(line, expected_line)
except AssertionError as e:
self.fail('line %d: %s' % (i, e.message))
self.assertEqual(mail.body, body)
if url.func:
self.assertEqual(response.resolver_match.func.__name__,
url.func.__name__,
'Getting \'{}\' resolve to wrong function'.format(url.location))
def test_names(self):
for url in self.urls:
if url.name:
response = self.client.get(reverse(url.name), follow=url.follow)
if url.redirect:
self.assertRedirects(response, url.redirect)
else:
self.assertEqual(response.status_code, url.status_code,
'Getting url named \'{}\' is not OK'.format(url.name))
if url.func:
self.assertEqual(response.resolver_match.func.__name__,
url.func.__name__,
'Getting url named \'{}\' resolve to wrong function'.format(url.name))
def setUp(self):
super(EmailTestCase, self).setUp()
app_config = apps.get_app_config('dav_base')
app_config.settings.email_sender = self.email_sender
app_config.settings.email_base_url = self.email_base_url
app_config.settings.email_subject_prefix = self.email_subject_prefix
class FormDataSet(object):
@@ -156,6 +154,52 @@ class FormsTestCase(TestCase):
self.assertIn(code, error_codes)
class Url(object):
def __init__(self, location, name=None, func=None, **kwargs):
self.location = location
self.name = name
self.func = func
self.redirect = kwargs.get('redirect', False)
self.status_code = kwargs.get('status_code', 200)
self.follow = kwargs.get('follow', False)
class UrlsTestCase(SimpleTestCase):
urls = ()
def test_locations(self):
for url in self.urls:
if url.location:
response = self.client.get(url.location, follow=url.follow)
if url.redirect:
self.assertRedirects(response, url.redirect)
else:
self.assertEqual(response.status_code, url.status_code,
'Getting \'{}\' is not OK'.format(url.location))
if url.func:
self.assertEqual(response.resolver_match.func.__name__,
url.func.__name__,
'Getting \'{}\' resolve to wrong function'.format(url.location))
def test_names(self):
for url in self.urls:
if url.name:
response = self.client.get(reverse(url.name), follow=url.follow)
if url.redirect:
self.assertRedirects(response, url.redirect)
else:
self.assertEqual(response.status_code, url.status_code,
'Getting url named \'{}\' is not OK'.format(url.name))
if url.func:
self.assertEqual(response.resolver_match.func.__name__,
url.func.__name__,
'Getting url named \'{}\' resolve to wrong function'.format(url.name))
class SeleniumTestCase(StaticLiveServerTestCase):
window_width = 1024
window_height = 768