Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
domi4484 committed Mar 4, 2024
1 parent fe58a08 commit f27075c
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
# Fix end of files
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -11,7 +11,7 @@ repos:

# Remove unused imports/variables
- repo: https://github.com/myint/autoflake
rev: v2.0.1
rev: v2.3.0
hooks:
- id: autoflake
args:
Expand All @@ -22,7 +22,7 @@ repos:

# Sort imports
- repo: https://github.com/pycqa/isort
rev: "5.12.0"
rev: "5.13.2"
hooks:
- id: isort
args:
Expand All @@ -31,7 +31,7 @@ repos:

# Black formatting
- repo: https://github.com/psf/black
rev: 23.1.0
rev: 24.2.0
hooks:
- id: black
language_version: python3
Expand All @@ -40,7 +40,7 @@ repos:

# Flake8 linter
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def updateUiMultiEdit(self):
# See https://github.com/qgis/QGIS/pull/45703
#
if self.nmRelation().isValid():
for featureIdMixedValue in copy.copy(featureIdsMixedValues):
for featureIdMixedValue in set(featureIdsMixedValues):
mixedValues = True
for parentTreeWidgetItem in parentTreeWidgetItems:
if featureIdMixedValue in multimapChildFeatures[id(parentTreeWidgetItem)]:
Expand Down
242 changes: 242 additions & 0 deletions linking_relation_editor/tests/test_relation_editor_multiedit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
from qgis.core import (
QgsApplication,
QgsFeature,
QgsProject,
QgsRelation,
QgsVectorLayer,
)
from qgis.gui import QgsGui
from qgis.PyQt.QtWidgets import QWidget
from qgis.testing import start_app, unittest

from linking_relation_editor.gui.linking_relation_editor_widget_factory import (
LinkingRelationEditorWidget,
)

start_app()


class TestRelationEditorMultiedit(unittest.TestCase):

def setUp(self):
pass
# QgsApplication.init()
QgsApplication.initQgis()
QgsGui.editorWidgetRegistry().initEditors()

def tearDown(self):
return
QgsApplication.exitQgis()

@classmethod
def setUpClass(cls):

cls.mLayer1 = None
cls.mLayer2 = None
cls.mLayerJoin = None
cls.mRelation = None
cls.mRelation1N = None
cls.mRelationNM = None

# create layer
cls.mLayer1 = QgsVectorLayer("LineString?field=pk:int&field=fk:int", "vl1", "memory")
cls.mLayer1.setDisplayExpression("'Layer1-' || pk")
QgsProject.instance().addMapLayer(cls.mLayer1, False)

cls.mLayer2 = QgsVectorLayer("LineString?field=pk:int", "vl2", "memory")
cls.mLayer2.setDisplayExpression("'Layer2-' || pk")
QgsProject.instance().addMapLayer(cls.mLayer2, False)

cls.mLayerJoin = QgsVectorLayer(
"LineString?field=pk:int&field=fk_layer1:int&field=fk_layer2:int", "join_layer", "memory"
)
cls.mLayerJoin.setDisplayExpression("'LayerJoin-' || pk")
QgsProject.instance().addMapLayer(cls.mLayerJoin, False)

# create relation
cls.mRelation = QgsRelation()
cls.mRelation.setId("vl1.vl2")
cls.mRelation.setName("vl1.vl2")
cls.mRelation.setReferencingLayer(cls.mLayer1.id())
cls.mRelation.setReferencedLayer(cls.mLayer2.id())
cls.mRelation.addFieldPair("fk", "pk")
assert cls.mRelation.isValid()
QgsProject.instance().relationManager().addRelation(cls.mRelation)

# create nm relations
cls.mRelation1N = QgsRelation()
cls.mRelation1N.setId("join_layer.vl1")
cls.mRelation1N.setName("join_layer.vl1")
cls.mRelation1N.setReferencingLayer(cls.mLayerJoin.id())
cls.mRelation1N.setReferencedLayer(cls.mLayer1.id())
cls.mRelation1N.addFieldPair("fk_layer1", "pk")
assert cls.mRelation1N.isValid()
QgsProject.instance().relationManager().addRelation(cls.mRelation1N)

cls.mRelationNM = QgsRelation()
cls.mRelationNM.setId("join_layer.vl2")
cls.mRelationNM.setName("join_layer.vl2")
cls.mRelationNM.setReferencingLayer(cls.mLayerJoin.id())
cls.mRelationNM.setReferencedLayer(cls.mLayer2.id())
cls.mRelationNM.addFieldPair("fk_layer2", "pk")
assert cls.mRelationNM.isValid()
QgsProject.instance().relationManager().addRelation(cls.mRelationNM)

# add features
ft0 = QgsFeature(cls.mLayer1.fields())
ft0.setAttribute("pk", 0)
ft0.setAttribute("fk", 10)
cls.mLayer1.startEditing()
cls.mLayer1.addFeature(ft0)
cls.mLayer1.commitChanges()

ft1 = QgsFeature(cls.mLayer1.fields())
ft1.setAttribute("pk", 1)
ft1.setAttribute("fk", 11)
cls.mLayer1.startEditing()
cls.mLayer1.addFeature(ft1)
cls.mLayer1.commitChanges()

ft2 = QgsFeature(cls.mLayer2.fields())
ft2.setAttribute("pk", 10)
cls.mLayer2.startEditing()
cls.mLayer2.addFeature(ft2)
cls.mLayer2.commitChanges()

ft3 = QgsFeature(cls.mLayer2.fields())
ft3.setAttribute("pk", 11)
cls.mLayer2.startEditing()
cls.mLayer2.addFeature(ft3)
cls.mLayer2.commitChanges()

ft4 = QgsFeature(cls.mLayer2.fields())
ft4.setAttribute("pk", 12)
cls.mLayer2.startEditing()
cls.mLayer2.addFeature(ft4)
cls.mLayer2.commitChanges()

# Add join features
jft1 = QgsFeature(cls.mLayerJoin.fields())
jft1.setAttribute("pk", 101)
jft1.setAttribute("fk_layer1", 0)
jft1.setAttribute("fk_layer2", 10)
cls.mLayerJoin.startEditing()
cls.mLayerJoin.addFeature(jft1)
cls.mLayerJoin.commitChanges()

jft2 = QgsFeature(cls.mLayerJoin.fields())
jft2.setAttribute("pk", 102)
jft2.setAttribute("fk_layer1", 1)
jft2.setAttribute("fk_layer2", 11)
cls.mLayerJoin.startEditing()
cls.mLayerJoin.addFeature(jft2)
cls.mLayerJoin.commitChanges()

jft3 = QgsFeature(cls.mLayerJoin.fields())
jft3.setAttribute("pk", 102)
jft3.setAttribute("fk_layer1", 0)
jft3.setAttribute("fk_layer2", 11)
cls.mLayerJoin.startEditing()
cls.mLayerJoin.addFeature(jft3)
cls.mLayerJoin.commitChanges()

@classmethod
def tearDownClass(cls):
QgsProject.instance().removeMapLayer(cls.mLayer1)
QgsProject.instance().removeMapLayer(cls.mLayer2)
QgsProject.instance().removeMapLayer(cls.mLayerJoin)

def testMultiEdit1N(self):
# Init a relation editor widget
parent = QWidget()
relationEditorWidget = LinkingRelationEditorWidget({}, parent)
relationEditorWidget.setRelations(self.mRelation, QgsRelation())

self.assertFalse(relationEditorWidget.multiEditModeActive())

featureIds = list()
for feature in self.mLayer2.getFeatures():
featureIds.append(feature.id())

relationEditorWidget.setMultiEditFeatureIds(featureIds)

# Update ui
relationEditorWidget.updateUiMultiEdit()

self.assertTrue(relationEditorWidget.multiEditModeActive())

setParentItemsText = set()
setChildrenItemsText = set()
for parentIndex in range(relationEditorWidget.mMultiEditTreeWidget.topLevelItemCount()):
parentItem = relationEditorWidget.mMultiEditTreeWidget.topLevelItem(parentIndex)
setParentItemsText.add(parentItem.text(0))
self.assertEqual(
parentItem.data(0, (LinkingRelationEditorWidget.MultiEditTreeWidgetRole.FeatureType)),
(LinkingRelationEditorWidget.MultiEditFeatureType.Parent),
)
for childIndex in range(parentItem.childCount()):
childItem = parentItem.child(childIndex)
setChildrenItemsText.add(childItem.text(0))
self.assertEqual(
childItem.data(0, (LinkingRelationEditorWidget.MultiEditTreeWidgetRole.FeatureType)),
(LinkingRelationEditorWidget.MultiEditFeatureType.Child),
)

if childItem.text(0) == "Layer1-0":
self.assertEqual(parentItem.text(0), "Layer2-10")

if childItem.text(0) == "Layer1-1":
self.assertEqual(parentItem.text(0), "Layer2-11")

self.assertEqual(setParentItemsText, {"Layer2-10", "Layer2-11", "Layer2-12"})
self.assertEqual(setChildrenItemsText, {"Layer1-0", "Layer1-1"})

def testMultiEditNM(self):
# Init a relation editor widget
parent = QWidget()
relationEditorWidget = LinkingRelationEditorWidget({}, parent)
relationEditorWidget.setRelations(self.mRelation1N, self.mRelationNM)

self.assertFalse(relationEditorWidget.multiEditModeActive())

featureIds = list()
for feature in self.mLayer1.getFeatures():
featureIds.append(feature.id())
relationEditorWidget.setMultiEditFeatureIds(featureIds)

# Update ui
relationEditorWidget.updateUiMultiEdit()

self.assertTrue(relationEditorWidget.multiEditModeActive())

setParentItemsText = set()
listChildrenItemsText = list()
for parentIndex in range(relationEditorWidget.mMultiEditTreeWidget.topLevelItemCount()):
parentItem = relationEditorWidget.mMultiEditTreeWidget.topLevelItem(parentIndex)
setParentItemsText.add(parentItem.text(0))
self.assertEqual(
parentItem.data(0, (LinkingRelationEditorWidget.MultiEditTreeWidgetRole.FeatureType)),
(LinkingRelationEditorWidget.MultiEditFeatureType.Parent),
)

for childIndex in range(parentItem.childCount()):
childItem = parentItem.child(childIndex)
listChildrenItemsText.append(childItem.text(0))
self.assertEqual(
childItem.data(0, (LinkingRelationEditorWidget.MultiEditTreeWidgetRole.FeatureType)),
(LinkingRelationEditorWidget.MultiEditFeatureType.Child),
)

if childItem.text(0) == "Layer2-10":
self.assertEqual(parentItem.text(0), "Layer1-0")

if childItem.text(0) == "Layer2-11":
possibleParents = list()
possibleParents.append("Layer1-0")
possibleParents.append("Layer1-1")
self.assertTrue(parentItem.text(0), possibleParents)

self.assertEqual(setParentItemsText, {"Layer1-0", "Layer1-1"})

listChildrenItemsText.sort()
self.assertEqual(listChildrenItemsText, ["Layer2-10", "Layer2-11", "Layer2-11"])

0 comments on commit f27075c

Please sign in to comment.