summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas 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
commitaf13ebbe3d2314cef33bd8f9e372f62518db4465 (patch)
treeb0078ec33a98b60862c966cab4e8d5c48f2ddd54
parente027200e47ed93b25e2cca5f3392ba7879ad69a1 (diff)
downloadIshtar-af13ebbe3d2314cef33bd8f9e372f62518db4465.tar.bz2
Ishtar-af13ebbe3d2314cef33bd8f9e372f62518db4465.zip
First tests for the uses of QField project with Ishtar datas and their importation
-rw-r--r--archaeological_context_records/tests.py43
-rw-r--r--archaeological_context_records/tests/qfield-importeur-data.csv2
-rw-r--r--archaeological_context_records/tests/qfield-ue-test.zipbin0 -> 4680 bytes
-rw-r--r--archaeological_finds/tests.py183
-rw-r--r--archaeological_finds/tests/Finds.csv1
-rw-r--r--archaeological_finds/tests/qfield-importeur-data.csv2
-rw-r--r--archaeological_finds/tests/qfield-importeur-images.zipbin0 -> 10794 bytes
-rw-r--r--archaeological_finds/tests/qfield-importeur-test.zipbin0 -> 5768 bytes
-rw-r--r--archaeological_finds/tests/qfield-mobilier-test.zipbin0 -> 5214 bytes
-rw-r--r--archaeological_finds/tests/qfield-prospection.zipbin0 -> 15446 bytes
-rw-r--r--ishtar_common/tests.py19
11 files changed, 249 insertions, 1 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py
index 852cd3aa7..1716920e9 100644
--- a/archaeological_context_records/tests.py
+++ b/archaeological_context_records/tests.py
@@ -224,6 +224,49 @@ class ImportContextRecordTest(ImportTest, TestCase):
self.assertEqual(current_nb, 4)
"""
+ def test_qfield_import_cr(self):
+ """
+ :function: Try the importation of a context record link to QField
+ """
+ # Definition of the path to test importer data for GIS data
+ root = settings.LIB_BASE_PATH + "archaeological_context_records/tests/"
+ filename = root + "qfield-ue-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-ue-test") # Change the name with the slug of the importeur !!!
+ # Opening of the CSV
+ with open(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)
+ # Initialisation de l'import
+ impt.initialize()
+ # Creation of an operation for the importation
+ ope, __ = models.Operation.objects.get_or_create(
+ code_patriarche="OP",
+ operation_type=models_ope.OperationType.objects.all()[0])
+
+ # Getting referential values (nb objects, containers,docs, etc.)
+ nb_cr = models.ContextRecord.objects.count()
+ # Beggining of importation
+ impt.importation()
+ # Getting values after modifications
+ self.assertEqual(models.ContextRecord.objects.count(), nb_cr + 1)
+
+
class ContextRecordInit(OperationInitTest):
def create_context_record(self, data=None, user=None, force=False):
diff --git a/archaeological_context_records/tests/qfield-importeur-data.csv b/archaeological_context_records/tests/qfield-importeur-data.csv
new file mode 100644
index 000000000..ee28f0043
--- /dev/null
+++ b/archaeological_context_records/tests/qfield-importeur-data.csv
@@ -0,0 +1,2 @@
+id,id_unique,ue,date,x,y,z,materiau(x),descr,media,WKT
+OP,123,CR,2025-04-07,14,3,2000,Indéterminé,Test,A.jpg,POINT (-2.26868001391598 47.3849390721505)
diff --git a/archaeological_context_records/tests/qfield-ue-test.zip b/archaeological_context_records/tests/qfield-ue-test.zip
new file mode 100644
index 000000000..42ab74b26
--- /dev/null
+++ b/archaeological_context_records/tests/qfield-ue-test.zip
Binary files differ
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
diff --git a/archaeological_finds/tests/Finds.csv b/archaeological_finds/tests/Finds.csv
new file mode 100644
index 000000000..41c4bfb63
--- /dev/null
+++ b/archaeological_finds/tests/Finds.csv
@@ -0,0 +1 @@
+id,id_unique,ue,date,x,y,z,materiau(x),descr,media,WKT
diff --git a/archaeological_finds/tests/qfield-importeur-data.csv b/archaeological_finds/tests/qfield-importeur-data.csv
new file mode 100644
index 000000000..ee28f0043
--- /dev/null
+++ b/archaeological_finds/tests/qfield-importeur-data.csv
@@ -0,0 +1,2 @@
+id,id_unique,ue,date,x,y,z,materiau(x),descr,media,WKT
+OP,123,CR,2025-04-07,14,3,2000,Indéterminé,Test,A.jpg,POINT (-2.26868001391598 47.3849390721505)
diff --git a/archaeological_finds/tests/qfield-importeur-images.zip b/archaeological_finds/tests/qfield-importeur-images.zip
new file mode 100644
index 000000000..bb6472f22
--- /dev/null
+++ b/archaeological_finds/tests/qfield-importeur-images.zip
Binary files differ
diff --git a/archaeological_finds/tests/qfield-importeur-test.zip b/archaeological_finds/tests/qfield-importeur-test.zip
new file mode 100644
index 000000000..35850620b
--- /dev/null
+++ b/archaeological_finds/tests/qfield-importeur-test.zip
Binary files differ
diff --git a/archaeological_finds/tests/qfield-mobilier-test.zip b/archaeological_finds/tests/qfield-mobilier-test.zip
new file mode 100644
index 000000000..9f9c9de6e
--- /dev/null
+++ b/archaeological_finds/tests/qfield-mobilier-test.zip
Binary files differ
diff --git a/archaeological_finds/tests/qfield-prospection.zip b/archaeological_finds/tests/qfield-prospection.zip
new file mode 100644
index 000000000..7a124d58f
--- /dev/null
+++ b/archaeological_finds/tests/qfield-prospection.zip
Binary files differ
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 7582bf163..8d3fd837a 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -2728,6 +2728,25 @@ class BaseImportTest(TestCase):
imp_media = SimpleUploadedFile(imp.name, imp.read())
return imp_group, imp_file, imp_media
+ def get_qfield_import(self):
+ """
+ :function: Reproduction of the get_group_import function, adapted for QField data
+ """
+ # Creation of path to tests
+ root = os.path.join(settings.LIB_BASE_PATH, "archaeological_finds", "tests")
+ self.root = root
+ # Path to zip
+ importer_filename = os.path.join(root, "qfield-importeur-test.zip")
+ restore_serialized(importer_filename)
+ imp_group = models.ImporterGroup.objects.get(slug="qfield-csv-test") # Must change the name !!!
+ # Opening of the csv
+ with open(os.path.join(root, "qfield-importeur-data.csv"), "rb") as imp:
+ imp_file = SimpleUploadedFile(imp.name, imp.read())
+ # Opening of the media
+ with open(os.path.join(root, "qfield-importeur-images.zip"), "rb") as imp:
+ imp_media = SimpleUploadedFile(imp.name, imp.read())
+ return imp_group, imp_file, imp_media
+
def create_group_import(self, init=True):
imp_group, imp_file, imp_media = self.get_group_import()
create_user()