diff options
author | Thomas André <thomas.andre@iggdrasil.net> | 2025-04-14 08:42:38 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-07-29 08:49:03 +0200 |
commit | af13ebbe3d2314cef33bd8f9e372f62518db4465 (patch) | |
tree | b0078ec33a98b60862c966cab4e8d5c48f2ddd54 /archaeological_finds/tests.py | |
parent | e027200e47ed93b25e2cca5f3392ba7879ad69a1 (diff) | |
download | Ishtar-af13ebbe3d2314cef33bd8f9e372f62518db4465.tar.bz2 Ishtar-af13ebbe3d2314cef33bd8f9e372f62518db4465.zip |
First tests for the uses of QField project with Ishtar datas and their importation
Diffstat (limited to 'archaeological_finds/tests.py')
-rw-r--r-- | archaeological_finds/tests.py | 183 |
1 files changed, 182 insertions, 1 deletions
diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 00eeef09e..8d3f3607d 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -23,6 +23,7 @@ import json import os import shutil import tempfile +from zipfile import ZipFile from rest_framework.test import APITestCase from rest_framework.authtoken.models import Token @@ -45,7 +46,8 @@ from ishtar_common.models import ( ProfileType, ImporterModel, DocumentTemplate, - GeoVectorData + GeoVectorData, + ImporterGroup ) from django.utils.text import slugify from django.utils.translation import pgettext_lazy, gettext_lazy as _ @@ -79,6 +81,7 @@ from archaeological_warehouse.models import ( from archaeological_operations.models import Operation, OperationType from ishtar_common import forms_common +from ishtar_common.serializers import restore_serialized from ishtar_common.tests import ( WizardTest, @@ -1009,6 +1012,184 @@ class ImportFindTest(BaseImportFindTest): # check errors self.assertEqual(len(impt.errors), 0) + def test_verify_qfield_zip(self): + """ + :function: Test if all the files of the QField zipped folder are correct + """ + # Definition of the path to test importer data for GIS data + root = settings.LIB_BASE_PATH + "archaeological_finds/tests/" # Ne pas ch + filename = os.path.join(root, "qfield-prospection.zip") + # Opening of the .zip + zip_file = ZipFile(filename, 'r') + # Verification of the number of files in the .zip + self.assertEqual(len(zip_file.namelist()),2) + # Verification of the names of the files in the .zip + list_files=["Qfield_prospection.qgs","Qfield_prospection_attachments.zip"] + self.assertEqual(zip_file.namelist(), list_files) + # Closing of the .zip + zip_file.close() + + def test_add_file_qfield_zip(self): + """ + :function: Try the addition of a file in the zip for QField that will be dowloaded + """ + # Definition of the path to test importer data for GIS data + root = settings.LIB_BASE_PATH + "archaeological_finds/tests/" # Ne pas ch + filename = os.path.join(root, "qfield-prospection.zip") + # Opening of the .zip + zip_file = ZipFile(filename, 'a') + # Verification of the number of files in the .zip before adding a new one + self.assertEqual(len(zip_file.namelist()), 2) + # Recovery of the .csv to add for the test + csv=os.path.join(root, "Finds.csv") + # Adding the .csv to the .zip + zip_file.write(csv, os.path.basename(csv)) + # Verification of the number of files in the .zip after adding the .csv + self.assertEqual(len(zip_file.namelist()), 3) + # Verification of the names of the files in the .zip + list_files = ["Qfield_prospection.qgs", "Qfield_prospection_attachments.zip","Finds.csv"] + self.assertEqual(zip_file.namelist(), list_files) + # Cloning and deletion of the .zip to have 2 files once again + zip_temp=filename+".temp" + with ZipFile(filename, 'r') as zip_orig: + with ZipFile(zip_temp, 'w') as zip_new: + for item in zip_orig.infolist(): + if item.filename!= "Finds.csv" : + zip_new.writestr(item,zip_orig.read(item.filename)) + # Closing of the old .zip + zip_file.close() + # Squashing the old .zip with the new one + os.replace(zip_temp,filename) + # Opening of the new .zip + zip_file = ZipFile(filename, 'r') + # Verification of the number of files in the .zip after deleting the .csv + self.assertEqual(len(zip_file.namelist()), 2) + # Closing of the new .zip + zip_file.close() + + def test_qfield_import_finds(self): + """ + :function: Try the importation of finds link to QField + """ + # Definition of the path to test importer data for GIS data + root = settings.LIB_BASE_PATH + "archaeological_finds/tests/" # Ne pas ch + filename = os.path.join(root, "qfield-mobilier-test.zip") + self.restore_serialized(filename) + # Uses of a class in ishtar_commons.model_import to retrieve a model via its slug (?) + imp_type = ImporterType.objects.get(slug="qfield-mobilier-test") # Change the name with the slug of the importeur !!! + # Opening of the CSV + with open(os.path.join(root, "qfield-importeur-data.csv"), "rb") as imp_file : + file_dict = { + "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()) + } + post_dict = { + "importer_type": imp_type.pk, + "name": "find_geo_import", + "encoding": "utf-8", + "skip_lines": 1, + "csv_sep": ",", + } + # Preparation of the data import + form = forms_common.NewImportGISForm( + data=post_dict, files=file_dict, user=self.user + ) + self.assertTrue(form.is_valid()) + impt = form.save(self.ishtar_user) + # Import initialization + impt.initialize() + # Creation of an operation and a context record for the importation + ope, __ = Operation.objects.get_or_create( + code_patriarche="GOA", + operation_type=OperationType.objects.all()[0]) + cr, __ = ContextRecord.objects.get_or_create( + operation=ope, + label="CR" + ) + # Getting referential values (nb objects, containers,docs, etc.) + nb_base_find = models.BaseFind.objects.count() + nb_find = models.Find.objects.count() + nb_docs = Document.objects.count() + # Beggining of importation + impt.importation() + # Getting values after modifications + self.assertEqual(models.BaseFind.objects.count(), nb_base_find + 1) + self.assertEqual(models.Find.objects.count(), nb_find + 1) + self.assertEqual(Document.objects.count(), nb_docs + 1) + # Verification of the imported values + new = GeoVectorData.objects.order_by("-pk").all()[:1] + for geo in new: + self.assertTrue(geo.x) + self.assertTrue(geo.y) + self.assertTrue(geo.z) + self.assertEqual(new[0].x, 14) + self.assertEqual(new[0].y, 3) + self.assertEqual(new[0].z, 2000) + + def test_qfield_import_group(self): + """ + :function: Try the importation of datas from a QField prodject (context record, finds and documents) + CURRENTLY BUGGED + """ + # Definition of the path to test importer data for GIS data + root = os.path.join(settings.LIB_BASE_PATH, "archaeological_finds", "tests") + self.root = root + importer_filename = os.path.join(root, "qfield-importeur-test.zip") + restore_serialized(importer_filename) + # Uses of a class in ishtar_commons.model_import to retrieve a model via its slug (?) + imp_group = ImporterGroup.objects.get(slug="qfield-csv-test") + # Opening of the CSV and the .zip of the media + with open(os.path.join(root, "qfield-importeur-data.csv"), "rb") as imp: + imp_file = SimpleUploadedFile(imp.name, imp.read()) + with open(os.path.join(root, "qfield-importeur-images.zip"), "rb") as imp: + imp_media = SimpleUploadedFile(imp.name, imp.read()) + file_dict = { + "imported_file": imp_file + } + post_dict = { + "importer_type": imp_group.pk, + "name": "find_group_import", + "encoding": "utf-8", + "skip_lines": 1, + "csv_sep": ",", + } + # Initialization of error values + form = forms_common.NewImportGroupForm( + data=post_dict, files=file_dict, user=self.user + ) + self.assertFalse(form.is_valid()) + self.assertIn(str(_("This importer need a document archive.")), + form.errors["__all__"]) + file_dict["imported_images"] = imp_media + form = forms_common.NewImportGroupForm( + data=post_dict, files=file_dict, user=self.user + ) + self.assertTrue(form.is_valid()) + impt = form.save(self.ishtar_user) + + # Import initialization + impt.initialize() + # Creation of an operation and a context record for the importation + ope, __ = Operation.objects.get_or_create( + code_patriarche="OP", + operation_type=OperationType.objects.all()[0]) + cr, __ = ContextRecord.objects.get_or_create( + operation=ope, + label="CR" + ) + + # Getting referential values (nb objects, containers,docs, etc.) + nb_base_find = models.BaseFind.objects.count() + nb_find = models.Find.objects.count() + nb_docs = Document.objects.count() + + # Beggining of importation + impt.importation() + + # Getting values after modifications + self.assertEqual(models.BaseFind.objects.count(), nb_base_find + 1) + self.assertEqual(models.Find.objects.count(), nb_find + 1) + self.assertEqual(Document.objects.count(), nb_docs + 1) + self.assertFalse(any(imp.error_file for imp in impt.imports.all()), msg="Error on group import") class ExportTest(FindInit, TestCase): fixtures = FIND_TOWNS_FIXTURES |