29
29
from qgis .core import (QgsApplication ,
30
30
QgsCoordinateReferenceSystem ,
31
31
QgsProcessingParameterMatrix ,
32
+ QgsProcessingOutputLayerDefinition ,
33
+ QgsProcessingParameterFeatureSink ,
34
+ QgsProcessingParameterFileDestination ,
35
+ QgsProcessingParameterFolderDestination ,
36
+ QgsProcessingParameterVectorDestination ,
37
+ QgsProcessingParameterRasterDestination ,
32
38
QgsVectorLayer ,
33
39
QgsProject )
34
40
from qgis .analysis import QgsNativeAlgorithms
37
43
from processing .gui .BatchAlgorithmDialog import BatchAlgorithmDialog
38
44
from processing .modeler .ModelerParametersDialog import ModelerParametersDialog
39
45
from processing .gui .wrappers import *
46
+ from processing .gui .DestinationSelectionPanel import DestinationSelectionPanel
40
47
41
48
start_app ()
42
49
QgsApplication .processingRegistry ().addProvider (QgsNativeAlgorithms ())
43
50
51
+ testDataPath = os .path .join (os .path .dirname (__file__ ), 'testdata' )
52
+
44
53
45
54
class AlgorithmDialogTest (unittest .TestCase ):
46
55
@@ -52,6 +61,10 @@ def testCreation(self):
52
61
53
62
class WrappersTest (unittest .TestCase ):
54
63
64
+ @classmethod
65
+ def setUpClass (cls ):
66
+ ProcessingConfig .initialize ()
67
+
55
68
def checkConstructWrapper (self , param , expected_wrapper_class ):
56
69
alg = QgsApplication .processingRegistry ().algorithmById ('native:centroids' )
57
70
@@ -233,6 +246,118 @@ def testNumber(self):
233
246
def testBand (self ):
234
247
self .checkConstructWrapper (QgsProcessingParameterBand ('test' ), BandWidgetWrapper )
235
248
249
+ def testFeatureSink (self ):
250
+ param = QgsProcessingParameterFeatureSink ('test' )
251
+ alg = QgsApplication .processingRegistry ().algorithmById ('native:centroids' )
252
+ panel = DestinationSelectionPanel (param , alg )
253
+
254
+ panel .setValue ('memory:' )
255
+ v = panel .getValue ()
256
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
257
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
258
+ self .assertEqual (v .sink .staticValue (), 'memory:' )
259
+
260
+ panel .setValue ('''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''' )
261
+ v = panel .getValue ()
262
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
263
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
264
+ self .assertEqual (v .sink .staticValue (), '''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''' )
265
+
266
+ panel .setValue ('''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''' )
267
+ v = panel .getValue ()
268
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
269
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
270
+ self .assertEqual (v .sink .staticValue (), '''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''' )
271
+
272
+ panel .setValue ('/home/me/test.shp' )
273
+ v = panel .getValue ()
274
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
275
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
276
+ self .assertEqual (v .sink .staticValue (), '/home/me/test.shp' )
277
+
278
+ ProcessingConfig .setSettingValue (ProcessingConfig .OUTPUT_FOLDER , testDataPath )
279
+ panel .setValue ('test.shp' )
280
+ v = panel .getValue ()
281
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
282
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
283
+ self .assertEqual (v .sink .staticValue (), os .path .join (testDataPath , 'test.shp' ))
284
+
285
+ def testVectorDestination (self ):
286
+ param = QgsProcessingParameterVectorDestination ('test' )
287
+ alg = QgsApplication .processingRegistry ().algorithmById ('native:centroids' )
288
+ panel = DestinationSelectionPanel (param , alg )
289
+
290
+ panel .setValue ('''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''' )
291
+ v = panel .getValue ()
292
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
293
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
294
+ self .assertEqual (v .sink .staticValue (), '''ogr:dbname='/me/a.gpkg' table="d" (geom) sql=''' )
295
+
296
+ panel .setValue ('''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''' )
297
+ v = panel .getValue ()
298
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
299
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
300
+ self .assertEqual (v .sink .staticValue (), '''postgis:dbname='oraclesux' host=10.1.1.221 port=5432 user='qgis' password='qgis' table="stufff"."output" (the_geom) sql=''' )
301
+
302
+ panel .setValue ('/home/me/test.shp' )
303
+ v = panel .getValue ()
304
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
305
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
306
+ self .assertEqual (v .sink .staticValue (), '/home/me/test.shp' )
307
+
308
+ ProcessingConfig .setSettingValue (ProcessingConfig .OUTPUT_FOLDER , testDataPath )
309
+ panel .setValue ('test.shp' )
310
+ v = panel .getValue ()
311
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
312
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
313
+ self .assertEqual (v .sink .staticValue (), os .path .join (testDataPath , 'test.shp' ))
314
+
315
+ def testRasterDestination (self ):
316
+ param = QgsProcessingParameterRasterDestination ('test' )
317
+ alg = QgsApplication .processingRegistry ().algorithmById ('native:centroids' )
318
+ panel = DestinationSelectionPanel (param , alg )
319
+
320
+ panel .setValue ('/home/me/test.tif' )
321
+ v = panel .getValue ()
322
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
323
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
324
+ self .assertEqual (v .sink .staticValue (), '/home/me/test.tif' )
325
+
326
+ ProcessingConfig .setSettingValue (ProcessingConfig .OUTPUT_FOLDER , testDataPath )
327
+ panel .setValue ('test.tif' )
328
+ v = panel .getValue ()
329
+ self .assertIsInstance (v , QgsProcessingOutputLayerDefinition )
330
+ self .assertEqual (v .createOptions , {'fileEncoding' : 'System' })
331
+ self .assertEqual (v .sink .staticValue (), os .path .join (testDataPath , 'test.tif' ))
332
+
333
+ def testFolderDestination (self ):
334
+ param = QgsProcessingParameterFolderDestination ('test' )
335
+ alg = QgsApplication .processingRegistry ().algorithmById ('native:centroids' )
336
+ panel = DestinationSelectionPanel (param , alg )
337
+
338
+ panel .setValue ('/home/me/test.tif' )
339
+ v = panel .getValue ()
340
+ self .assertEqual (v , '/home/me/test.tif' )
341
+
342
+ ProcessingConfig .setSettingValue (ProcessingConfig .OUTPUT_FOLDER , testDataPath )
343
+ panel .setValue ('test.tif' )
344
+ v = panel .getValue ()
345
+ self .assertEqual (v , os .path .join (testDataPath , 'test.tif' ))
346
+
347
+ def testFileDestination (self ):
348
+ param = QgsProcessingParameterFileDestination ('test' )
349
+ alg = QgsApplication .processingRegistry ().algorithmById ('native:centroids' )
350
+ panel = DestinationSelectionPanel (param , alg )
351
+
352
+ panel .setValue ('/home/me/test.tif' )
353
+ v = panel .getValue ()
354
+ self .assertEqual (v , '/home/me/test.tif' )
355
+
356
+ ProcessingConfig .setSettingValue (ProcessingConfig .OUTPUT_FOLDER , testDataPath )
357
+ panel .setValue ('test.tif' )
358
+ v = panel .getValue ()
359
+ self .assertEqual (v , os .path .join (testDataPath , 'test.tif' ))
360
+
236
361
237
362
if __name__ == '__main__' :
238
363
unittest .main ()
0 commit comments