Skip to content
Permalink
Browse files

add test for QgsFeaturePickerWidget

  • Loading branch information
3nids committed Apr 29, 2020
1 parent 0119bb5 commit 030d41dc26c5bb71caca5e265194b199699e421f
Showing with 61 additions and 24 deletions.
  1. +61 −24 tests/src/python/test_qgsfeaturepicker.py
@@ -15,59 +15,96 @@
import os

from qgis.core import (
QgsApplication,
QgsFeature,
QgsVectorLayer,
QgsProject,
QgsRelation,
QgsTransaction,
QgsFeatureRequest,
QgsVectorLayerTools,
QgsPointXY,
QgsGeometry
)

from qgis.gui import (
QgsGui,
QgsRelationWidgetWrapper,
QgsAttributeEditorContext,
QgsMapCanvas,
QgsAdvancedDigitizingDockWidget
QgsFeaturePickerWidget
)

from qgis.PyQt.QtCore import QTimer
from qgis.PyQt.QtWidgets import (
QToolButton,
QMessageBox,
QDialogButtonBox,
QTableView,
QDialog
)
from qgis.testing import start_app, unittest

from qgis.PyQt.QtWidgets import QComboBox
from qgis.PyQt.QtTest import QSignalSpy, QTest


start_app()


def createLayer():
def createLayer(manyFeatures: bool = False):
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"test layer", "memory")
pr = layer.dataProvider()
f = QgsFeature()
f.setAttributes(["test", 123])
f.setAttributes(["test1", 123])
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(100, 200)))
f2 = QgsFeature()
f2.setAttributes(["test2", 457])
f2.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(200, 200)))
f3 = QgsFeature()
f3.setAttributes(["test2", 888])
f3.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(300, 200)))
assert pr.addFeatures([f, f2, f3])
assert layer.featureCount() == 3
flist = [f, f2, f3]
if manyFeatures:
for i in range(4, 100):
f = QgsFeature()
f.setAttributes(["test{}".format(i), i])
flist.append(f)

assert pr.addFeatures(flist)
assert layer.featureCount() == 99 if manyFeatures else 3
return layer


class TestQgsRelationEditWidget(unittest.TestCase):

def testSetFeature(self):
createLayer()
layer = createLayer()
w = QgsFeaturePickerWidget()
w.setLayer(layer)
w.setFeature(2)
spy = QSignalSpy(w.currentFeatureChanged)
spy.wait()
self.assertEqual(w.findChild(QComboBox).lineEdit().text(), "test2")
self.assertTrue(w.feature().geometry().equals(QgsGeometry.fromPointXY(QgsPointXY(200, 200))))

def testSetAllowNull(self):
layer = createLayer()
w = QgsFeaturePickerWidget()
w.setLayer(layer)
w.setAllowNull(True)
spy = QSignalSpy(w.featureChanged)
spy.wait()
self.assertEqual(w.findChild(QComboBox).lineEdit().text(), QgsApplication.nullRepresentation())
w.setAllowNull(False)
spy.wait()
self.assertEqual(w.findChild(QComboBox).lineEdit().text(), "test1")

def testFetchLimit(self):
layer = createLayer()
w = QgsFeaturePickerWidget()
w.setAllowNull(False)
w.setFetchLimit(20)
w.setLayer(layer)
spy = QSignalSpy(w.featureChanged)
spy.wait()
self.assertEqual(w.findChild(QComboBox).model().rowCount(), 20)

def testLineEdit(self):
layer = createLayer()
w = QgsFeaturePickerWidget()
w.setAllowNull(False)
w.setFetchLimit(20)
w.setLayer(layer)
spy = QSignalSpy(w.featureChanged)
spy.wait()
w.findChild(QComboBox).lineEdit().clear()
QTest.keyClicks(w.findChild(QComboBox).lineEdit(), "test99");
spy.wait()
self.assertEqual(w.feature().id(), 99)


if __name__ == '__main__':

0 comments on commit 030d41d

Please sign in to comment.
You can’t perform that action at this time.