Skip to content
Permalink
Browse files

Test cases for threaded & unthread algorithms. Check outputs exist.

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@281 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
cpolymeris@gmail.com
cpolymeris@gmail.com committed Jul 4, 2012
1 parent 08aaa2b commit 21ac92210b351bf78dabc4e9c1b4478fb36bc98d
Showing with 69 additions and 30 deletions.
  1. +69 −30 src/sextante/tests/test.py
@@ -27,6 +27,7 @@

sys.path.append('/usr/share/qgis/python/plugins')

import itertools
from qgis.gui import QgsMapCanvas
from qgis.core import *
from qgis_interface import QgisInterface
@@ -38,8 +39,11 @@
from sextante.parameters.ParameterRaster import ParameterRaster
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.parameters.ParameterString import ParameterString
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.outputs.OutputRaster import OutputRaster
from sextante.outputs.OutputVector import OutputVector
from sextante.core.SextanteConfig import SextanteConfig
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()

class DataProviderStub:
@@ -48,57 +52,92 @@ def __init__(self, uri):

class SextantePluginTest(unittest.TestCase):
"""Test suite for Sextante QGis plugin"""

def test_createplugin(self):
"""Initialize plugin"""
self.sextanteplugin = SextantePlugin(IFACE)
self.assertIsNotNone(self.sextanteplugin)

def test_sextante_alglist(self):
"""Test alglist"""
self.sextanteplugin = SextantePlugin(IFACE)
self.providerToAlgs = Sextante.algs
self.assertTrue(self.providerToAlgs, "Alg list")

class SextanteProviderTestCase(unittest.TestCase):
def __init__(self, algId, alg, threaded):
self.algId = algId
self.alg = alg
self.threaded = threaded
self.msg = "ALG %s (%s)" % (self.algId, { True: "threaded" , False : "unthreaded"}[threaded])
unittest.TestCase.__init__(self, "test_runalg")

def gen_test_parameters(self, alg):
b = False
for p in alg.parameters:
if isinstance(p, ParameterRaster):
l = QgsRasterLayer('.data/raster', "test raster")
l = QgsRasterLayer('data/raster', "test raster")
l.dataProvider = lambda: DataProviderStub('data/raster')
yield l
elif isinstance(p, ParameterVector):
l = QgsVectorLayer('.data/vector', "test vector")
l = QgsVectorLayer('data/vector', "test vector")
l.dataProvider = lambda: DataProviderStub('data/vector')
yield l
elif isinstance(p, ParameterNumber):
yield p.max
if p.max:
yield p.max
elif p.min:
yield p.min
yield 42
elif isinstance(p, ParameterString):
yield str()
elif isinstance(p, ParameterBoolean):
b = not b
yield b
else:
yield
i = 0;
for o in alg.outputs:
if o.hidden:
continue;
i = i + 1
outbasename = self.msg.replace('/', '-')
if isinstance(o, OutputRaster):
yield 'output%i.tif' % i
yield 'outputs/%s - %i.tif' % (outbasename, i)
elif isinstance(o, OutputVector):
yield 'output%i.shp' % i
yield 'outputs/%s - %i.shp' % (outbasename, i)
else:
yield

def test_0createplugin(self):
"""Initialize plugin"""
self.sextanteplugin = SextantePlugin(IFACE)
self.assertIsNotNone(self.sextanteplugin)

def test_1sextante_alglist(self):
"""Test alglist"""
self.sextanteplugin = SextantePlugin(IFACE)
self.providerToAlgs = Sextante.algs
self.assertTrue(self.providerToAlgs, "Alg list")

def test_runalg(self):
self.sextanteplugin = SextantePlugin(IFACE)
self.providerToAlgs = Sextante.algs
for provider, algs in self.providerToAlgs.items():
if not algs.items():
print "WARINING: %s seems to provide no algs!" % provider
continue
algId, alg = algs.items()[-1]
args = list(self.gen_test_parameters(alg))
print "Alg: ", algId
print alg.parameters, ' => ', args
result = Sextante.runalg(algId, *args)
self.assertIsNotNone(result, "Running directly %s" % algId)
print algId, " ok."
SextanteConfig.setSettingValue(SextanteConfig.USE_THREADS, self.threaded)
args = list(self.gen_test_parameters(self.alg))
print
print self.msg, "Parameters: ", self.alg.parameters, ' => ', args
result = Sextante.runalg(self.algId, *args)
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 algSuite():
s = unittest.TestSuite()
for provider, algs in Sextante.algs.items():
if not algs.items():
print "WARNING: %s seems to provide no algs!" % provider
continue
algId, alg = algs.items()[-1]
s.addTest(SextanteProviderTestCase(algId, alg, True))
s.addTest(SextanteProviderTestCase(algId, alg, False))
return s

if __name__ == '__main__':
unittest.main()
if not os.path.exists("data/raster") or not os.path.exists("data/vector"):
print "Please install test data under ./data/raster and ./data/vector."
exit(1)
loadSuite = unittest.TestLoader().loadTestsFromTestCase(SextantePluginTest)
unittest.TextTestRunner(verbosity=2).run(loadSuite)
unittest.TextTestRunner(verbosity=2).run(algSuite())

0 comments on commit 21ac922

Please sign in to comment.
You can’t perform that action at this time.