1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
# -*- coding: utf-8 -*-
"""
InaSAFE Disaster risk assessment tool developed by AusAid -
**QGIS plugin test suite.**
Contact : ole.moller.nielsen@gmail.com
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'tim@linfiniti.com'
__version__ = '0.3.0'
__date__ = '10/01/2011'
__copyright__ = \
'Copyright 2012, Australia Indonesia Facility for Disaster Reduction'
import os
import sys
import time
import itertools
import unittest
from PyQt4.QtGui import QWidget
from qgis.gui import QgsMapCanvas
from qgis.core import *
import processing
from processing.ProcessingPlugin import ProcessingPlugin
from processing.core.Processing import Processing
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.ParametersDialog import ParametersDialog
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.Providers import Providers
from processing.parameters.ParameterRaster import ParameterRaster
from processing.parameters.ParameterVector import ParameterVector
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterString import ParameterString
from processing.parameters.ParameterBoolean import ParameterBoolean
from processing.outputs.OutputRaster import OutputRaster
from processing.outputs.OutputVector import OutputVector
from qgis_interface import QgisInterface
from utilities_test import getQgisTestApp
# Add parent directory to path to make test aware of other modules
pardir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
sys.path.append(pardir)
sys.path.append( qgis.utils.sys_plugin_path )
(QGISAPP, CANVAS, IFACE, PARENT) = getQgisTestApp()
class bcolors:
INFO = '\033[94m'
WARNING = '\033[91m'
ENDC = '\033[0m'
class ProcessingPluginTest(unittest.TestCase):
"""Test suite for Processing QGis plugin."""
def test_createplugin(self):
"""Initialize plugin."""
self.processingplugin = ProcessingPlugin(IFACE)
self.assertIsNotNone(self.processingplugin)
def test_processing_alglist(self):
"""Test alglist"""
self.processingplugin = ProcessingPlugin(IFACE)
self.providerToAlgs = Processing.algs
self.assertTrue(self.providerToAlgs, 'Alg list')
class ProcessingProviderTestCase(unittest.TestCase):
def __init__(
self,
algId,
alg,
threaded,
dialog='none',
):
self.algId = algId
self.alg = alg
self.threaded = threaded
self.msg = 'ALG %s (%s %s)' % (self.algId, {True: 'threaded',
False: 'unthreaded'}[threaded], dialog)
unittest.TestCase.__init__(self, 'runalg_%s' % dialog)
def gen_test_parameters(self, alg, doSet=False):
b = False
for p in alg.parameters:
if isinstance(p, ParameterRaster):
l = IFACE.testRaster
if doSet:
p.setValue(l)
yield l
elif isinstance(p, ParameterVector):
l = IFACE.testVector
if doSet:
p.setValue(l)
yield l
elif isinstance(p, ParameterNumber):
n = 42
if p.max:
n = p.max
elif p.min:
n = p.min
if doSet:
p.setValue(n)
yield n
elif isinstance(p, ParameterString):
s = 'Test string'
if doSet:
p.setValue(s)
yield s
elif isinstance(p, ParameterBoolean):
b = not b
if doSet:
p.setValue(b)
yield b
else:
if doSet:
p.setValue(None)
yield None
i = 0
for o in alg.outputs:
if o.hidden:
continue
i = i + 1
outbasename = self.msg.replace('/', '-')
if isinstance(o, OutputRaster):
fn = 'outputs/%s - %i.tif' % (outbasename, i)
if doSet:
o.setValue(fn)
yield fn
elif isinstance(o, OutputVector):
fn = 'outputs/%s - %i.shp' % (outbasename, i)
if doSet:
o.setValue(fn)
yield fn
else:
if doSet:
o.setValue(None)
yield None
def setUp(self):
print
print bcolors.INFO, self.msg, bcolors.ENDC,
print 'Parameters: ', self.alg.parameters,
print 'Outputs: ', [out for out in self.alg.outputs
if not out.hidden],
self.args = list(self.gen_test_parameters(self.alg, True))
print ' => ', self.args, bcolors.WARNING,
def runalg_none(self):
result = processing.runalg(self.alg, *self.args)
print bcolors.ENDC
self.assertIsNotNone(result, self.msg)
if not result:
return
for p in result.values():
if isinstance(p, str):
self.assertTrue(os.path.exists(p), 'Output %s exists' % p)
def runalg_parameters(self):
dlg = self.alg.getCustomParametersDialog()
if not dlg:
dlg = ParametersDialog(self.alg)
# Hack to handle that hacky code...
dlg.setParamValues = lambda : True
dlg.show()
dlg.accept()
while not dlg.executed:
time.sleep(.5)
def tearDown(self):
print bcolors.ENDC
def algSuite(dialog='none', threaded=True, unthreaded=True,
provider=None, algName=None):
s = unittest.TestSuite()
for (provider, algs) in Processing.algs.items():
if not algs.items():
print bcolors.WARNING, 'WARNING: %s seems to provide no algs!' \
% provider,
print bcolors.ENDC
continue
(algId, alg) = algs.items()[-1]
if threaded:
s.addTest(ProcessingProviderTestCase(algId, alg, True, dialog))
if unthreaded:
s.addTest(ProcessingProviderTestCase(algId, alg, False, dialog))
return s
def modelSuite(modelFile, dialog='none', threaded=True, unthreaded=True):
s = unittest.TestSuite()
model = ModelerAlgorithm()
model.openModel(modelFile)
if model.provider is None:
# Might happen if model is opened from modeler dialog
model.provider = Providers.providers['model']
if threaded:
s.addTest(ProcessingProviderTestCase(modelFile, model, True, dialog))
if unthreaded:
s.addTest(ProcessingProviderTestCase(modelFile, model, False, dialog))
return s
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Processing test suite.')
parser.add_argument('-l', action='store_true',
help='Test processing loading only. Ignore further arguments.')
parser.add_argument('-m', dest='model', help='Test a particular model.',
default=None)
parser.add_argument('-d', dest='dialog', help='Test a particular dialog.',
default='none')
parser.add_argument('-t', dest='tOnly', action='store_true',
help='Enable threaded execution only.')
parser.add_argument('-u', dest='uOnly', action='store_true',
help='Enable unthreaded execution only.')
parser.add_argument('-r', dest='raster',
help='Use specified raster as input.',
default='data/raster')
parser.add_argument('-v', dest='vector',
help='Use specified vectro as input.',
default='data/vector')
args = parser.parse_args()
threaded = not args.uOnly or args.tOnly
unthreaded = not args.tOnly or args.uOnly
try:
loadSuite = \
unittest.TestLoader().loadTestsFromTestCase(ProcessingPluginTest)
unittest.TextTestRunner(verbosity=2).run(loadSuite)
if args.l:
exit(0)
if not os.path.exists(args.raster) or not os.path.exists(args.vector):
print 'No data under %s or %s. Run with -h argument for help' \
% (args.raster, args.vector)
exit(1)
if args.model:
unittest.TextTestRunner(verbosity=2).run(modelSuite(args.model
or 'data/model', args.dialog, threaded, unthreaded))
exit(0)
unittest.TextTestRunner(verbosity=2).run(algSuite(args.dialog,
threaded, unthreaded, args.dialog))
except KeyboardInterrupt:
print bcolors.ENDC, 'Test interrupted.'
|