diff options
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r-- | ishtar_common/tests.py | 66 |
1 files changed, 46 insertions, 20 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 42bb1860e..a9e92e1f2 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -30,8 +30,9 @@ from django.core.files.base import File as DjangoFile from django.core.files.uploadedfile import SimpleUploadedFile from django.core.management import call_command from django.core.urlresolvers import reverse +from django.db import connection, transaction from django.template.defaultfilters import slugify -from django.test import TestCase +from django.test import TestCase as BaseTestCase from django.test.client import Client from django.test.simple import DjangoTestSuiteRunner @@ -39,6 +40,9 @@ from ishtar_common import models from ishtar_common import forms_common from ishtar_common.utils import post_save_point +from archaeological_context_records.models import CRBulkView +from archaeological_finds.models import BFBulkView, FBulkView, FirstBaseFindView + """ from django.conf import settings import tempfile, datetime @@ -82,6 +86,17 @@ def create_user(): return username, password, user +class TestCase(BaseTestCase): + def _pre_setup(self): + super(TestCase, self)._pre_setup() + if settings.USE_SPATIALITE_FOR_TESTS: + return + c = connection.cursor() + for view in [CRBulkView, FirstBaseFindView, BFBulkView, FBulkView]: + c.execute(view.CREATE_SQL) + transaction.commit_unless_managed() + + class CommandsTestCase(TestCase): def test_clean_ishtar(self): """ @@ -102,20 +117,30 @@ class WizardTestFormData(object): """ Test set to simulate wizard steps """ - def __init__(self, name, form_datas, ignored=[], extra_tests=[]): + def __init__(self, name, form_datas, ignored=[], pre_tests=[], + 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 pre_tests: list of function to be executed before the wizard :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.pre_tests = pre_tests self.extra_tests = extra_tests + def inits(self, test_object): + """ + Initialisations before the wizard. + """ + for pre in self.pre_tests: + pre(test_object) + def tests(self, test_object, final_step_response): """ Specific tests for theses datas. Raise Exception if not OK. @@ -185,6 +210,7 @@ class WizardTest(object): url = reverse(self.url_name) self.pre_wizard() for test_form_data in self.form_datas: + test_form_data.inits(self) form_data = test_form_data.form_datas ignored = test_form_data.ignored for idx, step in enumerate(self.steps): @@ -207,27 +233,27 @@ class WizardTest(object): for k in d: data['{}-{}'.format(current_step, k)] = d[k] - next_idx, next_form = idx + 1, None - while len(self.steps) > next_idx: - if self.steps[idx + 1][0] not in ignored: - next_form = self.steps[idx + 1][0] - break - 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)) - self.check_response(response, current_step) + next_form_is_checked = len(self.steps) > idx + 1 and \ + self.steps[idx + 1][0] not in ignored + try: + response = self.client.post(url, data, + follow=not next_form_is_checked) + 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)) + self.check_response(response, current_step) + if next_form_is_checked: + next_form = self.steps[idx + 1][0] self.assertRedirects( response, '/{}/{}'.format(self.url_name, next_form)) - else: - response = self.client.post(url, data, follow=True) - self.check_response(response, current_step) + if idx == len(self.steps) - 1: + # last form + self.assertRedirects( + response, + '/{}/done'.format(self.url_name)) test_form_data.tests(self, response) self.post_wizard() |