Skip to content

Commit

Permalink
Merge branch 'develop' into org_types
Browse files Browse the repository at this point in the history
  • Loading branch information
sosna committed May 13, 2024
2 parents f46606b + 02d7fbe commit b7f45e4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions src/pysdmx/model/__base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from msgspec import Struct

from pysdmx.errors import ClientError


class Annotation(Struct, frozen=True, omit_defaults=True):
"""Annotation class.
Expand All @@ -27,6 +29,24 @@ class Annotation(Struct, frozen=True, omit_defaults=True):
url: Optional[str] = None
type: Optional[str] = None

def __post_init__(self) -> None:
"""Additional validation checks for Annotation."""
if (
not self.id
and not self.title
and not self.text
and not self.url
and not self.type
):
raise ClientError(
422,
"Empty annotation",
(
"All fields of the annotation have been left empty."
"Please set at least one."
),
)

def __str__(self) -> str:
"""Returns a human-friendly description."""
out = []
Expand Down Expand Up @@ -210,6 +230,15 @@ class MaintainableArtefact(
structure_url: Optional[str] = None
agency: Union[str, Agency] = ""

def __post_init__(self) -> None:
"""Additional validation checks for maintainable artefacts."""
if not self.agency:
raise ClientError(
422,
"Missing agency",
"Maintainable artefacts must reference an agency.",
)


class ItemScheme(MaintainableArtefact, frozen=True, omit_defaults=True):
"""ItemScheme class.
Expand Down
14 changes: 6 additions & 8 deletions tests/model/test_annotation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from pysdmx.errors import Error
from pysdmx.model.__base import Annotation


Expand Down Expand Up @@ -60,14 +61,6 @@ def test_not_equal(id):
assert a1 != a2


def test_tostr_empty():
a = Annotation()

s = str(a)

assert s == ""


def test_tostr_id(id):
a = Annotation(id)

Expand All @@ -82,3 +75,8 @@ def test_tostr_all(id, title, text, url, type):
s = str(a)

assert s == f"id={id}, title={title}, text={text}, url={url}, type={type}"


def test_empty_annotation_not_allowed():
with pytest.raises(Error, match="empty"):
Annotation()
9 changes: 9 additions & 0 deletions tests/model/test_maintainable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

from pysdmx.errors import Error
from pysdmx.model.__base import MaintainableArtefact


def test_empty_agency_not_allowed():
with pytest.raises(Error, match="must reference an agency"):
MaintainableArtefact("ID")

0 comments on commit b7f45e4

Please sign in to comment.