diff options
Diffstat (limited to 'archaeological_context_records/tests.py')
-rw-r--r-- | archaeological_context_records/tests.py | 64 |
1 files changed, 59 insertions, 5 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 1c900184c..2e1355572 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -22,11 +22,10 @@ import json from django.conf import settings from django.core.exceptions import ValidationError, ImproperlyConfigured from django.core.urlresolvers import reverse -from django.test import TestCase from django.test.client import Client from ishtar_common.models import IshtarSiteProfile, ImporterModel -from ishtar_common.tests import create_superuser +from ishtar_common.tests import create_superuser, TestCase from archaeological_operations.tests import OperationInitTest, \ ImportTest, ImportOperationTest from archaeological_operations import models as models_ope @@ -147,16 +146,23 @@ class ContextRecordInit(OperationInitTest): def create_context_record(self, user=None, data={}, force=False): 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 @@ -200,6 +206,54 @@ class ContextRecordTest(ContextRecordInit, TestCase): cr.external_id, u"{}-{}".format(cr.parcel.external_id, cr.label)) + def test_lost_parcel_dont_delete_context_record(self): + cr = self.create_context_record(force=True)[0] + parcel = models.Parcel.objects.get(pk=cr.parcel.pk) + parcel.operation = None + parcel.save() + # associated context record is not removed + self.assertEqual( + models.ContextRecord.objects.filter(pk=cr.pk).count(), 1) + # associated operation is restored + self.assertEqual( + models.Parcel.objects.get(pk=parcel.pk).operation, + cr.operation + ) + + def test_cache_update(self): + cr = self.create_context_record()[0] + cr_pk = cr.pk + # OP2010 - 1 | A | 1 | CR 1 + ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') + self.assertEqual(ope_id, 'OP2010-1') + self.assertEqual(parcel_sec, cr.parcel.section) + self.assertEqual(parcel_nb, cr.parcel.parcel_number) + self.assertEqual(cr_label, cr.label) + + new_lbl = "UE 2" + cr.label = new_lbl + cr.save() + cr = models.ContextRecord.objects.get(pk=cr_pk) + ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') + self.assertEqual(cr_label, new_lbl) + + new_sec, new_nb = "B", "42" + parcel = cr.parcel + parcel.section = new_sec + parcel.parcel_number = new_nb + parcel.save() + cr = models.ContextRecord.objects.get(pk=cr_pk) + ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') + self.assertEqual(parcel_sec, new_sec) + self.assertEqual(parcel_nb, new_nb) + + cr.operation.year = 2017 + cr.operation.save() + cr = models.ContextRecord.objects.get(pk=cr_pk) + ope_id, parcel_sec, parcel_nb, cr_label = cr.cached_label.split(' | ') + self.assertEqual(ope_id, 'OP2017-1') + + class ContextRecordSearchTest(ContextRecordInit, TestCase): fixtures = ImportContextRecordTest.fixtures |