This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import datetime
|
||||
import os
|
||||
from urllib.parse import quote
|
||||
from django.apps import apps
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
||||
@@ -9,11 +10,10 @@ from django.test import SimpleTestCase, TestCase, tag
|
||||
from django.urls import reverse
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
from six.moves.urllib.parse import quote
|
||||
from selenium.webdriver.support import expected_conditions as ExpectedConditions
|
||||
|
||||
|
||||
class AppSetting(object):
|
||||
class AppSetting: # pylint: disable=too-few-public-methods
|
||||
def __init__(self, name, of=None):
|
||||
self.name = name
|
||||
self.of = of
|
||||
@@ -24,7 +24,7 @@ class AppsTestCase(SimpleTestCase):
|
||||
settings = ()
|
||||
|
||||
def setUp(self):
|
||||
super(AppsTestCase, self).setUp()
|
||||
super().setUp()
|
||||
if self.app_config:
|
||||
self.configured_settings = self.app_config.settings
|
||||
else:
|
||||
@@ -41,7 +41,7 @@ class AppsTestCase(SimpleTestCase):
|
||||
self.assertIsInstance(value, of)
|
||||
|
||||
|
||||
class EmailTestMixin(object):
|
||||
class EmailTestMixin:
|
||||
email_sender = 'Automatic Software Test <root@localhost>'
|
||||
email_base_url = 'http://localhost'
|
||||
email_subject_prefix = '[Test]'
|
||||
@@ -55,29 +55,29 @@ class EmailTestMixin(object):
|
||||
|
||||
return mails
|
||||
|
||||
def assertSender(self, mail):
|
||||
def assertSender(self, mail): # pylint: disable=invalid-name
|
||||
self.assertEqual(mail.from_email, self.email_sender)
|
||||
|
||||
def assertReplyTo(self, mail, addresses):
|
||||
def assertReplyTo(self, mail, addresses): # pylint: disable=invalid-name
|
||||
self.assertEqual(len(mail.reply_to), len(addresses))
|
||||
for expected_address in addresses:
|
||||
if isinstance(expected_address, AbstractUser):
|
||||
expected_address = u'"%s" <%s>' % (expected_address.get_full_name(), expected_address.email)
|
||||
expected_address = '"%s" <%s>' % (expected_address.get_full_name(), expected_address.email)
|
||||
self.assertIn(expected_address, mail.reply_to)
|
||||
|
||||
def assertRecipients(self, mail, recipients):
|
||||
def assertRecipients(self, mail, recipients): # pylint: disable=invalid-name
|
||||
self.assertEqual(len(mail.recipients()), len(recipients))
|
||||
for expected_recipient in recipients:
|
||||
if isinstance(expected_recipient, AbstractUser):
|
||||
expected_recipient = u'"%s" <%s>' % (expected_recipient.get_full_name(), expected_recipient.email)
|
||||
expected_recipient = '"%s" <%s>' % (expected_recipient.get_full_name(), expected_recipient.email)
|
||||
recipients = mail.recipients()
|
||||
self.assertIn(expected_recipient, recipients)
|
||||
|
||||
def assertSubject(self, mail, subject):
|
||||
expected_subject = u'{} {}'.format(self.email_subject_prefix, subject)
|
||||
def assertSubject(self, mail, subject): # pylint: disable=invalid-name
|
||||
expected_subject = '{} {}'.format(self.email_subject_prefix, subject)
|
||||
self.assertEqual(mail.subject, expected_subject)
|
||||
|
||||
def assertBody(self, mail, body):
|
||||
def assertBody(self, mail, body): # pylint: disable=invalid-name
|
||||
expected_lines = body.splitlines()
|
||||
lines = mail.body.splitlines()
|
||||
i = 0
|
||||
@@ -93,14 +93,14 @@ class EmailTestMixin(object):
|
||||
self.fail('line %d: %s' % (i, e))
|
||||
self.assertEqual(mail.body, body)
|
||||
|
||||
def setUp(self):
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
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):
|
||||
class FormDataSet: # pylint: disable=too-few-public-methods
|
||||
def __init__(self, data, expected_errors=None, form_kwargs=None):
|
||||
self.data = data
|
||||
self.expected_errors = expected_errors
|
||||
@@ -116,44 +116,46 @@ class FormsTestCase(TestCase):
|
||||
if form_class is None:
|
||||
form_class = self.form_class
|
||||
if form_class is None:
|
||||
return True
|
||||
return
|
||||
|
||||
if data_sets is None:
|
||||
data_sets = self.valid_data_sets
|
||||
|
||||
given_form_kwargs = form_kwargs
|
||||
for data_set in data_sets:
|
||||
fk = {}
|
||||
if form_kwargs is not None:
|
||||
fk.update(form_kwargs)
|
||||
form_kwargs = {}
|
||||
if given_form_kwargs is not None:
|
||||
form_kwargs.update(given_form_kwargs)
|
||||
if data_set.form_kwargs is not None:
|
||||
fk.update(data_set.form_kwargs)
|
||||
fk['data'] = data_set.data
|
||||
form = form_class(**fk)
|
||||
form_kwargs.update(data_set.form_kwargs)
|
||||
form_kwargs['data'] = data_set.data
|
||||
form = form_class(**form_kwargs)
|
||||
if not form.is_valid():
|
||||
errors = []
|
||||
for key in form.errors.as_data():
|
||||
for ve in form.errors[key].as_data():
|
||||
errors.append(u'%s (%s)' % (ve.code, ve.message))
|
||||
self.fail(u'Invalid form data \'%s\': %s' % (data_set.data, errors))
|
||||
for e in form.errors[key].as_data():
|
||||
errors.append('%s (%s)' % (e.code, e.message))
|
||||
self.fail('Invalid form data \'%s\': %s' % (data_set.data, errors))
|
||||
|
||||
def test_invalid_data(self, form_class=None, data_sets=None, form_kwargs=None):
|
||||
if form_class is None:
|
||||
form_class = self.form_class
|
||||
if form_class is None:
|
||||
return True
|
||||
return
|
||||
|
||||
if data_sets is None:
|
||||
data_sets = self.invalid_data_sets
|
||||
|
||||
given_form_kwargs = form_kwargs
|
||||
for data_set in data_sets:
|
||||
fk = {}
|
||||
if form_kwargs is not None:
|
||||
fk.update(form_kwargs)
|
||||
form_kwargs = {}
|
||||
if given_form_kwargs is not None:
|
||||
form_kwargs.update(given_form_kwargs)
|
||||
if data_set.form_kwargs is not None:
|
||||
fk.update(data_set.form_kwargs)
|
||||
fk['data'] = data_set.data
|
||||
form_kwargs.update(data_set.form_kwargs)
|
||||
form_kwargs['data'] = data_set.data
|
||||
|
||||
form = form_class(**fk)
|
||||
form = form_class(**form_kwargs)
|
||||
|
||||
if form.is_valid():
|
||||
self.fail('Valid form data: \'%s\'' % data_set.data)
|
||||
@@ -164,7 +166,7 @@ class FormsTestCase(TestCase):
|
||||
self.assertIn(code, error_codes)
|
||||
|
||||
|
||||
class Url(object):
|
||||
class Url: # pylint: disable=too-few-public-methods
|
||||
def __init__(self, location, name=None, func=None, **kwargs):
|
||||
self.location = location
|
||||
self.name = name
|
||||
@@ -210,15 +212,15 @@ class UrlsTestCase(TestCase):
|
||||
'Getting url named \'{}\' resolve to wrong function'.format(url.name))
|
||||
|
||||
|
||||
class ValidatorTestMixin(object):
|
||||
def assertValid(self, validator, data):
|
||||
class ValidatorTestMixin:
|
||||
def assertValid(self, validator, data): # pylint: disable=invalid-name
|
||||
for val in data:
|
||||
try:
|
||||
validator(val)
|
||||
except ValidationError as e: # pragma: no cover
|
||||
self.fail('%s: %s' % (val, e))
|
||||
|
||||
def assertInvalid(self, validator, data):
|
||||
def assertInvalid(self, validator, data): # pylint: disable=invalid-name
|
||||
for val in data:
|
||||
try:
|
||||
validator(val)
|
||||
@@ -234,7 +236,7 @@ class SeleniumTestCase(StaticLiveServerTestCase):
|
||||
window_height = 768
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SeleniumTestCase, self).__init__(*args, **kwargs)
|
||||
super().__init__(*args, **kwargs)
|
||||
self._driver = None
|
||||
self._driver_options = webdriver.FirefoxOptions()
|
||||
self.quit_selenium = None
|
||||
@@ -254,7 +256,7 @@ class SeleniumTestCase(StaticLiveServerTestCase):
|
||||
def tearDown(self):
|
||||
if self.quit_selenium:
|
||||
self.selenium.quit()
|
||||
super(SeleniumTestCase, self).tearDown()
|
||||
super().tearDown()
|
||||
|
||||
def complete_url(self, location):
|
||||
base_url = self.live_server_url
|
||||
@@ -264,8 +266,8 @@ class SeleniumTestCase(StaticLiveServerTestCase):
|
||||
return self.selenium.get(self.complete_url(location))
|
||||
|
||||
def wait_on(self, driver, ec_name, ec_argument, timeout=30):
|
||||
ec = getattr(EC, ec_name)
|
||||
return WebDriverWait(driver, timeout).until(ec(ec_argument))
|
||||
expected_condition = getattr(ExpectedConditions, ec_name)
|
||||
return WebDriverWait(driver, timeout).until(expected_condition(ec_argument))
|
||||
|
||||
def wait_on_presence(self, driver, locator, timeout=30):
|
||||
ec_name = 'presence_of_element_located'
|
||||
@@ -281,7 +283,7 @@ class ScreenshotTestCase(SeleniumTestCase):
|
||||
locations = ()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ScreenshotTestCase, self).__init__(*args, **kwargs)
|
||||
super().__init__(*args, **kwargs)
|
||||
screenshot_base_dir = os.path.join('tmp', 'test-screenshots')
|
||||
self.screenshot_path = screenshot_base_dir
|
||||
self.screenshot_sequences = {}
|
||||
@@ -298,7 +300,7 @@ class ScreenshotTestCase(SeleniumTestCase):
|
||||
else:
|
||||
self.screenshot_sequences[sequence] = 1
|
||||
n = self.screenshot_sequences[sequence]
|
||||
sequence = u'%s-%04d-' % (sequence, n)
|
||||
sequence = '%s-%04d-' % (sequence, n)
|
||||
if title is None:
|
||||
location = self.selenium.current_url
|
||||
if location.startswith(self.live_server_url):
|
||||
@@ -308,7 +310,7 @@ class ScreenshotTestCase(SeleniumTestCase):
|
||||
location = 'root'
|
||||
title = location
|
||||
|
||||
base_name = u'{timestamp}-{prefix}{sequence}{title}.png'.format(
|
||||
base_name = '{timestamp}-{prefix}{sequence}{title}.png'.format(
|
||||
timestamp=datetime.datetime.now().strftime('%Y%m%d-%H%M%S.%f'),
|
||||
prefix=self.screenshot_prefix,
|
||||
sequence=sequence,
|
||||
|
||||
Reference in New Issue
Block a user