Skip to content

Commit

Permalink
fixed test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mwigham committed Mar 1, 2024
1 parent d0ee38d commit 7531420
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 74 deletions.
28 changes: 13 additions & 15 deletions src/apis/dataset/dataset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def get(self, number=None):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD # we choose to set a default if the user has not specified
mime_type = (
MimeType.JSON_LD
) # we choose to set a default if the user has not specified
if best_match is not None:
mime_type = MimeType(best_match)

Expand Down Expand Up @@ -125,12 +127,8 @@ def get(self, number=None):
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(
f"Could not fetch the serialization for dataset {dataset_uri}."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid URI or return format"
)
logger.error(f"Could not fetch the serialization for dataset {dataset_uri}.")
return APIUtil.toErrorResponse("bad_request", "Invalid URI or return format")

def is_dataset(self, dataset_uri: str) -> bool:
return DataCatalogLODHandler(current_app.config).is_dataset(dataset_uri)
Expand Down Expand Up @@ -184,7 +182,9 @@ def get(self, number=None):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD # we choose to set a default if the user has not specified
mime_type = (
MimeType.JSON_LD
) # we choose to set a default if the user has not specified
if best_match is not None:
mime_type = MimeType(best_match)

Expand Down Expand Up @@ -219,9 +219,7 @@ def get(self, number=None):
logger.error(
f"Error in fetching the serialization for data catalog: {data_catalog_uri}."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid URI or return format"
)
return APIUtil.toErrorResponse("bad_request", "Invalid URI or return format")

def is_data_catalog(self, data_catalog_uri: str) -> bool:
return DataCatalogLODHandler(current_app.config).is_data_catalog(
Expand Down Expand Up @@ -274,7 +272,9 @@ def get(self, number=None):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD # we choose to set a default if the user has not specified
mime_type = (
MimeType.JSON_LD
) # we choose to set a default if the user has not specified
if best_match is not None:
mime_type = MimeType(best_match)

Expand Down Expand Up @@ -309,9 +309,7 @@ def get(self, number=None):
logger.error(
f"Error in fetching the serialization for data download: {data_download_uri}."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid URI or return format"
)
return APIUtil.toErrorResponse("bad_request", "Invalid URI or return format")

def is_data_download(self, data_download_uri: str) -> bool:
return DataCatalogLODHandler(current_app.config).is_data_download(
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit_tests/apis/dataset/lod_data_api_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from mockito import when, verify
from mockito import when

import util.ld_util

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_get_200_mime_type_None(application_settings, generic_client, datacatalo
).get_data_catalog(DUMMY_URI, mime_format=default_mimetype.to_ld_format())
verify(
apis.dataset.dataset_api.DataCatalogLODHandler,
times=1 if default_mimetype is not MimeType.HTML else 0
times=1 if default_mimetype is not MimeType.HTML else 0,
)._get_data_catalog_from_store(
application_settings.get("SPARQL_ENDPOINT"),
application_settings.get("DATA_CATALOG_GRAPH"),
Expand Down Expand Up @@ -368,9 +368,7 @@ def test_get_404(
unstub()


def test_get_500(
application_settings, generic_client, datacatalog_url, caplog
):
def test_get_500(application_settings, generic_client, datacatalog_url, caplog):
DUMMY_IDENTIFIER = 1234
DUMMY_URI = f"http://{DUMMY_IDENTIFIER}"

Expand Down Expand Up @@ -419,8 +417,7 @@ def test_get_500(
apis.dataset.dataset_api.LODDataCatalogAPI, times=1
).is_valid_data_catalog(DUMMY_URI)
verify(
apis.dataset.dataset_api.LODDataCatalogAPI,
times=1
apis.dataset.dataset_api.LODDataCatalogAPI, times=1
)._get_lod_view_resource(
DUMMY_URI,
application_settings.get("SPARQL_ENDPOINT"),
Expand Down
11 changes: 4 additions & 7 deletions src/tests/unit_tests/apis/dataset/lod_data_download_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_init():
)


@pytest.mark.parametrize("mime_type", [mime_type for mime_type in MimeType]) + [None]
@pytest.mark.parametrize("mime_type", [mime_type for mime_type in MimeType])
def test_get_200(mime_type, application_settings, generic_client, datadownload_url):
DUMMY_IDENTIFIER = 1234
DUMMY_URI = f"http://{DUMMY_IDENTIFIER}"
Expand Down Expand Up @@ -138,7 +138,7 @@ def test_get_200_mime_type_None(application_settings, generic_client, datadownlo
resp = generic_client.get(
"offline",
datadownload_url(DUMMY_IDENTIFIER),
headers={"Accept": input_mime_type.value},
headers={"Accept": input_mime_type},
)

assert resp.status_code == 200
Expand Down Expand Up @@ -370,9 +370,7 @@ def test_get_404(
unstub()


def test_get_500(
application_settings, generic_client, datadownload_url, caplog
):
def test_get_500(application_settings, generic_client, datadownload_url, caplog):
DUMMY_IDENTIFIER = 1234
DUMMY_URI = f"http://{DUMMY_IDENTIFIER}"

Expand Down Expand Up @@ -421,8 +419,7 @@ def test_get_500(
apis.dataset.dataset_api.LODDataDownloadAPI, times=1
).is_valid_data_download(DUMMY_URI)
verify(
apis.dataset.dataset_api.LODDataDownloadAPI,
times=1
apis.dataset.dataset_api.LODDataDownloadAPI, times=1
)._get_lod_view_resource(
DUMMY_URI,
application_settings.get("SPARQL_ENDPOINT"),
Expand Down
5 changes: 1 addition & 4 deletions src/tests/unit_tests/apis/dataset/lod_dataset_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,7 @@ def test_get_500(application_settings, generic_client, dataset_url, caplog):
verify(apis.dataset.dataset_api.LODDatasetAPI, times=1).is_valid_dataset(
DUMMY_URI
)
verify(
apis.dataset.dataset_api.LODDatasetAPI,
times=1
)._get_lod_view_resource(
verify(apis.dataset.dataset_api.LODDatasetAPI, times=1)._get_lod_view_resource(
DUMMY_URI,
application_settings.get("SPARQL_ENDPOINT"),
application_settings.get("URI_NISV_ORGANISATION"),
Expand Down
87 changes: 48 additions & 39 deletions src/tests/unit_tests/util/ld_util_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import pytest
import requests

# note:
# the following SDO import generates a warning, see
# https://github.com/RDFLib/rdflib/issues/1830

from enum import Enum
from rdflib import Graph, URIRef, Literal
from rdflib.namespace import SDO, RDF
from rdflib.compare import to_isomorphic
import requests
from requests.exceptions import ConnectionError
from typing import List, Tuple, Union

from mockito import when, unstub, mock, verify, KWARGS
from models.DatasetApiUriLevel import DatasetApiUriLevel
from models.ResourceApiUriLevel import ResourceApiUriLevel
Expand All @@ -33,47 +36,53 @@
"?s ?p ?o FILTER(!ISBLANK(?o)) }"
)

generate_lod_resource_uri_cases: List[
Tuple[Union[Enum, str], str, str, Union[str, None]]
] = []
generate_lod_resource_uri_cases += [
(
level, # program, series, season, scene
DUMMY_RESOURCE_ID,
DUMMY_BENG_DATA_DOMAIN,
f"{DUMMY_BENG_DATA_DOMAIN}id/{level.value}/{DUMMY_RESOURCE_ID}",
)
for level in ResourceApiUriLevel
]
generate_lod_resource_uri_cases += [
(
level, # datacatalog, dataset, datadownload
DUMMY_RESOURCE_ID,
DUMMY_BENG_DATA_DOMAIN,
f"{DUMMY_BENG_DATA_DOMAIN}id/{level.value}/{DUMMY_RESOURCE_ID}",
)
for level in DatasetApiUriLevel
]

generate_lod_resource_uri_cases += [
(
ResourceApiUriLevel.PROGRAM,
DUMMY_RESOURCE_ID,
"BROKEN_BENG_DATA_DOMAIN",
None,
), # invalid beng_data_domain
(
"dataset",
DUMMY_RESOURCE_ID,
DUMMY_RESOURCE_ID,
None,
), # invalid level param (no str allowed)
(
"program",
DUMMY_RESOURCE_ID,
DUMMY_RESOURCE_ID,
None,
), # another invalid level param
]


@pytest.mark.parametrize(
"resource_level, resource_id, beng_data_domain, resource_uri",
[
[
(
level, # program, series, season, scene
DUMMY_RESOURCE_ID,
DUMMY_BENG_DATA_DOMAIN,
f"{DUMMY_BENG_DATA_DOMAIN}id/{level.value}/{DUMMY_RESOURCE_ID}",
)
for level in ResourceApiUriLevel
],
[
(
level, # datacatalog, dataset, datadownload
DUMMY_RESOURCE_ID,
DUMMY_BENG_DATA_DOMAIN,
f"{DUMMY_BENG_DATA_DOMAIN}id/{level.value}/{DUMMY_RESOURCE_ID}",
)
for level in DatasetApiUriLevel
],
(
ResourceApiUriLevel.PROGRAM,
DUMMY_RESOURCE_ID,
"BROKEN_BENG_DATA_DOMAIN",
None,
), # invalid beng_data_domain
(
"dataset",
DUMMY_RESOURCE_ID,
DUMMY_RESOURCE_ID,
None,
), # invalid level param (no str allowed)
(
"program",
DUMMY_RESOURCE_ID,
DUMMY_RESOURCE_ID,
None,
), # another invalid level param
],
generate_lod_resource_uri_cases,
)
def test_generate_lod_resource_uri(
resource_level, resource_id, beng_data_domain, resource_uri
Expand Down
5 changes: 3 additions & 2 deletions src/util/ld_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json
import logging
import requests
Expand Down Expand Up @@ -38,7 +37,9 @@ def generate_lod_resource_uri(
:param beng_data_domain: see BENG_DATA_DOMAIN in settings.py
:returns: a proper URI as it should be listed in the LOD server.
"""
if not isinstance(level, DatasetApiUriLevel) and not isinstance(level, ResourceApiUriLevel):
if not isinstance(level, DatasetApiUriLevel) and not isinstance(
level, ResourceApiUriLevel
):
return None
url_parts = urlparse(str(beng_data_domain))
if url_parts.netloc is not None and url_parts.netloc != "":
Expand Down

0 comments on commit 7531420

Please sign in to comment.