Skip to content

Commit

Permalink
Start basic tests about project generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Jul 3, 2024
1 parent 77077a1 commit fbbb705
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 100 deletions.
7 changes: 7 additions & 0 deletions tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__license__ = 'GPL version 3'
__email__ = 'info@3liz.org'

import tempfile
import unittest

from qgis.core import QgsApplication
Expand All @@ -17,3 +18,9 @@ def setUpClass(cls):
@classmethod
def tearDownClass(cls):
cls.qgs.exitQgis()

def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()

def tearDown(self):
self.temp_dir.cleanup()
66 changes: 0 additions & 66 deletions tests/test_basic_replacement.py

This file was deleted.

75 changes: 41 additions & 34 deletions tests/test_generate_projects.py.txt → tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,33 @@
__license__ = 'GPL version 3'
__email__ = 'info@3liz.org'

import tempfile
import unittest

from pathlib import Path

from qgis.core import QgsFeature, QgsProject, QgsVectorLayer, edit
from qgis._core import QgsFeature
from qgis.core import QgsProject, QgsVectorLayer, edit

from dynamic_layers.core.dynamic_layers_engine import DynamicLayersEngine
from dynamic_layers.core.generate_projects import GenerateProjects
from dynamic_layers.definitions import CustomProperty
from tests.base_tests import BaseTests


class TestGenerateProjects(BaseTests):
class TestBasicReplacement(BaseTests):

def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()

def tearDown(self):
self.temp_dir.cleanup()

def test_generate_projects(self):
""" Test generate a bunch of projects. """
def test_replacement_map_layer(self):
""" Test datasource can be replaced. """
project = QgsProject()

folder_1 = 'folder_1'
folder_2 = 'folder_2'
folder_3 = 'folder_3'
folder_token = '$folder'

vector = QgsVectorLayer(str(Path(f"fixtures/{folder_1}/lines.geojson")), "Layer 1")
project.addMapLayer(vector)
self.assertEqual(1, len(project.mapLayers()))

coverage = QgsVectorLayer(
"None?&field=id:integer&field=name:string(20)&index=yes", "coverage", "memory")
with edit(coverage):
feature = QgsFeature(coverage.fields())
feature.setAttributes([0, "1"])
coverage.addFeature(feature)

feature = QgsFeature(coverage.fields())
feature.setAttributes([0, "2"])
coverage.addFeature(feature)

feature = QgsFeature(coverage.fields())
feature.setAttributes([0, "3"])
coverage.addFeature(feature)

self.assertEqual(3, coverage.featureCount())

generator = GenerateProjects(project, coverage, coverage.fields().at(1), Path(self.temp_dir.name))
self.assertTrue(generator.process())

engine = DynamicLayersEngine()
engine.set_dynamic_layers_from_project(project)
self.assertDictEqual({}, engine._dynamic_layers)
Expand All @@ -79,10 +52,9 @@ def test_generate_projects(self):
)

# Replace
variables = {
engine.search_and_replace_dictionary = {
'folder': folder_2,
}
engine.search_and_replace_dictionary = variables

engine.set_dynamic_layers_datasource_from_dic()
engine.set_dynamic_project_properties(project, "Test title", "Test abstract")
Expand All @@ -91,6 +63,41 @@ def test_generate_projects(self):
self.assertNotIn(folder_1, vector.source())
self.assertNotIn(folder_token, vector.source())

def test_generate_projects(self):
""" Test generate a bunch of projects. """
project = QgsProject()

folder_1 = 'folder_1'
# folder_2 = 'folder_2'
# folder_3 = 'folder_3'
# folder_token = '$folder'

vector = QgsVectorLayer(str(Path(f"fixtures/{folder_1}/lines.geojson")), "Layer 1")
project.addMapLayer(vector)
self.assertEqual(1, len(project.mapLayers()))

coverage = QgsVectorLayer(
"None?&field=id:integer&field=name:string(20)&index=yes", "coverage", "memory")
with edit(coverage):
feature = QgsFeature(coverage.fields())
feature.setAttributes([0, "1"])
coverage.addFeature(feature)

feature = QgsFeature(coverage.fields())
feature.setAttributes([0, "2"])
coverage.addFeature(feature)

feature = QgsFeature(coverage.fields())
feature.setAttributes([0, "3"])
coverage.addFeature(feature)

self.assertEqual(3, coverage.featureCount())

generator = GenerateProjects(project, coverage, coverage.fields().at(1), Path(self.temp_dir.name))
self.assertTrue(generator.process())

self.assertTrue(Path(self.temp_dir))


if __name__ == '__main__':
unittest.main()

0 comments on commit fbbb705

Please sign in to comment.