-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests but at the moment qgis is not built with gdal >3.4 but maybe we…
… will have later images soon.
- Loading branch information
Showing
2 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
""" | ||
/*************************************************************************** | ||
------------------- | ||
begin : 28.10.2024 | ||
git sha : :%H$ | ||
copyright : (C) 2024 by Dave Signer | ||
email : [email protected] | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
""" | ||
|
||
import datetime | ||
import logging | ||
import os | ||
import pathlib | ||
import tempfile | ||
|
||
from osgeo import gdal | ||
from qgis.core import QgsProject | ||
from qgis.testing import start_app, unittest | ||
|
||
import modelbaker.utils.db_utils as db_utils | ||
from modelbaker.dataobjects.project import Project | ||
from modelbaker.db_factory.gpkg_command_config_manager import GpkgCommandConfigManager | ||
from modelbaker.generator.generator import Generator | ||
from modelbaker.iliwrapper import iliimporter | ||
from modelbaker.iliwrapper.globals import DbIliMode | ||
from tests.utils import iliimporter_config, testdata_path | ||
|
||
CATALOGUE_DATASETNAME = "Catset" | ||
|
||
start_app() | ||
|
||
test_path = pathlib.Path(__file__).parent.absolute() | ||
|
||
|
||
class TestMultipleGeometriesPerTable(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
"""Run before all tests.""" | ||
cls.basetestpath = tempfile.mkdtemp() | ||
|
||
def test_multiple_geom_geopackage(self): | ||
""" | ||
Checks when the gdal version is sufficient (means >=3.4) if tables are created with multiple geometries and the correct layers are generated. | ||
This of course depends with what gdal version the current images are built. | ||
""" | ||
|
||
importer = iliimporter.Importer() | ||
importer.tool = DbIliMode.ili2gpkg | ||
importer.configuration = iliimporter_config(importer.tool) | ||
importer.configuration.ilifile = testdata_path("ilimodels/MultipleGeom.ili") | ||
importer.configuration.ilimodels = "MultipleGeom" | ||
importer.configuration.dbfile = os.path.join( | ||
self.basetestpath, | ||
"tmp_optimal_oid_madness_{:%Y%m%d%H%M%S%f}.gpkg".format( | ||
datetime.datetime.now() | ||
), | ||
) | ||
importer.configuration.srs_code = 2056 | ||
importer.configuration.inheritance = "smart2" | ||
importer.configuration.create_basket_col = True | ||
|
||
# create it when there's a sufficient gdal version | ||
importer.configuration.create_gpkg_multigeom = self._sufficient_gdal() | ||
|
||
importer.stdout.connect(self.print_info) | ||
importer.stderr.connect(self.print_error) | ||
assert importer.run() == iliimporter.Importer.SUCCESS | ||
|
||
# check geometry table | ||
self._check_geometry_table(importer.configuration) | ||
|
||
# create project | ||
config_manager = GpkgCommandConfigManager(importer.configuration) | ||
uri = config_manager.get_uri() | ||
|
||
generator = Generator( | ||
tool=DbIliMode.ili2gpkg, | ||
uri=uri, | ||
inheritance=importer.configuration.inheritance, | ||
consider_basket_handling=True, | ||
) | ||
|
||
project = Project() | ||
|
||
available_layers = generator.layers() | ||
relations, _ = generator.relations(available_layers) | ||
legend = generator.legend(available_layers) | ||
project.layers = available_layers | ||
project.relations = relations | ||
project.legend = legend | ||
project.post_generate() | ||
|
||
qgis_project = QgsProject.instance() | ||
project.create(None, qgis_project) | ||
|
||
# check layertree | ||
root = qgis_project.layerTreeRoot() | ||
assert root is not None | ||
|
||
tree_layers = root.findLayers() | ||
assert len(tree_layers) == 7 | ||
|
||
{layer.name() for layer in tree_layers} | ||
expected_layer_names_with_multigeometry = { | ||
"NoGeomClass", | ||
"POI", | ||
"GOI (Point)", | ||
"T_ILI2DB_BASKET", | ||
"T_ILI2DB_DATASET", | ||
"GOI (Line)", | ||
"GOI (Surface)", | ||
} | ||
expected_layer_names_without_multigeometry = { | ||
"NoGeomClass", | ||
"GOI", | ||
"POI", | ||
"GOI (Line)", | ||
"GOI (Surface)", | ||
"T_ILI2DB_BASKET", | ||
"T_ILI2DB_DATASET", | ||
} | ||
|
||
if self._sufficient_gdal(): | ||
assert { | ||
layer.name() for layer in tree_layers | ||
} == expected_layer_names_with_multigeometry | ||
else: | ||
assert { | ||
layer.name() for layer in tree_layers | ||
} == expected_layer_names_without_multigeometry | ||
|
||
def _check_geometry_table(self, configuration): | ||
# check if there are multiple geometry columns of the same table | ||
db_connector = db_utils.get_db_connector(configuration) | ||
tables_with_multiple_geometries = db_connector.multiple_geometry_tables() | ||
|
||
has_it = bool(len(tables_with_multiple_geometries) > 0) | ||
|
||
# should have multiple when having a sufficient gdal and otherwise not | ||
assert (has_it and self._sufficient_gdal()) or ( | ||
not has_it and not self._sufficient_gdal() | ||
) | ||
|
||
def _sufficient_gdal(self): | ||
return bool(int(gdal.VersionInfo("VERSION_NUM")) >= 3080000) | ||
|
||
def print_info(self, text): | ||
logging.info(text) | ||
|
||
def print_error(self, text): | ||
logging.error(text) | ||
|
||
def tearDown(self): | ||
QgsProject.instance().removeAllMapLayers() | ||
QgsProject.instance().clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters