2013-04-13 11:04:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2016-01-07 17:14:05 +01:00
|
|
|
ParametersTest
|
2013-04-13 11:04:22 +02:00
|
|
|
---------------------
|
|
|
|
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-10-01 20:52:22 +03:00
|
|
|
|
2013-04-13 11:04:22 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-13 11:04:22 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-12-21 19:52:04 +01:00
|
|
|
import sys
|
|
|
|
from inspect import isclass
|
2016-01-07 17:14:05 +01:00
|
|
|
from qgis.testing import start_app, unittest
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
from processing.core.parameters import (Parameter,
|
|
|
|
ParameterBoolean,
|
|
|
|
ParameterCrs,
|
|
|
|
ParameterDataObject,
|
|
|
|
ParameterExtent,
|
|
|
|
ParameterFile,
|
|
|
|
ParameterFixedTable,
|
|
|
|
ParameterMultipleInput,
|
2016-02-22 17:28:57 +02:00
|
|
|
ParameterNumber,
|
2016-05-23 14:05:03 +03:00
|
|
|
ParameterPoint,
|
2016-06-02 12:14:59 +02:00
|
|
|
ParameterString,
|
|
|
|
ParameterVector,
|
2016-11-23 15:44:37 +10:00
|
|
|
ParameterTable,
|
2016-10-19 14:55:49 +03:00
|
|
|
ParameterTableField,
|
2016-11-11 12:59:22 +10:00
|
|
|
ParameterSelection,
|
2016-11-13 18:44:08 +10:00
|
|
|
ParameterExpression,
|
|
|
|
getParameterFromString)
|
2016-08-23 19:33:42 +03:00
|
|
|
from processing.tools import dataobjects
|
2016-11-02 12:53:16 +02:00
|
|
|
from processing.tests.TestData import points
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
from qgis.core import (QgsRasterLayer,
|
|
|
|
QgsVectorLayer)
|
|
|
|
|
2016-01-07 17:14:05 +01:00
|
|
|
start_app()
|
|
|
|
|
2015-11-29 18:42:10 -06:00
|
|
|
|
2016-10-19 14:55:49 +03:00
|
|
|
class ParameterSelectionTest(unittest.TestCase):
|
|
|
|
|
2016-11-05 21:14:26 +01:00
|
|
|
def testTupleOptions(self):
|
|
|
|
options = (
|
|
|
|
('o1', 'option1'),
|
|
|
|
('o2', 'option2'),
|
|
|
|
('o3', 'option3'))
|
|
|
|
|
|
|
|
optionalParameter = ParameterSelection('myName', 'myDesc', options, default='o1')
|
|
|
|
self.assertEqual(optionalParameter.value, 'o1')
|
|
|
|
optionalParameter.setValue('o2')
|
|
|
|
self.assertEqual(optionalParameter.value, 'o2')
|
|
|
|
|
|
|
|
optionalParameter = ParameterSelection('myName', 'myDesc', options, default=['o1', 'o2'], multiple=True)
|
|
|
|
self.assertEqual(optionalParameter.value, ['o1', 'o2'])
|
|
|
|
optionalParameter.setValue(['o2'])
|
|
|
|
self.assertEqual(optionalParameter.value, ['o2'])
|
|
|
|
|
2016-10-19 14:55:49 +03:00
|
|
|
|
2015-11-29 18:42:10 -06:00
|
|
|
class TestParameterFixedTable(unittest.TestCase):
|
|
|
|
|
|
|
|
def testTableToString(self):
|
|
|
|
table = [
|
|
|
|
['a0', 'a1', 'a2'],
|
|
|
|
['b0', 'b1', 'b2']
|
|
|
|
]
|
2016-05-03 09:18:27 +02:00
|
|
|
self.assertEqual(ParameterFixedTable.tableToString(table), 'a0,a1,a2,b0,b1,b2')
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
table = [['a0']]
|
2016-05-03 09:18:27 +02:00
|
|
|
self.assertEqual(ParameterFixedTable.tableToString(table), 'a0')
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
table = [[]]
|
2016-05-03 09:18:27 +02:00
|
|
|
self.assertEqual(ParameterFixedTable.tableToString(table), '')
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
|
|
|
|
class ParameterMultipleInputTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def testGetAsStringWhenRaster(self):
|
2016-08-23 19:33:42 +03:00
|
|
|
parameter = ParameterMultipleInput('myName', 'myDesc', datatype=dataobjects.TYPE_RASTER)
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
# With Path
|
|
|
|
self.assertEqual(parameter.getAsString('/some/path'), '/some/path')
|
|
|
|
|
|
|
|
# With Layer
|
|
|
|
layer = QgsRasterLayer('/path/to/myRaster.tif', 'myRaster')
|
|
|
|
self.assertEqual(parameter.getAsString(layer), '/path/to/myRaster.tif')
|
|
|
|
|
|
|
|
# TODO With Layer Name, instead of Layer object
|
|
|
|
|
|
|
|
def testGetAsStringWhenFile(self):
|
2016-08-23 19:33:42 +03:00
|
|
|
parameter = ParameterMultipleInput('myName', 'myDesc', datatype=dataobjects.TYPE_FILE)
|
2015-11-29 18:42:10 -06:00
|
|
|
self.assertEqual(parameter.getAsString('/some/path'), '/some/path')
|
|
|
|
|
|
|
|
def testGetAsStringWhenVector(self):
|
2016-08-23 19:33:42 +03:00
|
|
|
parameter = ParameterMultipleInput('myName', 'myDesc', datatype=dataobjects.TYPE_VECTOR_ANY)
|
2015-11-29 18:42:10 -06:00
|
|
|
|
|
|
|
# With Path
|
|
|
|
self.assertEqual(parameter.getAsString('/some/path'), '/some/path')
|
|
|
|
|
|
|
|
# With Layer
|
|
|
|
layer = QgsVectorLayer('/path/to/myVector.shp', 'myVector', 'memory')
|
|
|
|
self.assertEqual(parameter.getAsString(layer), '/path/to/myVector.shp')
|
|
|
|
|
|
|
|
# TODO With Layer Name, instead of Layer object
|
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
|
2015-11-29 18:42:10 -06:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|