Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/bo4e/bo/marktlokation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

# pylint: disable=no-name-in-module
from pydantic import field_validator
from pydantic_core.core_schema import ValidationInfo

from bo4e.bo.geschaeftsobjekt import Geschaeftsobjekt
from bo4e.bo.geschaeftspartner import Geschaeftspartner
Expand All @@ -25,7 +24,7 @@
from bo4e.enum.netzebene import Netzebene
from bo4e.enum.sparte import Sparte
from bo4e.enum.verbrauchsart import Verbrauchsart
from bo4e.validators import validate_marktlokations_id
from bo4e.validators import combinations_of_fields, validate_marktlokations_id


class Marktlokation(Geschaeftsobjekt):
Expand Down Expand Up @@ -118,19 +117,20 @@ class Marktlokation(Geschaeftsobjekt):
kundengruppen: Optional[list[Kundentyp]] = None
#: Kundengruppen der Marktlokation

# pylint:disable=unused-argument, no-self-argument
@field_validator("katasterinformation")
def validate_address_info(
cls, katasterinformation: Optional[Katasteradresse], validation_info: ValidationInfo
) -> Optional[Katasteradresse]:
"""Checks that there is one and only one valid adress given."""
values = validation_info.data # type:ignore[attr-defined]
all_address_attributes = [
values["lokationsadresse"],
values["geoadresse"],
katasterinformation,
]
amount_of_given_address_infos = len([i for i in all_address_attributes if i is not None])
if amount_of_given_address_infos != 1:
raise ValueError("No or more than one address information is given.")
return katasterinformation
# pylint: disable=duplicate-code
_validate_address_info = combinations_of_fields(
"lokationsadresse",
"geoadresse",
"katasterinformation",
valid_combinations={
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(0, 0, 0),
},
custom_error_message=(
"More than one address information is given. "
'You have to chose either "lokationsadresse", "geoadresse" or "katasterinformation".'
),
)
"Checks that if an address is given, that there is only one valid address given"
6 changes: 5 additions & 1 deletion src/bo4e/bo/messlokation.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def _validate_messlokations_id_country_code(cls, messlokations_id: str) -> str:
raise ValueError(f"The country code '{messlokations_id[0:2]}' is not a valid country code")
return messlokations_id

# pylint: disable=duplicate-code
_validate_address_info = combinations_of_fields(
"messadresse",
"geoadresse",
Expand All @@ -113,7 +114,10 @@ def _validate_messlokations_id_country_code(cls, messlokations_id: str) -> str:
(0, 0, 1),
(0, 0, 0),
},
custom_error_message="More than one address information is given.",
custom_error_message=(
"More than one address information is given. "
'You have to chose either "messadresse", "geoadresse" or "katasterinformation".'
),
)
"Checks that if an address is given, that there is only one valid address given"

Expand Down
31 changes: 30 additions & 1 deletion tests/test_marktlokation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,36 @@ def test_address_validation(self) -> None:
katasterinformation=None,
)

assert "No or more than one address information is given." in str(excinfo.value)
assert (
"More than one address information is given. "
'You have to chose either "lokationsadresse", "geoadresse" or "katasterinformation".'
) in str(excinfo.value)

def test_address_validation_with_out_katasterinformation(self) -> None:
with pytest.raises(ValidationError) as excinfo:
_ = Marktlokation(
marktlokations_id="51238696781",
sparte=Sparte.GAS,
lokationsadresse=Adresse(
postleitzahl="04177",
ort="Leipzig",
hausnummer="1",
strasse="Jahnalle",
),
energierichtung=Energierichtung.EINSP,
bilanzierungsmethode=Bilanzierungsmethode.PAUSCHAL,
unterbrechbar=True, # optional attribute
netzebene=Netzebene.NSP,
geoadresse=Geokoordinaten(
breitengrad=Decimal("52"),
laengengrad=Decimal("9"),
),
)

assert (
"More than one address information is given. "
'You have to chose either "lokationsadresse", "geoadresse" or "katasterinformation".'
) in str(excinfo.value)

@pytest.mark.parametrize(
"malo_id_valid",
Expand Down
5 changes: 4 additions & 1 deletion tests/test_messlokation.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ def test_address_validation(self) -> None:
katasterinformation=Katasteradresse(gemarkung_flur="hello", flurstueck="world"),
)

assert "More than one address information is given." in str(excinfo.value)
assert (
"More than one address information is given. "
'You have to chose either "messadresse", "geoadresse" or "katasterinformation".'
) in str(excinfo.value)

def test_grundzustaendiger_x_codenr_validation(self) -> None:
with pytest.raises(ValidationError) as excinfo:
Expand Down