Skip to content
Permalink
Browse files

Add tests for QgsDefaultValue

  • Loading branch information
m-kuhn committed Sep 29, 2017
1 parent f0b7149 commit ff385fb0355b3b216992035265f1895e68da5a7a
@@ -135,8 +135,8 @@ void TestQgsField::gettersSetters()
QCOMPARE( field.comment(), QString( "comment" ) );
field.setAlias( QStringLiteral( "alias" ) );
QCOMPARE( field.alias(), QString( "alias" ) );
field.setDefaultValue( QStringLiteral( "1+2" ) );
QCOMPARE( field.defaultValue().expression(), QString( "1+2" ) );
field.setDefaultValueDefinition( QStringLiteral( "1+2" ) );
QCOMPARE( field.defaultValueDefinition().expression(), QString( "1+2" ) );
QgsFieldConstraints constraints;
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
field.setConstraints( constraints );
@@ -244,10 +244,10 @@ void TestQgsField::equality()
QVERIFY( !( field1 == field2 ) );
QVERIFY( field1 != field2 );
field2.setAlias( QString() );
field2.setDefaultValue( QStringLiteral( "1+2" ) );
field2.setDefaultValueDefinition( QStringLiteral( "1+2" ) );
QVERIFY( !( field1 == field2 ) );
QVERIFY( field1 != field2 );
field2.setDefaultValue( QString() );
field2.setDefaultValueDefinition( QString() );
constraints = field2.constraints();
constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
field2.setConstraints( constraints );
@@ -461,7 +461,7 @@ void TestQgsField::dataStream()
original.setTypeName( QStringLiteral( "typename1" ) );
original.setComment( QStringLiteral( "comment1" ) );
original.setAlias( QStringLiteral( "alias" ) );
original.setDefaultValue( QStringLiteral( "default" ) );
original.setDefaultValueDefinition( QStringLiteral( "default" ) );
QgsFieldConstraints constraints;
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginLayer );
@@ -39,6 +39,7 @@ ADD_PYTHON_TEST(PyQgsComposerPolyline test_qgscomposerpolyline.py)
ADD_PYTHON_TEST(PyQgsComposerView test_qgscomposerview.py)
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
ADD_PYTHON_TEST(PyQgsConditionalStyle test_qgsconditionalstyle.py)
ADD_PYTHON_TEST(PyQgsDefaultValue test_qgsdefaultvalue.py)
ADD_PYTHON_TEST(PyQgsXmlUtils test_qgsxmlutils.py)
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
ADD_PYTHON_TEST(PyQgsDateTimeStatisticalSummary test_qgsdatetimestatisticalsummary.py)
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsDefaultValue.
.. note:: 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__ = 'Matthias Kuhn'
__date__ = '26.9.2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA


from qgis.core import (QgsDefaultValue)
from qgis.testing import unittest


class TestQgsRasterColorRampShader(unittest.TestCase):

def testValid(self):
self.assertFalse(QgsDefaultValue())
self.assertTrue(QgsDefaultValue('test'))
self.assertTrue(QgsDefaultValue('abc', True))
self.assertTrue(QgsDefaultValue('abc', False))

def setGetExpression(self):
value = QgsDefaultValue('abc', False)
self.assertEqual(value.expression(), 'abc')
value.setExpression('def')
self.assertEqual(value.expression(), 'def')

def setGetApplyOnUpdate(self):
value = QgsDefaultValue('abc', False)
self.assertEqual(value.applyOnUpdate(), False)
value.setApplyOnUpdate(True)
self.assertEqual(value.applyOnUpdate(), True)

if __name__ == '__main__':
unittest.main()
@@ -22,6 +22,7 @@

from qgis.core import (QgsWkbTypes,
QgsAction,
QgsDefaultValue,
QgsEditorWidgetSetup,
QgsVectorLayer,
QgsRectangle,
@@ -1997,22 +1998,25 @@ def testGetSetDefaults(self):
""" test getting and setting default expressions """
layer = createLayerWithOnePoint()

self.assertFalse(layer.defaultValueExpression(0))
self.assertFalse(layer.defaultValueExpression(1))
self.assertFalse(layer.defaultValueExpression(2))

layer.setDefaultValueExpression(0, "'test'")
self.assertEqual(layer.defaultValueExpression(0), "'test'")
self.assertFalse(layer.defaultValueExpression(1))
self.assertFalse(layer.defaultValueExpression(2))
self.assertEqual(layer.fields().at(0).defaultValueExpression(), "'test'")

layer.setDefaultValueExpression(1, "2+2")
self.assertEqual(layer.defaultValueExpression(0), "'test'")
self.assertEqual(layer.defaultValueExpression(1), "2+2")
self.assertFalse(layer.defaultValueExpression(2))
self.assertEqual(layer.fields().at(0).defaultValueExpression(), "'test'")
self.assertEqual(layer.fields().at(1).defaultValueExpression(), "2+2")
self.assertFalse(layer.defaultValueDefinition(0))
self.assertFalse(layer.defaultValueDefinition(0).expression())
self.assertFalse(layer.defaultValueDefinition(0).applyOnUpdate())
self.assertFalse(layer.defaultValueDefinition(1))
self.assertFalse(layer.defaultValueDefinition(2))

layer.setDefaultValueDefinition(0, QgsDefaultValue("'test'"))
self.assertTrue(layer.defaultValueDefinition(0))
self.assertEqual(layer.defaultValueDefinition(0).expression(), "'test'")
self.assertFalse(layer.defaultValueDefinition(1))
self.assertFalse(layer.defaultValueDefinition(2))
self.assertEqual(layer.fields().at(0).defaultValueDefinition().expression(), "'test'")

layer.setDefaultValueDefinition(1, QgsDefaultValue("2+2"))
self.assertEqual(layer.defaultValueDefinition(0).expression(), "'test'")
self.assertEqual(layer.defaultValueDefinition(1).expression(), "2+2")
self.assertFalse(layer.defaultValueDefinition(2))
self.assertEqual(layer.fields().at(0).defaultValueDefinition().expression(), "'test'")
self.assertEqual(layer.fields().at(1).defaultValueDefinition().expression(), "2+2")

def testSaveRestoreDefaults(self):
""" test saving and restoring default expressions from xml"""
@@ -2025,34 +2029,34 @@ def testSaveRestoreDefaults(self):

layer2 = createLayerWithOnePoint()
self.assertTrue(layer2.readXml(elem, QgsReadWriteContext()))
self.assertFalse(layer2.defaultValueExpression(0))
self.assertFalse(layer2.defaultValueExpression(1))
self.assertFalse(layer2.defaultValueDefinition(0))
self.assertFalse(layer2.defaultValueDefinition(1))

# set some default expressions
layer.setDefaultValueExpression(0, "'test'")
layer.setDefaultValueExpression(1, "2+2")
layer.setDefaultValueDefinition(0, QgsDefaultValue("'test'"))
layer.setDefaultValueDefinition(1, QgsDefaultValue("2+2"))

doc = QDomDocument("testdoc")
elem = doc.createElement("maplayer")
self.assertTrue(layer.writeXml(elem, doc, QgsReadWriteContext()))

layer3 = createLayerWithOnePoint()
self.assertTrue(layer3.readXml(elem, QgsReadWriteContext()))
self.assertEqual(layer3.defaultValueExpression(0), "'test'")
self.assertEqual(layer3.defaultValueExpression(1), "2+2")
self.assertEqual(layer3.fields().at(0).defaultValueExpression(), "'test'")
self.assertEqual(layer3.fields().at(1).defaultValueExpression(), "2+2")
self.assertEqual(layer3.defaultValueDefinition(0).expression(), "'test'")
self.assertEqual(layer3.defaultValueDefinition(1).expression(), "2+2")
self.assertEqual(layer3.fields().at(0).defaultValueDefinition().expression(), "'test'")
self.assertEqual(layer3.fields().at(1).defaultValueDefinition().expression(), "2+2")

def testEvaluatingDefaultExpressions(self):
""" tests calculation of default values"""
layer = createLayerWithOnePoint()
layer.setDefaultValueExpression(0, "'test'")
layer.setDefaultValueExpression(1, "2+2")
layer.setDefaultValueDefinition(0, QgsDefaultValue("'test'"))
layer.setDefaultValueDefinition(1, QgsDefaultValue("2+2"))
self.assertEqual(layer.defaultValue(0), 'test')
self.assertEqual(layer.defaultValue(1), 4)

# using feature
layer.setDefaultValueExpression(1, '$id * 2')
layer.setDefaultValueDefinition(1, QgsDefaultValue('$id * 2'))
feature = QgsFeature(4)
feature.setValid(True)
feature.setFields(layer.fields())
@@ -2063,7 +2067,7 @@ def testEvaluatingDefaultExpressions(self):
self.assertEqual(layer.defaultValue(1, feature), 8)

# using feature geometry
layer.setDefaultValueExpression(1, '$x * 2')
layer.setDefaultValueDefinition(1, QgsDefaultValue('$x * 2'))
feature.setGeometry(QgsGeometry(QgsPoint(6, 7)))
self.assertEqual(layer.defaultValue(1, feature), 12)

@@ -2072,17 +2076,17 @@ def testEvaluatingDefaultExpressions(self):
scope.setVariable('var1', 16)
context = QgsExpressionContext()
context.appendScope(scope)
layer.setDefaultValueExpression(1, '$id + @var1')
layer.setDefaultValueDefinition(1, QgsDefaultValue('$id + @var1'))
self.assertEqual(layer.defaultValue(1, feature, context), 20)

# if no scope passed, should use a default constructed one including layer variables
QgsExpressionContextUtils.setLayerVariable(layer, 'var2', 4)
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), 'var3', 8)
layer.setDefaultValueExpression(1, 'to_int(@var2) + to_int(@var3) + $id')
layer.setDefaultValueDefinition(1, QgsDefaultValue('to_int(@var2) + to_int(@var3) + $id'))
self.assertEqual(layer.defaultValue(1, feature), 16)

# bad expression
layer.setDefaultValueExpression(1, 'not a valid expression')
layer.setDefaultValueDefinition(1, QgsDefaultValue('not a valid expression'))
self.assertFalse(layer.defaultValue(1))

def testGetSetConstraints(self):
@@ -2404,7 +2408,7 @@ def testClone(self):

layer.setConstraintExpression(0, "MyFieldConstraintExpression")
layer.setFieldConstraint(0, QgsFieldConstraints.ConstraintUnique, QgsFieldConstraints.ConstraintStrengthHard)
layer.setDefaultValueExpression(0, "MyDefaultValueExpression")
layer.setDefaultValueDefinition(0, QgsDefaultValue("MyDefaultValueExpression"))

action = QgsAction(QgsAction.Unix, "MyActionDescription", "MyActionCmd")
layer.actions().addAction(action)

0 comments on commit ff385fb

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