22
33"""
44***************************************************************************
5- test_algorithms .py
5+ AlgorithmsTest .py
66 ---------------------
77 Date : January 2016
88 Copyright : (C) 2016 by Matthias Kuhn
2727
2828import qgis
2929import os
30- import shutil
3130import yaml
3231import nose2
3332import gdal
3433import hashlib
3534import tempfile
35+ import re
3636
3737from osgeo .gdalconst import GA_ReadOnly
3838
4646 QgsMapLayerRegistry
4747)
4848
49- from qgis .testing import (
50- start_app ,
51- unittest
52- )
53-
5449from utilities import (
5550 unitTestDataPath
5651)
@@ -60,25 +55,13 @@ def processingTestDataPath():
6055 return os .path .join (os .path .dirname (__file__ ), 'testdata' )
6156
6257
63- class TestAlgorithms (unittest .TestCase ):
64-
65- @classmethod
66- def setUpClass (cls ):
67- start_app ()
68- from processing .core .Processing import Processing
69- Processing .initialize ()
70- cls .cleanup_paths = []
71-
72- @classmethod
73- def tearDownClass (cls ):
74- for path in cls .cleanup_paths :
75- shutil .rmtree (path )
58+ class AlgorithmsTest ():
7659
7760 def test_algorithms (self ):
7861 """
7962 This is the main test function. All others will be executed based on the definitions in testdata/algorithm_tests.yaml
8063 """
81- with open (os .path .join (processingTestDataPath (), 'algorithm_tests.yaml' ), 'r' ) as stream :
64+ with open (os .path .join (processingTestDataPath (), self . test_definition_file () ), 'r' ) as stream :
8265 algorithm_tests = yaml .load (stream )
8366
8467 for algtest in algorithm_tests ['tests' ]:
@@ -104,8 +87,8 @@ def check_algorithm(self, name, defs):
10487 for r , p in defs ['results' ].iteritems ():
10588 alg .setOutputValue (r , self .load_result_param (p ))
10689
107- self .assertTrue (AlgorithmExecutor .runalg (alg ))
10890 print (alg .getAsCommand ())
91+ self .assertTrue (AlgorithmExecutor .runalg (alg ))
10992 self .check_results (alg .getOutputValuesAsDictionary (), defs ['results' ])
11093
11194 def load_params (self , params ):
@@ -133,21 +116,27 @@ def load_param(self, param):
133116 # No type specified, use whatever is there
134117 return param
135118
136- raise KeyError ("Unknown type '{}' specified for parameter '{}' " .format (param ['type' ], param [ 'name ' ]))
119+ raise KeyError ("Unknown type '{}' specified for parameter" .format (param ['type' ]))
137120
138121 def load_result_param (self , param ):
139122 """
140123 Loads a result parameter. Creates a temporary destination where the result should go to and returns this location
141124 so it can be sent to the algorithm as parameter.
142125 """
143- if param ['type' ] in ['vector' , 'file' ]:
126+ if param ['type' ] in ['vector' , 'file' , 'regex' ]:
144127 outdir = tempfile .mkdtemp ()
145128 self .cleanup_paths .append (outdir )
146129 basename = os .path .basename (param ['name' ])
147130 filepath = os .path .join (outdir , basename )
148131 return filepath
132+ elif param ['type' ] == 'rasterhash' :
133+ outdir = tempfile .mkdtemp ()
134+ self .cleanup_paths .append (outdir )
135+ basename = 'raster.tif'
136+ filepath = os .path .join (outdir , basename )
137+ return filepath
149138
150- raise KeyError ("Unknown type '{}' specified for parameter '{}' " .format (param ['type' ], param [ 'name ' ]))
139+ raise KeyError ("Unknown type '{}' specified for parameter" .format (param ['type' ]))
151140
152141 def load_layer (self , param ):
153142 """
@@ -188,10 +177,7 @@ def check_results(self, results, expected):
188177
189178 result_lyr = QgsVectorLayer (results [id ], id , 'ogr' )
190179
191- try :
192- compare = expected_result ['compare' ]
193- except KeyError :
194- compare = {}
180+ compare = expected_result .get ('compare' , {})
195181
196182 self .assertLayersEqual (expected_lyr , result_lyr , compare = compare )
197183
@@ -205,6 +191,12 @@ def check_results(self, results, expected):
205191 result_filepath = results [id ]
206192
207193 self .assertFilesEqual (expected_filepath , result_filepath )
194+ elif 'regex' == expected_result ['type' ]:
195+ with open (results [id ], 'r' ) as file :
196+ data = file .read ()
197+
198+ for rule in expected_result .get ('rules' , []):
199+ self .assertRegexpMatches (data , rule )
208200
209201
210202if __name__ == '__main__' :
0 commit comments