From ed8cf502c3d4bde327981724580e7bd901dc5741 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Tue, 30 Apr 2024 12:24:53 +0200 Subject: [PATCH] Tests - Review the GetLegendGraphic function and tests --- lizmap_server/get_legend_graphic.py | 17 ++++++++---- test/test_legend.py | 43 ++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lizmap_server/get_legend_graphic.py b/lizmap_server/get_legend_graphic.py index e29f3afa..3fb38d3a 100644 --- a/lizmap_server/get_legend_graphic.py +++ b/lizmap_server/get_legend_graphic.py @@ -37,6 +37,15 @@ def match_label_feature_count(cls, symbol_label: str) -> Optional[re.Match]: """Regexp for extracting the feature count from the label. """ return re.match(cls.FEATURE_COUNT_REGEXP, symbol_label) + @classmethod + def warning_icon(cls) -> str: + """ Warning icon as base 64. """ + buffer = QBuffer() + buffer.open(QIODevice.WriteOnly) + qp = QImage(":/images/themes/default/mIconWarning.svg") + qp.save(buffer, "PNG") + return str(buffer.data().toBase64().data()) + @exception_handler def responseComplete(self): @@ -82,16 +91,14 @@ def responseComplete(self): return if not layer.isValid(): - buffer = QBuffer() - buffer.open(QIODevice.WriteOnly) - qp = QImage(":/images/themes/default/mIconWarning.svg") - qp.save(buffer, "PNG") + json_data = { 'title': '', 'nodes': [{ 'type': 'layer', 'title': layer_name, - 'icon': str(buffer.data().toBase64().data()), + 'icon': self.warning_icon(), + 'valid': False, }] } handler.clearBody() diff --git a/test/test_legend.py b/test/test_legend.py index 57dedec7..693483cf 100644 --- a/test/test_legend.py +++ b/test/test_legend.py @@ -4,6 +4,8 @@ from qgis.core import Qgis +from lizmap_server.get_legend_graphic import GetLegendGraphicFilter + LOGGER = logging.getLogger('server') __copyright__ = 'Copyright 2023, 3Liz' @@ -31,6 +33,7 @@ def test_unique_symbol(client): assert b['title'] == '' assert len(b['nodes']) == 1, b assert b['nodes'][0]['title'] == 'unique_symbol' + assert b['nodes'][0].get('valid') is None assert 'icon' in b['nodes'][0] assert 'symbols' not in b['nodes'][0] @@ -42,6 +45,7 @@ def test_categorized_symbol(client): rv = client.get(_build_query_string(qs), PROJECT) b = _check_request(rv) assert b['nodes'][0]['title'] == 'categorized' + assert b['nodes'][0].get('valid') is None assert 'icon' not in b['nodes'][0] assert 'symbols' in b['nodes'][0] @@ -80,6 +84,7 @@ def test_simple_rule_based(client): assert b['nodes'][0]['title'] == 'rule_based', b['nodes'][0]['title'] assert 'icon' not in b['nodes'][0] assert 'symbols' in b['nodes'][0] + assert b['nodes'][0].get('valid') is None symbols = b['nodes'][0]['symbols'] @@ -106,6 +111,7 @@ def test_categorized_symbol_feature_count(client): assert b['nodes'][0]['title'].startswith('categorized'), b['nodes'][0]['title'] assert 'icon' not in b['nodes'][0] assert 'symbols' in b['nodes'][0] + assert b['nodes'][0].get('valid') is None symbols = b['nodes'][0]['symbols'] # expected = { @@ -143,6 +149,7 @@ def test_simple_rule_based_feature_count(client): assert b['nodes'][0]['title'].startswith('rule_based'), b['nodes'][0]['title'] assert 'icon' not in b['nodes'][0] assert 'symbols' in b['nodes'][0] + assert b['nodes'][0].get('valid') is None symbols = b['nodes'][0]['symbols'] @@ -159,7 +166,7 @@ def test_simple_rule_based_feature_count(client): assert b['nodes'][0]['title'] == 'rule_based [4]', b['nodes'][0]['title'] -def test_invalid_layer(client): +def test_invalid_layer_symbol_layer(client): """ Test unique symbol for layer. """ qs = dict(BASE_QUERY) qs['MAP'] = PROJECT_INVALID @@ -170,9 +177,13 @@ def test_invalid_layer(client): assert b['title'] == '' assert len(b['nodes']) == 1, b assert b['nodes'][0]['title'] == 'unique_symbol' + assert b['nodes'][0].get('valid') is False + assert b['nodes'][0].get('icon') == GetLegendGraphicFilter.warning_icon() assert 'icon' in b['nodes'][0] assert 'symbols' not in b['nodes'][0] + +def test_invalid_layer_categorized_symbol_layer(client): """ Test categorized symbol for layer. """ qs = dict(BASE_QUERY) qs['MAP'] = PROJECT_INVALID @@ -183,9 +194,33 @@ def test_invalid_layer(client): assert b['title'] == '' assert len(b['nodes']) == 1, b assert b['nodes'][0]['title'] == 'categorized' + assert b['nodes'][0].get('valid') is False + assert b['nodes'][0].get('icon') == GetLegendGraphicFilter.warning_icon() assert 'icon' in b['nodes'][0] assert 'symbols' not in b['nodes'][0] + +def test_multiple_invalid_layers(client): + """ Test multiple invalid layers. """ + qs = dict(BASE_QUERY) + qs['MAP'] = PROJECT_INVALID + qs['LAYER'] = 'unique_symbol,categorized' + rv = client.get(_build_query_string(qs), PROJECT_INVALID) + b = _check_request(rv) + # {'nodes': [{'icon': 'ICON', 'title': 'unique_symbol', 'type': 'layer'}], 'title': ''} + assert len(b['nodes']) == 2, b + assert b['title'] == '' + assert b['nodes'][0]['title'] == 'categorized', b['nodes'][0]['title'] + assert b['nodes'][0].get('valid') is None + # assert b['nodes'][0].get('icon') == GetLegendGraphicFilter.warning_icon() + assert 'symbols' in b['nodes'][0] + assert b['nodes'][1]['title'] == 'unique_symbol' + assert b['nodes'][1].get('valid') is None + # assert b['nodes'][1].get('icon') == GetLegendGraphicFilter.warning_icon() + assert 'symbols' not in b['nodes'][1] + + +def test_invalid_layer_rule_based_symbol_layer(client): """ Test rule based layer, simple conversion from categorized. """ qs = dict(BASE_QUERY) qs['MAP'] = PROJECT_INVALID @@ -196,9 +231,13 @@ def test_invalid_layer(client): assert b['title'] == '' assert len(b['nodes']) == 1, b assert b['nodes'][0]['title'] == 'rule_based' + assert b['nodes'][0].get('valid') is False + assert b['nodes'][0].get('icon') == GetLegendGraphicFilter.warning_icon() assert 'icon' in b['nodes'][0] assert 'symbols' not in b['nodes'][0] + +def test_invalid_layer_categorized_symbol_layer_feature_count(client): """ Test categorized symbol for layer with SHOW FEATURE COUNT. """ qs = dict(BASE_QUERY) qs['MAP'] = PROJECT_INVALID @@ -210,5 +249,7 @@ def test_invalid_layer(client): assert b['title'] == '' assert len(b['nodes']) == 1, b assert b['nodes'][0]['title'] == 'categorized' + assert b['nodes'][0].get('valid') is False + assert b['nodes'][0].get('icon') == GetLegendGraphicFilter.warning_icon() assert 'icon' in b['nodes'][0] assert 'symbols' not in b['nodes'][0]