From b256afc15c158127c1fb8d0ce24a889b2cb8e039 Mon Sep 17 00:00:00 2001 From: "javier.hernandez" Date: Tue, 21 May 2024 14:12:23 +0200 Subject: [PATCH] Changed ValueError to NotFound exceptions in content getters. Updated tests. Signed-off-by: javier.hernandez --- src/pysdmx/model/message.py | 20 ++++++++++++++++---- tests/model/test_message.py | 29 +++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/pysdmx/model/message.py b/src/pysdmx/model/message.py index f5f1809..a6a8402 100644 --- a/src/pysdmx/model/message.py +++ b/src/pysdmx/model/message.py @@ -11,7 +11,7 @@ from msgspec import Struct -from pysdmx.errors import ClientError +from pysdmx.errors import ClientError, NotFound from pysdmx.model import Codelist, ConceptScheme from pysdmx.model.__base import ItemScheme @@ -87,17 +87,29 @@ def __get_elements(self, type_: str) -> Dict[str, Any]: """Returns the elements from content.""" if type_ in self.content: return self.content[type_] - raise ValueError(f"No {type_} found in content") + raise NotFound( + 404, + f"No {type_} found in content", + f"Could not find any {type_} in content.", + ) def __get_element_by_uid(self, type_: str, unique_id: str) -> Any: """Returns a specific element from content.""" if type_ not in self.content: - raise ValueError(f"No {type_} found") + raise NotFound( + 404, + f"No {type_} found.", + f"Could not find any {type_} in content.", + ) if unique_id in self.content[type_]: return self.content[type_][unique_id] - raise ValueError(f"No {type_} with id {unique_id} found in content") + raise NotFound( + 404, + f"No {type_} with id {unique_id} found in content", + "Could not find the requested element.", + ) def get_organisation_schemes(self) -> Dict[str, ItemScheme]: """Returns the OrganisationScheme.""" diff --git a/tests/model/test_message.py b/tests/model/test_message.py index afca81e..41f690d 100644 --- a/tests/model/test_message.py +++ b/tests/model/test_message.py @@ -1,6 +1,6 @@ import pytest -from pysdmx.errors import ClientError +from pysdmx.errors import ClientError, NotFound from pysdmx.model import Codelist, ConceptScheme from pysdmx.model.__base import ItemScheme from pysdmx.model.message import Message @@ -61,35 +61,48 @@ def test_get_concepts(): def test_empty_get_elements(): message = Message({}) - with pytest.raises(ValueError, match="No OrganisationSchemes found"): + with pytest.raises(NotFound) as exc_info: message.get_organisation_schemes() - with pytest.raises(ValueError, match="No Codelists found"): + assert "No OrganisationSchemes found" in str(exc_info.value.title) + + with pytest.raises(NotFound) as exc_info: message.get_codelists() - with pytest.raises(ValueError, match="No ConceptSchemes found"): + assert "No Codelists found" in str(exc_info.value.title) + + with pytest.raises(NotFound) as exc_info: message.get_concept_schemes() + assert "No ConceptSchemes found" in str(exc_info.value.title) + def test_empty_get_element_by_uid(): message = Message({}) - with pytest.raises(ValueError, match="No OrganisationSchemes found"): + with pytest.raises(NotFound) as exc_info: message.get_organisation_scheme_by_uid("org1:orgs1(1.0)") - with pytest.raises(ValueError, match="No Codelists found"): + assert "No OrganisationSchemes found" in str(exc_info.value.title) + + with pytest.raises(NotFound) as exc_info: message.get_codelist_by_uid("cl1:cl1(1.0)") - with pytest.raises(ValueError, match="No ConceptSchemes found"): + assert "No Codelists found" in str(exc_info.value.title) + + with pytest.raises(NotFound) as exc_info: message.get_concept_scheme_by_uid("cs1:cs1(1.0)") + assert "No ConceptSchemes found" in str(exc_info.value.title) + def test_invalid_get_element_by_uid(): message = Message({"OrganisationSchemes": {}}) e_m = "No OrganisationSchemes with id" - with pytest.raises(ValueError, match=e_m): + with pytest.raises(NotFound) as exc_info: message.get_organisation_scheme_by_uid("org12:orgs1(1.0)") + assert e_m in str(exc_info.value.title) def test_invalid_initialization_content_key():