summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2017-02-24 17:15:39 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2017-02-24 17:15:39 +0100
commit930a01ef1ca7061a45486e6fc2373c7f2d10647f (patch)
tree656c573a9fe6bcba550d0d87d1290779b8fb93f8
parentb9ae69e4a58f21baf942ce4e4901d5d973b85bbd (diff)
downloadIshtar-930a01ef1ca7061a45486e6fc2373c7f2d10647f.tar.bz2
Ishtar-930a01ef1ca7061a45486e6fc2373c7f2d10647f.zip
Wizard tests improvements. Test inappropriate parcel deletion.
-rw-r--r--archaeological_context_records/tests.py12
-rw-r--r--archaeological_operations/tests.py165
-rw-r--r--archaeological_operations/views.py16
-rw-r--r--ishtar_common/tests.py49
4 files changed, 213 insertions, 29 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py
index 83e013ee1..0018aa1f3 100644
--- a/archaeological_context_records/tests.py
+++ b/archaeological_context_records/tests.py
@@ -148,14 +148,22 @@ class ContextRecordInit(OperationInitTest):
if not getattr(self, 'context_records', None):
self.context_records = []
default = {'label': "Context record"}
- if force or not data.get('operation'):
+ if force or not data.get('operation') \
+ or not models.Operation.objects.filter(
+ pk=data['operation'].pk).count():
data['operation'] = self.get_default_operation(force=force)
- if not data.get('parcel') or not data['parcel'].pk:
+ if not data.get('parcel') or not data['parcel'].pk \
+ or not models.Parcel.objects.filter(
+ pk=data['parcel'].pk).count():
data['parcel'] = self.get_default_parcel(force=force)
if not data.get('history_modifier'):
data['history_modifier'] = self.get_default_user()
default.update(data)
+ data['operation'] = models.Operation.objects.get(
+ pk=data['operation'].pk)
+ data['parcel'] = models.Parcel.objects.get(
+ pk=data['parcel'].pk)
self.context_records.append(models.ContextRecord.objects.create(
**default))
return self.context_records
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 9cdeb67e6..a9fdf38cc 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -619,7 +619,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:
@@ -634,7 +638,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
@@ -976,6 +985,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'
diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py
index 005fae0db..c4e4acb5e 100644
--- a/archaeological_operations/views.py
+++ b/archaeological_operations/views.py
@@ -49,9 +49,9 @@ def autocomplete_patriarche(request, non_closed=True):
q = request.GET.get('term')
query = Q()
for q in q.split(' '):
- query = query & Q(code_patriarche__startswith=q)
+ query &= Q(code_patriarche__startswith=q)
if non_closed:
- query = query & Q(end_date__isnull=True)
+ query &= Q(end_date__isnull=True)
limit = 15
operations = models.Operation.objects\
.filter(query).order_by('code_patriarche')[:limit]
@@ -113,12 +113,12 @@ def autocomplete_operation(request, non_closed=True):
q = q[2:]
try:
int(q)
- extra = extra | Q(code_patriarche__contains=q)
+ extra |= Q(code_patriarche__contains=q)
except ValueError:
pass
query = query & extra
if non_closed:
- query = query & Q(end_date__isnull=True)
+ query &= Q(end_date__isnull=True)
limit = 15
operations = models.Operation.objects.filter(query)[:limit]
data = json.dumps([{'id': operation.pk, 'value': unicode(operation)}
@@ -215,7 +215,7 @@ operation_creation_wizard = OperationWizard.as_view(
condition_dict=ope_crea_condition_dict,
url_name='operation_creation',)
-operation_modification_wizard = OperationModificationWizard.as_view([
+operation_modif_wizard_steps = [
('selec-operation_modification', OperationFormSelection),
('general-operation_modification', OperationFormModifGeneral),
('archaeologicalsite-operation_modification', ArchaeologicalSiteFormSet),
@@ -229,7 +229,11 @@ operation_modification_wizard = OperationModificationWizard.as_view([
('periods-operation_modification', PeriodForm),
('relations-operation_modification', RecordRelationsFormSet),
('abstract-operation_modification', OperationFormAbstract),
- ('final-operation_modification', FinalForm)],
+ ('final-operation_modification', FinalForm)
+]
+
+operation_modification_wizard = OperationModificationWizard.as_view(
+ operation_modif_wizard_steps,
label=_(u"Operation modification"),
condition_dict={
'preventive-operation_modification': is_preventive(
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 42bb1860e..44d7f8275 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -102,20 +102,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 +195,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 +218,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()