102 lines
3.6 KiB
Python
Raw Normal View History

2013-04-13 11:04:22 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
RunAlgTest.py
---------------------
Date : March 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'March 2013'
__copyright__ = '(C) 2013, Victor Olaya'
2013-04-13 11:04:22 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2013-04-13 11:04:22 +02:00
__revision__ = '$Format:%H$'
2013-03-27 00:55:38 +01:00
import unittest
import processing
from processing.tools import dataobjects
from processing.tools.system import getTempFilename
2013-03-27 00:55:38 +01:00
from processing.tests.TestData import points, polygons
2013-03-27 00:55:38 +01:00
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
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
class RunAlgTest(ParametrizedTestCase):
"""This test takes a reduced set of algorithms and executes them in
different ways, changing parameters such as whether to use temp
outputs, the output file format, etc.
Basically, it uses some algorithms to test other parts of the
Processign framework, not the algorithms themselves
"""
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def getOutputFile(self):
if self.useTempFiles:
return None
else:
return getTempFilename('shp')
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def test_qgiscountpointsinpolygon(self):
outputs = processing.runalg('qgis:countpointsinpolygon', polygons(),
points(), 'NUMPOINTS',
self.getOutputFile())
output = outputs['OUTPUT']
layer = dataobjects.getObjectFromUri(output, True)
fields = layer.pendingFields()
expectednames = ['ID', 'POLY_NUM_A', 'POLY_ST_A', 'NUMPOINTS']
expectedtypes = ['Integer', 'Real', 'String', 'Real']
names = [unicode(f.name()) for f in fields]
types = [unicode(f.typeName()) for f in fields]
2013-03-27 00:55:38 +01:00
self.assertEqual(expectednames, names)
self.assertEqual(expectedtypes, types)
features = processing.features(layer)
2013-03-27 00:55:38 +01:00
self.assertEqual(2, len(features))
feature = features.next()
attrs = feature.attributes()
expectedvalues = ['1', '1.1', 'string a', '6']
values = [unicode(attr) for attr in attrs]
2013-03-27 00:55:38 +01:00
self.assertEqual(expectedvalues, values)
2013-03-31 21:18:27 +02:00
2013-03-27 00:55:38 +01:00
def suite():
suite = unittest.TestSuite()
suite.addTest(ParametrizedTestCase.parametrize(RunAlgTest, False))
2013-03-31 21:18:27 +02:00
suite.addTest(ParametrizedTestCase.parametrize(RunAlgTest, True))
2013-03-27 00:55:38 +01:00
return suite
2013-03-27 00:55:38 +01:00
def runtests():
2013-03-31 21:18:27 +02:00
result = unittest.TestResult()
2013-03-27 00:55:38 +01:00
testsuite = suite()
testsuite.run(result)
return result