summaryrefslogtreecommitdiff
path: root/ishtar_common/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r--ishtar_common/tests.py87
1 files changed, 75 insertions, 12 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 0c4bbda08..6db425cab 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -29,6 +29,7 @@ from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify
from django.test import TestCase
from django.test.client import Client
+from django.test.simple import DjangoTestSuiteRunner
from ishtar_common import models
@@ -75,6 +76,55 @@ def create_user():
return username, password, user
+class WizardTestFormData(object):
+ """
+ Test set to simulate wizard steps
+ """
+ def __init__(self, name, form_datas, ignored=[], extra_tests=[]):
+ """
+ :param name: explicit name of the test
+ :param form_datas: dict with data for each step - dict key are wizard
+ step name
+ :param ignored: steps to be ignored in wizard processing
+ :param extra_tests: list of extra tests. Theses tests must be functions
+ accepting two parameters: the current test object and the final step
+ response
+ """
+ self.form_datas = form_datas
+ self.ignored = ignored[:]
+ self.extra_tests = extra_tests
+
+ def tests(self, test_object, final_step_response):
+ """
+ Specific tests for theses datas. Raise Exception if not OK.
+ """
+ for test in self.extra_tests:
+ test(test_object, final_step_response)
+
+
+class ManagedModelTestRunner(DjangoTestSuiteRunner):
+ """
+ Test runner that automatically makes all unmanaged models in your Django
+ project managed for the duration of the test run, so that one doesn't need
+ to execute the SQL manually to create them.
+ """
+ def setup_test_environment(self, *args, **kwargs):
+ from django.db.models.loading import get_models
+ self.unmanaged_models = [m for m in get_models()
+ if not m._meta.managed]
+ for m in self.unmanaged_models:
+ m._meta.managed = True
+ super(ManagedModelTestRunner, self).setup_test_environment(*args,
+ **kwargs)
+
+ def teardown_test_environment(self, *args, **kwargs):
+ super(ManagedModelTestRunner, self).teardown_test_environment(*args,
+ **kwargs)
+ # reset unmanaged models
+ for m in self.unmanaged_models:
+ m._meta.managed = False
+
+
class WizardTest(object):
url_name = None
wizard_name = ''
@@ -92,10 +142,29 @@ class WizardTest(object):
def post_wizard(self):
pass
+ def pass_test(self):
+ return False
+
+ def check_response(self, response, current_step):
+ if "errorlist" in response.content:
+ soup = Soup(response.content)
+ errorlist = soup.findAll(
+ "ul", {"class": "errorlist"})
+ errors = []
+ for li in errorlist:
+ lbl = li.findParent().findParent().findChild().text
+ errors.append(u"{} - {}".format(lbl, li.text))
+ raise ValidationError(u"Errors: {} on {}.".format(
+ u" ".join(errors), current_step))
+
def test_wizard(self):
+ if self.pass_test():
+ return
url = reverse(self.url_name)
self.pre_wizard()
- for form_data, ignored in self.form_datas:
+ for test_form_data in self.form_datas:
+ form_data = test_form_data.form_datas
+ ignored = test_form_data.ignored
for idx, step in enumerate(self.steps):
current_step, current_form = step
if current_step in ignored:
@@ -116,28 +185,22 @@ class WizardTest(object):
next_form = self.steps[idx + 1][0]
break
next_idx = next_idx + 1
-
if next_form:
try:
response = self.client.post(url, data)
except ValidationError as e:
+ # on ManagementForm data is missing or has been tampered
+ # error verify the wizard_name or step name
raise ValidationError(u"Errors: {} on {}.".format(
u" - ".join(e.messages), current_step))
- if "errorlist" in response.content:
- soup = Soup(response.content)
- errorlist = soup.findAll(
- "ul", {"class": "errorlist"})
- errors = []
- for li in errorlist:
- lbl = li.findParent().findParent().findChild().text
- errors.append(u"{} - {}".format(lbl, li.text))
- raise ValidationError(u"Errors: {} on {}.".format(
- u" ".join(errors), current_step))
+ self.check_response(response, current_step)
self.assertRedirects(
response,
'/{}/{}'.format(self.url_name, next_form))
else:
response = self.client.post(url, data, follow=True)
+ self.check_response(response, current_step)
+ test_form_data.tests(self, response)
self.post_wizard()