From 1790404d81bdbadd5168b6407f09d107abbc859c Mon Sep 17 00:00:00 2001 From: amercader Date: Tue, 4 Jun 2024 15:44:48 +0200 Subject: [PATCH] [#56] Fix spatial_resolution validators --- ckanext/dcat/tests/test_scheming_support.py | 15 ++++++---- ckanext/dcat/tests/utils.py | 32 ++++++++++++--------- ckanext/dcat/validators.py | 20 ++++++++----- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/ckanext/dcat/tests/test_scheming_support.py b/ckanext/dcat/tests/test_scheming_support.py index 2d66d716..d9ec2efc 100644 --- a/ckanext/dcat/tests/test_scheming_support.py +++ b/ckanext/dcat/tests/test_scheming_support.py @@ -241,11 +241,12 @@ def test_e2e_ckan_to_dcat(self): == dataset["applicable_legislation"] ) - # TODO: enable after validator - # assert ( - # self._triples_list_values(g, dataset_ref, DCAT.spatialResolutionInMeters) - # == dataset["spatial_resolution_in_meters"] - # ) + assert ( + self._triples_list_python_values( + g, dataset_ref, DCAT.spatialResolutionInMeters + ) + == dataset["spatial_resolution_in_meters"] + ) # Repeating subfields @@ -647,6 +648,10 @@ def test_e2e_dcat_to_ckan(self): "P1D", "PT15M", ] + assert sorted(dataset["spatial_resolution_in_meters"]) == [ + 1.5, + 2.0, + ] assert sorted(dataset["is_referenced_by"]) == [ "https://doi.org/10.1038/sdata.2018.22", "test_isreferencedby", diff --git a/ckanext/dcat/tests/utils.py b/ckanext/dcat/tests/utils.py index 8c0e8a18..53618366 100644 --- a/ckanext/dcat/tests/utils.py +++ b/ckanext/dcat/tests/utils.py @@ -4,32 +4,32 @@ class BaseParseTest(object): - def _extras(self, dataset): extras = {} - for extra in dataset.get('extras'): - extras[extra['key']] = extra['value'] + for extra in dataset.get("extras"): + extras[extra["key"]] = extra["value"] return extras def _get_file_contents(self, file_name): - path = os.path.join(os.path.dirname(__file__), - '..', '..', '..', 'examples', - file_name) - with open(path, 'r') as f: + path = os.path.join( + os.path.dirname(__file__), "..", "..", "..", "examples", file_name + ) + with open(path, "r") as f: return f.read() class BaseSerializeTest(object): - def _extras(self, dataset): extras = {} - for extra in dataset.get('extras'): - extras[extra['key']] = extra['value'] + for extra in dataset.get("extras"): + extras[extra["key"]] = extra["value"] return extras def _triples(self, graph, subject, predicate, _object, data_type=None): - if not (isinstance(_object, URIRef) or isinstance(_object, BNode) or _object is None): + if not ( + isinstance(_object, URIRef) or isinstance(_object, BNode) or _object is None + ): if data_type: _object = Literal(_object, datatype=data_type) else: @@ -42,7 +42,13 @@ def _triple(self, graph, subject, predicate, _object, data_type=None): return triples[0] if triples else None def _triples_list_values(self, graph, subject, predicate): - return [str(t[2]) for t in graph.triples((subject, predicate, None))] + return [str(t[2]) for t in graph.triples((subject, predicate, None))] + + def _triples_list_python_values(self, graph, subject, predicate): + return [ + t[2].value if isinstance(t[2], Literal) else str(t[2]) + for t in graph.triples((subject, predicate, None)) + ] def _get_typed_list(self, list, datatype): """ returns the list with the given rdf type """ @@ -51,6 +57,6 @@ def _get_typed_list(self, list, datatype): def _get_dict_from_list(self, dict_list, key, value): """ returns the dict with the given key-value """ for dict in dict_list: - if(dict.get(key) == value): + if dict.get(key) == value: return dict return None diff --git a/ckanext/dcat/validators.py b/ckanext/dcat/validators.py index 9e3e110a..4db20cdb 100644 --- a/ckanext/dcat/validators.py +++ b/ckanext/dcat/validators.py @@ -1,4 +1,3 @@ -import numbers import json from ckantoolkit import ( @@ -34,14 +33,21 @@ def _scheming_multiple_number(key, data, errors, context): return value = data[key] - # 1. list of strings or 2. single string if value is not missing: + if not isinstance(value, list): - try: - value = [float(value)] - except ValueError: - errors[key].append(_("expecting list of numbers")) - raise StopOnError + if isinstance(value, str) and value.startswith("["): + try: + value = json.loads(value) + except ValueError: + errors[key].append(_("Could not parse value")) + raise StopOnError + else: + try: + value = [float(value)] + except ValueError: + errors[key].append(_("expecting list of numbers")) + raise StopOnError out = [] for element in value: