summaryrefslogtreecommitdiff
path: root/ishtar_common/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r--ishtar_common/tests.py179
1 files changed, 116 insertions, 63 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index c3ca7cfd6..909380e77 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -29,7 +29,6 @@ from time import time
import zipfile
from io import StringIO
from unittest.runner import TextTestRunner, TextTestResult
-from osgeo import ogr, osr
from zipfile import ZipFile
from django.apps import apps
@@ -97,6 +96,7 @@ from ishtar_common.utils import (
)
from ishtar_common.tasks import launch_export
from ishtar_common import utils_secretary
+from ishtar_common import views_item
LIB_BASE_PATH = settings.LIB_BASE_PATH
FIXTURE_AUTH_PATH = settings.FIXTURE_AUTH_PATH
@@ -2782,12 +2782,8 @@ class BaseImportTest(TestCase):
root = os.path.join(settings.LIB_BASE_PATH, "ishtar_common")
folder = os.path.join(root, "qfield", "model")
dir_list = os.listdir(folder)
- try:
- self.assertEqual(len(dir_list), 4)
- self.assertEqual(dir_list,['Finds.gpkg', 'Prospections_attachments.zip', 'Prospections_qfield.zip', 'Prospections.qgs'])
- except:
- self.assertEqual(len(dir_list), 5)
- self.assertEqual(dir_list,['Finds.gpkg', 'Prospections_attachments.zip', 'Prospections.qgs~', 'Prospections_qfield.zip', 'Prospections.qgs'])
+ self.assertEqual(len(dir_list), 6)
+ self.assertEqual(dir_list,['Finds.gpkg', 'Prospections_attachments.zip', 'Context_records.gpkg', 'Prospections.qgs~', 'Prospections_qfield.zip', 'Prospections.qgs'])
with ZipFile(os.path.join(folder, "Prospections_qfield.zip"), 'r') as zip_file:
# Verification of the number of files in the .zip
self.assertEqual(len(zip_file.namelist()),1)
@@ -2820,73 +2816,130 @@ class BaseImportTest(TestCase):
os.remove(new_file)
- def test_export_qfield(self):
+ def export_qfield(self, mode, root, table_cols, col_names, datas):
"""
- :function: Test for the creation of a .gpkg, the modification of the .qgs and creation of a .zip test
+ :function: Core part of the test for the creation of a .gpkg, the modification of the .qgs and creation of a .zip test
"""
- # Verification of the original files
- self.test_verify_qfield_folder()
- # Preparation for the .gpkg
- driver = ogr.GetDriverByName("GPKG")
- root = settings.LIB_BASE_PATH + "ishtar_common/qfield/"
- filename = os.path.join(root, "export", "Finds.gpkg")
- # Verification to delete it if already existing
- if os.path.exists(filename):
- os.remove(filename)
- datasource = driver.CreateDataSource(filename)
- srs = osr.SpatialReference()
- srs.ImportFromEPSG(4326)
- # Preparation for the copy of the .qgs
- qgs_path = os.path.join(root, "model", "Prospections.qgs")
- new_qgs = os.path.join(root, "export", "Prospections.qgs")
- # Preparation for the copy of the .zip
- project = os.path.join(root, "model", "Prospections_qfield.zip")
- duplicate = os.path.join(root, "export", "Prospections_qfield_export.zip")
- # Verification to delete it if already existing
+ # 2) Creation of the .gpkg
+ finds, cr, list_ope, list_crea = views_item.gpkg_creation(root, table_cols, col_names, datas)
+ # 3) Preparations for the modification of the style in the .qgs file
+ qgs_path = os.path.join(root, 'model', 'Prospections.qgs')
+ new_qgs = os.path.join(root, 'tests', str(mode), 'Prospections.qgs')
+ if os.path.exists(new_qgs):
+ os.remove(new_qgs)
+ text = views_item.modification_style(qgs_path, table_cols, col_names, list_ope, list_crea)
+ with open(new_qgs, 'w', encoding='utf-8') as file:
+ file.write(text)
+
+ # II. Duplication of the .zip for export
+ project = os.path.join(root, 'model', 'Prospections_qfield.zip')
+ duplicate = os.path.join(root, 'tests', str(mode), 'Prospections_qfield_export.zip')
if os.path.exists(duplicate):
os.remove(duplicate)
shutil.copyfile(project, duplicate)
- layer = datasource.CreateLayer("Finds", srs, ogr.wkbPoint)
- list = ["x", "y", "z"]
- for elem in list:
- layer.CreateField(ogr.FieldDefn(elem, ogr.OFTReal))
- feature = ogr.Feature(layer.GetLayerDefn())
- for elem in list:
- feature.SetField(elem, 14.0)
- layer.CreateFeature(feature)
- feature = None
- datasource = None
- # Verification of the modifications
- text = open(qgs_path, encoding='utf-8').read()
- for elem in list:
- text = text.replace(f"champ_{elem}", elem)
- with open(new_qgs, 'w', encoding='utf-8') as file:
- file.write(text)
- # Creation of the new .zip
+
+ # III. Moving the .gpkg in a copy of the Qfield test project
with ZipFile(duplicate, 'a') as zip_file:
- zip_file.write(filename, os.path.basename(filename))
+ # Adding the .gpkg to the .zip
+ zip_file.write(finds, os.path.basename(finds))
+ zip_file.write(cr, os.path.basename(cr))
zip_file.write(new_qgs, os.path.basename(new_qgs))
+ # Closing of the .zip
zip_file.close()
- # Verification of the content of the .zip
- folder = os.path.join(root, "export")
+
+ # IV. Verification phase
+ folder = os.path.join(root, 'tests', str(mode))
dir_list = os.listdir(folder)
- self.assertEqual(len(dir_list), 3)
- self.assertEqual(dir_list, ['Finds.gpkg', 'Prospections_qfield_export.zip','Prospections.qgs'])
- with open(new_qgs, 'r', encoding='utf-8') as file:
- style = file.read()
- for elem in list:
- bool = f'"{elem}"' in style
- self.assertEqual(bool, True)
- bool = f'champ_{elem}' in style
- self.assertEqual(bool, False)
- with ZipFile(os.path.join(folder, "Prospections_qfield_export.zip"), 'r') as zip_file:
- self.assertEqual(len(zip_file.namelist()),3)
- list_files = ["Prospections_attachments.zip", "Finds.gpkg", "Prospections.qgs"]
+ # Verification if the file was well created
+ self.assertEqual(len(dir_list), 2)
+ self.assertEqual(dir_list, ['Prospections_qfield_export.zip', 'Prospections.qgs'])
+ # Verification of the content of the .zip
+ with ZipFile(os.path.join(folder, 'Prospections_qfield_export.zip'), 'r') as zip_file:
+ self.assertEqual(len(zip_file.namelist()), 4)
+ list_files = ['Prospections_attachments.zip', 'Finds.gpkg', 'Context_records.gpkg', 'Prospections.qgs']
self.assertEqual(zip_file.namelist(), list_files)
zip_file.close()
- os.remove(filename)
- os.remove(new_qgs)
- os.remove(duplicate)
+ # List of modified and new values to verify the absence/presence depending on the situation
+ if mode == 1:
+ old = ['champ_id', 'champ_ue', 'champ_x', 'champ_y', 'champ_z', 'champ_wkt_modif', 'champ_nom', 'champ_ope',
+ 'champ_insee', 'champ_parc', 'champ_type', 'champ_occup', 'champ_desc', 'champ_loca', 'champ_media_cr',
+ 'champ_periode', 'champ_acti', 'champ_geom']
+ new = ['Identifiant', 'UE', 'X', 'Y', 'Z', 'WKT_point', 'Unité_Enregistrement', 'Opération', 'INSEE_Commune',
+ 'Parcelle', 'Type', 'Interprétation', 'Description', 'Localisation', 'Media', 'Periode', 'Type_Activité', 'WKT']
+ if mode == 2:
+ old = ['champ_id', 'champ_ue', 'champ_wkt_simple', 'champ_nom', 'champ_ope', 'champ_insee', 'champ_parc',
+ 'champ_type', 'champ_occup', 'champ_desc', 'champ_loca', 'champ_media_cr', 'champ_periode', 'champ_acti', 'champ_geom']
+ new = ['Identifiant', 'UE', 'WKT_point', 'Unité_Enregistrement', 'Opération', 'INSEE_Commune', 'Parcelle',
+ 'Type', 'Interprétation', 'Description', 'Localisation', 'Media', 'Periode', 'Type_Activité', 'WKT']
+ if mode == 3:
+ old = ['champ_nom', 'champ_ope', 'champ_geom', 'champ_id', 'champ_ue', 'champ_date', 'champ_x', 'champ_y',
+ 'champ_z', 'champ_matériaux', 'champ_desc', 'champ_media_finds', 'champ_wkt_modif']
+ new = ['Nom', 'Opération', 'WKT_polygon', 'Identifiant', 'UE', 'Date', 'X', 'Y', 'Z', 'Description', 'Media', 'WKT_point']
+ with open(new_qgs, 'r', encoding='utf-8') as file:
+ style = file.read()
+ for elem in old:
+ print(mode, elem)
+ bool = f'name="{elem}"' in style
+ self.assertEqual(bool, False)
+ for elem in new:
+ bool = f'name="{elem}"' in style
+ if not bool:
+ print(elem)
+ self.assertEqual(bool, True)
+ for file in dir_list:
+ os.remove(os.path.join(folder, file))
+
+
+ def test_export_qfield(self):
+ """
+ :function: Test for the creation of a .gpkg, the modification of the .qgs and creation of a .zip test
+ """
+ # Verification of the original files
+ self.test_verify_qfield_folder()
+
+ # I. Preparations
+ root = settings.LIB_BASE_PATH + 'ishtar_common/qfield/'
+ # 1) Creating the necessary datas for the test
+ Operation = apps.get_model('archaeological_operations', 'Operation')
+ OperationType = apps.get_model('ishtar_common', 'OperationType')
+ ope_type = OperationType.objects.create(
+ label='Diag',
+ )
+ ope, __ = Operation.objects.get_or_create(
+ code_patriarche='Ope_Test',
+ operation_type=ope_type)
+ ContextRecord = apps.get_model('archaeological_context_records', 'ContextRecord')
+ cr, __ = ContextRecord.objects.get_or_create(
+ operation=ope,
+ label='Test_UE')
+ BaseFind = apps.get_model('archaeological_finds', 'BaseFind')
+ base_find, __ = BaseFind.objects.get_or_create(
+ context_record=cr,
+ label='Test_Mobilier',
+ external_id='Test_Mobilier',
+ auto_external_id=False,
+ x=0.7,
+ y=1.4,
+ z=2.1,
+ )
+
+ for mode in range(1, 4):
+ if mode == 1:
+ table_cols = ['label', 'context_record__label', 'geodata__x', 'geodata__y', 'geodata__z', 'geodata__point_3d']
+ col_names = ['Identifiant', 'UE', 'X', 'Y', 'Z', 'WKT_point']
+ datas = [['', 'Test_Mobilier', 'Test_UE', 7.0, 1.4, 2.1, 'Point Z (0.7 1.4 2.1)']]
+ self.export_qfield(mode, root, table_cols, col_names, datas)
+ if mode == 2:
+ table_cols = ['label', 'context_record__label', 'geodata__point_3d']
+ col_names = ['Identifiant', 'UE', 'WKT_point']
+ datas = [['', 'Test_Mobilier', 'Test_UE', 'POINT Z (0.7 1.4 0)']]
+ self.export_qfield(mode, root, table_cols, col_names, datas)
+ if mode == 3:
+ table_cols = [['label'], ['operation__code_patriarche'], ['geodata__multi_polygon']]
+ col_names = ['Nom', 'Opération', 'WKT_polygon']
+ datas = [['', 'Test_UE', 'Ope_Test', 'MultiPolygon (((0.0 0.0, 1.0 1.0, 2.0 2.0)))']]
+ self.export_qfield(mode, root, table_cols, col_names, datas)
+
class ImportTestInterface(BaseImportTest):