31
31
import hashlib
32
32
import shutil
33
33
import nose2
34
+ import tempfile
34
35
from qgis .core import (QgsProcessingParameterNumber ,
35
36
QgsApplication ,
37
+ QgsMapLayer ,
38
+ QgsProject ,
39
+ QgsProcessingContext ,
40
+ QgsProcessingUtils ,
41
+ QgsProcessingFeedback ,
36
42
QgsProcessingParameterDefinition )
37
43
from qgis .testing import start_app , unittest
38
44
from processing .core .ProcessingConfig import ProcessingConfig , Setting
46
52
from processing .algs .otb .OtbChoiceWidget import OtbParameterChoice , OtbChoiceWidgetWrapper
47
53
import AlgorithmsTestBase
48
54
55
+ import processing
49
56
OTB_INSTALL_DIR = os .environ .get ('OTB_INSTALL_DIR' )
50
57
51
58
52
59
class TestOtbAlgorithms (unittest .TestCase , AlgorithmsTestBase .AlgorithmsTest ):
53
60
54
61
def test_init_algorithms (self ):
62
+ """
63
+ This test will read each otb algorithm in 'algs.txt'
64
+ and creates an instance of OtbAlgorithm and check if it can be executed
65
+ This is done in :class: `OtbAlgorithmProvider` load() method
66
+ """
55
67
algs_txt = os .path .join (self .descrFolder , 'algs.txt' )
56
68
with open (algs_txt ) as lines :
57
69
line = lines .readline ().strip ('\n ' ).strip ()
@@ -69,7 +81,35 @@ def test_init_algorithms(self):
69
81
self .assertEqual (ret , True )
70
82
line = lines .readline ().strip ('\n ' ).strip ()
71
83
72
- def test_OTBParameterChoice (self ):
84
+ def test_parameterAs_ScriptMode (self ):
85
+ """
86
+ This test will pass an instance of QgsCoordinateReferenceSystem for 'epsg' parameter
87
+ of otb::Rasterization. There is same test in otb_algorithm_tests.yaml which passes
88
+ an instance of str for epsg parameter.
89
+ """
90
+ outdir = tempfile .mkdtemp ()
91
+ self .cleanup_paths .append (outdir )
92
+ parameters = {
93
+ 'in' : os .path .join (AlgorithmsTestBase .processingTestDataPath (), 'polys.gml' ),
94
+ 'epsg' : QgsCoordinateReferenceSystem ('EPSG:4326' ),
95
+ 'spx' : 1.0 ,
96
+ 'spy' : 1.0 ,
97
+ 'outputpixeltype' : 1 ,
98
+ 'out' : os .path .join (outdir , 'raster.tif' )
99
+ }
100
+ context = QgsProcessingContext ()
101
+ context .setProject (QgsProject .instance ())
102
+ feedback = QgsProcessingFeedback ()
103
+ results = processing .run ('otb:Rasterization' , parameters , None , feedback )
104
+ result_lyr = QgsProcessingUtils .mapLayerFromString (results ['out' ], context )
105
+ self .assertTrue (result_lyr .isValid ())
106
+
107
+ def test_OTBParameterChoiceExists (self ):
108
+ """
109
+ This test is here to know if we have change `type()` method of :class: `OtbParameterChoice`
110
+ That value is used by Otb when it creates descriptor files. So changes to this string must be test
111
+ in a unit-test.
112
+ """
73
113
alg_smoothing = OtbAlgorithm ('Image Filtering' , 'Smoothing' , os .path .join (self .descrFolder , 'Smoothing.txt' ))
74
114
found = False
75
115
for param in alg_smoothing .parameterDefinitions ():
@@ -80,6 +120,11 @@ def test_OTBParameterChoice(self):
80
120
self .assertEqual (found , True )
81
121
82
122
def test_OTBParameterChoice_Gui (self ):
123
+ """
124
+ This test is similar to GuiTests in processing that is done on other parameter widget in processing
125
+ Main difference is this test uses create_wrapper_from_metadata() rather than create_wrapper_from_class()
126
+ like rest of processing widgets.
127
+ """
83
128
param = OtbParameterChoice ('test' )
84
129
85
130
alg = QgsApplication .processingRegistry ().createAlgorithmById ('otb:Smoothing' )
@@ -134,6 +179,9 @@ def tearDownClass(cls):
134
179
shutil .rmtree (path )
135
180
136
181
def test_definition_file (self ):
182
+ """
183
+ return name of yaml file containing test definitions
184
+ """
137
185
print ("OTB_INSTALL_DIR = '{}'" .format (OTB_INSTALL_DIR ))
138
186
return 'otb_algorithm_tests.yaml'
139
187
0 commit comments