Skip to content

Commit ee63cff

Browse files
committedApr 25, 2019
[processing] Add 'project' to test definition and 'directory' output test
- for algorithms that produce directory output, it is possible to test that directory contents are exactly the same (recursively) - added possibility to have a project file loaded before an algorithm is run - documented the new additions (+ few existing ones)
1 parent 0bb701a commit ee63cff

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed
 

‎python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ def check_algorithm(self, name, defs):
8686
:param defs: A python dict containing a test algorithm definition
8787
"""
8888
self.vector_layer_params = {}
89-
QgsProject.instance().removeAllMapLayers()
89+
QgsProject.instance().clear()
90+
91+
if 'project' in defs:
92+
full_project_path = os.path.join(processingTestDataPath(), defs['project'])
93+
project_read_success = QgsProject.instance().read(full_project_path)
94+
self.assertTrue(project_read_success, 'Failed to load project file: ' + defs['project'])
9095

9196
if 'project_crs' in defs:
9297
QgsProject.instance().setCrs(QgsCoordinateReferenceSystem(defs['project_crs']))
@@ -212,6 +217,9 @@ def load_result_param(self, param):
212217
basename = 'raster.tif'
213218
filepath = os.path.join(outdir, basename)
214219
return filepath
220+
elif param['type'] == 'directory':
221+
outdir = tempfile.mkdtemp()
222+
return outdir
215223

216224
raise KeyError("Unknown type '{}' specified for parameter".format(param['type']))
217225

@@ -350,6 +358,11 @@ def check_results(self, results, context, params, expected):
350358
result_filepath = results[id]
351359

352360
self.assertFilesEqual(expected_filepath, result_filepath)
361+
elif 'directory' == expected_result['type']:
362+
expected_dirpath = self.filepath_from_param(expected_result)
363+
result_dirpath = results[id]
364+
365+
self.assertDirectoriesEqual(expected_dirpath, result_dirpath)
353366
elif 'regex' == expected_result['type']:
354367
with open(results[id], 'r') as file:
355368
data = file.read()

‎python/plugins/processing/tests/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@ OUTPUT:
194194
- 'Feature Count: 6'
195195
```
196196

197+
#### Directories
198+
199+
You can compare the content of an output directory by en expected result reference directory
200+
201+
```yaml
202+
OUTPUT_DIR:
203+
name: expected/tiles_xyz/test_1
204+
type: directory
205+
```
206+
207+
### Algorithm Context
208+
209+
There are few more definitions that can modify context of the algorithm - these can be specified at top level of test:
210+
211+
- `project` - will load a specified QGIS project file before running the algorithm. If not specified, algorithm will run with empty project
212+
- `project_crs` - overrides the default project CRS - e.g. `EPSG:27700`
213+
- `ellipsoid` - overrides the default project ellipsoid used for measurements - e.g. `GRS80`
214+
215+
197216
Running tests locally
198217
------------------
199218
```bash

‎python/testing/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import sys
3030
import difflib
3131
import functools
32+
import filecmp
3233

3334
from qgis.PyQt.QtCore import QVariant
3435
from qgis.core import QgsApplication, QgsFeatureRequest, NULL
@@ -196,6 +197,20 @@ def assertFilesEqual(self, filepath_expected, filepath_result):
196197
diff = list(diff)
197198
self.assertEqual(0, len(diff), ''.join(diff))
198199

200+
def assertDirectoriesEqual(self, dirpath_expected, dirpath_result):
201+
""" Checks whether both directories have the same content (recursively) and raises an assertion error if not. """
202+
dc = filecmp.dircmp(dirpath_expected, dirpath_result)
203+
dc.report_full_closure()
204+
205+
def _check_dirs_equal_recursive(dcmp):
206+
self.assertEqual(dcmp.left_only, [])
207+
self.assertEqual(dcmp.right_only, [])
208+
self.assertEqual(dcmp.diff_files, [])
209+
for sub_dcmp in dcmp.subdirs.values():
210+
_check_dirs_equal_recursive(sub_dcmp)
211+
212+
_check_dirs_equal_recursive(dc)
213+
199214
def assertGeometriesEqual(self, geom0, geom1, geom0_id='geometry 1', geom1_id='geometry 2', precision=14, topo_equal_check=False):
200215
self.checkGeometriesEqual(geom0, geom1, geom0_id, geom1_id, use_asserts=True, precision=precision, topo_equal_check=topo_equal_check)
201216

0 commit comments

Comments
 (0)
Please sign in to comment.