Skip to content
Permalink
Browse files

[sextante] more tests

  • Loading branch information
volaya committed Apr 9, 2013
1 parent c57a1a4 commit 8c714a9d41ba21ad15495021b10684695be82877
@@ -1,5 +1,5 @@
NAME:Model with algorithms not in running order
GROUP:[Test algorithms]
GROUP:[Test models]
PARAMETER:ParameterRaster|RASTERLAYER_RASTER|raster|False
120.0,60.0
VALUE:HARDCODEDPARAMVALUE_MINSLOPE_1===0.01
@@ -23,7 +23,7 @@ None
None
None
-1|HARDCODEDPARAMVALUE_CONVERGENCE_0
None
catchment area
None
None
None
@@ -1,4 +1,3 @@
##[Test scripts]=group
##number=output number

number = 10
@@ -1,21 +1,18 @@
from sextante.core.Sextante import Sextante
from sextante.modeler.ModelerAlgorithm import ModelerAlgorithm
import sextante
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table, polygonsGeoJson, raster
from sextante.core.QGisLayers import QGisLayers

def testAlg(algname, *args):
class GeoAlgorithmTest(unittest.TestCase):
pass

#test simple execution
alg = Sextante.runAlgorithm(algname, None, *args)
assert alg is not None
def suite():
suite = unittest.makeSuite(GeoAlgorithmTest, 'test')
return suite

out = alg.getOutputValuesAsDictionary()

return out

#test execution in a model

#===========================================================================
# model = ModelerAlgorithm()
# model.addAlgorithm(alg, parametersMap, valuesMap, outputsMap, dependencies)
#===========================================================================

#test
def runtests():
result = unittest.TestResult()
testsuite = suite()
testsuite.run(result)
return result
@@ -8,25 +8,40 @@
from osgeo.gdalconst import GA_ReadOnly
from sextante.modeler import ModelerAlgorithmProvider
from sextante.modeler.ModelerAlgorithm import ModelerAlgorithm
from sextante.modeler.Providers import Providers

class ModelerAlgorithmTest(unittest.TestCase):

def testCreateModel(self):
pass

def testEditModelParameter(self):
pass

def testEditModelAlgorithm(self):
pass

def testRemoveAlgorithm(self):
folder = os.path.join(os.path.dirname(ModelerAlgorithmProvider.__file__), "models")
modelfile = os.path.join(folder, "noinputs.model")
model = ModelerAlgorithm()
model.openModel(modelfile)
model.provider = Providers.providers['model']
self.assertTrue(2, len(model.algs))
self.assertFalse(model.removeAlgorithm(0))
self.assertTrue(model.removeAlgorithm(len(model.algs) - 1));
outputs = model.execute(None)
self.assertEquals(2, len(outputs))
output=outputs['SAVENAME_ALG0']
layer=QGisLayers.getObjectFromUri(output, True)
self.assertIsNone(layer)
from threading import settrace

import sys
sys.path.append("D:\eclipse\plugins\org.python.pydev_2.6.0.2012062818\pysrc")
from pydevd import *
settrace()

model.execute(None)
outputs = model.outputs
self.assertEquals(1, len(outputs))
output=outputs[0].value
self.assertTrue(os.path.exists(output))

def testRemoveParameter(self):
folder = os.path.join(os.path.dirname(ModelerAlgorithmProvider.__file__), "models")
@@ -67,8 +82,6 @@ def testComputingDependecies(self):
self.assertEquals([3,2,1,0], depends)




'''The following tests correspond to example models'''

def test_modelersagagrass(self):
@@ -894,21 +894,18 @@ def test_qgisexportaddgeometrycolumnspolygons(self):
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

def test_qgisstatisticsbycategories(self):
outputs=sextante.runalg("qgis:statisticsbycategories",points2(),"POLY_NUM_A","POLY_ST_B",None)
outputs=sextante.runalg("qgis:statisticsbycategories",points2(),"POLY_NUM_A","POLY_ST_B", None)
output=outputs['OUTPUT']
layer=QGisLayers.getObjectFromUri(output, True)
fields=layer.pendingFields()
expectednames=['Category','min','max','mean','stddev']
expectedtypes=['String','Real','Real','Real','Real']
expectednames=['category','min','max','mean','stddev']
names=[str(f.name()) for f in fields]
types=[str(f.typeName()) for f in fields]
self.assertEqual(expectednames, names)
self.assertEqual(expectedtypes, types)
self.assertEqual(expectednames, names)
features=sextante.getfeatures(layer)
self.assertEqual(3, len(features))
feature=features.next()
attrs=feature.attributes()
expectedvalues=["NULL","1.1","2.2","1.925","0.55"]
expectedvalues=["","1.1","2.2","1.925","0.55"]
values=[str(attr.toString()) for attr in attrs]
self.assertEqual(expectedvalues, values)

@@ -37,6 +37,7 @@ def test_SagaVectorAlgorithmWithSelection(self):
selected = [feature.id()]
layer.setSelectedFeatures(selected)
outputs=sextante.runalg("saga:polygoncentroids",polygons2(),True,None)
layer.setSelectedFeatures([])
output=outputs['CENTROIDS']
layer=QGisLayers.getObjectFromUri(output, True)
fields=layer.pendingFields()
@@ -53,7 +54,7 @@ def test_SagaVectorAlgorithmWithSelection(self):
expectedvalues=["2","1","string a"]
values=[str(attr.toString()) for attr in attrs]
self.assertEqual(expectedvalues, values)
wkt='POINT(270820.58189697 4458968.73298999)'
wkt='POINT(270806.69221918 4458924.97720492)'
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
@@ -63,6 +64,7 @@ def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
selected = [feature.id()]
layer.setSelectedFeatures(selected)
outputs=sextante.runalg("saga:polygoncentroids",polygonsGeoJson(),True, SextanteUtils.getTempFilename("geojson"))
layer.setSelectedFeatures([])
output=outputs['CENTROIDS']
layer=QGisLayers.getObjectFromUri(output, True)
fields=layer.pendingFields()
@@ -81,17 +83,14 @@ def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self):
self.assertEqual(expectedvalues, values)
wkt='POINT(270787.49991451 4458955.46775295)'
self.assertEqual(wkt, str(feature.geometry().exportToWkt()))

def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self):
outputs=sextante.runalg("saga:convergenceindex",raster(),0,0,None)
output=outputs['RESULT']
self.assertTrue(os.path.isfile(output))
dataset=gdal.Open(output, GA_ReadOnly)
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,485390137)




self.assertEqual(strhash,-807227462)


def suite():
@@ -0,0 +1,26 @@
'''Convenience module to create a test suite will all SEXTANTE tests'''
import unittest
from sextante.tests import QgisAlgsTest
from sextante.tests import ParametersTest
from sextante.tests import ModelerAlgorithmTest
from sextante.tests import SextanteToolsTest
from sextante.tests import ScriptTest
from sextante.tests import SagaTest
from sextante.tests import GeoAlgorithmTest

def suite():
suite = unittest.TestSuite()
suite.addTests(QgisAlgsTest.suite())
suite.addTests(ModelerAlgorithmTest.suite())
suite.addTests(SagaTest.suite())
suite.addTests(ScriptTest.suite())
suite.addTests(SextanteToolsTest.suite())
#suite.addTests(ParametersTest.suite())
suite.addTests(GeoAlgorithmTest.suite())
return suite

def runtests():
result = unittest.TestResult()
testsuite = suite()
testsuite.run(result)
return result
@@ -0,0 +1,59 @@
import sextante
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table, polygonsGeoJson, raster
from sextante.core import Sextante
from sextante.tools.vector import getAttributeValues

class SextanteToolsTest(unittest.TestCase):
'''tests the method imported when doing an "import sextante", and also in sextante.tools.
They are mostly convenience tools'''

def test_getobject(self):
layer = sextante.getobject(points());
self.assertIsNotNone(layer)
layer = sextante.getobject("points");
self.assertIsNotNone(layer)

def test_runandload(self):
sextante.runandload("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS", None)
layer = Sextante.getObjectFromName("Result")
self.assertIsNotNone(layer)

def test_featuresWithoutSelection(self):
layer = sextante.getobject(points())
features = sextante.getfeatures(layer)
self.assertEqual(12, len(features))

def test_featuresWithSelection(self):
layer = sextante.getobject(points())
feature = layer.getFeatures().next()
selected = [feature.id()]
layer.setSelectedFeatures(selected)
features = sextante.getfeatures(layer)
self.assertEqual(1, len(features))
layer.setSelectedFeatures([])

def test_attributeValues(self):
layer = sextante.getobject(points())
values = getAttributeValues(layer, "ID")
i = 1
for value in values['ID']:
self.assertEqual(int(i), int(value))
i+=1
self.assertEquals(13,i)

def test_extent(self):
pass



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

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

0 comments on commit 8c714a9

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