15 changes: 10 additions & 5 deletions python/plugins/sextante/core/Sextante.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
from sextante.gui.AlgorithmExecutor import AlgorithmExecutor
from sextante.gui.RenderingStyles import RenderingStyles
from sextante.gui.SextantePostprocessing import SextantePostprocessing
from sextante.gui.UnthreadedAlgorithmExecutor import UnthreadedAlgorithmExecutor,\
SilentProgress
from sextante.gui.UnthreadedAlgorithmExecutor import UnthreadedAlgorithmExecutor
from sextante.core.SilentProgress import SilentProgress
from sextante.modeler.Providers import Providers
from sextante.modeler.ModelerAlgorithmProvider import ModelerAlgorithmProvider
from sextante.modeler.ModelerOnlyAlgorithmProvider import ModelerOnlyAlgorithmProvider
Expand Down Expand Up @@ -384,13 +384,15 @@ def runalg(algOrName, *args):
return alg.getOutputValuesAsDictionary()

def runandload(name, *args):
Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)
return Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)

def extent(layers):
first = True
for layer in layers:
if not isinstance(layer, (QgsRasterLayer, QgsVectorLayer)):
layer = QGisLayers.getObjectFromUri(layer)
if layer is None:
continue
if first:
xmin = layer.extent().xMinimum()
xmax = layer.extent().xMaximum()
Expand All @@ -402,7 +404,10 @@ def extent(layers):
ymin = min(ymin, layer.extent().yMinimum())
ymax = max(ymax, layer.extent().yMaximum())
first = False
return str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)
if first:
return "0,0,0,0"
else:
return str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)

def getObjectFromName(name):
layers = QGisLayers.getAllLayers()
Expand All @@ -411,7 +416,7 @@ def getObjectFromName(name):
return layer

def getObjectFromUri(uri):
return QGisLayers.getObjectFromUri(uri, False)
return QGisLayers.getObjectFromUri(uri, True)

def getobject(uriorname):
ret = getObjectFromName(uriorname)
Expand Down
19 changes: 19 additions & 0 deletions python/plugins/sextante/core/SilentProgress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SilentProgress():

def setText(self, text):
pass

def setPercentage(self, i):
pass

def setInfo(self, _):
pass

def setCommand(self, _):
pass

def setDebugInfo(self, _):
pass

def setConsoleInfo(self, _):
pass
2 changes: 1 addition & 1 deletion python/plugins/sextante/gui/BatchProcessingDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def accept(self):
self.progress.setMaximum(len(self.algs))
for alg in self.algs:
self.setBaseText("Processing algorithm " + str(i+1) + "/" + str(len(self.algs)) + "...")
if UnthreadedAlgorithmExecutor.runalg(alg, self):#SilentProgress()):
if UnthreadedAlgorithmExecutor.runalg(alg, self):
#self.progress.setValue(i)
#self.loadHTMLResults(alg, i)
if self.load[i]:
Expand Down
21 changes: 1 addition & 20 deletions python/plugins/sextante/gui/UnthreadedAlgorithmExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteUtils import SextanteUtils
from sextante.gui.SextantePostprocessing import SextantePostprocessing
from sextante.core.SilentProgress import SilentProgress
import traceback

class UnthreadedAlgorithmExecutor:
Expand Down Expand Up @@ -91,23 +92,3 @@ def runalgIterating(alg,paramToIter,progress):

return True


class SilentProgress():

def setText(self, text):
pass

def setPercentage(self, i):
pass

def setInfo(self, _):
pass

def setCommand(self, _):
pass

def setDebugInfo(self, _):
pass

def setConsoleInfo(self, _):
pass
4 changes: 2 additions & 2 deletions python/plugins/sextante/modeler/models/notinorder.model
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,7 @@ None
None
None
-1|HARDCODEDPARAMVALUE_CONVERGENCE_0
None
catchment area
None
None
None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
##[Test scripts]=group
##number=output number

number = 10
33 changes: 15 additions & 18 deletions python/plugins/sextante/tests/GeoAlgorithmTest.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 20 additions & 7 deletions python/plugins/sextante/tests/ModelerAlgorithmTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 4 additions & 7 deletions python/plugins/sextante/tests/QgisAlgsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 5 additions & 6 deletions python/plugins/sextante/tests/SagaTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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()
Expand All @@ -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():
Expand Down
26 changes: 26 additions & 0 deletions python/plugins/sextante/tests/SextanteTests.py
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions python/plugins/sextante/tests/SextanteToolsTest.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, 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