Skip to content

Commit

Permalink
Merge pull request #67 from Gustry/qgis-3-36
Browse files Browse the repository at this point in the history
Add tests and fix tests for QGIS 3.36
  • Loading branch information
Gustry authored Mar 15, 2024
2 parents 61e4d39 + b0d0690 commit 21190a0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 61 deletions.
15 changes: 13 additions & 2 deletions lizmap_server/get_legend_graphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import json
import re

from typing import Optional

from qgis.core import Qgis, QgsDataSourceUri, QgsProject
from qgis.server import QgsServerFilter

Expand All @@ -21,6 +23,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):

Expand Down Expand Up @@ -129,11 +138,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']
Expand Down
131 changes: 72 additions & 59 deletions test/test_get_feature_info.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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'''<GetFeatureInfoResponse>
<Layer name="{LAYER_DEFAULT_POPUP}"/>
</GetFeatureInfoResponse>
'''
title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
expected = f'''
<GetFeatureInfoResponse>
<Layer name="{LAYER_DEFAULT_POPUP}" {title_attribute}/>
</GetFeatureInfoResponse>
'''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff

Expand All @@ -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'''<GetFeatureInfoResponse>
<Layer name="{LAYER_DEFAULT_POPUP}">
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
<Attribute name="lwc_user" value="No user provided"/>
<Attribute name="lwc_groups" value="No user groups provided"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
expected = f'''
<GetFeatureInfoResponse>
<Layer name="{LAYER_DEFAULT_POPUP}" {title_attribute}>
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
<Attribute name="lwc_user" value="No user provided"/>
<Attribute name="lwc_groups" value="No user groups provided"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff

Expand All @@ -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'''<GetFeatureInfoResponse>
<Layer name="{LAYER_DEFAULT_POPUP}">
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
<Attribute name="lwc_user" value="Bretagne"/>
<Attribute name="lwc_groups" value="test1"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
title_attribute = f'title="{LAYER_DEFAULT_POPUP}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''
expected = f'''
<GetFeatureInfoResponse>
<Layer name="{LAYER_DEFAULT_POPUP}" {title_attribute}>
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
<Attribute name="lwc_user" value="Bretagne"/>
<Attribute name="lwc_groups" value="test1"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff

Expand All @@ -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 <TH>maptip</TH>
expected = f'''<GetFeatureInfoResponse>
<Layer name="{LAYER_QGIS_POPUP}">
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
<Attribute name="maptip" value="&lt;p>France&lt;/p>"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
expected = f'''
<GetFeatureInfoResponse>
<Layer name="{LAYER_QGIS_POPUP}" {title_attribute}>
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
<Attribute name="maptip" value="&lt;p>France&lt;/p>"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
diff = xml_diff.diff_texts(expected, rv.content)
assert diff == [], diff

Expand All @@ -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'''<GetFeatureInfoResponse>
<Layer name="{LAYER_QGIS_FORM}">
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
</Feature>
</Layer>
title_attribute = f'title="{LAYER_QGIS_FORM}"' if Qgis.QGIS_VERSION_INT >= 33600 else ''

expected = f'''
<GetFeatureInfoResponse>
<Layer name="{LAYER_QGIS_FORM}" {title_attribute}>
<Feature id="1">
<Attribute name="OBJECTID" value="2662"/>
<Attribute name="NAME_0" value="France"/>
<Attribute name="VARNAME_1" value="Bretaa|Brittany"/>
<Attribute name="Region" value="Bretagne"/>
<Attribute name="Shape_Leng" value="18.39336934850"/>
<Attribute name="Shape_Area" value="3.30646936365"/>
</Feature>
</Layer>
</GetFeatureInfoResponse>
'''
diff = xml_diff.diff_texts(expected, xml_string.strip())
Expand Down
14 changes: 14 additions & 0 deletions test/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -137,3 +139,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

0 comments on commit 21190a0

Please sign in to comment.