diff --git a/src/bo4e/bo/marktlokation.py b/src/bo4e/bo/marktlokation.py index dec3bad36..caf3ba9b2 100644 --- a/src/bo4e/bo/marktlokation.py +++ b/src/bo4e/bo/marktlokation.py @@ -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 @@ -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): @@ -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" diff --git a/src/bo4e/bo/messlokation.py b/src/bo4e/bo/messlokation.py index 4bee7da4e..6a26168aa 100644 --- a/src/bo4e/bo/messlokation.py +++ b/src/bo4e/bo/messlokation.py @@ -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", @@ -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" diff --git a/tests/test_marktlokation.py b/tests/test_marktlokation.py index 281cdf83c..956e1d062 100644 --- a/tests/test_marktlokation.py +++ b/tests/test_marktlokation.py @@ -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", diff --git a/tests/test_messlokation.py b/tests/test_messlokation.py index dc68819e7..8438c7035 100644 --- a/tests/test_messlokation.py +++ b/tests/test_messlokation.py @@ -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: