Skip to content

Commit

Permalink
Merge pull request #258 from GovDataOfficial/improve-access-service-t…
Browse files Browse the repository at this point in the history
…ests

Improves access service tests
  • Loading branch information
amercader authored Oct 4, 2023
2 parents 93bf901 + 47a1633 commit 33498df
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased](https://github.com/ckan/ckanext-dcat/compare/v1.5.1...HEAD)

* Improve access service tests (#258)
* Fix missing access service items when parsing dataset (#256)

## [v1.5.1](https://github.com/ckan/ckanext-dcat/compare/v1.5.0...v1.5.1) - 2023-06-20

Expand Down
59 changes: 59 additions & 0 deletions ckanext/dcat/tests/test_euro_dcatap_2_profile_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,65 @@ def test_parse_distribution_access_service_literal(self):

self._run_parse_access_service(expected_access_services)

def test_dataset_distribution_access_service_list_values_only(self):

data = '''<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
xmlns:dct="http://purl.org/dc/terms/"
xmlns:dcat="http://www.w3.org/ns/dcat#"
xmlns:dcatap="http://data.europa.eu/r5r/"
xmlns:schema="http://schema.org/"
xmlns:time="http://www.w3.org/2006/time"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<dcat:Dataset rdf:about="http://example.org">
<dcat:distribution>
<dcat:Distribution rdf:about="https://data.some.org/catalog/datasets/9df8df51-63db-37a8-e044-0003ba9b0d98/1">
<dct:description>Das ist eine deutsche Beschreibung der Distribution</dct:description>
<dct:title>Download WFS Naturräume Geest und Marsch (GML)</dct:title>
<dcat:accessService>
<dcat:DataService>
<dcat:endpointURL rdf:resource="http://publications.europa.eu/webapi/rdf/sparql"/>
<dcat:servesDataset rdf:resource="http://example.org" />
</dcat:DataService>
</dcat:accessService>
</dcat:Distribution>
</dcat:distribution>
</dcat:Dataset>
</rdf:RDF>
'''

p = RDFParser(profiles=DCAT_AP_PROFILES)

p.parse(data)

datasets = [d for d in p.datasets()]

assert len(datasets) == 1

dataset = datasets[0]

# Resources
assert len(dataset['resources']) == 1

resource = dataset['resources'][0]

# Access services
access_service_list = json.loads(resource.get('access_services'))
assert len(access_service_list) == 1

access_service = access_service_list[0]

# List
endpoint_url_list = access_service.get('endpoint_url')
print(access_service)
assert len(endpoint_url_list) == 1
assert 'http://publications.europa.eu/webapi/rdf/sparql' in endpoint_url_list

serves_dataset_list = access_service.get('serves_dataset')
assert len(serves_dataset_list) == 1
assert 'http://example.org' in serves_dataset_list

def _build_access_services_graph_from_list(self, access_service_list):
"""
Creates an access service graph based on the given list.
Expand Down
45 changes: 45 additions & 0 deletions ckanext/dcat/tests/test_euro_dcatap_2_profile_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,51 @@ def test_access_services_absent(self):
assert len(object_list) == 0
assert 'access_services' not in dataset['resources'][0]

def test_access_services_list_values_only(self):

resource = {
'id': 'c041c635-054f-4431-b647-f9186926d021',
'package_id': '4b6fe9ca-dc77-4cec-92a4-55c6624a5bd6',
'name': 'Distribution name',
'access_services': json.dumps([
{
'endpoint_url': ['http://publications.europa.eu/webapi/rdf/sparql'],
'serves_dataset': ['http://data.europa.eu/88u/dataset/eu-whoiswho-the-official-directory-of-the-european-union']
}
])
}

dataset = {
'id': '4b6fe9ca-dc77-4cec-92a4-55c6624a5bd6',
'name': 'test-dataset',
'title': 'Test DCAT dataset',
'resources': [
resource
]
}

s = RDFSerializer(profiles=DCAT_AP_PROFILES)
g = s.g

dataset_ref = s.graph_from_dataset(dataset)

assert len([t for t in g.triples((dataset_ref, DCAT.distribution, None))]) == 1

distribution = self._triple(g, dataset_ref, DCAT.distribution, None)[2]

assert len([t for t in g.triples((distribution, DCAT.accessService, None))]) == 1

# Access services
access_service_object = self._triple(g, distribution, DCAT.accessService, None)[2]

access_services = json.loads(resource['access_services'])

# Lists
self._assert_values_list(g, access_service_object, DCAT.endpointURL,
self._get_typed_list(access_services[0].get('endpoint_url'), URIRef))
self._assert_values_list(g, access_service_object, DCAT.servesDataset,
self._get_typed_list(access_services[0].get('serves_dataset'), URIRef))

def _assert_simple_value(self, graph, object, predicate, value):
"""
Checks if a triple with the given value is present in the graph
Expand Down

0 comments on commit 33498df

Please sign in to comment.