Skip to content

Commit

Permalink
Changed ValueError to NotFound exceptions in content getters. Updated…
Browse files Browse the repository at this point in the history
… tests.

Signed-off-by: javier.hernandez <javier.hernandez@meaningfuldata.eu>
  • Loading branch information
javihern98 committed May 21, 2024
1 parent db4114c commit b256afc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
20 changes: 16 additions & 4 deletions src/pysdmx/model/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down
29 changes: 21 additions & 8 deletions tests/model/test_message.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit b256afc

Please sign in to comment.