Skip to content

Commit

Permalink
feat: add validation of attribute-topic combination
Browse files Browse the repository at this point in the history
#810

Co-authored-by: Sascha Fendrich <[email protected]>
  • Loading branch information
mmerdes and Sascha Fendrich committed Aug 28, 2024
1 parent fcb0761 commit 299afc1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ohsome_quality_api/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@
hyphen_to_camel,
json_serialize,
)
from ohsome_quality_api.utils.validators import validate_indicator_topic_combination
from ohsome_quality_api.utils.validators import (
validate_attribute_topic_combination,
validate_indicator_topic_combination,
)

MEDIA_TYPE_GEOJSON = "application/geo+json"
MEDIA_TYPE_JSON = "application/json"
Expand Down Expand Up @@ -286,6 +289,10 @@ async def post_attribute_completeness(
parameters: AttributeCompletenessRequest,
) -> Any:
"""Request the Attribute Completeness indicator for your area of interest."""
validate_attribute_topic_combination(
parameters.attribute_key.value, parameters.topic_key.value
)

return await _post_indicator(request, "attribute-completeness", parameters)


Expand Down
11 changes: 11 additions & 0 deletions ohsome_quality_api/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def __init__(self):
)


class AttributeTopicCombinationError(ValidationError):
"""Invalid attribute topic combination error."""

def __init__(self, attribute, topic):
self.name = "AttributeTopicCombinationError"
self.message = "Invalid combination of attribute and topic: {} and {}".format(
attribute,
topic,
)


class IndicatorTopicCombinationError(ValidationError):
"""Invalid indicator topic combination error."""

Expand Down
13 changes: 13 additions & 0 deletions ohsome_quality_api/utils/validators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from geojson import Feature, FeatureCollection, GeoJSON, MultiPolygon, Polygon
from pyproj import CRS

from ohsome_quality_api.attributes.definitions import (
AttributeEnum,
get_attributes,
)
from ohsome_quality_api.config import get_config_value
from ohsome_quality_api.indicators.definitions import get_valid_indicators
from ohsome_quality_api.topics.definitions import TopicEnum
from ohsome_quality_api.utils.exceptions import (
AttributeTopicCombinationError,
GeoJSONError,
GeoJSONGeometryTypeError,
GeoJSONObjectTypeError,
Expand All @@ -14,6 +20,13 @@
from ohsome_quality_api.utils.helper_geo import calculate_area


def validate_attribute_topic_combination(attribute: AttributeEnum, topic: TopicEnum):
valid_attributes_for_topic = get_attributes()[topic]

if attribute not in valid_attributes_for_topic:
raise AttributeTopicCombinationError(attribute, topic)


def validate_indicator_topic_combination(indicator: str, topic: str):
if indicator not in get_valid_indicators(topic):
raise IndicatorTopicCombinationError(indicator, topic)
Expand Down
19 changes: 19 additions & 0 deletions tests/integrationtests/api/test_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ def test_indicators_attribute_completeness_without_attribute(
assert content["type"] == "RequestValidationError"


def test_indicators_attribute_completeness_with_invalid_attribute_for_topic(
client,
bpolys,
headers,
schema,
):
endpoint = ENDPOINT + "attribute-completeness"
parameters = {
"bpolys": bpolys,
"topic": "building-count",
# the following attribute is not valid for topic 'building-count'
"attribute": "maxspeed",
}
response = client.post(endpoint, json=parameters, headers=headers)
assert response.status_code == 422
content = response.json()
assert content["type"] == "AttributeTopicCombinationError"


@oqapi_vcr.use_cassette
def test_minimal_fc(
client,
Expand Down

0 comments on commit 299afc1

Please sign in to comment.