263 lines
8.7 KiB
Python
Raw Normal View History

# -*- 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 unittest
import qgis
import processing
2013-08-12 20:44:27 +02:00
from processing.ProcessingPlugin import ProcessingPlugin
from processing.core.Processing import Processing
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.Providers import Providers
2013-08-12 20:44:27 +02:00
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.core.outputs import OutputRaster
from processing.core.outputs import OutputVector
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'
2013-08-12 20:44:27 +02:00
class ProcessingPluginTest(unittest.TestCase):
"""Test suite for Processing QGis plugin."""
2012-12-10 00:12:07 +01:00
def test_createplugin(self):
"""Initialize plugin."""
2013-08-12 20:44:27 +02:00
self.processingplugin = ProcessingPlugin(IFACE)
self.assertIsNotNone(self.processingplugin)
2013-08-12 20:44:27 +02:00
def test_processing_alglist(self):
"""Test alglist"""
2013-08-12 20:44:27 +02:00
self.processingplugin = ProcessingPlugin(IFACE)
self.providerToAlgs = Processing.algs
self.assertTrue(self.providerToAlgs, 'Alg list')
2013-08-12 20:44:27 +02:00
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):
2012-12-10 00:12:07 +01:00
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,
2012-12-10 00:12:07 +01:00
def runalg_none(self):
2013-08-12 20:44:27 +02:00
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 = AlgorithmDialog(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
2012-12-10 00:12:07 +01:00
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:
2013-08-12 20:44:27 +02:00
s.addTest(ProcessingProviderTestCase(algId, alg, True, dialog))
if unthreaded:
2013-08-12 20:44:27 +02:00
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:
2013-08-12 20:44:27 +02:00
s.addTest(ProcessingProviderTestCase(modelFile, model, True, dialog))
if unthreaded:
2013-08-12 20:44:27 +02:00
s.addTest(ProcessingProviderTestCase(modelFile, model, False, dialog))
return s
if __name__ == '__main__':
import argparse
2013-08-12 20:44:27 +02:00
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')
2012-12-10 00:12:07 +01:00
args = parser.parse_args()
threaded = not args.uOnly or args.tOnly
unthreaded = not args.tOnly or args.uOnly
2012-12-10 00:12:07 +01:00
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.'