Skip to content

Commit

Permalink
propertyname filtering support for wfs
Browse files Browse the repository at this point in the history
added property name filtering for django-gisserver

added tests for django-gisserver PROPERTYNAME

ruff checks fixed

black fixes

added more descriptive comments to tests
  • Loading branch information
barrydaniels-nl committed Nov 26, 2024
1 parent e33ef38 commit c64ef91
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/dso_api/dynamic_api/views/wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,17 @@ def get_feature_fields(self, model, main_geometry_field_name) -> list[FieldDef]:
)
)

return fields + other_geo_fields
all_fields = fields + other_geo_fields

# Filter fields if propertyname is specified
if "PROPERTYNAME" in self.KVP:
property_names = {name.strip() for name in self.KVP["PROPERTYNAME"].split(",")}
return [
field for field in all_fields
if field.name == main_geometry_field_name or field.name in property_names
]

return all_fields

def get_expanded_fields(self, model) -> list[FieldDef]:
"""Define which fields to include in an expanded relation.
Expand Down
68 changes: 67 additions & 1 deletion src/tests/test_dynamic_api/views/test_wfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from schematools.contrib.django.db import create_tables

from tests.utils import read_response, read_response_xml, xml_element_to_dict
from tests.utils import read_response, read_response_json, read_response_xml, xml_element_to_dict


@pytest.mark.django_db
Expand Down Expand Up @@ -224,6 +224,72 @@ def test_wfs_non_default_dataset_not_exposed(
response = api_client.get(wfs_url)
assert response.status_code == 404

def test_wfs_propertyname_filter(self, api_client, fietspaaltjes_model, fietspaaltjes_data):
"""Test that propertyname parameter correctly filters fields."""

fietspaaltjes_data.save()

wfs_url = (
"/v1/wfs/fietspaaltjes/"
"?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=fietspaaltjes"
"&PROPERTYNAME=at,area,score_current"
"&OUTPUTFORMAT=geojson"
)

response = api_client.get(wfs_url)
assert response.status_code == 200

data = read_response_json(response)
assert "features" in data, "Response should contain 'features' key"
assert len(data["features"]) > 0, "Response should contain at least one feature"

properties = data["features"][0]["properties"]
assert "at" in properties
assert "area" in properties
assert "score_current" in properties
assert "ruimte" not in properties

def test_wfs_propertyname_empty(
self, api_client, fietspaaltjes_model_no_display, fietspaaltjes_data_no_display
):
"""Test that empty propertyname return no properties."""

fietspaaltjes_data_no_display.save()

wfs_url = (
"/v1/wfs/fietspaaltjesnodisplay/"
"?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=fietspaaltjesnodisplay"
"&PROPERTYNAME="
"&OUTPUTFORMAT=geojson"
)

response = api_client.get(wfs_url)
assert response.status_code == 200

data = read_response_json(response)

assert "features" in data
assert len(data["features"]) > 0
assert len(data["features"][0]["properties"]) == 0

def test_wfs_propertyname_invalid_field(
self, api_client, fietspaaltjes_model_no_display, fietspaaltjes_data_no_display
):
"""Test behavior with invalid field in propertyname."""

fietspaaltjes_data_no_display.save()

wfs_url = (
"/v1/wfs/fietspaaltjesnodisplay/"
"?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=fietspaaltjesnodisplay"
"&PROPERTYNAME=nonexistent_field"
"&OUTPUTFORMAT=geojson"
)

response = api_client.get(wfs_url)
assert response.status_code == 400
assert "InvalidParameterValue" in response.content.decode("utf-8")


@pytest.mark.django_db
class TestDatasetWFSViewAuth:
Expand Down

0 comments on commit c64ef91

Please sign in to comment.