diff options
Diffstat (limited to 'archaeological_warehouse/tests.py')
-rw-r--r-- | archaeological_warehouse/tests.py | 353 |
1 files changed, 253 insertions, 100 deletions
diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index a5610893a..91773b77b 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -17,6 +17,7 @@ # See the file COPYING for details. +import datetime import json from django.contrib.auth.models import Permission @@ -34,12 +35,13 @@ from ishtar_common.tests import ( GenericSerializationTest, COMMON_FIXTURES, create_user, + create_superuser, ) -from ishtar_common.models import IshtarSiteProfile, SpatialReferenceSystem +from ishtar_common.models import IshtarSiteProfile from archaeological_operations.models import Operation from archaeological_context_records.models import ContextRecord -from archaeological_finds.models import Find, MaterialType +from archaeological_finds.models import Find, MaterialType, Treatment, TreatmentType, FindTreatment, TreatmentState from archaeological_warehouse import models, views, forms, serializers @@ -552,6 +554,224 @@ class WarehouseTest(TestCase): self.assertEqual(q.count(), 1) +class ContainerQATest(FindInit, TestCase): + fixtures = WAREHOUSE_FIXTURES + model = models.Container + + def setUp(self): + self.get_default_user() + self.main_warehouse = models.Warehouse.objects.create( + name="Main", warehouse_type=models.WarehouseType.objects.all()[0] + ) + self.alt_warehouse = models.Warehouse.objects.create( + name="Alt", warehouse_type=models.WarehouseType.objects.all()[0] + ) + ct1 = models.ContainerType.objects.order_by("id").all()[0] + ct2 = models.ContainerType.objects.order_by("id").all()[1] + self.container_1 = models.Container.objects.create( + reference="1", container_type=ct1, + location=self.main_warehouse + ) + self.container_2 = models.Container.objects.create( + reference="2", container_type=ct1, + location=self.main_warehouse + ) + self.container_3 = models.Container.objects.create( + reference="3", container_type=ct2, + location=self.main_warehouse + ) + self.container_4 = models.Container.objects.create( + reference="4", container_type=ct2, + location=self.main_warehouse + ) + self.create_finds(data_base={"label": "Find 1"}, force=True) + + def get_default_user(self): + self.username, self.password, self.user = create_superuser() + return self.user + + def test_bulk_update(self): + username, password, user = create_user() + user.user_permissions.add(Permission.objects.get(codename="change_container")) + client = Client() + + ct = models.ContainerType.objects.all()[0] + ct2 = models.ContainerType.objects.all()[1] + + container_parent = models.Container.objects.create( + reference="Parent container", + location=self.main_warehouse, + container_type=ct, + ) + + container = models.Container.objects.create( + reference="Test", + location=self.main_warehouse, + parent=container_parent, + container_type=ct2, + ) + + # base modification + url = reverse("container-qa-bulk-update", args=[container.pk]) + data = {"qacontainer_type": ct.pk, "qalocation": "", "qaparent": ""} + response = client.post(url, data) + self.assertRedirects(response, "/") + client.login(username=username, password=password) + response = client.post(url, data) + self.assertEqual(response.status_code, 200) + confirm_url = reverse("container-qa-bulk-update-confirm", args=[container.pk]) + response = client.post(confirm_url, data) + self.assertRedirects(response, "/success/") + + container = models.Container.objects.get(pk=container.pk) + self.assertEqual(container.container_type_id, ct.pk) + + def test_move(self): + c = Client() + find_0 = self.finds[0] + find_0.container = self.container_3 + find_0.save() + + pks_1 = f"{self.container_3.pk}-{self.container_4.pk}" + pks_2 = str(self.container_1.pk) + url = reverse("container-qa-move", args=[pks_1]) + response = c.get(url) + self.assertRedirects(response, "/") + c = Client() + c.login(username=self.username, password=self.password) + response = c.get(url) + # change_container permission is needed + self.assertEqual(response.status_code, 404) + + c.logout() + self.user.user_permissions.add( + Permission.objects.get(codename="change_container") + ) + c.login(username=self.username, password=self.password) + response = c.get(url) + self.assertEqual(response.status_code, 200) + + packaging = TreatmentType.objects.get(txt_idx="packaging") + + def check_location_of_children(): + # check change location of children + container3 = models.Container.objects.get(pk=self.container_3.pk) + self.assertEqual(container3.location, self.alt_warehouse) + container4 = models.Container.objects.get(pk=self.container_4.pk) + self.assertEqual(container4.location, self.alt_warehouse) + + def check_reinit_parent(): + # reinit parent when not provided and location changed + container = models.Container.objects.get(pk=self.container_3.pk) + self.assertEqual(container.parent, None) + + + data_check_lst = [ + ( + { + "qa-move-qaparent": self.container_2.pk, + }, + {"parent": self.container_2}, + 0, + pks_1, + None + ), + ( + { + "qa-move-qalocation": self.alt_warehouse.pk, + }, + {"location": self.alt_warehouse}, + 0, + pks_1, + None + ), + ( + { + "qa-move-qaparent": self.container_2.pk, + "qa-move-create_treatment": True, + "qa-move-year": 2019, + "qa-move-treatment_type": packaging.pk, + }, + {"parent": self.container_2}, + 2, + pks_1, + None + ), + ( + { + "qa-move-qalocation": self.alt_warehouse.pk, + "qa-move-create_treatment": True, + "qa-move-year": 2019, + "qa-move-treatment_type": packaging.pk, + }, + {"location": self.alt_warehouse}, + 2, + pks_1, + None + ), + ( + { + "qa-move-qalocation": self.alt_warehouse.pk, + }, + {"location": self.alt_warehouse}, + 0, + pks_2, + check_location_of_children + ), + ( + { + "qa-move-qalocation": self.alt_warehouse.pk, + }, + {"location": self.alt_warehouse}, + 0, + pks_1, + check_reinit_parent + ), + ] + + for idx, lst in enumerate(data_check_lst): + data, check, nb_treat, pks, extra_check = lst + # reinit + self.container_1.location = self.main_warehouse + self.container_1.parent = None + self.container_1.save() + self.container_3.location = self.main_warehouse + self.container_3.parent = self.container_1 + self.container_3.save() + self.container_4.location = self.main_warehouse + self.container_4.parent = self.container_1 + self.container_4.save() + + init_nb_treat = Treatment.objects.count() + + response = c.post(reverse("container-qa-move", args=[pks]), data) + self.assertRedirects(response, "/success/") + for k in check: + for pk in pks.split("-"): + container = models.Container.objects.get(pk=pk) + self.assertEqual(getattr(container, k), check[k]) + + final_nb_treat = Treatment.objects.count() + self.assertEqual(init_nb_treat + nb_treat, final_nb_treat) + + if extra_check: + extra_check() + + if not final_nb_treat: + self.assertEqual(FindTreatment.objects.filter(find=find_0).count(), 0) + continue + + treatment = Treatment.objects.filter(container=self.container_3).order_by("-pk")[0] + q = FindTreatment.objects.filter(treatment=treatment) + self.assertEqual(q.filter(find=find_0).count(), 1) + ft = q.all()[0] + self.assertTrue(ft.full_location) + for value in check.values(): + self.assertIn(getattr(value, "reference", None) or value.name, ft.full_location) + self.assertTrue(ft.full_location) + self.assertTrue(ft.location_type) + + class ContainerTest(FindInit, TestCase): fixtures = WAREHOUSE_FIXTURES @@ -699,6 +919,37 @@ class ContainerTest(FindInit, TestCase): self.assertEqual(find.container_fisrt_full_location, "") self.assertEqual(find.container_ref_fisrt_full_location, "") + # do not update first location when a packaging treatment is associated + ## reinit + find.container = container + find.container_ref = container + find.save() + find = Find.objects.get(pk=find.pk) + full_localisation = container.generate_full_location() + self.assertEqual(find.container_fisrt_full_location, full_localisation) + self.assertEqual(find.container_ref_fisrt_full_location, full_localisation) + + treat_state = TreatmentState.get_completed_state() + t = Treatment.objects.create( + container=container_2, + year=2024, + start_date=datetime.date(2024, 2, 29), + location=self.main_warehouse, + treatment_state=treat_state + ) + treat_type = TreatmentType.objects.get(txt_idx="packaging") + t.treatment_types.add(treat_type) + t.save() + t.finds.add(find) + t.save() # force find container history + + container.location = self.main_warehouse + container.save() + find = Find.objects.get(pk=find.pk) + self.assertNotEqual(find.container_fisrt_full_location, container.generate_full_location()) + self.assertEqual(find.container_fisrt_full_location, full_localisation) + self.assertEqual(find.container_ref_fisrt_full_location, full_localisation) + """ def test_reassign_existing_division_on_warehouse_change(self): container = models.Container.objects.create( @@ -953,103 +1204,6 @@ class ContainerTest(FindInit, TestCase): self.assertEqual(bottom.parent, middle) self.assertEqual(bottom.location, self.main_warehouse) - def test_bulk_update(self): - username, password, user = create_user() - user.user_permissions.add(Permission.objects.get(codename="change_container")) - client = Client() - - ct = models.ContainerType.objects.all()[0] - ct2 = models.ContainerType.objects.all()[1] - - container_parent = models.Container.objects.create( - reference="Parent container", - location=self.main_warehouse, - container_type=ct, - ) - container_parent2 = models.Container.objects.create( - reference="Parent container 2", - location=self.alt_warehouse, - container_type=ct, - ) - - container = models.Container.objects.create( - reference="Test", - location=self.main_warehouse, - parent=container_parent, - container_type=ct2, - ) - - # base modification - url = reverse("container-qa-bulk-update", args=[container.pk]) - data = {"qacontainer_type": ct.pk, "qalocation": "", "qaparent": ""} - response = client.post(url, data) - self.assertRedirects(response, "/") - client.login(username=username, password=password) - response = client.post(url, data) - self.assertEqual(response.status_code, 200) - confirm_url = reverse("container-qa-bulk-update-confirm", args=[container.pk]) - response = client.post(confirm_url, data) - self.assertRedirects(response, "/success/") - - container = models.Container.objects.get(pk=container.pk) - self.assertEqual(container.container_type_id, ct.pk) - - container.container_type = ct2 - container.save() - - data = { - "qacontainer_type": "", - "qalocation": self.alt_warehouse.pk, - "qaparent": container_parent2.pk, - } - response = client.post(confirm_url, data) - self.assertRedirects(response, "/success/") - - container = models.Container.objects.get(pk=container.pk) - self.assertEqual(container.location, self.alt_warehouse) - self.assertEqual(container.parent, container_parent2) - - container.location = self.main_warehouse - container.parent = container_parent - container.save() - - # change location of children - - data = { - "qacontainer_type": "", - "qalocation": self.alt_warehouse.pk, - "qaparent": "", - } - confirm_parent_url = reverse( - "container-qa-bulk-update-confirm", args=[container_parent.pk] - ) - response = client.post(confirm_parent_url, data) - self.assertRedirects(response, "/success/") - - container_parent = models.Container.objects.get(pk=container_parent.pk) - self.assertEqual(container_parent.location, self.alt_warehouse) - container = models.Container.objects.get(pk=container.pk) - self.assertEqual(container.location, self.alt_warehouse) - - container_parent.location = self.main_warehouse - container_parent.save() - container.location = self.main_warehouse - container.parent = container_parent - container.save() - - # reinit parent when not provided and location changed - - data = { - "qacontainer_type": "", - "qalocation": self.alt_warehouse.pk, - "qaparent": "", - } - response = client.post(confirm_url, data) - self.assertRedirects(response, "/success/") - container = models.Container.objects.get(pk=container.pk) - self.assertEqual(container.location, self.alt_warehouse) - self.assertEqual(container.parent, None) - def test_index(self): ct = models.ContainerType.objects.all()[0] ct.stationary = False @@ -1153,7 +1307,6 @@ class ContainerTest(FindInit, TestCase): container_1.save() self.assertIsNone(models.Container.objects.get(pk=container_1.pk).parent) - def test_calculated_weight(self): self.create_finds() self.create_finds() |