Skip to content

Commit ed698a5

Browse files
hf-kkleinKonstantin
andauthored
fix: return actual json objects/dicts inside the fristenlist instead of __str__ representation (#160)
and we should use Pydantic instead of dataclasses such pain Co-authored-by: Konstantin <[email protected]>
1 parent 0370cd5 commit ed698a5

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/GenerateAllFristen/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# we ignore the invalid module name because in this case it's ok that the module/dir has the name of the relative path
22
# pylint:disable=invalid-name
33
"""Contains function generating all fristen for the given year"""
4+
import dataclasses
45
import json
56
import logging
67
from http import HTTPStatus
@@ -33,7 +34,9 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
3334

3435
try:
3536
all_fristen = FristenkalenderGenerator().generate_all_fristen(year)
36-
all_fristen_json = json.dumps(all_fristen, indent=4, sort_keys=True, default=str)
37+
all_fristen_json = json.dumps(
38+
[dataclasses.asdict(x) for x in all_fristen], indent=4, sort_keys=True, default=str
39+
)
3740
return func.HttpResponse(
3841
body=all_fristen_json,
3942
status_code=HTTPStatus.OK,

src/GenerateFristenForType/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# we ignore the invalid module name because in this case it's ok that the module/dir has the name of the relative path
22
# pylint:disable=invalid-name
33
"""Contains function generating all fristen for a given type and a given year"""
4+
import dataclasses
45
import json
56
import logging
67
from http import HTTPStatus
@@ -39,5 +40,7 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
3940
)
4041

4142
fristen_with_type = FristenkalenderGenerator().generate_fristen_for_type(year, fristen_type)
42-
fristen_with_type_json = json.dumps(fristen_with_type, indent=4, sort_keys=True, default=str)
43+
fristen_with_type_json = json.dumps(
44+
[dataclasses.asdict(x) for x in fristen_with_type], indent=4, sort_keys=True, default=str
45+
)
4346
return func.HttpResponse(body=fristen_with_type_json, status_code=HTTPStatus.OK, mimetype="application/json")

unittests/test_generate_all_fristen.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from http import HTTPStatus
23

34
import azure.functions as func
@@ -25,6 +26,14 @@ def test_ok_get(self, ok_request: func.HttpRequest):
2526
assert actual_response.status_code == HTTPStatus.OK
2627
body = actual_response.get_body()
2728
assert body is not None
29+
deserialized_body = json.loads(body)
30+
assert isinstance(deserialized_body, list)
31+
assert not isinstance(
32+
deserialized_body[0], str
33+
) # should not be the python object __str__ "FristWithAttributes(date=datetime.date(2022, 12, 1), label='21WT', ref_not_in_the_same_month=10, description='NKP (VNB ⟶ MGV)')"
34+
assert all(
35+
isinstance(x, dict) for x in deserialized_body
36+
) # should not be the python object __str__ "FristWithAttributes(date=datetime.date(2022, 12, 1), label='21WT', ref_not_in_the_same_month=10, description='NKP (VNB ⟶ MGV)')"
2837

2938
@pytest.mark.parametrize(
3039
"bad_request",

unittests/test_generate_fristen_for_type.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ def test_ok_get(self, ok_request: func.HttpRequest):
2626
assert actual_response.status_code == HTTPStatus.OK
2727
actual_fristen_list = json.loads(actual_response.get_body().decode("utf-8"))
2828
actual_frist = actual_fristen_list[0]
29-
expected = "FristWithAttributesAndType(date=datetime.date(2022, 12, 28), label='3LWT', ref_not_in_the_same_month=None, description='Letzter Termin Anmeldung asynchrone Bilanzierung (Strom)', fristen_type=<FristenType.GPKE: 'GPKE'>)"
29+
expected = {
30+
"date": "2022-12-28",
31+
"description": "Letzter Termin Anmeldung asynchrone Bilanzierung (Strom)",
32+
"fristen_type": "FristenType.GPKE",
33+
"label": "3LWT",
34+
"ref_not_in_the_same_month": None,
35+
}
3036
assert actual_frist == expected
3137

3238
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)