Skip to content

Commit

Permalink
[Test] PyQgsSymbolLayer: add usedAttributes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rldhont authored and nyalldawson committed Jul 31, 2019
1 parent 91dee0e commit 955d4b5
Showing 1 changed file with 87 additions and 4 deletions.
91 changes: 87 additions & 4 deletions tests/src/python/test_qgssymbollayer.py
Expand Up @@ -15,6 +15,9 @@
* (at your option) any later version. *
* *
***************************************************************************
From build dir, run: ctest -R PyQgsSymbolLayer -V
"""

__author__ = 'Massimo Endrighi'
Expand Down Expand Up @@ -72,7 +75,9 @@
QgsProject,
QgsMultiRenderChecker,
QgsSingleSymbolRenderer,
QgsProperty
QgsProperty,
QgsExpressionContext,
QgsExpressionContextUtils
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath
Expand Down Expand Up @@ -406,11 +411,23 @@ def testRenderFillLayerDataDefined(self):
ms.setExtent(QgsRectangle(-133, 22, -70, 52))
ms.setLayers([polys_layer])

# Test usedAttributes
ctx = QgsRenderContext.fromMapSettings(ms)
ctx.expressionContext().appendScope(polys_layer.createExpressionContextScope())
# for symbol layer
self.assertCountEqual(layer.usedAttributes(ctx), {'Name'})
# for symbol
self.assertCountEqual(symbol.usedAttributes(ctx), {'Name'})
# for symbol renderer
self.assertCountEqual(polys_layer.renderer().usedAttributes(ctx), {'Name'})

# Test rendering
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(ms)
renderchecker.setControlPathPrefix('symbol_layer')
renderchecker.setControlName('expected_filllayer_ddenabled')
self.assertTrue(renderchecker.runTest('filllayer_ddenabled'))

QgsProject.instance().removeMapLayer(polys_layer)

def testRenderLineLayerDisabled(self):
Expand Down Expand Up @@ -471,11 +488,23 @@ def testRenderLineLayerDataDefined(self):
ms.setExtent(QgsRectangle(-133, 22, -70, 52))
ms.setLayers([lines_layer])

# Test usedAttributes
ctx = QgsRenderContext.fromMapSettings(ms)
ctx.expressionContext().appendScope(lines_layer.createExpressionContextScope())
# for symbol layer
self.assertCountEqual(layer.usedAttributes(ctx), {'Name'})
# for symbol
self.assertCountEqual(symbol.usedAttributes(ctx), {'Name'})
# for symbol renderer
self.assertCountEqual(lines_layer.renderer().usedAttributes(ctx), {'Name'})

# Test rendering
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(ms)
renderchecker.setControlPathPrefix('symbol_layer')
renderchecker.setControlName('expected_linelayer_ddenabled')
self.assertTrue(renderchecker.runTest('linelayer_ddenabled'))

QgsProject.instance().removeMapLayer(lines_layer)

def testRenderMarkerLayerDisabled(self):
Expand Down Expand Up @@ -535,11 +564,23 @@ def testRenderMarkerLayerDataDefined(self):
ms.setExtent(QgsRectangle(-133, 22, -70, 52))
ms.setLayers([points_layer])

# Test usedAttributes
ctx = QgsRenderContext.fromMapSettings(ms)
ctx.expressionContext().appendScope(points_layer.createExpressionContextScope())
# for symbol layer
self.assertCountEqual(layer.usedAttributes(ctx), {'Class'})
# for symbol
self.assertCountEqual(symbol.usedAttributes(ctx), {'Class'})
# for symbol renderer
self.assertCountEqual(points_layer.renderer().usedAttributes(ctx), {'Class'})

# Test rendering
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(ms)
renderchecker.setControlPathPrefix('symbol_layer')
renderchecker.setControlName('expected_markerlayer_ddenabled')
self.assertTrue(renderchecker.runTest('markerlayer_ddenabled'))

QgsProject.instance().removeMapLayer(points_layer)

def testQgsSimpleFillSymbolLayer(self):
Expand Down Expand Up @@ -581,6 +622,9 @@ def testQgsSimpleFillSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsGradientFillSymbolLayer(self):
"""Test setting and getting QgsGradientFillSymbolLayer properties.
"""
Expand Down Expand Up @@ -663,6 +707,9 @@ def testQgsGradientFillSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mGradientLayer.usedAttributes(ctx), {})

def testQgsCentroidFillSymbolLayer(self):
"""
Create a new style from a .sld file and match test
Expand Down Expand Up @@ -703,6 +750,9 @@ def testQgsCentroidFillSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer.setColor(QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.color(), QColor(150, 50, 100))
Expand Down Expand Up @@ -751,6 +801,9 @@ def testQgsLinePatternFillSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer.setColor(QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.color(), QColor(150, 50, 100))
Expand Down Expand Up @@ -807,13 +860,19 @@ def testQgsPointPatternFillSymbolLayerSld(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsPointPatternFillSymbolLayer(self):
"""
Test point pattern fill
"""
# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer = QgsPointPatternFillSymbolLayer.create()

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer.setColor(QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.color(), QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.subSymbol().color(), QColor(150, 50, 100))
Expand Down Expand Up @@ -851,6 +910,9 @@ def testQgsSVGFillSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsMarkerLineSymbolLayer(self):
"""
Create a new style from a .sld file and match test
Expand Down Expand Up @@ -891,6 +953,9 @@ def testQgsMarkerLineSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer.setColor(QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.color(), QColor(150, 50, 100))
Expand Down Expand Up @@ -949,6 +1014,9 @@ def testQgsSimpleLineSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsEllipseSymbolLayer(self):
"""
Create a new style from a .sld file and match test
Expand Down Expand Up @@ -994,6 +1062,9 @@ def testQgsEllipseSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsFontMarkerSymbolLayer(self):
"""
Create a new style from a .sld file and match test
Expand Down Expand Up @@ -1034,6 +1105,9 @@ def testQgsFontMarkerSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsSvgMarkerSymbolLayer(self):
"""
Create a new style from a .sld file and match test
Expand Down Expand Up @@ -1069,13 +1143,19 @@ def testQgsSvgMarkerSymbolLayer(self):
mMessage = 'Expected "%s" got "%s"' % (mExpectedValue, mValue)
assert mExpectedValue == mValue, mMessage

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

def testQgsFilledMarkerSymbolLayer(self):
"""
Test QgsFilledMarkerSymbolLayer
"""
# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer = QgsFilledMarkerSymbolLayer.create()

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer.setColor(QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.color(), QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.subSymbol().color(), QColor(150, 50, 100))
Expand All @@ -1087,9 +1167,12 @@ def testQgsVectorFieldSymbolLayer(self):
"""
Test QgsVectorFieldSymbolLayer
"""
# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer = QgsVectorFieldSymbolLayer.create()

ctx = QgsRenderContext()
self.assertCountEqual(mSymbolLayer.usedAttributes(ctx), {})

# test colors, need to make sure colors are passed/retrieved from subsymbol
mSymbolLayer.setColor(QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.color(), QColor(150, 50, 100))
self.assertEqual(mSymbolLayer.subSymbol().color(), QColor(150, 50, 100))
Expand Down

0 comments on commit 955d4b5

Please sign in to comment.