forked from frequenz-floss/frequenz-client-dispatch-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_proto.py
More file actions
170 lines (153 loc) · 5.33 KB
/
test_proto.py
File metadata and controls
170 lines (153 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
"""Tests for the frequenz.client.dispatch types."""
from datetime import datetime, timedelta, timezone
from frequenz.client.common.microgrid.components import ComponentCategory
from frequenz.client.dispatch._internal_types import DispatchCreateRequest
from frequenz.client.dispatch.recurrence import (
EndCriteria,
Frequency,
RecurrenceRule,
Weekday,
)
from frequenz.client.dispatch.types import (
Dispatch,
_target_components_from_protobuf,
_target_components_to_protobuf,
)
def test_target_components() -> None:
"""Test the target components."""
for components in (
[1, 2, 3],
[10, 20, 30],
[ComponentCategory.BATTERY],
[ComponentCategory.GRID],
[ComponentCategory.METER],
[ComponentCategory.EV_CHARGER, ComponentCategory.BATTERY],
):
protobuf = _target_components_to_protobuf(components)
assert _target_components_from_protobuf(protobuf) == components
def test_end_criteria() -> None:
"""Test the end criteria."""
for end_criteria in (
EndCriteria(
until=datetime(2023, 1, 1, tzinfo=timezone.utc),
),
EndCriteria(
count=10,
),
):
assert EndCriteria.from_protobuf(end_criteria.to_protobuf()) == end_criteria
def test_recurrence_rule() -> None:
"""Test the recurrence rule."""
for recurrence_rule in (
RecurrenceRule(
frequency=Frequency.DAILY,
interval=1,
end_criteria=EndCriteria(until=datetime(2023, 1, 1, tzinfo=timezone.utc)),
byminutes=[1, 20, 59],
),
RecurrenceRule(
frequency=Frequency.MONTHLY,
interval=3,
end_criteria=EndCriteria(count=20),
byhours=[1, 12, 23],
byweekdays=[Weekday.SUNDAY, Weekday.FRIDAY],
),
RecurrenceRule(
frequency=Frequency.HOURLY,
interval=30,
end_criteria=EndCriteria(until=datetime(2025, 1, 1, tzinfo=timezone.utc)),
bymonthdays=[1, 15, 31],
),
RecurrenceRule(
frequency=Frequency.MONTHLY,
interval=10,
end_criteria=EndCriteria(count=5),
bymonths=[1, 6, 12],
),
RecurrenceRule(
frequency=Frequency.WEEKLY,
interval=2,
end_criteria=EndCriteria(count=10),
byweekdays=[Weekday.MONDAY, Weekday.TUESDAY],
),
):
assert (
RecurrenceRule.from_protobuf(recurrence_rule.to_protobuf())
== recurrence_rule
)
def test_dispatch() -> None:
"""Test the dispatch."""
for dispatch in (
Dispatch(
id=123,
type="test",
create_time=datetime(2023, 1, 1, tzinfo=timezone.utc),
update_time=datetime(2023, 1, 1, tzinfo=timezone.utc),
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
end_time=datetime(2024, 10, 20, tzinfo=timezone.utc),
duration=timedelta(days=10),
target=[1, 2, 3],
active=True,
dry_run=False,
payload={"key": "value"},
recurrence=RecurrenceRule(
frequency=Frequency.DAILY,
interval=1,
end_criteria=EndCriteria(
until=datetime(2023, 1, 1, tzinfo=timezone.utc)
),
byminutes=[1, 20, 59],
),
),
Dispatch(
id=124,
type="test-2",
create_time=datetime(2024, 3, 10, tzinfo=timezone.utc),
update_time=datetime(2024, 3, 11, tzinfo=timezone.utc),
start_time=datetime(2024, 11, 10, tzinfo=timezone.utc),
end_time=datetime(2024, 11, 20, tzinfo=timezone.utc),
duration=timedelta(seconds=20),
target=[ComponentCategory.BATTERY],
active=False,
dry_run=True,
payload={"key": "value1"},
recurrence=RecurrenceRule(
frequency=Frequency.MONTHLY,
interval=3,
end_criteria=EndCriteria(count=20),
byhours=[1, 12, 23],
byweekdays=[Weekday.SUNDAY, Weekday.FRIDAY],
),
),
):
assert Dispatch.from_protobuf(dispatch.to_protobuf()) == dispatch
def test_dispatch_create_request_with_no_recurrence() -> None:
"""Test the dispatch create request with no recurrence."""
request = DispatchCreateRequest(
microgrid_id=123,
type="test",
start_time=datetime(2024, 10, 10, tzinfo=timezone.utc),
duration=timedelta(days=10),
target=[1, 2, 3],
active=True,
dry_run=False,
payload={"key": "value"},
recurrence=None,
)
assert request.to_protobuf().dispatch_data.HasField("recurrence") is False
def test_dispatch_create_start_immediately() -> None:
"""Test the dispatch create request with no start time."""
request = DispatchCreateRequest(
microgrid_id=123,
type="test",
start_time="NOW",
duration=timedelta(days=10),
target=[1, 2, 3],
active=True,
dry_run=False,
payload={"key": "value"},
recurrence=RecurrenceRule(),
)
assert request == DispatchCreateRequest.from_protobuf(request.to_protobuf())