Skip to content

Commit

Permalink
added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwigham committed Jan 17, 2024
1 parent 9a5b09f commit 8c5a31a
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 198 deletions.
105 changes: 0 additions & 105 deletions src/apis/resource/ResourceHandler.py

This file was deleted.

112 changes: 101 additions & 11 deletions src/apis/resource/resource_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging

from flask import current_app, request
from flask import current_app, request, render_template
from flask_restx import Namespace, Resource

from apis.resource.ResourceHandler import ResourceHandler
from util.mime_type_util import MimeType
import util.ld_util

from models.ResourceURILevel import ResourceURILevel
from util.APIUtil import APIUtil
from util.mime_type_util import MimeType

logger = logging.getLogger()

Expand Down Expand Up @@ -33,18 +35,106 @@ class ResourceAPI(Resource):

@api.produces([mt.value for mt in MimeType])
def get(self, identifier, cat_type="program"):
resource_handler = ResourceHandler
lod_server_supported_mime_types = [mt.value for mt in MimeType]
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD
mime_type = None
if best_match is not None:
mime_type = MimeType(best_match)

return resource_handler.get_lod_for_resource_in_type(mime_type,
identifier,
current_app.config.get("BENG_DATA_DOMAIN"),
current_app.config.get("SPARQL_ENDPOINT"),
current_app.config.get("URI_NISV_ORGANISATION"),
cat_type)
lod_url = None
try:
lod_url = util.ld_util.generate_lod_resource_uri(
ResourceURILevel(cat_type),
identifier,
current_app.config.get("BENG_DATA_DOMAIN"),
)
except ValueError:
logger.error(
"Could not generate LOD resource URI. Invalid resource level supplied."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid resource level supplied"
)
except Exception as e:
print("test")

if mime_type is MimeType.HTML:
# note that data for HTML is requested from the RDF store, so no need to do is_public_resource
logger.info(f"Generating HTML page for {lod_url}.")
html_page = self._get_lod_view_resource(
lod_url,
current_app.config.get("SPARQL_ENDPOINT"),
current_app.config.get("URI_NISV_ORGANISATION")
)
if html_page:
return APIUtil.toSuccessResponse(html_page)
else:
logger.error(
f"Could not generate an HTML view for {lod_url}.",
)
return APIUtil.toErrorResponse(
"internal_server_error",
"Could not generate an HTML view for this resource.",
)

if mime_type:
logger.info(
f"Getting the RDF in the proper serialization format for {lod_url}."
)
rdf_graph = util.ld_util.get_lod_resource_from_rdf_store(
lod_url,
current_app.config.get("SPARQL_ENDPOINT"),
current_app.config.get("URI_NISV_ORGANISATION")
)
if rdf_graph is not None:
serialised_graph = rdf_graph.serialize(format=mime_type)
if serialised_graph:
return APIUtil.toSuccessResponse(serialised_graph)
else:
return APIUtil.toErrorResponse(
"internal_server_error","Serialisation failed")
else:
return APIUtil.toErrorResponse(
"internal_server_error","No graph created")

logger.error("No mime type was given.")
return APIUtil.toErrorResponse("bad_request", "No mime type detected...")

def _get_lod_view_resource(
self, resource_url: str, sparql_endpoint: str, nisv_organisation_uri: str
):
"""Handler that, given a URI, gets RDF from the SPARQL endpoint and generates an HTML page.
:param resource_url: The URI for the resource.
:param sparql_endpoint - the SPARQL endpoint to get the resource from
:param nisv_organisation_uri - the URI identifying the NISV organisation, for provenance
"""
logger.info(
f"Getting the graph from the triple store for resource {resource_url}."
)
rdf_graph = util.ld_util.get_lod_resource_from_rdf_store(
resource_url, sparql_endpoint, nisv_organisation_uri
)
if rdf_graph:
logger.info(
f"A valid graph ({len(rdf_graph)} triples) was retrieved from the RDF store. "
"Returning a rendered HTML template 'resource.html'."
)
return render_template(
"resource.html",
resource_uri=resource_url,
structured_data=util.ld_util.json_ld_structured_data_for_resource(
rdf_graph, resource_url
),
json_header=util.ld_util.json_header_from_rdf_graph(rdf_graph, resource_url),
json_iri_iri=util.ld_util.json_iri_iri_from_rdf_graph(rdf_graph, resource_url),
json_iri_lit=util.ld_util.json_iri_lit_from_rdf_graph(rdf_graph, resource_url),
json_iri_bnode=util.ld_util.json_iri_bnode_from_rdf_graph(rdf_graph, resource_url),
nisv_sparql_endpoint=sparql_endpoint,
)
logger.error(
f"Triple data for lod view could not be retrieved from the triple store for {resource_url}."
)
return None

44 changes: 35 additions & 9 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from flask import Flask
import json
import os
import pytest

from flask import Flask
from rdflib import Graph

from config import cfg


Expand Down Expand Up @@ -47,6 +49,21 @@ def return_json_data_from_file(test_path, fn):
return return_json_data_from_file


@pytest.fixture(scope="module")
def load_json_file_as_graph():
def return_json_graph_from_file(test_path, fn):
path = test_path
tmp = test_path.split(os.sep)
if len(tmp) > 1:
path = os.sep.join(test_path.split(os.sep)[:-1])
full_path = os.path.join(path, fn)
if os.path.exists(full_path):
g = Graph()
return g.parse(full_path)
return None

return return_json_graph_from_file

@pytest.fixture(scope="module")
def open_file():
"""Returns the contents of a file that is in the test directory."""
Expand All @@ -62,16 +79,18 @@ def return_contents_of_file(test_path, fn):


"""------------------------ APPLICATION SETTINGS (VALID) ----------------------"""


@pytest.fixture(scope="session")
def application_settings():
"""Returns the application settings."""
app = Flask(__name__)
def application():
app = Flask(__name__, template_folder="../templates")
app.config.update(cfg) # merge config with app config
app.config["GLOBAL_CACHE"] = {}
return app.config

return app

@pytest.fixture(scope="session")
def application_settings(application):
"""Returns the application settings."""
return application.config

"""------------------------ APPLICATION CLIENT (VALID & INVALID) ----------------------"""

Expand Down Expand Up @@ -142,12 +161,19 @@ def post(self, mode, path, data=None):
else:
return http_test_client.post(path, data=data)

def get(self, mode, path):
def get(self, mode, path, headers=None):
if mode == "offline":
r = flask_test_client.get(path)
r = flask_test_client.get(path, headers=headers)
return Response(r.data, r.status_code, r.headers)
else:
return http_test_client.get(path)

return GenericClient()


@pytest.fixture(scope="module")
def resource_query_url():
def genResourceURL(type, identifier):
return f"/id/{type}/{identifier}"

return genResourceURL
21 changes: 20 additions & 1 deletion src/tests/unit_tests/apis/resource/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest


@pytest.fixture()
def o_get_schema(open_file):
"""Returns an example version of the schema."""
Expand All @@ -10,3 +9,23 @@ def o_get_schema(open_file):
def i_ob_scene_rdf(load_json_file):
"""Returns RDF of an example Open Beelden item in JSON-LD."""
return load_json_file(__file__, "rdf_ob_scene_2101703040124290024.json")

@pytest.fixture()
def i_scene_graph(load_json_file_as_graph):
"""Returns graph of an example NISV scene in JSON-LD."""
return load_json_file_as_graph(__file__, "rdf_scene_1635932280680.json")

@pytest.fixture()
def i_program_graph(load_json_file_as_graph):
"""Returns graph of an example NISV program in JSON-LD."""
return load_json_file_as_graph(__file__, "rdf_program_1635930242168.json")

@pytest.fixture()
def i_season_graph(load_json_file_as_graph):
"""Returns graph of an example NISV season in JSON-LD."""
return load_json_file_as_graph(__file__, "rdf_season_1635930556031.json")

@pytest.fixture()
def i_series_graph(load_json_file_as_graph):
"""Returns graph of an example NISV series in JSON-LD."""
return load_json_file_as_graph(__file__, "rdf_series_1635930663729.json")
Loading

0 comments on commit 8c5a31a

Please sign in to comment.