QGIS/python/plugins/sextante/tests/RunAlgTest.py

64 lines
2.4 KiB
Python
Raw Normal View History

2013-03-27 00:55:38 +01:00
import sextante
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteUtils import SextanteUtils
class ParametrizedTestCase(unittest.TestCase):
def __init__(self, methodName='runTest', useTempFiles=False):
super(ParametrizedTestCase, self).__init__(methodName)
self.useTempFiles = useTempFiles
@staticmethod
def parametrize(testcase_klass, useTempFiles):
testloader = unittest.TestLoader()
testnames = testloader.getTestCaseNames(testcase_klass)
suite = unittest.TestSuite()
for name in testnames:
suite.addTest(testcase_klass(name, useTempFiles=useTempFiles))
return suite
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
class RunAlgTest(ParametrizedTestCase):
2013-03-31 21:18:27 +02:00
'''This test takes a reduced set of algorithms and executes them in different ways, changing
parameters such as whether to use temp outputs, the output file format, etc.
2013-03-27 00:55:38 +01:00
Basically, it uses some algorithms to test other parts of SEXTANTE, not the algorithms themselves'''
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def getOutputFile(self):
if self.useTempFiles:
return None
else:
return SextanteUtils.getTempFilename('shp')
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def test_qgiscountpointsinpolygon(self):
outputs=sextante.runalg("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS", self.getOutputFile())
output=outputs['OUTPUT']
layer=QGisLayers.getObjectFromUri(output, True)
fields=layer.pendingFields()
expectednames=['ID','POLY_NUM_A','POLY_ST_A','NUMPOINTS']
expectedtypes=['Integer','Real','String','Real']
names=[str(f.name()) for f in fields]
types=[str(f.typeName()) for f in fields]
self.assertEqual(expectednames, names)
self.assertEqual(expectedtypes, types)
features=sextante.getfeatures(layer)
self.assertEqual(2, len(features))
feature=features.next()
attrs=feature.attributes()
expectedvalues=["1","1.1","string a","6"]
values=[str(attr.toString()) for attr in attrs]
self.assertEqual(expectedvalues, values)
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def suite():
suite = unittest.TestSuite()
suite.addTest(ParametrizedTestCase.parametrize(RunAlgTest, False))
2013-03-31 21:18:27 +02:00
suite.addTest(ParametrizedTestCase.parametrize(RunAlgTest, True))
2013-03-27 00:55:38 +01:00
return suite
def runtests():
2013-03-31 21:18:27 +02:00
result = unittest.TestResult()
2013-03-27 00:55:38 +01:00
testsuite = suite()
testsuite.run(result)
return result