diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-04-06 12:31:38 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-28 12:15:20 +0100 |
commit | 0f96790ca2866d205b262c96ddb1b155abb80ef6 (patch) | |
tree | 1ac684557b5b6d2b7457b80040f8955e0db19d07 /archaeological_finds/tests.py | |
parent | 8b69114b1932329283f3563077a3cb53149af7c8 (diff) | |
download | Ishtar-0f96790ca2866d205b262c96ddb1b155abb80ef6.tar.bz2 Ishtar-0f96790ca2866d205b262c96ddb1b155abb80ef6.zip |
New container localisation - adapt sheets, imports
Diffstat (limited to 'archaeological_finds/tests.py')
-rw-r--r-- | archaeological_finds/tests.py | 164 |
1 files changed, 161 insertions, 3 deletions
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 55006c4ca..80202f442 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -18,8 +18,11 @@ # See the file COPYING for details. from copy import deepcopy +import csv import datetime import json +import os +import tempfile from rest_framework.test import APITestCase from rest_framework.authtoken.models import Token @@ -35,6 +38,7 @@ from django.test import tag from django.test.client import Client from ishtar_common.models import ImporterType, IshtarUser, ImporterColumn,\ FormaterType, ImportTarget, IshtarSiteProfile, ProfileType +from django.utils.text import slugify from django.utils.translation import pgettext_lazy, gettext_lazy as _ from ishtar_common.models import Person, get_current_profile, UserProfile, \ @@ -598,15 +602,22 @@ class TreatmentWizardCreationTest(WizardTest, FindInit, TestCase): # treat) -class ImportFindTest(ImportTest, TestCase): - fixtures = FIND_TOWNS_FIXTURES +class ImportFindTest(ImportTest, FindInit, TestCase): + fixtures = FIND_TOWNS_FIXTURES + [ + settings.ROOT_PATH + + '../archaeological_finds/tests/import_loca_test.json', + ] + + def setUp(self): + super(ImportFindTest, self).setUp() + self.tmpdir = tempfile.TemporaryDirectory() def test_mcc_import_finds(self): self.init_context_record() old_nb = models.BaseFind.objects.count() old_nb_find = models.Find.objects.count() - MCC = ImporterType.objects.get(name=u"MCC - Mobilier") + MCC = ImporterType.objects.get(name="MCC - Mobilier") col = ImporterColumn.objects.create(col_number=25, importer_type_id=MCC.pk) @@ -671,6 +682,153 @@ class ImportFindTest(ImportTest, TestCase): f.index, expected_index )) + def test_import_locations(self): + self.create_finds() + self.create_finds() + self.create_finds() + self.create_finds() + external_ids = [] + for idx, f in enumerate(self.finds): + f.label = "Find {}".format(idx) + f.save() + external_ids.append(f.external_id) + + wt, __ = WarehouseType.objects.get_or_create(label="WT", txt_idx="WT") + + warehouse, __ = Warehouse.objects.get_or_create( + external_id="warehouse-test", + defaults={"name": "Warehouse test", + "warehouse_type": wt} + ) + + container_types = [] + levels = ["Building", "Area", "Shelf", "Box"] + for level in levels: + container_type, __ = ContainerType.objects.get_or_create( + label=level, txt_idx=slugify(level) + ) + container_types.append(container_type) + + for idx in range(len(levels[:-1])): + WarehouseDivisionLink.objects.get_or_create( + warehouse=warehouse, + container_type=container_types[idx], + order=(idx + 1) * 10 + ) + + old_nb = models.BaseFind.objects.count() + old_nb_find = models.Find.objects.count() + old_nb_container = Container.objects.count() + + importer_type = ImporterType.objects.get(slug="importeur-test") + + imp_filename = os.path.join(self.tmpdir.name, "imp_locations.csv") + + with open(imp_filename, "w") as impfile: + w = csv.writer(impfile) + w.writerow(['External ID', "Warehouse", "Ref.", + "Container type", "Localisation 1", "Localisation 2", + "Localisation 3"]) + for idx, ext_id in enumerate(external_ids): + if idx < 2: + w.writerow([ext_id, "warehouse-test", + "Réf. {}".format((idx + 1) * 10), + container_types[-1].name, + "A", "42", idx + 1]) + else: + w.writerow([ext_id, "warehouse-test", + "Réf. {}".format((idx + 1) * 10), + container_types[-1].name, + "A", 43]) + + imp_file = open(imp_filename, "rb") + file_dict = {'imported_file': SimpleUploadedFile(imp_file.name, + imp_file.read())} + post_dict = {'importer_type': importer_type.pk, 'skip_lines': 1, + "encoding": 'utf-8', "name": 'init_find_import', + "csv_sep": ","} + form = forms_common.NewImportForm(data=post_dict, files=file_dict, + user=self.user) + form.is_valid() + self.assertTrue(form.is_valid()) + impt = form.save(self.ishtar_user) + impt.initialize() + + impt.importation() + # no new finds has now been imported + current_nb = models.BaseFind.objects.count() + self.assertEqual(current_nb, old_nb) + current_nb = models.Find.objects.count() + self.assertEqual(current_nb, old_nb_find) + + current_nb = Container.objects.count() + self.assertEqual(current_nb, old_nb_container + 4 + 2 + 2 + 1) + containers = list(Container.objects.all()) + for container in containers: + self.assertEqual(container.location, warehouse) + q = Container.objects.filter(container_type=container_types[0]) + self.assertEqual(q.count(), 1) + building = q.all()[0] + self.assertIsNone(building.parent) + + q = Container.objects.filter(container_type=container_types[1]) + self.assertEqual(q.count(), 2) + areas = list(q.all()) + area = q.all()[0] + self.assertEqual(area.parent, building) + + q = Container.objects.filter(container_type=container_types[2]) + self.assertEqual(q.count(), 2) + shelves = list(q.all()) + for shelf in shelves: + self.assertEqual(shelf.parent, area) + + q = Container.objects.filter(container_type=container_types[3]) + self.assertEqual(q.count(), 4) + boxes = list(q.all().order_by("id")) + previous_shelf = None + for box in boxes[:2]: + if not previous_shelf: + previous_shelf = box.parent + else: + # on a different shelf + self.assertNotEqual(previous_shelf, box.parent) + self.assertIn(box.parent_id, [s.pk for s in shelves]) + previous_area = None + for box in boxes[2:]: + if not previous_area: + previous_area = box.parent + else: + self.assertEqual(previous_area, box.parent) # on the same area + self.assertIn(box.parent_id, [s.pk for s in areas]) + + importer_type = ImporterType.objects.get(slug="importeur-test") + cols = list(ImporterColumn.objects.filter( + importer_type=importer_type, + col_number__gte=5).values_list("id", flat=True)) + for target in ImportTarget.objects.filter(column_id__in=cols): + target.target = target.target.replace("set_localisation", + "set_static_localisation") + target.save() + + # delete area 43 and all boxes + Container.objects.filter(reference="43").delete() + Container.objects.filter(container_type=container_types[-1]).delete() + + # reimport + impt.initialize() + impt.importation() + # check errors + self.assertEqual(len(impt.errors), 2) + for error in impt.errors: + self.assertEqual( + error['error'], + "The division Area 43 do not exist for the location Warehouse " + "test.") + + def tearDown(self): + self.tmpdir.cleanup() + class FindTest(FindInit, TestCase): fixtures = FIND_FIXTURES |