Skip to content

Commit

Permalink
collection records endopint
Browse files Browse the repository at this point in the history
  • Loading branch information
lukavdplas authored and jgonggrijp committed Dec 12, 2024
1 parent 0a3d961 commit 9618451
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
5 changes: 3 additions & 2 deletions backend/collect/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from triplestore.constants import EDPOPCOL
from collect.serializers import CollectionSerializer
from collect.permissions import CollectionPermission
from collect.graphs import as_collection_from_records

class CollectionViewSet(ModelViewSet):
'''
Expand Down Expand Up @@ -50,7 +49,7 @@ class CollectionRecordsView(RDFView):
View the records inside a collection
'''

def get_graph(self, request: Request, collection: str, **kwargs):
def get_graph(self, request: Request, collection: str, **kwargs) -> Graph:
collection_uri = URIRef(collection)

if not collection_exists(collection_uri):
Expand All @@ -59,5 +58,7 @@ def get_graph(self, request: Request, collection: str, **kwargs):
collection_obj = EDPOPCollection(collection_graph(collection_uri), collection_uri)

g = Graph()
g += collection_obj._class_triples()
g += EDPOPCollection.records._stored_triples(collection_obj)

return g
55 changes: 49 additions & 6 deletions backend/collect/api_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.test import Client
from rest_framework.status import is_success, is_client_error
from rdflib import URIRef, RDF, Literal
from rdflib import URIRef, RDF, Graph, Literal
from django.conf import settings
from urllib.parse import quote
from typing import Dict

from triplestore.constants import EDPOPCOL, AS
from collect.utils import collection_uri
from projects.models import Project
from collect.rdf_models import EDPOPCollection
from collect.utils import collection_graph

def example_collection_data(project_name) -> Dict:
return {
Expand Down Expand Up @@ -131,9 +133,50 @@ def test_project_validation(db, user, client: Client):
def test_collection_records(db, user, project, client: Client):
client.force_login(user)
create_response = post_collection(client, project.name)
collection_uri = create_response.data['uri']

records_url = '/api/collection-records/' + collection_uri + '/'

collection_uri = URIRef(create_response.data['uri'])

records_url = '/api/collection-records/' + str(collection_uri) + '/'

# check response with empty data
empty_response = client.get(records_url)
assert is_success(empty_response.status_code)
g = Graph().parse(empty_response.content)
result = g.query(f'''
ASK {{
<{collection_uri}> a edpopcol:Collection ;
a as:Collection ;
as:items ?items ;
as:totalItems 0 .
?items rdf:rest rdf:nil .
}}
''',
initNs={'as': AS, 'rdf': RDF, 'edpopcol': EDPOPCOL}
)
assert result.askAnswer

# add some records to the collection
collection_obj = EDPOPCollection(collection_graph(collection_uri), collection_uri)
collection_obj.records = [
URIRef('https://example.com/example1'), URIRef('https://example.com/example2')
]
collection_obj.save()

# check response contains records
response = client.get(records_url)
assert is_success(response.status_code)
assert is_success(response.status_code)
g = Graph().parse(response.content)
result = g.query(f'''
ASK {{
<{collection_uri}> a edpopcol:Collection ;
a as:Collection ;
as:items ?items ;
as:totalItems 2 .
?items rdf:first <https://example.com/example1> ;
rdf:rest ?rest .
?rest rdf:first <https://example.com/example2> ;
rdf:rest rdf:nil .
}}
''',
initNs={'as': AS, 'rdf': RDF, 'edpopcol': EDPOPCOL}
)
assert result.askAnswer
3 changes: 1 addition & 2 deletions backend/collect/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def list_to_graph_collection(items: List[Node], items_node: IdentifiedNode) -> G

g = Graph()
collection = g.collection(items_node)
for item in items:
collection.append(item)
collection += items
return g


Expand Down

0 comments on commit 9618451

Please sign in to comment.