summaryrefslogtreecommitdiff
path: root/ishtar_common/tests.py
diff options
context:
space:
mode:
authorThomas André <thomas.andre@iggdrasil.net>2025-05-02 18:13:45 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2025-07-29 08:49:03 +0200
commit0c247e34e79978ba910c01c56528718cbce4e2d5 (patch)
treece9fd077037d72b815afdda1cdf5524c7fdd50a7 /ishtar_common/tests.py
parent63a6b5021626e956e6151ba3cc1e358aabf29e8a (diff)
downloadIshtar-0c247e34e79978ba910c01c56528718cbce4e2d5.tar.bz2
Ishtar-0c247e34e79978ba910c01c56528718cbce4e2d5.zip
QField project export done and tested + saving some of the specifics importer for QField
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r--ishtar_common/tests.py115
1 files changed, 114 insertions, 1 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index d14c861bd..fd56b1a83 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -29,7 +29,8 @@ 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
@@ -2773,6 +2774,118 @@ class BaseImportTest(TestCase):
return impt
+ def test_verify_qfield_folder(self):
+ """
+ :function: Verify if all folders necessary for QField are here
+ """
+ # Definition of the path to test importer data for GIS data
+ 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'])
+ 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)
+ # Verification of the names of the files in the .zip
+ list_files = ["Prospections_attachments.zip"]
+ self.assertEqual(zip_file.namelist(), list_files)
+ # Closing of the .zip
+ zip_file.close()
+
+
+ def test_modification_xml(self):
+ """
+ :function: Test to modify the content of a style file for QGIS
+ """
+ # Definition of the path to test importer data for GIS data
+ root = os.path.join(settings.LIB_BASE_PATH, "ishtar_common")
+ filename = os.path.join(root, "qfield", "model", "Prospections.qgs")
+ new_file = os.path.join(root, "qfield", "model", "Test.qgs")
+ with open(filename, 'r', encoding ='utf-8') as file:
+ style = file.read()
+ modifications = style.replace('champ_','test_modif_')
+ with open(new_file, 'w', encoding ='utf-8') as file:
+ file.write(modifications)
+ with open(new_file, 'r', encoding='utf-8') as file:
+ style = file.read()
+ bool = 'test_modif_' in style
+ self.assertEqual(bool, True)
+ bool = 'champ_' in style
+ self.assertEqual(bool, False)
+ os.remove(new_file)
+
+
+ def test_export_qfield(self):
+ # 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
+ 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
+ with ZipFile(duplicate, 'a') as zip_file:
+ zip_file.write(filename, os.path.basename(filename))
+ zip_file.write(new_qgs, os.path.basename(new_qgs))
+ zip_file.close()
+ # Verification of the content of the .zip
+ folder = os.path.join(root, "export")
+ 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"]
+ self.assertEqual(zip_file.namelist(), list_files)
+ zip_file.close()
+ os.remove(filename)
+ os.remove(new_qgs)
+ os.remove(duplicate)
+
+
class ImportTestInterface(BaseImportTest):
def setUp(self):