Skip to content
Permalink
Browse files

[processing] Add unit tests for retrieving param from script code

And fix number param retrieving from script code
  • Loading branch information
nyalldawson committed Nov 13, 2016
1 parent 3ef7b3b commit f24cda44025eca0f0c9f8e270fcdefd60f8e7979
Showing with 134 additions and 3 deletions.
  1. +8 −2 python/plugins/processing/core/parameters.py
  2. +126 −1 python/plugins/processing/tests/ParametersTest.py
@@ -863,14 +863,20 @@ def getAsScriptCode(self):
if self.optional:
param_type += 'optional '
param_type += 'number'
return '##' + self.name + '=' + param_type + str(self.default)
code = '##' + self.name + '=' + param_type
if self.default:
code += str(self.default)
return code

@classmethod
def fromScriptCode(self, line):

isOptional, name, definition = _splitParameterOptions(line)
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('number'):
default = definition.strip()[len('number') + 1:] or None
default = definition.strip()[len('number'):] or None
if default == 'None':
default = None
return ParameterNumber(name, descName, default=default, optional=isOptional)

def _evaluate(self, value):
@@ -41,7 +41,8 @@
ParameterVector,
ParameterTableField,
ParameterSelection,
ParameterExpression)
ParameterExpression,
getParameterFromString)
from processing.tools import dataobjects
from processing.tests.TestData import points2

@@ -96,6 +97,19 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, True)

def testScriptCode(self):
parameter = ParameterBoolean('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterBoolean))
self.assertFalse(result.optional)

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterBoolean))
self.assertTrue(result.optional)


class ParameterCRSTest(unittest.TestCase):

@@ -119,6 +133,18 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, 'EPSG:12003')

def testScriptCode(self):
parameter = ParameterCrs('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterCrs))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterCrs))
self.assertTrue(result.optional)


class ParameterDataObjectTest(unittest.TestCase):

@@ -161,6 +187,18 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, '1,2,3,4')

def testScriptCode(self):
parameter = ParameterExtent('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterExtent))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterExtent))
self.assertTrue(result.optional)


class ParameterPointTest(unittest.TestCase):

@@ -191,6 +229,18 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, '1,2')

def testScriptCode(self):
parameter = ParameterPoint('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterPoint))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterPoint))
self.assertTrue(result.optional)


class ParameterSelectionTest(unittest.TestCase):

@@ -283,6 +333,18 @@ def testGetValueAsCommandLineParameter(self):
parameter.setValue('myFile.png')
self.assertEqual(parameter.getValueAsCommandLineParameter(), '"myFile.png"')

def testScriptCode(self):
parameter = ParameterFile('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterFile))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterFile))
self.assertTrue(result.optional)


class TestParameterFixedTable(unittest.TestCase):

@@ -393,6 +455,18 @@ def testGetAsStringWhenVector(self):

# TODO With Layer Name, instead of Layer object

def testScriptCode(self):
parameter = ParameterMultipleInput('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterMultipleInput))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterMultipleInput))
self.assertTrue(result.optional)


class ParameterNumberTest(unittest.TestCase):

@@ -447,6 +521,18 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, 5)

def testScriptCode(self):
parameter = ParameterNumber('myName', 'myDescription')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterNumber))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterNumber))
self.assertTrue(result.optional)


class ParameterStringTest(unittest.TestCase):

@@ -470,6 +556,18 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, 'check')

def testScriptCode(self):
parameter = ParameterString('myName', 'myDescription', default='test')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterString))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterString))
self.assertTrue(result.optional)


class ParameterExpressionTest(unittest.TestCase):

@@ -493,6 +591,18 @@ def testOptional(self):
self.assertFalse(requiredParameter.setValue(None))
self.assertEqual(requiredParameter.value, 'check')

def testScriptCode(self):
parameter = ParameterExpression('myName', 'myDescription', default='test')
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterExpression))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterExpression))
self.assertTrue(result.optional)


class ParameterTableFieldTest(unittest.TestCase):

@@ -505,6 +615,21 @@ def testOptional(self):
parameter = ParameterTableField(
'myName', 'myDesc', parent_name, optional=True)

def testScriptCode(self):
parent_name = 'test_parent_layer'
test_data = points2()
test_layer = QgsVectorLayer(test_data, parent_name, 'ogr')
parameter = ParameterTableField(
'myName', 'myDesc', parent_name)
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterTableField))

parameter.optional = True
code = parameter.getAsScriptCode()
result = getParameterFromString(code)
self.assertTrue(isinstance(result, ParameterTableField))
self.assertTrue(result.optional)

if __name__ == '__main__':
unittest.main()

0 comments on commit f24cda4

Please sign in to comment.
You can’t perform that action at this time.