-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Franziska <73471037+hf-fvesely@users.noreply.github.com>
- Loading branch information
1 parent
487dbd9
commit 2c7dde4
Showing
4 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Contains Region class and corresponding marshmallow schema for de-/serialization | ||
""" | ||
from typing import List, Optional | ||
|
||
import attr | ||
from marshmallow import fields | ||
|
||
from bo4e.bo.geschaeftsobjekt import Geschaeftsobjekt, GeschaeftsobjektSchema | ||
from bo4e.com.regionskriterium import Regionskriterium, RegionskriteriumSchema | ||
from bo4e.enum.botyp import BoTyp | ||
from bo4e.validators import check_list_length_at_least_one | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
@attr.s(auto_attribs=True, kw_only=True) | ||
class Region(Geschaeftsobjekt): | ||
""" | ||
Modellierung einer Region als Menge von Kriterien, die eine Region beschreiben | ||
""" | ||
|
||
# required attributes | ||
bo_typ: BoTyp = attr.ib(default=BoTyp.REGION) | ||
#: Bezeichnung der Region | ||
bezeichnung: str = attr.ib(validator=attr.validators.instance_of(str)) | ||
|
||
#: Positivliste der Kriterien zur Definition der Region | ||
positiv_liste: List[Regionskriterium] = attr.ib( | ||
validator=attr.validators.deep_iterable( | ||
member_validator=attr.validators.instance_of(Regionskriterium), | ||
iterable_validator=check_list_length_at_least_one, | ||
) | ||
) | ||
|
||
# optional attributes | ||
#: Negativliste der Kriterien zur Definition der Region | ||
negativ_liste: Optional[List[Regionskriterium]] = attr.ib( | ||
default=None, | ||
validator=attr.validators.optional( | ||
attr.validators.deep_iterable( | ||
member_validator=attr.validators.instance_of(Regionskriterium), | ||
iterable_validator=attr.validators.instance_of(list), # no min length for negativListe | ||
) | ||
), | ||
) | ||
|
||
|
||
class RegionSchema(GeschaeftsobjektSchema): | ||
""" | ||
Schema for de-/serialization of Region | ||
""" | ||
|
||
class_name = Region | ||
# required attributes | ||
bezeichnung = fields.Str() | ||
positiv_liste = fields.List(fields.Nested(RegionskriteriumSchema)) | ||
|
||
# optional attributes | ||
negativ_liste = fields.List(fields.Nested(RegionskriteriumSchema), load_default=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest # type:ignore[import] | ||
|
||
from bo4e.bo.region import Region, RegionSchema | ||
from tests.serialization_helper import assert_serialization_roundtrip # type:ignore[import] | ||
from tests.test_regionskriterium import example_regionskriterium # type:ignore[import] | ||
|
||
|
||
class TestRegion: | ||
@pytest.mark.parametrize( | ||
"region", | ||
[ | ||
pytest.param( | ||
Region( | ||
bezeichnung="Bikini Bottom", | ||
positiv_liste=[example_regionskriterium], | ||
negativ_liste=[example_regionskriterium], | ||
), | ||
id="max attributes", | ||
), | ||
pytest.param( | ||
Region(bezeichnung="Bikini Bottom", positiv_liste=[example_regionskriterium]), id="min attributes" | ||
), | ||
], | ||
) | ||
def test_serialization_roundtrip(self, region: Region): | ||
assert_serialization_roundtrip(region, RegionSchema()) | ||
|
||
def test_missing_required_attribute(self): | ||
with pytest.raises(TypeError) as excinfo: | ||
_ = Region() | ||
|
||
assert "missing 2 required" in str(excinfo.value) | ||
|
||
def test_region_positiv_liste_required_and_negativ_liste_not_required(self): | ||
with pytest.raises(ValueError) as excinfo: | ||
_ = Region( | ||
bezeichnung="Bikini Bottom", | ||
positiv_liste=[], | ||
negativ_liste=[], | ||
) | ||
|
||
assert "List positiv_liste must not be empty." in str(excinfo.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters