Skip to content

Commit

Permalink
Add tests for traceback on eval error and "" on eval success
Browse files Browse the repository at this point in the history
  • Loading branch information
kannes authored and nyalldawson committed Feb 18, 2022
1 parent 1877b36 commit 24c8014
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/src/python/test_qgsexpression.py
Expand Up @@ -70,6 +70,10 @@ def null_mean(values, feature, parent):
vals = [val for val in values if val != NULL]
return sum(vals) / len(vals)

@qgsfunction(group='testing', register=False)
def raise_exception(feature, parent):
foo # an undefined variable

def tearDown(self):
QgsExpression.unregisterFunction('testfun')

Expand Down Expand Up @@ -290,6 +294,26 @@ def testReferencedAttributeIndexesNonExistingField(self):
e.setExpression("foo = 1")
self.assertTrue(e.isValid())
self.assertEqual(len(e.referencedAttributeIndexes(QgsFields())), 0)

def testSuccessfulEvaluationReturnsNoEvalErrorString(self):
exp = QgsExpression("True is False") # the result does not matter
self.assertEqual(exp.evalErrorString(), "")

def testExceptionDuringEvalReturnsTraceback(self):
QgsExpression.registerFunction(self.raise_exception)
exp = QgsExpression('raise_exception()')
result = exp.evaluate()
# The file paths and line offsets are dynamic
regex = (
"name 'foo' is not defined:<pre>Traceback \\(most recent call last\\):\n"
" File \".*qgsfunction.py\", line [0-9]+, in func\n"
" return self.function\\(\\*values\\)\n"
" File \".*test_qgsexpression.py\", line [0-9]+, in raise_exception\n"
" foo # an undefined variable\n"
"NameError: name \'foo\' is not defined"
"\n</pre>"
)
self.assertRegex(exp.evalErrorString(), regex)


if __name__ == "__main__":
Expand Down

0 comments on commit 24c8014

Please sign in to comment.