Skip to content

Commit 0a34b9d

Browse files
committed
adding a unit test to verify that serializing and deserializing works if you declare an attribute as a dict or a nested dataclass
1 parent a0b4820 commit 0a34b9d

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import json
2+
from dataclasses import asdict, dataclass
3+
from typing import TypeVar
4+
5+
from ocpp.v16.call import RemoteStartTransaction
6+
from ocpp.v16.datatypes import ChargingProfile, ChargingSchedule, ChargingSchedulePeriod
7+
from ocpp.v16.enums import (
8+
ChargingProfileKindType,
9+
ChargingProfilePurposeType,
10+
ChargingRateUnitType,
11+
RecurrencyKind,
12+
)
13+
14+
T = TypeVar("T", bound="dataclass")
15+
16+
17+
def to_datatype(cls, dc: T):
18+
to_dict = asdict(dc)
19+
to_json = json.dumps(to_dict)
20+
from_json = json.loads(to_json)
21+
return cls(**from_json)
22+
23+
24+
def test_remote_start_transaction_as_dict():
25+
cp: T = ChargingProfile(
26+
charging_profile_id=1,
27+
stack_level=1,
28+
charging_profile_purpose=ChargingProfilePurposeType.charge_point_max_profile,
29+
charging_profile_kind=ChargingProfileKindType.absolute,
30+
charging_schedule=ChargingSchedule(
31+
charging_rate_unit=ChargingRateUnitType.watts,
32+
charging_schedule_period=[
33+
ChargingSchedulePeriod(start_period=0, limit=10, number_phases=3)
34+
],
35+
),
36+
transaction_id=1,
37+
recurrency_kind=RecurrencyKind.daily,
38+
valid_from="2021-01-01T00:00:00Z",
39+
valid_to="2021-01-02T00:00:00Z",
40+
)
41+
42+
cp_dict = asdict(cp)
43+
44+
rst = RemoteStartTransaction(
45+
id_tag="12345", connector_id=1, charging_profile=cp_dict
46+
)
47+
48+
new_rst = to_datatype(RemoteStartTransaction, rst)
49+
50+
# Verify top level attributes
51+
assert rst.id_tag == new_rst.id_tag
52+
assert rst.connector_id == new_rst.connector_id
53+
54+
# Verify ChargingProfile attributes
55+
charging_profile = rst.charging_profile
56+
new_charging_profile = new_rst.charging_profile
57+
assert (
58+
charging_profile["charging_profile_id"]
59+
== new_charging_profile["charging_profile_id"]
60+
)
61+
assert charging_profile["stack_level"] == new_charging_profile["stack_level"]
62+
assert (
63+
charging_profile["charging_profile_purpose"]
64+
== new_charging_profile["charging_profile_purpose"]
65+
)
66+
assert (
67+
charging_profile["charging_profile_kind"]
68+
== new_charging_profile["charging_profile_kind"]
69+
)
70+
assert charging_profile["transaction_id"] == new_charging_profile["transaction_id"]
71+
assert (
72+
charging_profile["recurrency_kind"] == new_charging_profile["recurrency_kind"]
73+
)
74+
assert charging_profile["valid_from"] == new_charging_profile["valid_from"]
75+
assert charging_profile["valid_to"] == new_charging_profile["valid_to"]
76+
77+
# Verify ChargingSchedule attributes
78+
charging_schedule = charging_profile["charging_schedule"]
79+
new_charging_schedule = new_charging_profile["charging_schedule"]
80+
assert (
81+
charging_schedule["charging_rate_unit"]
82+
== new_charging_schedule["charging_rate_unit"]
83+
)
84+
85+
# Verify ChargingSchedulePeriod attributes
86+
charging_period = charging_schedule["charging_schedule_period"][0]
87+
new_charging_period = new_charging_schedule["charging_schedule_period"][0]
88+
assert charging_period["start_period"] == new_charging_period["start_period"]
89+
assert charging_period["limit"] == new_charging_period["limit"]
90+
assert charging_period["number_phases"] == new_charging_period["number_phases"]
91+
92+
93+
def test_remote_start_transaction_as_class():
94+
cp: T = ChargingProfile(
95+
charging_profile_id=1,
96+
stack_level=1,
97+
charging_profile_purpose=ChargingProfilePurposeType.charge_point_max_profile,
98+
charging_profile_kind=ChargingProfileKindType.absolute,
99+
charging_schedule=ChargingSchedule(
100+
charging_rate_unit=ChargingRateUnitType.watts,
101+
charging_schedule_period=[
102+
ChargingSchedulePeriod(start_period=0, limit=10, number_phases=3)
103+
],
104+
),
105+
transaction_id=1,
106+
recurrency_kind=RecurrencyKind.daily,
107+
valid_from="2021-01-01T00:00:00Z",
108+
valid_to="2021-01-02T00:00:00Z",
109+
)
110+
111+
rst = RemoteStartTransaction(id_tag="12345", connector_id=1, charging_profile=cp)
112+
113+
new_rst = to_datatype(RemoteStartTransaction, rst)
114+
115+
# Verify top level attributes
116+
assert rst.id_tag == new_rst.id_tag
117+
assert rst.connector_id == new_rst.connector_id
118+
119+
# Verify ChargingProfile attributes
120+
charging_profile = rst.charging_profile
121+
new_charging_profile = new_rst.charging_profile
122+
assert (
123+
charging_profile.charging_profile_id
124+
== new_charging_profile["charging_profile_id"]
125+
)
126+
assert charging_profile.stack_level == new_charging_profile["stack_level"]
127+
assert (
128+
charging_profile.charging_profile_purpose
129+
== new_charging_profile["charging_profile_purpose"]
130+
)
131+
assert (
132+
charging_profile.charging_profile_kind
133+
== new_charging_profile["charging_profile_kind"]
134+
)
135+
assert charging_profile.transaction_id == new_charging_profile["transaction_id"]
136+
assert charging_profile.recurrency_kind == new_charging_profile["recurrency_kind"]
137+
assert charging_profile.valid_from == new_charging_profile["valid_from"]
138+
assert charging_profile.valid_to == new_charging_profile["valid_to"]
139+
140+
# Verify ChargingSchedule attributes
141+
charging_schedule = charging_profile.charging_schedule
142+
new_charging_schedule = new_charging_profile["charging_schedule"]
143+
assert (
144+
charging_schedule.charging_rate_unit
145+
== new_charging_schedule["charging_rate_unit"]
146+
)
147+
148+
# Verify ChargingSchedulePeriod attributes
149+
charging_period = charging_schedule.charging_schedule_period[0]
150+
new_charging_period = new_charging_schedule["charging_schedule_period"][0]
151+
assert charging_period.start_period == new_charging_period["start_period"]
152+
assert charging_period.limit == new_charging_period["limit"]
153+
assert charging_period.number_phases == new_charging_period["number_phases"]

0 commit comments

Comments
 (0)