Skip to content

Commit

Permalink
[sextante] added tools to ease creating unit test for algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Feb 17, 2013
1 parent 7c67101 commit 7275525
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
32 changes: 29 additions & 3 deletions python/plugins/sextante/gui/HistoryDialog.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from sextante.gui import TestTools

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand All @@ -25,11 +26,10 @@

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from sextante.core.SextanteLog import SextanteLog

from sextante.ui.ui_DlgHistory import Ui_DlgHistory


class HistoryDialog(QDialog, Ui_DlgHistory):
def __init__(self):
QDialog.__init__(self)
Expand All @@ -48,9 +48,11 @@ def __init__(self):
self.clearButton.setToolTip(self.tr("Clear history and log"))
self.buttonBox.addButton(self.clearButton, QDialogButtonBox.ActionRole)

self.tree.doubleClicked.connect(self.executeAlgorithm)
self.tree.doubleClicked.connect(self.executeAlgorithm)
self.tree.currentItemChanged.connect(self.changeText)
self.clearButton.clicked.connect(self.clearLog)

self.tree.customContextMenuRequested.connect(self.showPopupMenu)

self.fillTree()

Expand Down Expand Up @@ -83,6 +85,30 @@ def changeText(self):
item = self.tree.currentItem()
if isinstance(item, TreeLogEntryItem):
self.text.setText(item.entry.text.replace("|","\n"))



def createTest(self):
item = self.tree.currentItem()
if isinstance(item, TreeLogEntryItem):
if item.isAlg:
TestTools.createTest(item)







def showPopupMenu(self,point):
item = self.tree.currentItem()
if isinstance(item, TreeLogEntryItem):
if item.isAlg:
popupmenu = QMenu()
createTestAction = QAction(self.tr("Create test"), self.tree)
createTestAction.triggered.connect(self.createTest)
popupmenu.addAction(createTestAction)
popupmenu.exec_(self.tree.mapToGlobal(point))

class TreeLogEntryItem(QTreeWidgetItem):
def __init__(self, entry, isAlg):
Expand Down
84 changes: 84 additions & 0 deletions python/plugins/sextante/gui/TestTools.py
@@ -0,0 +1,84 @@
from sextante.core.Sextante import Sextante
from sextante.outputs.OutputNumber import OutputNumber
from sextante.outputs.OutputString import OutputString
from sextante.outputs.OutputRaster import OutputRaster
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

def createTest(item):
s = ""
tokens = item.entry.text[len("sextante.runalg("):-1].split(",")
cmdname = tokens[0][1:-1];
methodname = "test_" + cmdname.replace(":","")
s += "def " + methodname + "():\n"
alg = Sextante.getAlgorithm(cmdname)
execcommand = "sextante.runalg("
i = 0
for token in tokens:
if i < alg.getVisibleParametersCount():
execcommand+=token + ","
else:
execcommand+="None,"
i+=1
s += "\toutputs=" + execcommand[:-1] + ")\n"

i = -1 * len(alg.outputs)
for out in alg.outputs:
filename = tokens[i][1:-1]
s+="\toutput=outputs['" + out.name + "']\n"
if isinstance(out, (OutputNumber, OutputString)):
s+="self.assertTrue(" + str(out) + ", output)\n"
if isinstance(out, OutputRaster):
dataset = gdal.Open(filename, GA_ReadOnly)
array = dataset.ReadAsArray(1)
s+="\tself.assertTrue(os.path.isfile(output))\n"
s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n"
if isinstance(out, OutputVector):
layer = Sextante.getObject(filename)
fields = layer.pendingFields()
s+="\tlayer=sextante.getobject(output)\n"
s+="\tfields=layer.pendingFields()\n"
s+="\texpectednames=[" + ",".join([str(f.name()) for f in fields]) + "]\n"
s+="\texpectedtypes=[" + ",".join([str(f.typeName()) for f in fields]) + "]\n"
s+="\tnames=[str(f.name()) for f in fields]\n"
s+="\ttypes=[str(f.typeName()) for f in fields]\n"
s+="\tself.assertEqual(exceptednames, names)\n"
s+="\tself.assertEqual(exceptedtypes, types)\n"
features = QGisLayers.features(layer)
numfeat = len(features)
s+="\tfeatures=sextante.getfeatures(layer))\n"
s+="\tself.assertEqual(" + str(numfeat) + ", len(features)\n"
if numfeat > 0:
feature = features.next()
attrs = feature.attributes()
s+="\tfeature=features.next()\n"
s+="\tattrs=feature.attributes()\n"
s+="\texpectedvalues=[" + ",".join([str(attr.toString()) for attr in attrs]) + "]\n"
s+="\tvalues=[str(attr.toString()) for attr in attrs]\n"
s+="\tself.assertEqual(exceptedtypes, types)\n"

dlg = ShowTestDialog(s)
dlg.exec_()

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ShowTestDialog(QtGui.QDialog):
def __init__(self, s):
QtGui.QDialog.__init__(self)
self.setModal(True)
self.resize(600,400)
self.setWindowTitle("Unit test")
layout = QVBoxLayout()
self.text = QtGui.QTextEdit()
self.text.setEnabled(True)
self.text.setText(s)
layout.addWidget(self.text)
self.setLayout(layout)
QtCore.QMetaObject.connectSlotsByName(self)



1 change: 1 addition & 0 deletions python/plugins/sextante/ui/ui_DlgHistory.py
Expand Up @@ -29,6 +29,7 @@ def setupUi(self, DlgHistory):
self.tree.setObjectName(_fromUtf8("tree"))
self.tree.headerItem().setText(0, _fromUtf8("1"))
self.tree.header().setVisible(False)
self.tree.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.text = QtGui.QTextEdit(self.splitter)
self.text.setReadOnly(True)
self.text.setObjectName(_fromUtf8("text"))
Expand Down

0 comments on commit 7275525

Please sign in to comment.