Skip to content

Commit

Permalink
Add basic unit tests for constructing processing widget wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 22, 2017
1 parent 9882e4f commit c119c28
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
12 changes: 8 additions & 4 deletions python/plugins/processing/gui/ExtentSelectionPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ def __init__(self, dialog, param):

self.btnSelect.clicked.connect(self.selectExtent)

canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()
self.tool = RectangleMapTool(canvas)
self.tool.rectangleCreated.connect(self.updateExtent)
if iface is not None:
canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()
self.tool = RectangleMapTool(canvas)
self.tool.rectangleCreated.connect(self.updateExtent)
else:
self.prevMapTool = None
self.tool = None

if param.defaultValue() is not None:
context = createContext()
Expand Down
12 changes: 8 additions & 4 deletions python/plugins/processing/gui/PointSelectionPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ def __init__(self, dialog, default=None):

self.dialog = dialog

canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()
if iface is not None:
canvas = iface.mapCanvas()
self.prevMapTool = canvas.mapTool()

self.tool = PointMapTool(canvas)
self.tool.canvasClicked.connect(self.updatePoint)
self.tool = PointMapTool(canvas)
self.tool.canvasClicked.connect(self.updatePoint)
else:
self.prevMapTool = None
self.tool = None

if default:
tokens = str(default).split(',')
Expand Down
79 changes: 79 additions & 0 deletions python/plugins/processing/tests/GuiTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
from qgis.core import QgsApplication

from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
from processing.gui.wrappers import *

start_app()

Expand All @@ -41,5 +44,81 @@ def testCreation(self):
self.assertEqual(a.mainWidget.alg, alg)


class WrappersTest(unittest.TestCase):

def checkConstructWrapper(self, param, expected_wrapper_class):
alg = QgsApplication.processingRegistry().algorithmById('native:centroids')

# algorithm dialog
dlg = AlgorithmDialog(alg)
wrapper = WidgetWrapperFactory.create_wrapper_from_class(param, dlg)
self.assertIsNotNone(wrapper)
self.assertIsInstance(wrapper, expected_wrapper_class)
self.assertEqual(wrapper.dialog, dlg)
self.assertIsNotNone(wrapper.widget)

# batch dialog
dlg = BatchAlgorithmDialog(alg)
wrapper = WidgetWrapperFactory.create_wrapper_from_class(param, dlg)
self.assertIsNotNone(wrapper)
self.assertIsInstance(wrapper, expected_wrapper_class)
self.assertEqual(wrapper.dialog, dlg)
self.assertIsNotNone(wrapper.widget)

# modeler dialog
model = QgsProcessingModelAlgorithm()
dlg = ModelerParametersDialog(alg, model)
wrapper = WidgetWrapperFactory.create_wrapper_from_class(param, dlg)
self.assertIsNotNone(wrapper)
self.assertIsInstance(wrapper, expected_wrapper_class)
self.assertEqual(wrapper.dialog, dlg)
self.assertIsNotNone(wrapper.widget)

def testBoolean(self):
self.checkConstructWrapper(QgsProcessingParameterBoolean('test'), BooleanWidgetWrapper)

def testCrs(self):
self.checkConstructWrapper(QgsProcessingParameterCrs('test'), CrsWidgetWrapper)

def testExtent(self):
self.checkConstructWrapper(QgsProcessingParameterExtent('test'), ExtentWidgetWrapper)

def testPoint(self):
self.checkConstructWrapper(QgsProcessingParameterPoint('test'), PointWidgetWrapper)

def testFile(self):
self.checkConstructWrapper(QgsProcessingParameterFile('test'), FileWidgetWrapper)

def testMultiInput(self):
self.checkConstructWrapper(QgsProcessingParameterMultipleLayers('test'), MultipleInputWidgetWrapper)

def testRasterInput(self):
self.checkConstructWrapper(QgsProcessingParameterRasterLayer('test'), RasterWidgetWrapper)

def testEnum(self):
self.checkConstructWrapper(QgsProcessingParameterEnum('test'), SelectionWidgetWrapper)

def testString(self):
self.checkConstructWrapper(QgsProcessingParameterString('test'), StringWidgetWrapper)

def testExpression(self):
self.checkConstructWrapper(QgsProcessingParameterExpression('test'), ExpressionWidgetWrapper)

def testVector(self):
self.checkConstructWrapper(QgsProcessingParameterVectorLayer('test'), TableWidgetWrapper)

def testField(self):
self.checkConstructWrapper(QgsProcessingParameterField('test'), TableFieldWidgetWrapper)

def testSource(self):
self.checkConstructWrapper(QgsProcessingParameterFeatureSource('test'), VectorWidgetWrapper)

def testSource(self):
self.checkConstructWrapper(QgsProcessingParameterBand('test'), BandWidgetWrapper)

def testMapLayer(self):
self.checkConstructWrapper(QgsProcessingParameterMapLayer('test'), MapLayerWidgetWrapper)


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

0 comments on commit c119c28

Please sign in to comment.