Skip to content

Commit

Permalink
Move dataset dict to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jul 12, 2024
1 parent 61bc89d commit a71b5ab
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 132 deletions.
149 changes: 17 additions & 132 deletions ckanext/dcat/tests/test_shacl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
from random import randrange

Expand All @@ -7,152 +8,32 @@
from ckan.tests.helpers import call_action

from ckanext.dcat.processors import RDFSerializer


dataset_dict = {
# Core fields
"name": "test-dataset-shacl",
"title": "Test DCAT dataset",
"notes": "Lorem ipsum",
"url": "http://example.org/ds1",
"version": "1.0b",
"tags": [{"name": "Tag 1"}, {"name": "Tag 2"}],
# Standard fields
"issued": "2024-05-01",
"modified": "2024-05-05",
"identifier": "xx-some-dataset-id-yy",
"frequency": "monthly",
"provenance": "Statement about provenance",
"dcat_type": "test-type",
"version_notes": "Some version notes",
"access_rights": "Statement about access rights",
# List fields (lists)
"alternate_identifier": ["alt-id-1", "alt-id-2"],
"theme": [
"https://example.org/uri/theme1",
"https://example.org/uri/theme2",
"https://example.org/uri/theme3",
],
"language": ["en", "ca", "es"],
"documentation": ["https://example.org/some-doc.html"],
"conforms_to": ["Standard 1", "Standard 2"],
"is_referenced_by": [
"https://doi.org/10.1038/sdata.2018.22",
"test_isreferencedby",
],
"applicable_legislation": [
"http://data.europa.eu/eli/reg_impl/2023/138/oj",
"http://data.europa.eu/eli/reg_impl/2023/138/oj_alt",
],
# Repeating subfields
"contact": [
{"name": "Contact 1", "email": "contact1@example.org"},
{"name": "Contact 2", "email": "contact2@example.org"},
],
"publisher": [
{
"name": "Test Publisher",
"email": "publisher@example.org",
"url": "https://example.org",
"type": "public_body",
},
],
"temporal_coverage": [
{"start": "1905-03-01", "end": "2013-01-05"},
],
"temporal_resolution": "PT15M",
"spatial_coverage": [
{
"geom": {
"type": "Polygon",
"coordinates": [
[
[11.9936, 54.0486],
[11.9936, 54.2466],
[12.3045, 54.2466],
[12.3045, 54.0486],
[11.9936, 54.0486],
]
],
},
"text": "Tarragona",
"uri": "https://sws.geonames.org/6361390/",
"bbox": {
"type": "Polygon",
"coordinates": [
[
[-2.1604, 42.7611],
[-2.0938, 42.7611],
[-2.0938, 42.7931],
[-2.1604, 42.7931],
[-2.1604, 42.7611],
]
],
},
"centroid": {"type": "Point", "coordinates": [1.26639, 41.12386]},
}
],
"spatial_resolution_in_meters": 1.5,
"resources": [
{
"name": "Resource 1",
"description": "Some description",
"url": "https://example.com/data.csv",
"format": "CSV",
"availability": "http://publications.europa.eu/resource/authority/planned-availability/EXPERIMENTAL",
"compress_format": "http://www.iana.org/assignments/media-types/application/gzip",
"package_format": "http://publications.europa.eu/resource/authority/file-type/TAR",
"size": 12323,
"hash": "4304cf2e751e6053c90b1804c89c0ebb758f395a",
"hash_algorithm": "http://spdx.org/rdf/terms#checksumAlgorithm_sha1",
"status": "http://purl.org/adms/status/Completed",
"access_url": "https://example.com/data.csv",
"download_url": "https://example.com/data.csv",
"issued": "2024-05-01T01:20:33",
"modified": "2024-05-05T09:33:20",
"license": "http://creativecommons.org/licenses/by/3.0/",
"rights": "Some stament about rights",
"language": ["en", "ca", "es"],
"access_services": [
{
"title": "Access Service 1",
"endpoint_description": "https://example.org/endpoint_description",
"endpoint_url": [
"https://example.org/access_service/1",
"https://example.org/access_service/2",
],
"serves_dataset": [
"https://example.org/dataset/1",
"https://example.org/dataset/2",
],
}
],
}
],
}
from ckanext.dcat.tests.utils import get_file_contents


def _get_shacl_file_path(file_name):
return os.path.join(os.path.dirname(__file__), "shacl", file_name)


generated_graph = None
generated_graphs = {}


@pytest.fixture
def graph():
global generated_graph
def graph_from_dataset(file_name):
global generated_graphs

if not generated_graph:
if not generated_graphs.get(file_name):
if not file_name.startswith("ckan/"):
file_name = "ckan/" + file_name
dataset_dict = json.loads(get_file_contents(file_name))
dataset_dict["name"] += "-" + str(randrange(0, 1000))
dataset = call_action("package_create", **dataset_dict)

s = RDFSerializer()
s.graph_from_dataset(dataset)

generated_graph = s.g
generated_graphs[file_name] = s.g

return generated_graph
return generated_graphs[file_name]


@pytest.mark.usefixtures("with_plugins")
Expand All @@ -167,7 +48,9 @@ def graph():
@pytest.mark.ckan_config(
"ckanext.dcat.rdf.profiles", "euro_dcat_ap_2 euro_dcat_ap_scheming"
)
def test_validate_dcat_ap_2_graph_shapes(graph):
def test_validate_dcat_ap_2_graph_shapes():

graph = graph_from_dataset("ckan_full_dataset_dcat_ap_2.json")

# dcat-ap_2.1.1_shacl_shapes.ttl: constraints concerning existance, domain and
# literal range, and cardinalities.
Expand All @@ -189,7 +72,9 @@ def test_validate_dcat_ap_2_graph_shapes(graph):
@pytest.mark.ckan_config(
"ckanext.dcat.rdf.profiles", "euro_dcat_ap_2 euro_dcat_ap_scheming"
)
def test_validate_dcat_ap_2_graph_shapes_recommended(graph):
def test_validate_dcat_ap_2_graph_shapes_recommended():

graph = graph_from_dataset("ckan_full_dataset_dcat_ap_2.json")

# dcat-ap_2.1.1_shacl_shapes_recommended.ttl: constraints concerning existance
# of recommended properties.
Expand Down
185 changes: 185 additions & 0 deletions examples/ckan/ckan_full_dataset_dcat_ap_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
{
"name": "test-dataset-shacl",
"title": "Test DCAT dataset",
"notes": "Lorem ipsum",
"url": "http://example.org/ds1",
"version": "1.0b",
"tags": [
{
"name": "Tag 1"
},
{
"name": "Tag 2"
}
],
"issued": "2024-05-01",
"modified": "2024-05-05",
"identifier": "xx-some-dataset-id-yy",
"frequency": "monthly",
"provenance": "Statement about provenance",
"dcat_type": "test-type",
"version_notes": "Some version notes",
"access_rights": "Statement about access rights",
"alternate_identifier": [
"alt-id-1",
"alt-id-2"
],
"theme": [
"https://example.org/uri/theme1",
"https://example.org/uri/theme2",
"https://example.org/uri/theme3"
],
"language": [
"en",
"ca",
"es"
],
"documentation": [
"https://example.org/some-doc.html"
],
"conforms_to": [
"Standard 1",
"Standard 2"
],
"is_referenced_by": [
"https://doi.org/10.1038/sdata.2018.22",
"test_isreferencedby"
],
"applicable_legislation": [
"http://data.europa.eu/eli/reg_impl/2023/138/oj",
"http://data.europa.eu/eli/reg_impl/2023/138/oj_alt"
],
"contact": [
{
"name": "Contact 1",
"email": "contact1@example.org"
},
{
"name": "Contact 2",
"email": "contact2@example.org"
}
],
"publisher": [
{
"name": "Test Publisher",
"email": "publisher@example.org",
"url": "https://example.org",
"type": "public_body"
}
],
"temporal_coverage": [
{
"start": "1905-03-01",
"end": "2013-01-05"
}
],
"temporal_resolution": "PT15M",
"spatial_coverage": [
{
"geom": {
"type": "Polygon",
"coordinates": [
[
[
11.9936,
54.0486
],
[
11.9936,
54.2466
],
[
12.3045,
54.2466
],
[
12.3045,
54.0486
],
[
11.9936,
54.0486
]
]
]
},
"text": "Tarragona",
"uri": "https://sws.geonames.org/6361390/",
"bbox": {
"type": "Polygon",
"coordinates": [
[
[
-2.1604,
42.7611
],
[
-2.0938,
42.7611
],
[
-2.0938,
42.7931
],
[
-2.1604,
42.7931
],
[
-2.1604,
42.7611
]
]
]
},
"centroid": {
"type": "Point",
"coordinates": [
1.26639,
41.12386
]
}
}
],
"spatial_resolution_in_meters": 1.5,
"resources": [
{
"name": "Resource 1",
"description": "Some description",
"url": "https://example.com/data.csv",
"format": "CSV",
"availability": "http://publications.europa.eu/resource/authority/planned-availability/EXPERIMENTAL",
"compress_format": "http://www.iana.org/assignments/media-types/application/gzip",
"package_format": "http://publications.europa.eu/resource/authority/file-type/TAR",
"size": 12323,
"hash": "4304cf2e751e6053c90b1804c89c0ebb758f395a",
"hash_algorithm": "http://spdx.org/rdf/terms#checksumAlgorithm_sha1",
"status": "http://purl.org/adms/status/Completed",
"access_url": "https://example.com/data.csv",
"download_url": "https://example.com/data.csv",
"issued": "2024-05-01T01:20:33",
"modified": "2024-05-05T09:33:20",
"license": "http://creativecommons.org/licenses/by/3.0/",
"rights": "Some stament about rights",
"language": [
"en",
"ca",
"es"
],
"access_services": [
{
"title": "Access Service 1",
"endpoint_description": "https://example.org/endpoint_description",
"endpoint_url": [
"https://example.org/access_service/1",
"https://example.org/access_service/2"
],
"serves_dataset": [
"https://example.org/dataset/1",
"https://example.org/dataset/2"
]
}
]
}
]
}

0 comments on commit a71b5ab

Please sign in to comment.