Skip to content

Commit

Permalink
[#56] Review validators for resource fields
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jun 4, 2024
1 parent c6fc970 commit 99b4c89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
8 changes: 3 additions & 5 deletions ckanext/dcat/schemas/dcat_ap_2.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,21 @@ resource_fields:

- field_name: mimetype
label: Media type
# TODO: get from format
validators: if_empty_guess_format ignore_missing unicode_safe

- field_name: compress_format
label: Compress format
# TODO: media type validator

- field_name: package_format
label: Package format
# TODO: media type validator

- field_name: size
label: Size
# TODO: number validator / snippet
validators: ignore_missing int_validator
form_snippet: number.html

- field_name: hash
label: Hash
# TODO: generate for uploads?

- field_name: hash_algorithm
label: Hash Algorithm
Expand Down
16 changes: 16 additions & 0 deletions ckanext/dcat/templates/scheming/form_snippets/number.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% import 'macros/form.html' as form %}
{% call form.input(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
placeholder=h.scheming_language_text(field.form_placeholder),
type='number',
value=(data.get(field.field_name) or '').split()[0],
error=errors[field.field_name],
classes=field.classes if 'classes' in field else ['control-medium'],
attrs=dict({"class": "form-control"}, **(field.get('form_attrs', {}))),
is_required=h.scheming_field_required(field)
)
%}
{%- snippet 'scheming/form_snippets/help_text.html', field=field -%}
{% endcall %}
33 changes: 32 additions & 1 deletion ckanext/dcat/tests/test_scheming_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ def test_e2e_ckan_to_dcat(self):
URIRef(resource["download_url"]),
)

assert self._triple(g, distribution_ref, DCAT.byteSize, float(resource['size']), XSD.decimal)
assert self._triple(
g, distribution_ref, DCAT.byteSize, float(resource["size"]), XSD.decimal
)
# Checksum
checksum = self._triple(g, distribution_ref, SPDX.checksum, None)[2]
assert checksum
Expand Down Expand Up @@ -539,6 +541,35 @@ def test_legacy_fields(self):
assert self._triple(g, publisher[0][2], FOAF.name, "Test Publisher")


@pytest.mark.usefixtures("with_plugins", "clean_db")
@pytest.mark.ckan_config("ckan.plugins", "dcat scheming_datasets")
@pytest.mark.ckan_config(
"scheming.dataset_schemas", "ckanext.dcat.schemas:dcat_ap_2.1.yaml"
)
@pytest.mark.ckan_config("scheming.presets", "ckanext.scheming:presets.json")
@pytest.mark.ckan_config(
"ckanext.dcat.rdf.profiles", "euro_dcat_ap_2 euro_dcat_ap_scheming"
)
class TestSchemingValidators:
def test_mimetype_is_guessed(self):
dataset_dict = {
"name": "test-dataset-2",
"title": "Test DCAT dataset 2",
"notes": "Lorem ipsum",
"resources": [
{"url": "https://example.org/data.csv"},
{"url": "https://example.org/report.pdf"},
],
}

dataset = call_action("package_create", **dataset_dict)

assert sorted([r["mimetype"] for r in dataset["resources"]]) == [
"application/pdf",
"text/csv",
]


@pytest.mark.usefixtures("with_plugins", "clean_db")
@pytest.mark.ckan_config("ckan.plugins", "dcat scheming_datasets")
@pytest.mark.ckan_config(
Expand Down

0 comments on commit 99b4c89

Please sign in to comment.