Skip to content
Permalink
Browse files

Decoupled tests by removing shared self-variables.

  • Loading branch information
henrik authored and nyalldawson committed Mar 25, 2019
1 parent 4b90afd commit 5c38ba39417a8ca3d27a26cd73e90a4e42c7057e
Showing with 137 additions and 107 deletions.
  1. +137 −107 tests/src/python/test_qgspointdisplacementrenderer.py
@@ -60,30 +60,32 @@

class TestQgsPointDisplacementRenderer(unittest.TestCase):

def setUp(self):
def _setUp(self):
myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp')
self.layer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
QgsProject.instance().addMapLayer(self.layer)
layer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
QgsProject.instance().addMapLayer(layer)

self.renderer = QgsPointDisplacementRenderer()
renderer = QgsPointDisplacementRenderer()
sym1 = QgsMarkerSymbol.createSimple({'color': '#ff00ff', 'size': '3', 'outline_style': 'no'})
renderer = QgsSingleSymbolRenderer(sym1)
self.renderer.setEmbeddedRenderer(renderer)
self.renderer.setCircleRadiusAddition(2)
self.renderer.setCircleWidth(1)
self.renderer.setCircleColor(QColor(0, 0, 0))
self.renderer.setCenterSymbol(QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'}))
self.layer.setRenderer(self.renderer)

rendered_layers = [self.layer]
self.mapsettings = QgsMapSettings()
self.mapsettings.setOutputSize(QSize(400, 400))
self.mapsettings.setOutputDpi(96)
self.mapsettings.setExtent(QgsRectangle(-123, 18, -70, 52))
self.mapsettings.setLayers(rendered_layers)

def tearDown(self):
QgsProject.instance().removeAllMapLayers()
sym_renderer = QgsSingleSymbolRenderer(sym1)
renderer.setEmbeddedRenderer(sym_renderer)
renderer.setCircleRadiusAddition(2)
renderer.setCircleWidth(1)
renderer.setCircleColor(QColor(0, 0, 0))
renderer.setCenterSymbol(QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'}))
layer.setRenderer(renderer)

rendered_layers = [layer]
mapsettings = QgsMapSettings()
mapsettings.setOutputSize(QSize(400, 400))
mapsettings.setOutputDpi(96)
mapsettings.setExtent(QgsRectangle(-123, 18, -70, 52))
mapsettings.setLayers(rendered_layers)
return layer, renderer, mapsettings

def _tearDown(self, layer):
#QgsProject.instance().removeAllMapLayers()
QgsProject.instance().removeMapLayer(layer)

def _setProperties(self, r):
""" set properties for a renderer for testing with _checkProperties"""
@@ -125,15 +127,15 @@ def _checkProperties(self, r):
self.assertEqual(r.embeddedRenderer().symbol().color().name(), '#fdbf6f')
self.assertEqual(r.labelDistanceFactor(), 0.25)

def _set_categorized_renderer(self):
renderer = QgsCategorizedSymbolRenderer(attrName='Class')
def _create_categorized_renderer(self):
cat_renderer = QgsCategorizedSymbolRenderer(attrName='Class')
sym1 = QgsMarkerSymbol.createSimple({'color': '#ff00ff', 'size': '6', 'outline_style': 'no'})
cat1 = QgsRendererCategory('Biplane', sym1, 'Big')
renderer.addCategory(cat1)
cat_renderer.addCategory(cat1)
sym2 = QgsMarkerSymbol.createSimple({'color': '#ff00ff', 'size': '3', 'outline_style': 'no'})
cat2 = QgsRendererCategory(['B52', 'Jet'], sym2, 'Smaller')
renderer.addCategory(cat2)
self.renderer.setEmbeddedRenderer(renderer)
cat_renderer.addCategory(cat2)
return cat_renderer

def testGettersSetters(self):
""" test getters and setters """
@@ -187,160 +189,188 @@ def testConvert(self):
self.assertEqual(d.embeddedRenderer().symbol().color().name(), '#fdbf6f')

def testRenderNoCluster(self):
self.layer.renderer().setTolerance(1)
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(1)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_no_cluster')
self.assertTrue(renderchecker.runTest('displacement_no_cluster'))
self._tearDown(layer)

def testRenderWithin(self):
self.layer.renderer().setTolerance(10)
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster'))
self._tearDown(layer)

def testRenderVariables(self):
""" test rendering with expression variables in marker """
self.layer.renderer().setTolerance(10)
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)

old_marker = self.layer.renderer().centerSymbol().clone()
old_marker = layer.renderer().centerSymbol().clone()

new_marker = QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'})
new_marker.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertyFillColor, QgsProperty.fromExpression('@cluster_color'))
new_marker.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertySize, QgsProperty.fromExpression('@cluster_size*2'))
self.layer.renderer().setCenterSymbol(new_marker)
layer.renderer().setCenterSymbol(new_marker)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_variables')
result = renderchecker.runTest('expected_displacement_variables')
self.layer.renderer().setCenterSymbol(old_marker)
layer.renderer().setCenterSymbol(old_marker)
self.assertTrue(result)
self._tearDown(layer)

def testRenderGrid(self):
self.layer.renderer().setTolerance(10)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_grid')
self.assertTrue(renderchecker.runTest('expected_displacement_grid'))
self._tearDown(layer)

def testRenderGridAdjust(self):
self.layer.renderer().setTolerance(10)
self.layer.renderer().setCircleRadiusAddition(5)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
self.layer.renderer().setCircleColor(QColor())
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)
layer.renderer().setCircleRadiusAddition(5)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
layer.renderer().setCircleColor(QColor())
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_adjust_grid')
self.assertTrue(renderchecker.runTest('expected_displacement_adjust_grid'))
self._tearDown(layer)

def testClusterRingLabels(self):
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(0.35)
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(0.35)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_ring_labels')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_ring_labels'))

def testClusterGridLabels(self):
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(0.35)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
self._tearDown(layer)

def _testClusterGridLabels(self):
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(0.35)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_grid_labels')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_grid_labels'))

def testClusterConcentricLabels(self):
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(0.35)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.ConcentricRings)
self._tearDown(layer)

def _testClusterConcentricLabels(self):
layer, renderer, mapsettings = self._setUp()
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(0.35)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.ConcentricRings)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_concentric_labels')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_concentric_labels'))

def testClusterRingLabelsDifferentSizes(self):
self._set_categorized_renderer()
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(0.35)
self._tearDown(layer)

def _testClusterRingLabelsDifferentSizes(self):
layer, renderer, mapsettings = self._setUp()
renderer.setEmbeddedRenderer(self._create_categorized_renderer())
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(0.35)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_ring_labels_diff_size')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_ring_labels_diff_size'))

def testClusterGridLabelsDifferentSizes(self):
self._set_categorized_renderer()
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(0.35)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
self._tearDown(layer)

def _testClusterGridLabelsDifferentSizes(self):
layer, renderer, mapsettings = self._setUp()
renderer.setEmbeddedRenderer(self._create_categorized_renderer())
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(0.35)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_grid_labels_diff_size')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_grid_labels_diff_size'))

def testClusterConcentricLabelsDifferentSizes(self):
self._set_categorized_renderer()
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(0.35)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.ConcentricRings)
self._tearDown(layer)

def _testClusterConcentricLabelsDifferentSizes(self):
layer, renderer, mapsettings = self._setUp()
renderer.setEmbeddedRenderer(self._create_categorized_renderer())
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(0.35)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.ConcentricRings)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_concentric_labels_diff_size')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_concentric_labels_diff_size'))

def testClusterRingLabelsDifferentSizesFarther(self):
self._set_categorized_renderer()
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(1)
self._tearDown(layer)

def _testClusterRingLabelsDifferentSizesFarther(self):
layer, renderer, mapsettings = self._setUp()
renderer.setEmbeddedRenderer(self._create_categorized_renderer())
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(1)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_ring_labels_diff_size_farther')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_ring_labels_diff_size_farther'))

def testClusterGridLabelsDifferentSizesFarther(self):
self._set_categorized_renderer()
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(1)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
self._tearDown(layer)

def _testClusterGridLabelsDifferentSizesFarther(self):
layer, renderer, mapsettings = self._setUp()
renderer.setEmbeddedRenderer(self._create_categorized_renderer())
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(1)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.Grid)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_grid_labels_diff_size_farther')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_grid_labels_diff_size_farther'))

def testClusterConcentricLabelsDifferentSizesFarther(self):
self._set_categorized_renderer()
self.layer.renderer().setTolerance(10)
self.layer.renderer().setLabelAttributeName('Class')
self.layer.renderer().setLabelDistanceFactor(1)
self.layer.renderer().setPlacement(QgsPointDisplacementRenderer.ConcentricRings)
self._tearDown(layer)

def _testClusterConcentricLabelsDifferentSizesFarther(self):
layer, renderer, mapsettings = self._setUp()
renderer.setEmbeddedRenderer(self._create_categorized_renderer())
layer.renderer().setTolerance(10)
layer.renderer().setLabelAttributeName('Class')
layer.renderer().setLabelDistanceFactor(1)
layer.renderer().setPlacement(QgsPointDisplacementRenderer.ConcentricRings)
renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setMapSettings(mapsettings)
renderchecker.setControlPathPrefix('displacement_renderer')
renderchecker.setControlName('expected_displacement_cluster_concentric_labels_diff_size_farther')
self.assertTrue(renderchecker.runTest('expected_displacement_cluster_concentric_labels_diff_size_farther'))
self._tearDown(layer)



0 comments on commit 5c38ba3

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