Skip to content

Commit 70be3aa

Browse files
Rashad Kanavathnyalldawson
Rashad Kanavath
authored andcommitted
add unit-test for passing values other than type str
1 parent 9a6a70a commit 70be3aa

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

python/plugins/processing/tests/OtbAlgorithmsTest.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
import hashlib
3232
import shutil
3333
import nose2
34+
import tempfile
3435
from qgis.core import (QgsProcessingParameterNumber,
3536
QgsApplication,
37+
QgsMapLayer,
38+
QgsProject,
39+
QgsProcessingContext,
40+
QgsProcessingUtils,
41+
QgsProcessingFeedback,
3642
QgsProcessingParameterDefinition)
3743
from qgis.testing import start_app, unittest
3844
from processing.core.ProcessingConfig import ProcessingConfig, Setting
@@ -46,12 +52,18 @@
4652
from processing.algs.otb.OtbChoiceWidget import OtbParameterChoice, OtbChoiceWidgetWrapper
4753
import AlgorithmsTestBase
4854

55+
import processing
4956
OTB_INSTALL_DIR = os.environ.get('OTB_INSTALL_DIR')
5057

5158

5259
class TestOtbAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
5360

5461
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+
"""
5567
algs_txt = os.path.join(self.descrFolder, 'algs.txt')
5668
with open(algs_txt) as lines:
5769
line = lines.readline().strip('\n').strip()
@@ -69,7 +81,35 @@ def test_init_algorithms(self):
6981
self.assertEqual(ret, True)
7082
line = lines.readline().strip('\n').strip()
7183

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+
"""
73113
alg_smoothing = OtbAlgorithm('Image Filtering', 'Smoothing', os.path.join(self.descrFolder, 'Smoothing.txt'))
74114
found = False
75115
for param in alg_smoothing.parameterDefinitions():
@@ -80,6 +120,11 @@ def test_OTBParameterChoice(self):
80120
self.assertEqual(found, True)
81121

82122
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+
"""
83128
param = OtbParameterChoice('test')
84129

85130
alg = QgsApplication.processingRegistry().createAlgorithmById('otb:Smoothing')
@@ -134,6 +179,9 @@ def tearDownClass(cls):
134179
shutil.rmtree(path)
135180

136181
def test_definition_file(self):
182+
"""
183+
return name of yaml file containing test definitions
184+
"""
137185
print("OTB_INSTALL_DIR = '{}'".format(OTB_INSTALL_DIR))
138186
return 'otb_algorithm_tests.yaml'
139187

python/plugins/processing/tests/testdata/otb_algorithm_tests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ tests:
6262
outputpixeltype: 3
6363
results:
6464
out:
65-
hash: 1f48ca4c1bce526086ed3b9a54c7d15cfc33ade657f1189c29d4e563
65+
hash: 987b1488daaa16d09ad6f40039ee85101c557354a4d821d3f31ad235
6666
type: rasterhash

0 commit comments

Comments
 (0)