-
Notifications
You must be signed in to change notification settings - Fork 5
✨Add COM Energieherkunft #238
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
63f7137
✨Add COM Energieherkunft
hf-aschloegl 295ac7d
✅Add test
hf-aschloegl bfe79f2
isort
hf-aschloegl 26ac378
Merge branch 'master' into com-energieherkunft
hf-aschloegl dcd6bb2
📦📝 Generated docs
hf-aschloegl 88221cd
black
hf-aschloegl 733a531
📝Add docstrings
hf-aschloegl f3e5310
black
hf-aschloegl 1dcbc9f
Add validation for percentage
hf-aschloegl 70bd747
linting
hf-aschloegl 8dd7424
Merge branch 'master' into com-energieherkunft
hf-aschloegl 6837e7c
Merge branch 'master' into com-energieherkunft
hf-aschloegl 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
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,50 @@ | ||
| """ | ||
| Contains Energieherkunft class | ||
| and corresponding marshmallow schema for de-/serialization | ||
| """ | ||
|
|
||
| from decimal import Decimal | ||
|
|
||
| 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.erzeugungsart import Erzeugungsart | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class Energieherkunft(COM): | ||
| """ | ||
| Abbildung einer Energieherkunft. | ||
| """ | ||
|
|
||
| # required attributes | ||
| #: Art der Erzeugung der Energie. | ||
| erzeugungsart: Erzeugungsart = attr.ib(validator=attr.validators.in_(Erzeugungsart)) | ||
| #: Prozentualer Anteil der jeweiligen Erzeugungsart. | ||
| anteil_prozent: Decimal = attr.ib(validator=attr.validators.instance_of(Decimal)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kannste da noch prüfen, dass es 0<=anteil_prozent<=100 ist?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @anteil_prozent.validator | ||
| # pylint: disable=unused-argument, no-self-use | ||
| def check_percentage_between_0_100(self, attribute, value): | ||
| """Checks that the percentage is between 0 and 100.""" | ||
| if not 0 <= value <= 100: | ||
| raise ValueError("anteil_prozent must be between 0 and 100") | ||
|
|
||
|
|
||
| class EnergieherkunftSchema(COMSchema): | ||
| """ | ||
| Schema for de-/serialization of Energieherkunft. | ||
| """ | ||
|
|
||
| # required attributes | ||
| erzeugungsart = EnumField(Erzeugungsart) | ||
| anteil_prozent = fields.Decimal(as_string=True) | ||
|
|
||
| # pylint: disable=no-self-use, unused-argument | ||
| @post_load | ||
| def deserialize(self, data, **kwargs) -> Energieherkunft: | ||
| """Deserialize JSON to Energieherkunft object""" | ||
| return Energieherkunft(**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,46 @@ | ||
| from decimal import Decimal | ||
|
|
||
| import pytest # type:ignore[import] | ||
|
|
||
| from bo4e.com.energieherkunft import Energieherkunft, EnergieherkunftSchema | ||
| from bo4e.enum.erzeugungsart import Erzeugungsart | ||
| from tests.serialization_helper import assert_serialization_roundtrip # type:ignore[import] | ||
|
|
||
|
|
||
| class TestEnergieherkunft: | ||
| @pytest.mark.parametrize( | ||
| "energieherkunft, expected_json_dict", | ||
| [ | ||
| pytest.param( | ||
| Energieherkunft(erzeugungsart=Erzeugungsart.BIOMASSE, anteil_prozent=Decimal(25.5)), | ||
| { | ||
| "erzeugungsart": "BIOMASSE", | ||
| "anteilProzent": "25.5", | ||
| }, | ||
| ), | ||
| ], | ||
| ) | ||
| def test_energieherkunft_required_attributes(self, energieherkunft, expected_json_dict): | ||
| """ | ||
| Test de-/serialisation of Energieherkunft with minimal attributes. | ||
| """ | ||
| assert_serialization_roundtrip(energieherkunft, EnergieherkunftSchema(), expected_json_dict) | ||
|
|
||
| def test_energieherkunft_missing_required_attribute(self): | ||
| with pytest.raises(TypeError) as excinfo: | ||
| _ = Energieherkunft() | ||
|
|
||
| assert "missing 2 required" in str(excinfo.value) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "failing_percentage", | ||
| [ | ||
| pytest.param(100.1), | ||
| pytest.param(-2), | ||
| ], | ||
| ) | ||
| def test_energieherkunft_failing_validation(self, failing_percentage): | ||
| with pytest.raises(ValueError) as excinfo: | ||
| _ = (Energieherkunft(erzeugungsart=Erzeugungsart.BIOMASSE, anteil_prozent=Decimal(failing_percentage)),) | ||
|
|
||
| assert "anteil_prozent must be between 0 and 100" in str(excinfo.value) |
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.