diff options
Diffstat (limited to 'archaeological_operations/tests.py')
-rw-r--r-- | archaeological_operations/tests.py | 272 |
1 files changed, 268 insertions, 4 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 364cc4c8e..23c32434b 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -26,7 +26,6 @@ import datetime from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.core.urlresolvers import reverse -from django.test import TestCase from django.test.client import Client from django.contrib.auth.models import Permission @@ -41,7 +40,7 @@ from archaeological_context_records.models import Unit from ishtar_common import forms_common from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ - create_superuser, create_user + create_superuser, create_user, TestCase class ImportTest(object): @@ -607,6 +606,9 @@ class OperationInitTest(object): def create_parcel(self, data={}): default = {'town': self.get_default_town(), 'section': 'A', 'parcel_number': '1'} + if not hasattr(self, 'operations'): + self.create_operation() + default['operation'] = self.operations[0] default.update(data) if not getattr(self, 'parcels', None): self.parcels = [] @@ -616,7 +618,11 @@ class OperationInitTest(object): def get_default_parcel(self, force=False): if force: return self.create_parcel()[-1] - return self.create_parcel()[0] + parcel = self.create_parcel()[0] + if models.Parcel.objects.filter(pk=parcel.pk).count(): + return parcel + self.parcels.pop(0) + return self.create_operation()[-1] def create_operation(self, user=None, orga=None): if not orga: @@ -631,7 +637,12 @@ class OperationInitTest(object): def get_default_operation(self, force=False): if force: return self.create_operation()[-1] - return self.create_operation()[0] + ope = self.create_operation()[0] + if models.Operation.objects.filter(pk=ope.pk).count(): + return ope + self.operations.pop(0) + return self.create_operation()[-1] + def tearDown(self): # cleanup for further test @@ -741,6 +752,107 @@ class OperationTest(TestCase, OperationInitTest): ope2 = create_operation(self.user, values={'year': 0}) self.assertEqual(ope2.operation_code, 2) + def test_cache_update(self): + self.create_towns() + operation = self.operations[0] + town, ope_id = operation.cached_label.split(' | ') + self.assertIn(town, (u'Intercommunal', u"Multi-town")) + self.assertEqual(ope_id, 'OP2010-1') + operation = models.Operation.objects.get(pk=operation.pk) + operation.year = 2011 + operation.save() + operation.towns.add(self.towns[0]) + operation = models.Operation.objects.get(pk=operation.pk) + town, ope_id = operation.cached_label.split(' | ') + self.assertEqual(ope_id, 'OP2011-1') + self.assertEqual(town, self.towns[0].name) + + def test_cache_bulk_update(self): + if settings.USE_SPATIALITE_FOR_TESTS: + # using views - can only be tested with postgresql + return + + operation = self.operations[0] + init_parcel = self.create_parcel()[0] + operation.parcels.add(init_parcel) + + from archaeological_context_records.models import ContextRecord + cr_data = {'label': "Context record", "operation": operation, + 'parcel': init_parcel, + 'history_modifier': self.get_default_user()} + cr = ContextRecord.objects.create(**cr_data) + + from archaeological_finds.models import BaseFind, Find, MaterialType + bf_data = { + 'label': "Base find", 'history_modifier': self.get_default_user(), + 'context_record': cr + } + base_find = BaseFind.objects.create(**bf_data) + find = Find.objects.create( + history_modifier=self.get_default_user(), + label='Find me' + ) + find.base_finds.add(base_find) + mat = MaterialType.objects.create( + label='Adamentium', txt_idx='admentium', code='ADA') + find.material_types.add(mat) + + class TestObj(object): + def __init__(self): + self.context_record_reached = [] + + def reached(self, sender, **kwargs): + instance = kwargs.get('instance') + if sender == ContextRecord: + self.context_record_reached.append(instance) + + test_obj = TestObj() + operation = models.Operation.objects.get(pk=operation.pk) + operation.test_obj = test_obj + operation.year = 2011 + operation.save() + # bulk update of context records cached label gen don't have to be + # reached + self.assertEqual(len(test_obj.context_record_reached), 0) + + # verify the relevance of the update + cr = ContextRecord.objects.get(pk=cr.pk) + ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') + self.assertEqual(ope_id, '{}2011-1'.format( + settings.ISHTAR_DEF_OPE_PREFIX)) + + base_find = BaseFind.objects.get(pk=base_find.pk) + op_code, idx = base_find.cache_short_id.split(' | ') + self.assertEqual(op_code, 'OP2011-1') + self.assertEqual(idx, '00001') + op_code, mat_code, lbl, idx = base_find.cache_complete_id.split(' | ') + self.assertEqual(op_code, 'OP2011-1') + self.assertEqual(mat_code, 'ADA') + self.assertEqual(lbl, 'Context record') + self.assertEqual(idx, '00001') + + find = Find.objects.get(pk=find.pk) + op_code_idx, lbl = find.cached_label.split(' | ') + self.assertEqual(op_code_idx, 'OP2011-1-00001') + self.assertEqual(lbl, 'Find me') + + operation = models.Operation.objects.get(pk=operation.pk) + operation.code_patriarche = '666' + operation.save() + cr = ContextRecord.objects.get(pk=cr.pk) + ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') + self.assertEqual(ope_id, '{}666'.format(settings.ISHTAR_OPE_PREFIX)) + + base_find = BaseFind.objects.get(pk=base_find.pk) + op_code, idx = base_find.cache_short_id.split(' | ') + self.assertEqual(op_code, 'OA666') + op_code, mat_code, lbl, idx = base_find.cache_complete_id.split(' | ') + self.assertEqual(op_code, 'OA666') + + find = Find.objects.get(pk=find.pk) + op_code_idx, lbl = find.cached_label.split(' | ') + self.assertEqual(op_code_idx, 'OA666-00001') + class OperationSearchTest(TestCase, OperationInitTest): fixtures = [settings.ROOT_PATH + @@ -958,6 +1070,158 @@ class OperationWizardCreationTest(WizardTest, OperationInitTest, TestCase): self.parcel_number + 2) +class OperationWizardModifTest(WizardTest, OperationInitTest, TestCase): + fixtures = OperationWizardCreationTest.fixtures + url_name = 'operation_modification' + wizard_name = url_name + '_wizard' + steps = views.operation_modif_wizard_steps + base_ignored_steps = ( + 'archaeologicalsite-operation_modification', + 'preventive-operation_modification', + 'preventivediag-operation_modification', + 'towns-operation_modification', + 'parcels-operation_modification', + 'remains-operation_modification', + 'periods-operation_modification', + 'relations-operation_modification', + 'abstract-operation_modification',) + form_datas = [ + FormData( + "Update an operation", + form_datas={ + 'selec-operation_modification': {}, + 'general-operation_modification': { + 'operation_type': 2, + 'year': 2017}, + 'townsgeneral-operation_modification': [], + 'parcelsgeneral-operation_modification': [], + }, + ignored=base_ignored_steps + ), + FormData( + "Operation: try to remove a parcel with attached context record", + form_datas={ + 'selec-operation_modification': {}, + 'general-operation_modification': { + 'operation_type': 2, + 'year': 2017}, + 'townsgeneral-operation_modification': [], + 'parcelsgeneral-operation_modification': [], + }, + ignored=base_ignored_steps + ), + FormData( + "Operation: remove a parcel with no attached context record", + form_datas={ + 'selec-operation_modification': {}, + 'general-operation_modification': { + 'operation_type': 2, + 'year': 2017}, + 'townsgeneral-operation_modification': [], + 'parcelsgeneral-operation_modification': [], + }, + ignored=base_ignored_steps + ), + ] + + def pre_wizard(self): + self.create_operation() + operation = self.operations[0] + init_town = self.create_towns()[0] + operation.towns.add(init_town) + init_parcel = self.create_parcel()[0] + operation.parcels.add(init_parcel) + + from archaeological_context_records.models import ContextRecord + cr_data = {'label': "Context record", "operation": operation, + 'parcel': init_parcel, + 'history_modifier': self.get_default_user()} + self.cr = ContextRecord.objects.create(**cr_data) + + data = self.form_datas[0].form_datas + data2 = self.form_datas[1].form_datas + data3 = self.form_datas[2].form_datas + + data['selec-operation_modification']['pk'] = operation.pk + data2['selec-operation_modification']['pk'] = operation.pk + data3['selec-operation_modification']['pk'] = operation.pk + + town = self.create_towns( + datas={'numero_insee': '67890', 'name': 'Twin Peaks'})[-1] + towns = [{'town': town.pk}, {'town': init_town.pk}] + data['townsgeneral-operation_modification'] = towns + data2['townsgeneral-operation_modification'] = towns + data3['townsgeneral-operation_modification'] = towns + + parcel_data = { + 'town': town.pk, 'year': 2017, 'section': 'S', + 'parcel_number': '42'} + data['parcelsgeneral-operation_modification'].append(parcel_data) + data2['parcelsgeneral-operation_modification'].append(parcel_data) + data3['parcelsgeneral-operation_modification'].append(parcel_data) + + parcel_data_2 = { + 'town': init_parcel.town.pk, 'year': init_parcel.year or '', + 'section': init_parcel.section, + 'parcel_number': init_parcel.parcel_number} + data['parcelsgeneral-operation_modification'].append(parcel_data_2) + # no init parcel for data2 and data3 + + self.operation_number = models.Operation.objects.count() + self.parcel_number = models.Parcel.objects.count() + + def post_first_wizard(test_object, final_step_response): + test_object.assertEqual(models.Operation.objects.count(), + test_object.operation_number) + operation = models.Operation.objects.get( + pk=test_object.operations[0].pk) + test_object.assertEqual(operation.operation_type.pk, 2) + test_object.assertEqual(operation.year, 2017) + test_object.assertEqual(models.Parcel.objects.count(), + test_object.parcel_number + 1) + test_object.assertEqual(operation.parcels.count(), + test_object.parcel_number + 1) + + def post_second_wizard(test_object, final_step_response): + test_object.assertEqual(models.Operation.objects.count(), + test_object.operation_number) + operation = models.Operation.objects.get( + pk=test_object.operations[0].pk) + test_object.assertEqual(operation.operation_type.pk, 2) + test_object.assertEqual(operation.year, 2017) + test_object.assertEqual(models.Parcel.objects.count(), + test_object.parcel_number + 1) + # the init parcel is not submited but have a context record + # the init parcel is not detached from the operation + test_object.assertEqual(operation.parcels.count(), + test_object.parcel_number + 1) + + def pre_third_wizard(test_object): + parcel_nb = models.Parcel.objects.count() + test_object.cr.delete() + test_object.assertEqual( + parcel_nb, models.Parcel.objects.count()) + + def post_third_wizard(test_object, final_step_response): + test_object.assertEqual(models.Operation.objects.count(), + test_object.operation_number) + operation = models.Operation.objects.get( + pk=test_object.operations[0].pk) + test_object.assertEqual(operation.operation_type.pk, 2) + test_object.assertEqual(operation.year, 2017) + # with no attach the parcel is deleted + test_object.assertEqual(operation.parcels.count(), + test_object.parcel_number) + test_object.assertEqual(models.Parcel.objects.count(), + test_object.parcel_number) + + self.form_datas[0].extra_tests = [post_first_wizard] + self.form_datas[1].extra_tests = [post_second_wizard] + self.form_datas[2].pre_tests = [pre_third_wizard] + self.form_datas[2].extra_tests = [post_third_wizard] + super(OperationWizardModifTest, self).pre_wizard() + + class OperationWizardDeleteTest(OperationWizardCreationTest): fixtures = OperationWizardCreationTest.fixtures url_name = 'operation_deletion' |