-
Notifications
You must be signed in to change notification settings - Fork 5
Add COM Preis #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add COM Preis #234
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5171ab7
add Preis COM
a671220
add as_string to optional Decimal
2fa22b9
add unittests
1e9756f
black .
hf-kklein 5d23d5d
isort .
hf-kklein 343d544
Add docstrings to Preis COM (#237)
rmorlock 8941983
Merge branch 'master' into Robi_COM_Preis
rmorlock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,56 @@ | ||
| """ | ||
| Contains Preis class | ||
| and corresponding marshmallow schema for de-/serialization | ||
| """ | ||
|
|
||
| from decimal import Decimal | ||
| from typing import Optional | ||
|
|
||
| import attr | ||
| from marshmallow import fields, post_load | ||
| from marshmallow_enum import EnumField # type:ignore[import] | ||
|
|
||
| from bo4e.com.com import COM, COMSchema | ||
| from bo4e.enum.mengeneinheit import Mengeneinheit | ||
| from bo4e.enum.preisstatus import Preisstatus | ||
| from bo4e.enum.waehrungseinheit import Waehrungseinheit | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class Preis(COM): | ||
| """ | ||
| Abbildung eines Preises mit Wert, Einheit, Bezugswert und Status. | ||
| """ | ||
|
|
||
| # required attributes | ||
| #: Gibt die nomiale Höhe des Preises an. | ||
| wert: Decimal = attr.ib(validator=attr.validators.instance_of(Decimal)) | ||
| #: Währungseinheit für den Preis, z.B. Euro oder Ct. | ||
| einheit: Waehrungseinheit = attr.ib(validator=attr.validators.in_(Waehrungseinheit)) | ||
| #: Angabe, für welche Bezugsgröße der Preis gilt. Z.B. kWh. | ||
| bezugswert: Mengeneinheit = attr.ib(validator=attr.validators.in_(Mengeneinheit)) | ||
|
|
||
| # optional attributes | ||
| #: Gibt den Status des veröffentlichten Preises an | ||
| status: Optional[Preisstatus] = attr.ib(default=None) | ||
|
|
||
|
|
||
| class PreisSchema(COMSchema): | ||
| """ | ||
| Schema for de-/serialization of Preis. | ||
| """ | ||
|
|
||
| # required attributes | ||
| wert = fields.Decimal(as_string=True) | ||
| einheit = EnumField(Waehrungseinheit) | ||
| bezugswert = EnumField(Mengeneinheit) | ||
|
|
||
| # optional attributes | ||
| status = EnumField(Preisstatus, load_default=None) | ||
|
|
||
| # pylint: disable=no-self-use, unused-argument | ||
| @post_load | ||
| def deserialize(self, data, **kwargs) -> Preis: | ||
| """Deserialize JSON to Preis object""" | ||
| return Preis(**data) | ||
This file contains hidden or 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,60 @@ | ||
| from decimal import Decimal | ||
|
|
||
| import pytest # type:ignore[import] | ||
|
|
||
| from bo4e.com.preis import Preis, PreisSchema | ||
| from bo4e.enum.mengeneinheit import Mengeneinheit | ||
| from bo4e.enum.preisstatus import Preisstatus | ||
| from bo4e.enum.waehrungseinheit import Waehrungseinheit | ||
|
|
||
|
|
||
| class TestPreis: | ||
| def test_preis_only_required(self): | ||
| """ | ||
| Test de-/serialisation of Preis (only has required attributes). | ||
| """ | ||
| preis = Preis(wert=Decimal(2.53), einheit=Waehrungseinheit.EUR, bezugswert=Mengeneinheit.KWH) | ||
|
|
||
| schema = PreisSchema() | ||
| json_string = schema.dumps(preis, ensure_ascii=False) | ||
|
|
||
| assert "KWH" in json_string | ||
| assert "EUR" in json_string | ||
| assert "null" in json_string | ||
|
|
||
| preis_deserialized = schema.loads(json_string) | ||
|
|
||
| assert isinstance(preis_deserialized.wert, Decimal) | ||
| assert isinstance(preis_deserialized.einheit, Waehrungseinheit) | ||
| assert isinstance(preis_deserialized.bezugswert, Mengeneinheit) | ||
| assert preis_deserialized.status is None | ||
| assert preis == preis_deserialized | ||
|
|
||
| def test_wrong_datatype(self): | ||
| with pytest.raises(TypeError) as excinfo: | ||
| _ = Preis(wert=3.50, einheit=Waehrungseinheit.EUR, bezugswert=Mengeneinheit.KWH) | ||
|
|
||
| assert "'wert' must be <class 'decimal.Decimal'>" in str(excinfo.value) | ||
|
|
||
| def test_missing_required_attribute(self): | ||
| with pytest.raises(TypeError) as excinfo: | ||
| _ = Preis(wert=Decimal(3.50), einheit=Waehrungseinheit.EUR, status=Preisstatus.ENDGUELTIG) | ||
|
|
||
| assert "missing 1 required" in str(excinfo.value) | ||
|
|
||
| def test_optional_attribute(self): | ||
| preis = Preis( | ||
| wert=Decimal(3.50), | ||
| einheit=Waehrungseinheit.EUR, | ||
| bezugswert=Mengeneinheit.KWH, | ||
| status=Preisstatus.ENDGUELTIG, | ||
| ) | ||
|
|
||
| schema = PreisSchema() | ||
| json_string = schema.dumps(preis, ensure_ascii=False) | ||
|
|
||
| assert "ENDGUELTIG" in json_string | ||
|
|
||
| preis_deserialized = schema.loads(json_string) | ||
|
|
||
| assert isinstance(preis_deserialized.status, Preisstatus) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.