diff --git a/dts_validator/client.py b/dts_validator/client.py index db7ab15..549e489 100644 --- a/dts_validator/client.py +++ b/dts_validator/client.py @@ -1,13 +1,14 @@ from __future__ import annotations import logging import requests +import random from requests.models import Response from typing import Optional, Union, List, Tuple, Dict from uritemplate import URITemplate from .validation import check_required_property -LOGGER = logging.getLogger(__name__) +LOGGER = logging.getLogger() class DTS_Collection(object): """Class representing a DTS Collection object.""" @@ -157,6 +158,7 @@ def collections( def get_one_resource(self): collections = self.collections() + random.shuffle(collections) for collection in collections: resource = get_resource_recursively(collection, self) if resource: diff --git a/dts_validator/validation.py b/dts_validator/validation.py index 56d88ee..8b2efec 100644 --- a/dts_validator/validation.py +++ b/dts_validator/validation.py @@ -7,9 +7,7 @@ from uritemplate import URITemplate from .exceptions import URITemplateMissingParameter, JSONResponseMissingProperty -LOGGER = logging.getLogger() - - +LOGGER = logging.getLogger(__name__) def validate_json(json_data, json_schema): # Set up resolver to correctly handle relative paths diff --git a/pyproject.toml b/pyproject.toml index 98ac037..c5c477e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,4 +23,4 @@ build-backend = "poetry.core.masonry.api" [tool.pytest.ini_options] log_cli = true -#log_cli_level = "INFO" \ No newline at end of file +log_cli_level = "INFO" \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 80a490e..c594500 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ LOGGER = logging.getLogger() SKIP_MOCK_TESTS_MESSAGE = 'A remote DTS API is provided; skipping tests on mock/example data' +SKIP_NO_CITABLE_UNITS_MESSAGE = 'No citable units found in the navigation object' def pytest_addoption(parser): parser.addoption( @@ -273,10 +274,14 @@ def navigation_endpoint_response_ref( # first we need to get all resource's citable units navigation_obj, response_obj = dts_client.navigation(resource=readable_resource, down=1) - # then we pick one citable unit, by default the last one + # if the Resource has citable units, we pick by default the last one # and use it to query its subtree - target_citable_unit = navigation_obj.citable_units[-1] - return dts_client.navigation(resource=readable_resource, reference=target_citable_unit, down=-1) + if navigation_obj.citable_units: + target_citable_unit = navigation_obj.citable_units[-1] + return dts_client.navigation(resource=readable_resource, reference=target_citable_unit, down=-1) + # otherwise we skip the test + else: + pytest.skip(f'{navigation_obj.resource}: {SKIP_NO_CITABLE_UNITS_MESSAGE}') # use mock/example data for tests elif request.param and dts_client is None: tests_dir = os.path.dirname(request.module.__file__) @@ -312,10 +317,13 @@ def navigation_endpoint_response_top_ref_down_two( # first we need to get all resource's citable units navigation_obj, response_obj = dts_client.navigation(resource=readable_resource, down=1) - # then we pick one citable unit, by default the last one + # if the Resource has citable units, we pick by default the last one # and use it to query its subtree - target_citable_unit = navigation_obj.citable_units[-1] - return dts_client.navigation(resource=readable_resource, reference=target_citable_unit, down=2) + if navigation_obj.citable_units: + target_citable_unit = navigation_obj.citable_units[-1] + return dts_client.navigation(resource=readable_resource, reference=target_citable_unit, down=2) + else: + pytest.skip(f'{navigation_obj.resource}: {SKIP_NO_CITABLE_UNITS_MESSAGE}') # use mock/example data for tests elif request.param and dts_client is None: tests_dir = os.path.dirname(request.module.__file__) @@ -369,7 +377,7 @@ def navigation_endpoint_response_range_plus_down( dts_client: Optional[DTS_API] ) -> Tuple[Optional[Dict], requests.models.Response]: """ - This fixture returns a DTS Navigation endpoint response, when retrieving + This fixture returns a DTS Navigation endpoint response when retrieving an array of `CitableUnit`s in a specified range, including the direct children of the specified start and end points (`?start=&end=&down=1`). See DTS API specs, section "Navigation Endpoint", example #6. @@ -384,15 +392,19 @@ def navigation_endpoint_response_range_plus_down( # first we need to get all resource's citable units navigation_obj, response_obj = dts_client.navigation(resource=readable_resource, down=1) - # then define a range of refs, and query based on that - start_ref = navigation_obj.citable_units[0] - end_ref = navigation_obj.citable_units[-1] - return dts_client.navigation( - resource=readable_resource, - start=start_ref, - end=end_ref, - down=1 - ) + # if the Resource has citable units, we define a range of refs (first-last), + # and use this range to query the navigation endpoint + if navigation_obj.citable_units: + start_ref = navigation_obj.citable_units[0] + end_ref = navigation_obj.citable_units[-1] + return dts_client.navigation( + resource=readable_resource, + start=start_ref, + end=end_ref, + down=1 + ) + else: + pytest.skip(f'{navigation_obj.resource}: {SKIP_NO_CITABLE_UNITS_MESSAGE}') # use mock/example data for tests elif request.param and dts_client is None: tests_dir = os.path.dirname(request.module.__file__) @@ -413,7 +425,7 @@ def navigation_endpoint_response_range( dts_client: Optional[DTS_API] ) -> Tuple[Optional[Dict], requests.models.Response]: """ - This fixture returns a DTS Navigation endpoint response, when retrieving + This fixture returns a DTS Navigation endpoint response when retrieving an array of `CitableUnit`s in a specified range (`?start=&end=