Skip to content

Commit

Permalink
[#56] Fix spatial_resolution validators
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jun 4, 2024
1 parent 4763d2b commit 1790404
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
15 changes: 10 additions & 5 deletions ckanext/dcat/tests/test_scheming_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down
32 changes: 19 additions & 13 deletions ckanext/dcat/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 """
Expand All @@ -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
20 changes: 13 additions & 7 deletions ckanext/dcat/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numbers
import json

from ckantoolkit import (
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 1790404

Please sign in to comment.