From d732db6167030263dccd99b34d9ee0f2003d6497 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Fri, 15 Mar 2024 13:59:43 +0100 Subject: [PATCH 1/2] Tests - Add basic test for the regexp --- lizmap_server/get_legend_graphic.py | 14 ++++++++++++-- test/test_legend.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lizmap_server/get_legend_graphic.py b/lizmap_server/get_legend_graphic.py index e337be91..d011dd31 100644 --- a/lizmap_server/get_legend_graphic.py +++ b/lizmap_server/get_legend_graphic.py @@ -7,6 +7,7 @@ import json import re +from typing import Optional from qgis.core import Qgis, QgsDataSourceUri, QgsProject from qgis.server import QgsServerFilter @@ -21,6 +22,13 @@ class GetLegendGraphicFilter(QgsServerFilter): only works for single LAYER and STYLE(S) and json format. """ + FEATURE_COUNT_REGEXP = r"(.*) \[≈?(?:\d+|N/A)\]" + + @classmethod + 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) + @exception_handler def responseComplete(self): @@ -129,11 +137,13 @@ def responseComplete(self): symbol = symbols[idx] symbol_label = symbol['title'] if show_feature_count: - match_label = re.match(r"(.*) \[≈?(?:\d+|N/A)\]", symbol_label) + match_label = self.match_label_feature_count(symbol_label) if match_label: symbol_label = match_label.group(1) else: - logger.info("GetLegendGraphic JSON: symbol label does not match '(.*) \\[≈?(?:\\d+|N/A)\\]' '{}'".format(symbol['title'])) + logger.info( + "GetLegendGraphic JSON: symbol label does not match '{}' '{}'".format( + self.FEATURE_COUNT_REGEXP, symbol['title'])) try: category = categories[symbol_label] symbol['ruleKey'] = category['ruleKey'] diff --git a/test/test_legend.py b/test/test_legend.py index 5005f1c5..aa3d7bac 100644 --- a/test/test_legend.py +++ b/test/test_legend.py @@ -1,6 +1,7 @@ import logging from test.utils import _build_query_string, _check_request +from lizmap_server.get_legend_graphic import GetLegendGraphicFilter from qgis.core import Qgis @@ -137,3 +138,15 @@ def test_simple_rule_based_feature_count(client): assert symbols[0]['expression'] == expected, symbols[0]['expression'] assert b['title'] == '' assert b['nodes'][0]['title'] == 'rule_based [4]', b['nodes'][0]['title'] + + +def test_regexp_feature_count(): + """ Test the regexp about the feature count. """ + result = GetLegendGraphicFilter.match_label_feature_count("A label [22]") + assert result.group(1) == "A label", result.group(1) + + result = GetLegendGraphicFilter.match_label_feature_count("A label [≈2]") + assert result.group(1) == "A label", result.group(1) + + result = GetLegendGraphicFilter.match_label_feature_count("A label") + assert result is None From b0d06908f5e1a63998708ed772879f8b134b3a3f Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Fri, 15 Mar 2024 14:33:25 +0100 Subject: [PATCH 2/2] Fix tests for QGIS 3.36, "name" is now provided in GetFeatureInfo --- lizmap_server/get_legend_graphic.py | 1 + test/test_get_feature_info.py | 131 +++++++++++++++------------- test/test_legend.py | 3 +- 3 files changed, 75 insertions(+), 60 deletions(-) diff --git a/lizmap_server/get_legend_graphic.py b/lizmap_server/get_legend_graphic.py index d011dd31..227ef538 100644 --- a/lizmap_server/get_legend_graphic.py +++ b/lizmap_server/get_legend_graphic.py @@ -7,6 +7,7 @@ import json import re + from typing import Optional from qgis.core import Qgis, QgsDataSourceUri, QgsProject diff --git a/test/test_get_feature_info.py b/test/test_get_feature_info.py index 5110643c..86a90b90 100644 --- a/test/test_get_feature_info.py +++ b/test/test_get_feature_info.py @@ -1,6 +1,7 @@ import logging import xml.etree.ElementTree as ET +from qgis.core import Qgis from xmldiff import main as xml_diff LOGGER = logging.getLogger('server') @@ -52,10 +53,12 @@ def test_no_get_feature_info_default_popup(client): rv = client.get(qs, PROJECT) assert rv.status_code == 200 assert rv.headers.get('Content-Type', '').find('text/xml') == 0 - expected = f''' - - -''' + title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else '' + expected = f''' + + + + ''' diff = xml_diff.diff_texts(expected, rv.content) assert diff == [], diff @@ -66,21 +69,23 @@ def test_single_get_feature_info_default_popup(client): rv = client.get(qs, PROJECT) assert rv.status_code == 200 assert rv.headers.get('Content-Type', '').find('text/xml') == 0 - expected = f''' - - - - - - - - - - - - - -''' + title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else '' + expected = f''' + + + + + + + + + + + + + + + ''' diff = xml_diff.diff_texts(expected, rv.content) assert diff == [], diff @@ -92,21 +97,23 @@ def test_single_get_feature_info_default_popup_user(client): rv = client.get(qs, PROJECT, headers) assert rv.status_code == 200 assert rv.headers.get('Content-Type', '').find('text/xml') == 0 - expected = f''' - - - - - - - - - - - - - -''' + title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else '' + expected = f''' + + + + + + + + + + + + + + + ''' diff = xml_diff.diff_texts(expected, rv.content) assert diff == [], diff @@ -118,21 +125,24 @@ def test_single_get_feature_info_qgis_popup(client): assert rv.status_code == 200 assert rv.headers.get('Content-Type', '').find('text/xml') == 0 + title_attribute = f'title="{LAYER_QGIS_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else '' + # Note the line maptip - expected = f''' - - - - - - - - - - - - -''' + expected = f''' + + + + + + + + + + + + + + ''' diff = xml_diff.diff_texts(expected, rv.content) assert diff == [], diff @@ -156,17 +166,20 @@ def test_single_get_feature_info_form_popup(client): xml_lines = ET.tostring(root, encoding='utf8', method='xml').decode("utf-8").split('\n') xml_string = '\n'.join(xml_lines[1:]) - expected = f''' - - - - - - - - - - + title_attribute = f'title="{LAYER_QGIS_FORM}"' if Qgis.QGIS_VERSION_INT >= 33600 else '' + + expected = f''' + + + + + + + + + + + ''' diff = xml_diff.diff_texts(expected, xml_string.strip()) diff --git a/test/test_legend.py b/test/test_legend.py index aa3d7bac..f87f6149 100644 --- a/test/test_legend.py +++ b/test/test_legend.py @@ -1,10 +1,11 @@ import logging from test.utils import _build_query_string, _check_request -from lizmap_server.get_legend_graphic import GetLegendGraphicFilter from qgis.core import Qgis +from lizmap_server.get_legend_graphic import GetLegendGraphicFilter + LOGGER = logging.getLogger('server') __copyright__ = 'Copyright 2023, 3Liz'