From 20979c137f35b8313815ae6db05bb3a33ec7fbf7 Mon Sep 17 00:00:00 2001 From: sethg Date: Sun, 25 May 2025 23:51:56 +0200 Subject: [PATCH] Add None guard when checking for XML list elements with test --- owslib/util.py | 5 +- ...wfs_mapserver_demo_getcapabilities_100.xml | 103 ++++++++++++++++++ tests/test_wfs_schema.py | 39 ++++++- 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 tests/resources/wfs_mapserver_demo_getcapabilities_100.xml diff --git a/owslib/util.py b/owslib/util.py index 12c73400..8aefd079 100644 --- a/owslib/util.py +++ b/owslib/util.py @@ -692,9 +692,12 @@ def extract_time(element): def extract_xml_list(elements): """ -Some people don't have seperate tags for their keywords and seperate them with +Some people don't have separate tags for their keywords and separate them with a newline. This will extract out all of the keywords correctly. """ + if elements is None: + return [] + keywords = (re.split(r'[\n\r]+', f.text) for f in elements if f.text) flattened = (item.strip() for sublist in keywords for item in sublist) remove_blank = [_f for _f in flattened if _f] diff --git a/tests/resources/wfs_mapserver_demo_getcapabilities_100.xml b/tests/resources/wfs_mapserver_demo_getcapabilities_100.xml new file mode 100644 index 00000000..416e4cc7 --- /dev/null +++ b/tests/resources/wfs_mapserver_demo_getcapabilities_100.xml @@ -0,0 +1,103 @@ + + + + MapServer WFS + WFS Demo Server for MapServer + This demonstration server showcases MapServer (www.mapserver.org) and its OGC support + https://demo.mapserver.org/cgi-bin/wfs? + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + continents + World continents + EPSG:4326 + + https://demo.mapserver.org/cgi-bin/wfs?request=GetMetadata&layer=continents + + + cities + World cities + EPSG:4326 + + https://demo.mapserver.org/cgi-bin/wfs?request=GetMetadata&layer=cities + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/test_wfs_schema.py b/tests/test_wfs_schema.py index e0ccfd5d..80a2e206 100644 --- a/tests/test_wfs_schema.py +++ b/tests/test_wfs_schema.py @@ -8,6 +8,29 @@ WFS_SERVICE_URL = 'https://www.dov.vlaanderen.be/geoserver/wfs?request=GetCapabilities' +@pytest.fixture +def mp_wfs_100(monkeypatch): + """Monkeypatch the call to the remote GetCapabilities request of WFS + version 1.0.0. + + Parameters + ---------- + monkeypatch : pytest.fixture + PyTest monkeypatch fixture. + + """ + def read(*args, **kwargs): + with open('tests/resources/wfs_mapserver_demo_getcapabilities_100.xml', 'r') as f: + data = f.read() + if type(data) is not bytes: + data = data.encode('utf-8') + data = etree.fromstring(data) + return data + + monkeypatch.setattr( + owslib.feature.common.WFSCapabilitiesReader, 'read', read) + + @pytest.fixture def mp_wfs_110(monkeypatch): """Monkeypatch the call to the remote GetCapabilities request of WFS @@ -122,7 +145,21 @@ def test_schema_result(self, wfs_version): class TestOffline(object): """Class grouping offline tests for the WFS get_schema method.""" - def test_get_schema(self, mp_wfs_110, mp_remote_describefeaturetype): + def test_get_schema_100(self, mp_wfs_100): + """Test the get_schema method for a standard schema. + + Parameters + ---------- + mp_wfs_100 : pytest.fixture + Monkeypatch the call to the remote GetCapabilities request. + """ + wfs100 = WebFeatureService(WFS_SERVICE_URL, version='1.0.0') + assert wfs100.identification.title == 'WFS Demo Server for MapServer' + assert wfs100.identification.keywords == [] + assert list(wfs100.contents) == ['continents', 'cities'] + + + def test_get_schema(self, mp_wfs_100, mp_remote_describefeaturetype): """Test the get_schema method for a standard schema. Parameters