Skip to content
Permalink
Browse files

Add tests

  • Loading branch information
pblottiere committed Jul 11, 2017
1 parent a20b0dc commit 0a13019cef9e7d42c78dc750efc6667692f530d5
Showing with 55 additions and 2 deletions.
  1. +34 −0 tests/src/python/test_qgsexpression.py
  2. +21 −2 tests/src/python/test_qgsrelation.py
@@ -195,5 +195,39 @@ def testValid(self):
e.setExpression('1')
self.assertTrue(e.isValid())

def testCreateFieldEqualityExpression(self):
e = QgsExpression()

# test when value is null
field = "myfield"
value = None
res = '"myfield" IS NULL'
self.assertEqual(e.createFieldEqualityExpression(field, value), res)

# test when value is null and field name has a quote
field = "my'field"
value = None
res = '"my\'field" IS NULL'
self.assertEqual(e.createFieldEqualityExpression(field, value), res)

# test when field name has a quote and value is an int
field = "my'field"
value = 5
res = '"my\'field" = 5'
self.assertEqual(e.createFieldEqualityExpression(field, value), res)

# test when field name has a quote and value is a string
field = "my'field"
value = '5'
res = '"my\'field" = \'5\''
self.assertEqual(e.createFieldEqualityExpression(field, value), res)

# test when field name has a quote and value is a boolean
field = "my'field"
value = True
res = '"my\'field" = TRUE'
self.assertEqual(e.createFieldEqualityExpression(field, value), res)


if __name__ == "__main__":
unittest.main()
@@ -17,6 +17,7 @@
from qgis.PyQt.QtCore import QFileInfo
from qgis.core import (QgsVectorLayer,
QgsFeature,
QgsFeatureRequest,
QgsRelation,
QgsGeometry,
QgsPoint,
@@ -43,7 +44,11 @@ def createReferencingLayer():
f2.setFields(layer.pendingFields())
f2.setAttributes(["test2", 123])
f2.setGeometry(QgsGeometry.fromPoint(QgsPoint(101, 201)))
assert pr.addFeatures([f1, f2])
f3 = QgsFeature()
f3.setFields(layer.pendingFields())
f3.setAttributes(["foobar'bar", 124])
f3.setGeometry(QgsGeometry.fromPoint(QgsPoint(101, 201)))
assert pr.addFeatures([f1, f2, f3])
return layer


@@ -62,7 +67,7 @@ def createReferencedLayer():
f2.setGeometry(QgsGeometry.fromPoint(QgsPoint(2, 2)))
f3 = QgsFeature()
f3.setFields(layer.pendingFields())
f3.setAttributes(["foobar", 789, 554])
f3.setAttributes(["foobar'bar", 789, 554])
f3.setGeometry(QgsGeometry.fromPoint(QgsPoint(2, 3)))
assert pr.addFeatures([f1, f2, f3])
return layer
@@ -117,6 +122,20 @@ def test_getRelatedFeatures(self):
it = rel.getRelatedFeatures(feat)
self.assertEqual([a.attributes() for a in it], [[u'test1', 123], [u'test2', 123]])

def test_getRelatedFeaturesWithQuote(self):
rel = QgsRelation()

rel.setRelationId('rel1')
rel.setRelationName('Relation Number One')
rel.setReferencingLayer(self.referencingLayer.id())
rel.setReferencedLayer(self.referencedLayer.id())
rel.addFieldPair('fldtxt', 'x')

feat = next(self.referencedLayer.getFeatures(QgsFeatureRequest().setFilterFid(3)))

it = rel.getRelatedFeatures(feat)
assert next(it).attributes() == ["foobar'bar", 124]

def test_getReferencedFeature(self):
rel = QgsRelation()
rel.setRelationId('rel1')

0 comments on commit 0a13019

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