Skip to content

Commit

Permalink
Merge pull request #88 from MIERUNE/testing
Browse files Browse the repository at this point in the history
make some methods private, add test for renderer
  • Loading branch information
Kanahiro authored May 29, 2024
2 parents d507c87 + e58030e commit 56dd749
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
23 changes: 12 additions & 11 deletions gtfs_go_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
QgsSingleSymbolRenderer,
QgsSvgMarkerSymbolLayer,
QgsSymbol,
QgsVectorLayer,
QgsWkbTypes,
)
from qgis.PyQt.QtCore import Qt
Expand All @@ -21,26 +22,26 @@
)


def get_random_color():
def _get_random_color():
import random

random_index = random.randrange(0, len(ROUTES_COLOR_LIST) - 1, 1)
return QColor(ROUTES_COLOR_LIST[random_index])


class Renderer:
def __init__(self, target_layer, target_field_name):
def __init__(self, target_layer: QgsVectorLayer, target_field_name: str):
self.target_layer = target_layer
self.target_field_name = target_field_name

def is_point_layer(self):
def _is_point_layer(self):
return (
self.target_layer.geometryType() == QgsWkbTypes.GeometryType.PointGeometry
)

def make_symbol(self):
def _make_symbol(self):
symbol = QgsSymbol.defaultSymbol(self.target_layer.geometryType())
if self.is_point_layer():
if self._is_point_layer():
symbol_layer = QgsSvgMarkerSymbolLayer(STOPS_SVG_PATH)
symbol_layer.setSize(STOPS_ICON_SIZE_MM)
symbol.changeSymbolLayer(0, symbol_layer)
Expand All @@ -53,14 +54,14 @@ def make_symbol(self):
line_layer = symbol.symbolLayer(0)
line_layer.setPenJoinStyle(Qt.RoundJoin)
line_layer.setWidth(ROUTES_LINE_WIDTH_MM)
line_layer.setColor(get_random_color())
line_layer.setColor(_get_random_color())
outline_layer = symbol.symbolLayer(0).clone()
outline_layer.setColor(QColor(ROUTES_OUTLINE_COLOR))
outline_layer.setWidth(ROUTES_OUTLINE_WIDTH_MM)
symbol.insertSymbolLayer(0, outline_layer)
return symbol

def make_categories_by(self):
def _make_categories_by(self):
categories = []
# get all target field value with removing dupulicates
target_field_values = set(
Expand All @@ -70,15 +71,15 @@ def make_categories_by(self):
]
)
for value in target_field_values:
symbol = self.make_symbol()
symbol = self._make_symbol()
category = QgsRendererCategory(value, symbol, value)
categories.append(category)
return categories

def make_renderer(self):
if self.is_point_layer():
renderer = QgsSingleSymbolRenderer(self.make_symbol())
if self._is_point_layer():
renderer = QgsSingleSymbolRenderer(self._make_symbol())
else:
categories = self.make_categories_by()
categories = self._make_categories_by()
renderer = QgsCategorizedSymbolRenderer(self.target_field_name, categories)
return renderer
52 changes: 52 additions & 0 deletions tests/test_gtfs_go_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from pytest_mock import MockerFixture
from qgis.core import (
QgsCategorizedSymbolRenderer,
QgsSingleSymbolRenderer,
QgsVectorLayer,
)

from gtfs_go_renderer import Renderer


def test_renderer_point():
target_layer = QgsVectorLayer(r"""{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0.0, 0.0]
},
"properties": {
"testname": "Null Island"
}
}""")
renderer = Renderer(target_layer, "testname")
symbol_renderer = renderer.make_renderer()
assert isinstance(symbol_renderer, QgsSingleSymbolRenderer)


def test_renderer_polygon(mocker: MockerFixture):
# in QGIS-API there are some classes which crash on test environment
# -> mock methods using them
mocker.patch("gtfs_go_renderer.Renderer._make_categories_by", return_value=[])

polygon_layer = QgsVectorLayer(r"""{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[[
0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
0.0, 0.0
]]]
},
"properties": {
"testname": "Null Square"
}
}""")

p_renderer = Renderer(polygon_layer, "testname")
p_symbol_renderer = p_renderer.make_renderer()
# when input is polygon, QgsCategorizedSymbolRenderer is used
assert isinstance(p_symbol_renderer, QgsCategorizedSymbolRenderer)

0 comments on commit 56dd749

Please sign in to comment.