Skip to content

Commit

Permalink
Implement BO Region (#311)
Browse files Browse the repository at this point in the history

Co-authored-by: Franziska <73471037+hf-fvesely@users.noreply.github.com>
  • Loading branch information
hf-kklein and hf-fvesely authored Jan 18, 2022
1 parent 487dbd9 commit 2c7dde4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
59 changes: 59 additions & 0 deletions src/bo4e/bo/region.py
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)
1 change: 1 addition & 0 deletions src/bo4e/enum/botyp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BoTyp(StrEnum):
PREISBLATTNETZNUTZUNG = "PREISBLATTNETZNUTZUNG"
PREISBLATTUMLAGEN = "PREISBLATTUMLAGEN"
RECHNUNG = "RECHNUNG"
REGION = "REGION"
REGIONALTARIF = "REGIONALTARIF"
STANDORTEIGENSCHAFTEN = "STANDORTEIGENSCHAFTEN"
TARIFINFO = "TARIFINFO"
Expand Down
42 changes: 42 additions & 0 deletions tests/test_region.py
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)
12 changes: 7 additions & 5 deletions tests/test_regionskriterium.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
from bo4e.enum.regionskriteriumtyp import Regionskriteriumtyp
from tests.serialization_helper import assert_serialization_roundtrip # type:ignore[import]

example_regionskriterium = Regionskriterium(
regionskriteriumtyp=Regionskriteriumtyp.REGELGEBIET_NAME,
gueltigkeitstyp=Gueltigkeitstyp.NICHT_IN,
wert="Was ist ein Regionskriterium?",
)


class TestRegionskriterium:
@pytest.mark.parametrize(
"regionskriterium, expected_json_dict",
[
pytest.param(
Regionskriterium(
regionskriteriumtyp=Regionskriteriumtyp.REGELGEBIET_NAME,
gueltigkeitstyp=Gueltigkeitstyp.NICHT_IN,
wert="Was ist ein Regionskriterium?",
),
example_regionskriterium,
{
"gueltigkeitstyp": "NICHT_IN",
"regionskriteriumtyp": "REGELGEBIET_NAME",
Expand Down

0 comments on commit 2c7dde4

Please sign in to comment.