From 8b666443667f92e014d70a4247c7885375bba610 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 30 Nov 2016 13:20:53 +0100 Subject: Simple treatment form. Treatment listing. (refs #3365) --- ishtar_common/tests.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'ishtar_common/tests.py') diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 0c4bbda08..dbe3df4a5 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -75,6 +75,32 @@ 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 WizardTest(object): url_name = None wizard_name = '' @@ -95,7 +121,9 @@ class WizardTest(object): def test_wizard(self): 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: @@ -138,6 +166,7 @@ class WizardTest(object): '/{}/{}'.format(self.url_name, next_form)) else: response = self.client.post(url, data, follow=True) + test_form_data.tests(self, response) self.post_wizard() -- cgit v1.2.3 From 8b2ce8ee16a8ce40c09c5e5cb7bd9e8cd48a3368 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 20 Dec 2016 13:50:37 +0100 Subject: Fix find creation (refs #3399) - improve wizard test management --- archaeological_finds/tests.py | 53 ++++++++++++++++++++++++++++++++++++++ archaeological_finds/views.py | 8 ++++-- archaeological_finds/wizards.py | 5 ++-- archaeological_operations/tests.py | 6 ++--- archaeological_warehouse/models.py | 4 +-- ishtar_common/tests.py | 27 +++++++++++-------- 6 files changed, 83 insertions(+), 20 deletions(-) (limited to 'ishtar_common/tests.py') diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index b5096ae01..4153a79ad 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -17,6 +17,8 @@ # See the file COPYING for details. +import datetime + from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.test import TestCase @@ -24,6 +26,7 @@ from ishtar_common.models import ImporterType, IshtarUser, ImporterColumn,\ FormaterType, ImportTarget from ishtar_common.models import Person +from archaeological_context_records.models import ContextRecord from archaeological_finds import models, views from archaeological_warehouse.models import Warehouse, WarehouseType @@ -80,6 +83,56 @@ class FindInit(ContextRecordInit): self.base_find = [] +class AFindWizardCreationTest(WizardTest, FindInit, TestCase): + # TODO: first to be run because of strange init things... + fixtures = [settings.ROOT_PATH + + '../fixtures/initial_data.json', + settings.ROOT_PATH + + '../ishtar_common/fixtures/initial_data.json', + settings.ROOT_PATH + + '../archaeological_files/fixtures/initial_data.json', + settings.ROOT_PATH + + '../archaeological_operations/fixtures/initial_data-fr.json', + settings.ROOT_PATH + + '../archaeological_finds/fixtures/initial_data-fr.json', + settings.ROOT_PATH + + '../archaeological_warehouse/fixtures/initial_data-fr.json', + ] + url_name = 'find_creation' + wizard_name = 'find_wizard' + steps = views.find_creation_steps + form_datas = [ + FormData( + 'Find creation', + form_datas={ + 'selecrecord-find_creation': {'pk': 1}, + 'find-find_creation': { + 'label': 'hop', + 'checked': 'NC', + 'check_date': '2016-01-01' + }, + 'dating-find_creation': [] + }, + ) + ] + + def pre_wizard(self): + q = ContextRecord.objects.filter(pk=1) + if not q.count(): + cr = self.create_context_record()[0] + cr.pk = 1 + cr.save() + self.find_number = models.Find.objects.count() + self.basefind_number = models.BaseFind.objects.count() + super(AFindWizardCreationTest, self).pre_wizard() + + def post_wizard(self): + self.assertEqual(models.BaseFind.objects.count(), + self.basefind_number + 1) + self.assertEqual(models.Find.objects.count(), + self.find_number + 1) + + class ATreatmentWizardCreationTest(WizardTest, FindInit, TestCase): # TODO: first to be run because of strange init things... fixtures = [settings.ROOT_PATH + diff --git a/archaeological_finds/views.py b/archaeological_finds/views.py index 52144cadd..ef8dfb16b 100644 --- a/archaeological_finds/views.py +++ b/archaeological_finds/views.py @@ -112,11 +112,15 @@ revert_find = revert_item(models.Find) show_findbasket = show_item(models.FindBasket, 'findbasket') -find_creation_wizard = FindWizard.as_view([ +find_creation_steps = [ ('selecrecord-find_creation', RecordFormSelectionTable), ('find-find_creation', FindForm), ('dating-find_creation', DatingFormSet), - ('final-find_creation', FinalForm)], + ('final-find_creation', FinalForm) +] + +find_creation_wizard = FindWizard.as_view( + find_creation_steps, label=_(u"New find"), url_name='find_creation',) diff --git a/archaeological_finds/wizards.py b/archaeological_finds/wizards.py index c9d492329..d7e3dbac0 100644 --- a/archaeological_finds/wizards.py +++ b/archaeological_finds/wizards.py @@ -25,6 +25,7 @@ from ishtar_common.wizards import Wizard, DeletionWizard, SourceWizard from archaeological_operations.wizards import OperationAdministrativeActWizard from archaeological_operations.models import AdministrativeAct +from archaeological_context_records.models import ContextRecord import models @@ -40,7 +41,7 @@ class FindWizard(Wizard): main_form_key = 'selecrecord-' + self.url_name try: idx = int(self.session_get_value(main_form_key, 'pk')) - current_cr = models.ContextRecord.objects.get(pk=idx) + current_cr = ContextRecord.objects.get(pk=idx) return current_cr except(TypeError, ValueError, ObjectDoesNotExist): pass @@ -66,7 +67,7 @@ class FindWizard(Wizard): def get_extra_model(self, dct, form_list): dct = super(FindWizard, self).get_extra_model(dct, form_list) dct['order'] = 1 - if 'pk' in dct and type(dct['pk']) == models.ContextRecord: + if 'pk' in dct and type(dct['pk']) == ContextRecord: dct['base_finds__context_record'] = dct.pop('pk') return dct diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 80569fb1b..93c4fbe92 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -673,9 +673,9 @@ class OperationWizardCreationTest(WizardTest, OperationInitTest, TestCase): FormData( "Create a preventive diag", form_datas={ - 'general-operation_creation': { - 'operation_type': 1, # preventive diag - 'year': 2016} + 'general-operation_creation': { + 'operation_type': 1, # preventive diag + 'year': 2016} }, ignored=('filechoice-operation_creation', 'preventive-operation_creation', diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 0872df220..0eb19814d 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -149,8 +149,8 @@ class Container(LightHistorizedItem): def precise_location(self): location = unicode(self.location) locas = [ - u"{} {}".format(loca.division.division, loca.reference) - for loca in ContainerLocalisation.objects.filter( + u"{} {}".format(loca.division.division, loca.reference) + for loca in ContainerLocalisation.objects.filter( container=self) ] return location + u" - " + u", ".join(locas) diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index dbe3df4a5..095956090 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -118,6 +118,18 @@ class WizardTest(object): def post_wizard(self): pass + 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): url = reverse(self.url_name) self.pre_wizard() @@ -144,28 +156,21 @@ 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() -- cgit v1.2.3 From 61c3267725423ca44d75e5e3c70cd89f3d384ce5 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 21 Dec 2016 12:18:04 +0100 Subject: Disable some test for spatialite (temp fix) --- archaeological_operations/tests.py | 5 +++++ ishtar_common/tests.py | 5 +++++ 2 files changed, 10 insertions(+) (limited to 'ishtar_common/tests.py') diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 2aa64ed15..fa5d8f32b 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -711,6 +711,11 @@ class OperationWizardDeleteTest(OperationWizardCreationTest): ) ] + def pass_test(self): + if settings.USE_SPATIALITE_FOR_TESTS: + # SPATIALITE mess with views so no test for now... + return True + def pre_wizard(self): self.ope = self.get_default_operation(force=True) self.form_datas[0].form_datas['selec-operation_deletion']['pk'] = \ diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 095956090..a00f8a174 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -118,6 +118,9 @@ 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) @@ -131,6 +134,8 @@ class WizardTest(object): u" ".join(errors), current_step)) def test_wizard(self): + if self.pass_test(): + return url = reverse(self.url_name) self.pre_wizard() for test_form_data in self.form_datas: -- cgit v1.2.3 From 1aa58f90a149d7de7813dcf45fe8c1fb04a63143 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 9 Jan 2017 13:30:23 +0100 Subject: tests: force managing of unmanage tables --- example_project/settings.py | 2 ++ ishtar_common/tests.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'ishtar_common/tests.py') diff --git a/example_project/settings.py b/example_project/settings.py index a191b83b3..437c75673 100644 --- a/example_project/settings.py +++ b/example_project/settings.py @@ -229,6 +229,8 @@ OP_PREFIX = 'OP' PRE_APPS = [] EXTRA_APPS = [] +TEST_RUNNER = 'ishtar_common.tests.ManagedModelTestRunner' + try: from local_settings import * except ImportError, e: diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index a00f8a174..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 @@ -101,6 +102,29 @@ class WizardTestFormData(object): 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 = '' -- cgit v1.2.3