1
+ from sextante .core .Sextante import Sextante
2
+ from sextante .outputs .OutputNumber import OutputNumber
3
+ from sextante .outputs .OutputString import OutputString
4
+ from sextante .outputs .OutputRaster import OutputRaster
5
+ from osgeo import gdal
6
+ from osgeo .gdalconst import GA_ReadOnly
7
+ from sextante .core .QGisLayers import QGisLayers
8
+ from sextante .outputs .OutputVector import OutputVector
9
+
10
+ def createTest (item ):
11
+ s = ""
12
+ tokens = item .entry .text [len ("sextante.runalg(" ):- 1 ].split ("," )
13
+ cmdname = tokens [0 ][1 :- 1 ];
14
+ methodname = "test_" + cmdname .replace (":" ,"" )
15
+ s += "def " + methodname + "():\n "
16
+ alg = Sextante .getAlgorithm (cmdname )
17
+ execcommand = "sextante.runalg("
18
+ i = 0
19
+ for token in tokens :
20
+ if i < alg .getVisibleParametersCount ():
21
+ execcommand += token + ","
22
+ else :
23
+ execcommand += "None,"
24
+ i += 1
25
+ s += "\t outputs=" + execcommand [:- 1 ] + ")\n "
26
+
27
+ i = - 1 * len (alg .outputs )
28
+ for out in alg .outputs :
29
+ filename = tokens [i ][1 :- 1 ]
30
+ s += "\t output=outputs['" + out .name + "']\n "
31
+ if isinstance (out , (OutputNumber , OutputString )):
32
+ s += "self.assertTrue(" + str (out ) + ", output)\n "
33
+ if isinstance (out , OutputRaster ):
34
+ dataset = gdal .Open (filename , GA_ReadOnly )
35
+ array = dataset .ReadAsArray (1 )
36
+ s += "\t self.assertTrue(os.path.isfile(output))\n "
37
+ s += "\t self.assertEqual(hashraster(output)," + str (hash (array )) + ")\n "
38
+ if isinstance (out , OutputVector ):
39
+ layer = Sextante .getObject (filename )
40
+ fields = layer .pendingFields ()
41
+ s += "\t layer=sextante.getobject(output)\n "
42
+ s += "\t fields=layer.pendingFields()\n "
43
+ s += "\t expectednames=[" + "," .join ([str (f .name ()) for f in fields ]) + "]\n "
44
+ s += "\t expectedtypes=[" + "," .join ([str (f .typeName ()) for f in fields ]) + "]\n "
45
+ s += "\t names=[str(f.name()) for f in fields]\n "
46
+ s += "\t types=[str(f.typeName()) for f in fields]\n "
47
+ s += "\t self.assertEqual(exceptednames, names)\n "
48
+ s += "\t self.assertEqual(exceptedtypes, types)\n "
49
+ features = QGisLayers .features (layer )
50
+ numfeat = len (features )
51
+ s += "\t features=sextante.getfeatures(layer))\n "
52
+ s += "\t self.assertEqual(" + str (numfeat ) + ", len(features)\n "
53
+ if numfeat > 0 :
54
+ feature = features .next ()
55
+ attrs = feature .attributes ()
56
+ s += "\t feature=features.next()\n "
57
+ s += "\t attrs=feature.attributes()\n "
58
+ s += "\t expectedvalues=[" + "," .join ([str (attr .toString ()) for attr in attrs ]) + "]\n "
59
+ s += "\t values=[str(attr.toString()) for attr in attrs]\n "
60
+ s += "\t self.assertEqual(exceptedtypes, types)\n "
61
+
62
+ dlg = ShowTestDialog (s )
63
+ dlg .exec_ ()
64
+
65
+ from PyQt4 import QtCore , QtGui
66
+ from PyQt4 .QtCore import *
67
+ from PyQt4 .QtGui import *
68
+
69
+ class ShowTestDialog (QtGui .QDialog ):
70
+ def __init__ (self , s ):
71
+ QtGui .QDialog .__init__ (self )
72
+ self .setModal (True )
73
+ self .resize (600 ,400 )
74
+ self .setWindowTitle ("Unit test" )
75
+ layout = QVBoxLayout ()
76
+ self .text = QtGui .QTextEdit ()
77
+ self .text .setEnabled (True )
78
+ self .text .setText (s )
79
+ layout .addWidget (self .text )
80
+ self .setLayout (layout )
81
+ QtCore .QMetaObject .connectSlotsByName (self )
82
+
83
+
84
+
0 commit comments