Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions python/plugins/sextante/tests/RunAlgTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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

class RunAlgTest(ParametrizedTestCase):
'''This test takes a reduced set of algorithms and executes them in different ways, changing
parameters such as whether to use thread or not, the output file format, etc.
Basically, it uses some algorithms to test other parts of SEXTANTE, not the algorithms themselves'''

def getOutputFile(self):
if self.useTempFiles:
return None
else:
return SextanteUtils.getTempFilename('shp')

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)

def suite():
suite = unittest.TestSuite()
suite.addTest(ParametrizedTestCase.parametrize(RunAlgTest, False))
suite.addTest(ParametrizedTestCase.parametrize(RunAlgTest, True))
return suite

def runtests():
result = unittest.TestResult()
testsuite = suite()
testsuite.run(result)
return result

59 changes: 59 additions & 0 deletions python/plugins/sextante/tests/ScriptTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import sextante
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table
from sextante.core.QGisLayers import QGisLayers

class ScriptTest(unittest.TestCase):

def test_scriptcreatetilingfromvectorlayer(self):
outputs=sextante.runalg("script:createtilingfromvectorlayer",union(),10,None)
output=outputs['polygons']
layer=QGisLayers.getObjectFromUri(output, True)
fields=layer.pendingFields()
expectednames=['longitude','latitude']
expectedtypes=['Real','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(10, len(features))
feature=features.next()
attrs=feature.attributes()
expectedvalues=["270761.415396242","4458948.29588823"]
values=[str(attr.toString()) for attr in attrs]
self.assertEqual(expectedvalues, values)
wkt='POLYGON((270755.54427424 4458901.23378639,270755.54427424 4458995.35799007,270767.28651824 4458995.35799007,270767.28651824 4458901.23378639,270755.54427424 4458901.23378639))'
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

def test_scripthexgridfromlayerbounds(self):
outputs=sextante.runalg("script:hexgridfromlayerbounds",polygons(),10,None)
output=outputs['grid']
layer=QGisLayers.getObjectFromUri(output, True)
fields=layer.pendingFields()
expectednames=['longitude','latitude']
expectedtypes=['Real','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(117, len(features))
feature=features.next()
attrs=feature.attributes()
expectedvalues=["270765.621834001","4458907.27146471"]
values=[str(attr.toString()) for attr in attrs]
self.assertEqual(expectedvalues, values)
wkt='POLYGON((270771.39533669 4458907.27146471,270768.50858535 4458902.27146471,270762.73508265 4458902.27146471,270759.84833131 4458907.27146471,270762.73508265 4458912.27146471,270768.50858535 4458912.27146471,270771.39533669 4458907.27146471))'
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

def suite():
suite = unittest.makeSuite(ScriptTest, 'test')
return suite

def runtests():
result = unittest.TestResult()
testsuite = suite()
testsuite.run(result)
return result