-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#56] Add spatial_resolution_in_meters
This required a new scheming_multiple_number validator, adapted from scheming_multiple_text
- Loading branch information
Showing
5 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
import pytest | ||
|
||
from ckantoolkit import StopOnError | ||
from ckanext.dcat.validators import scheming_multiple_number | ||
|
||
|
||
def test_scheming_multiple_number(): | ||
|
||
expected_value = [1.5, 2.0, 0.345] | ||
|
||
key = ("some_number_field",) | ||
errors = {key: []} | ||
|
||
values = [ | ||
expected_value, | ||
[1.5, 2, 0.345], | ||
["1.5", "2", ".345"], | ||
] | ||
for value in values: | ||
data = {key: value} | ||
scheming_multiple_number({}, {})(key, data, errors, {}) | ||
|
||
assert data[key] == json.dumps(expected_value) | ||
|
||
|
||
def test_scheming_multiple_number_single_value(): | ||
|
||
expected_value = [1.5] | ||
|
||
key = ("some_number_field",) | ||
errors = {key: []} | ||
|
||
values = [ | ||
expected_value, | ||
1.5, | ||
"1.5", | ||
] | ||
for value in values: | ||
data = {key: value} | ||
scheming_multiple_number({}, {})(key, data, errors, {}) | ||
|
||
assert data[key] == json.dumps(expected_value) | ||
|
||
|
||
def test_scheming_multiple_number_wrong_value(): | ||
|
||
key = ("some_number_field",) | ||
errors = {key: []} | ||
|
||
values = [ | ||
["a", 2, 0.345], | ||
["1..5", "2", ".345"], | ||
] | ||
for value in values: | ||
with pytest.raises(StopOnError): | ||
data = {key: value} | ||
scheming_multiple_number({}, {})(key, data, errors, {}) | ||
|
||
assert errors[key][0].startswith("invalid type for repeating number") | ||
|
||
errors = {key: []} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import numbers | ||
import json | ||
|
||
from ckantoolkit import ( | ||
missing, | ||
StopOnError, | ||
_, | ||
) | ||
from ckanext.scheming.validation import scheming_validator | ||
|
||
|
||
@scheming_validator | ||
def scheming_multiple_number(field, schema): | ||
""" | ||
Accept repeating numbers input in the following forms and convert to a | ||
json list of decimal values for storage. Also act like scheming_required | ||
to check for at least one non-empty string when required is true: | ||
1. a list of numbers, eg. | ||
[22, 1.3] | ||
2. a single number value to allow single text fields to be | ||
migrated to repeating numbers | ||
33.4 | ||
""" | ||
|
||
def _scheming_multiple_number(key, data, errors, context): | ||
# just in case there was an error before our validator, | ||
# bail out here because our errors won't be useful | ||
if errors[key]: | ||
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 | ||
|
||
out = [] | ||
for element in value: | ||
if not element: | ||
continue | ||
try: | ||
element = float(element) | ||
except ValueError: | ||
errors[key].append( | ||
_("invalid type for repeating number: %r") % element | ||
) | ||
continue | ||
|
||
out.append(element) | ||
|
||
if errors[key]: | ||
raise StopOnError | ||
|
||
data[key] = json.dumps(out) | ||
|
||
if (data[key] is missing or data[key] == "[]") and field.get("required"): | ||
errors[key].append(_("Missing value")) | ||
raise StopOnError | ||
|
||
return _scheming_multiple_number | ||
|
||
|
||
dcat_validators = { | ||
"scheming_multiple_number": scheming_multiple_number, | ||
} |