@@ -78,7 +78,6 @@ async def test_matryoshka_algorithm() -> None: # pylint: disable=too-many-state
7878 """Tests for the power managing actor."""
7979 batteries = frozenset ({2 , 5 })
8080
81- algorithm = Matryoshka ()
8281 system_bounds = battery_pool .PowerMetrics (
8382 timestamp = datetime .now (tz = timezone .utc ),
8483 inclusion_bounds = timeseries .Bounds (
@@ -87,139 +86,96 @@ async def test_matryoshka_algorithm() -> None: # pylint: disable=too-many-state
8786 exclusion_bounds = timeseries .Bounds (lower = Power .zero (), upper = Power .zero ()),
8887 )
8988
90- call_count = 0
89+ tester = StatefulTester ( batteries , system_bounds )
9190
92- def test_tgt_power (
93- priority : int ,
94- power : float | None ,
95- bounds : tuple [float | None , float | None ],
96- expected : float | None ,
97- must_send : bool = False ,
98- ) -> None :
99- nonlocal call_count
100- call_count += 1
101- tgt_power = algorithm .calculate_target_power (
102- batteries ,
103- Proposal (
104- battery_ids = batteries ,
105- source_id = f"actor-{ priority } " ,
106- preferred_power = None if power is None else Power .from_watts (power ),
107- bounds = timeseries .Bounds (
108- None if bounds [0 ] is None else Power .from_watts (bounds [0 ]),
109- None if bounds [1 ] is None else Power .from_watts (bounds [1 ]),
110- ),
111- priority = priority ,
112- ),
113- system_bounds ,
114- must_send ,
115- )
116- assert tgt_power == (
117- Power .from_watts (expected ) if expected is not None else None
118- )
119-
120- def test_bounds (
121- priority : int ,
122- expected_power : float | None ,
123- expected_bounds : tuple [float , float ],
124- ) -> None :
125- report = algorithm .get_status (batteries , priority , system_bounds , None )
126- if expected_power is None :
127- assert report .target_power is None
128- else :
129- assert report .target_power is not None
130- assert report .target_power .as_watts () == expected_power
131- assert report .inclusion_bounds is not None
132- assert report .inclusion_bounds .lower .as_watts () == expected_bounds [0 ]
133- assert report .inclusion_bounds .upper .as_watts () == expected_bounds [1 ]
134-
135- test_tgt_power (priority = 2 , power = 25.0 , bounds = (25.0 , 50.0 ), expected = 25.0 )
136- test_bounds (priority = 2 , expected_power = 25.0 , expected_bounds = (- 200.0 , 200.0 ))
137- test_bounds (priority = 1 , expected_power = 25.0 , expected_bounds = (25.0 , 50.0 ))
91+ tester .tgt_power (priority = 2 , power = 25.0 , bounds = (25.0 , 50.0 ), expected = 25.0 )
92+ tester .bounds (priority = 2 , expected_power = 25.0 , expected_bounds = (- 200.0 , 200.0 ))
93+ tester .bounds (priority = 1 , expected_power = 25.0 , expected_bounds = (25.0 , 50.0 ))
13894
139- test_tgt_power (priority = 1 , power = 20.0 , bounds = (20.0 , 50.0 ), expected = None )
140- test_tgt_power (
95+ tester . tgt_power (priority = 1 , power = 20.0 , bounds = (20.0 , 50.0 ), expected = None )
96+ tester . tgt_power (
14197 priority = 1 , power = 20.0 , bounds = (20.0 , 50.0 ), expected = 25.0 , must_send = True
14298 )
143- test_bounds (priority = 1 , expected_power = 25.0 , expected_bounds = (25.0 , 50.0 ))
144-
145- test_tgt_power (priority = 3 , power = 10.0 , bounds = (10.0 , 15.0 ), expected = 15.0 )
146- test_bounds (priority = 3 , expected_power = 15.0 , expected_bounds = (- 200.0 , 200.0 ))
147- test_bounds (priority = 2 , expected_power = 15.0 , expected_bounds = (10.0 , 15.0 ))
148- test_bounds (priority = 1 , expected_power = 15.0 , expected_bounds = (10.0 , 15.0 ))
149-
150- test_tgt_power (priority = 3 , power = 10.0 , bounds = (10.0 , 22.0 ), expected = 22.0 )
151- test_bounds (priority = 3 , expected_power = 22.0 , expected_bounds = (- 200.0 , 200.0 ))
152- test_bounds (priority = 2 , expected_power = 22.0 , expected_bounds = (10.0 , 22.0 ))
153- test_bounds (priority = 1 , expected_power = 22.0 , expected_bounds = (10.0 , 22.0 ))
154-
155- test_tgt_power (priority = 1 , power = 30.0 , bounds = (20.0 , 50.0 ), expected = None )
156- test_bounds (priority = 1 , expected_power = 22.0 , expected_bounds = (10.0 , 22.0 ))
157-
158- test_tgt_power (priority = 3 , power = 10.0 , bounds = (10.0 , 50.0 ), expected = 30.0 )
159- test_bounds (priority = 3 , expected_power = 30.0 , expected_bounds = (- 200.0 , 200.0 ))
160- test_bounds (priority = 2 , expected_power = 30.0 , expected_bounds = (10.0 , 50.0 ))
161- test_bounds (priority = 1 , expected_power = 30.0 , expected_bounds = (25.0 , 50.0 ))
162-
163- test_tgt_power (priority = 2 , power = 40.0 , bounds = (40.0 , None ), expected = 40.0 )
164- test_bounds (priority = 3 , expected_power = 40.0 , expected_bounds = (- 200.0 , 200.0 ))
165- test_bounds (priority = 2 , expected_power = 40.0 , expected_bounds = (10.0 , 50.0 ))
166- test_bounds (priority = 1 , expected_power = 40.0 , expected_bounds = (40.0 , 50.0 ))
167-
168- test_tgt_power (priority = 2 , power = 0.0 , bounds = (None , None ), expected = 30.0 )
169- test_bounds (priority = 4 , expected_power = 30.0 , expected_bounds = (- 200.0 , 200.0 ))
170- test_bounds (priority = 3 , expected_power = 30.0 , expected_bounds = (- 200.0 , 200.0 ))
171- test_bounds (priority = 2 , expected_power = 30.0 , expected_bounds = (10.0 , 50.0 ))
172- test_bounds (priority = 1 , expected_power = 30.0 , expected_bounds = (10.0 , 50.0 ))
173-
174- test_tgt_power (priority = 4 , power = - 50.0 , bounds = (None , - 50.0 ), expected = - 50.0 )
175- test_bounds (priority = 4 , expected_power = - 50.0 , expected_bounds = (- 200.0 , 200.0 ))
176- test_bounds (priority = 3 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
177- test_bounds (priority = 2 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
178- test_bounds (priority = 1 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
179-
180- test_tgt_power (priority = 3 , power = - 0.0 , bounds = (- 200.0 , 200.0 ), expected = None )
181- test_bounds (priority = 1 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
182-
183- test_tgt_power (priority = 1 , power = - 150.0 , bounds = (- 200.0 , - 150.0 ), expected = - 150.0 )
184- test_bounds (priority = 2 , expected_power = - 150.0 , expected_bounds = (- 200.0 , - 50.0 ))
185- test_bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , - 50.0 ))
186-
187- test_tgt_power (priority = 4 , power = - 180.0 , bounds = (- 200.0 , - 50.0 ), expected = None )
188- test_bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , - 50.0 ))
189-
190- test_tgt_power (priority = 4 , power = 50.0 , bounds = (50.0 , None ), expected = 50.0 )
191- test_bounds (priority = 4 , expected_power = 50.0 , expected_bounds = (- 200.0 , 200.0 ))
192- test_bounds (priority = 3 , expected_power = 50.0 , expected_bounds = (50.0 , 200.0 ))
193- test_bounds (priority = 2 , expected_power = 50.0 , expected_bounds = (50.0 , 200.0 ))
194- test_bounds (priority = 1 , expected_power = 50.0 , expected_bounds = (50.0 , 200.0 ))
195-
196- test_tgt_power (priority = 4 , power = 0.0 , bounds = (- 200.0 , 200.0 ), expected = - 150.0 )
197- test_bounds (priority = 4 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
198- test_bounds (priority = 3 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
199- test_bounds (priority = 2 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
200- test_bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
201-
202- test_tgt_power (priority = 3 , power = 0.0 , bounds = (- 200.0 , 200.0 ), expected = None )
203- test_bounds (priority = 3 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
204- test_bounds (priority = 2 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
205- test_bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
206-
207- test_tgt_power (priority = 2 , power = 50.0 , bounds = (- 100 , 100 ), expected = - 100.0 )
208- test_bounds (priority = 3 , expected_power = - 100.0 , expected_bounds = (- 200.0 , 200.0 ))
209- test_bounds (priority = 2 , expected_power = - 100.0 , expected_bounds = (- 200.0 , 200.0 ))
210- test_bounds (priority = 1 , expected_power = - 100.0 , expected_bounds = (- 100.0 , 100.0 ))
211-
212- test_tgt_power (priority = 1 , power = 100.0 , bounds = (100 , 200 ), expected = 100.0 )
213- test_bounds (priority = 1 , expected_power = 100.0 , expected_bounds = (- 100.0 , 100.0 ))
214-
215- test_tgt_power (priority = 1 , power = 50.0 , bounds = (50 , 200 ), expected = 50.0 )
216- test_bounds (priority = 1 , expected_power = 50.0 , expected_bounds = (- 100.0 , 100.0 ))
217-
218- test_tgt_power (priority = 1 , power = 200.0 , bounds = (50 , 200 ), expected = 100.0 )
219- test_bounds (priority = 1 , expected_power = 100.0 , expected_bounds = (- 100.0 , 100.0 ))
220-
221- test_tgt_power (priority = 1 , power = 0.0 , bounds = (- 200 , 200 ), expected = 0.0 )
222- test_bounds (priority = 1 , expected_power = 0.0 , expected_bounds = (- 100.0 , 100.0 ))
223-
224- test_tgt_power (priority = 1 , power = None , bounds = (- 200 , 200 ), expected = 50.0 )
225- test_bounds (priority = 1 , expected_power = 50.0 , expected_bounds = (- 100.0 , 100.0 ))
99+ tester . bounds (priority = 1 , expected_power = 25.0 , expected_bounds = (25.0 , 50.0 ))
100+
101+ tester . tgt_power (priority = 3 , power = 10.0 , bounds = (10.0 , 15.0 ), expected = 15.0 )
102+ tester . bounds (priority = 3 , expected_power = 15.0 , expected_bounds = (- 200.0 , 200.0 ))
103+ tester . bounds (priority = 2 , expected_power = 15.0 , expected_bounds = (10.0 , 15.0 ))
104+ tester . bounds (priority = 1 , expected_power = 15.0 , expected_bounds = (10.0 , 15.0 ))
105+
106+ tester . tgt_power (priority = 3 , power = 10.0 , bounds = (10.0 , 22.0 ), expected = 22.0 )
107+ tester . bounds (priority = 3 , expected_power = 22.0 , expected_bounds = (- 200.0 , 200.0 ))
108+ tester . bounds (priority = 2 , expected_power = 22.0 , expected_bounds = (10.0 , 22.0 ))
109+ tester . bounds (priority = 1 , expected_power = 22.0 , expected_bounds = (10.0 , 22.0 ))
110+
111+ tester . tgt_power (priority = 1 , power = 30.0 , bounds = (20.0 , 50.0 ), expected = None )
112+ tester . bounds (priority = 1 , expected_power = 22.0 , expected_bounds = (10.0 , 22.0 ))
113+
114+ tester . tgt_power (priority = 3 , power = 10.0 , bounds = (10.0 , 50.0 ), expected = 30.0 )
115+ tester . bounds (priority = 3 , expected_power = 30.0 , expected_bounds = (- 200.0 , 200.0 ))
116+ tester . bounds (priority = 2 , expected_power = 30.0 , expected_bounds = (10.0 , 50.0 ))
117+ tester . bounds (priority = 1 , expected_power = 30.0 , expected_bounds = (25.0 , 50.0 ))
118+
119+ tester . tgt_power (priority = 2 , power = 40.0 , bounds = (40.0 , None ), expected = 40.0 )
120+ tester . bounds (priority = 3 , expected_power = 40.0 , expected_bounds = (- 200.0 , 200.0 ))
121+ tester . bounds (priority = 2 , expected_power = 40.0 , expected_bounds = (10.0 , 50.0 ))
122+ tester . bounds (priority = 1 , expected_power = 40.0 , expected_bounds = (40.0 , 50.0 ))
123+
124+ tester . tgt_power (priority = 2 , power = 0.0 , bounds = (None , None ), expected = 30.0 )
125+ tester . bounds (priority = 4 , expected_power = 30.0 , expected_bounds = (- 200.0 , 200.0 ))
126+ tester . bounds (priority = 3 , expected_power = 30.0 , expected_bounds = (- 200.0 , 200.0 ))
127+ tester . bounds (priority = 2 , expected_power = 30.0 , expected_bounds = (10.0 , 50.0 ))
128+ tester . bounds (priority = 1 , expected_power = 30.0 , expected_bounds = (10.0 , 50.0 ))
129+
130+ tester . tgt_power (priority = 4 , power = - 50.0 , bounds = (None , - 50.0 ), expected = - 50.0 )
131+ tester . bounds (priority = 4 , expected_power = - 50.0 , expected_bounds = (- 200.0 , 200.0 ))
132+ tester . bounds (priority = 3 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
133+ tester . bounds (priority = 2 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
134+ tester . bounds (priority = 1 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
135+
136+ tester . tgt_power (priority = 3 , power = - 0.0 , bounds = (- 200.0 , 200.0 ), expected = None )
137+ tester . bounds (priority = 1 , expected_power = - 50.0 , expected_bounds = (- 200.0 , - 50.0 ))
138+
139+ tester . tgt_power (priority = 1 , power = - 150.0 , bounds = (- 200.0 , - 150.0 ), expected = - 150.0 )
140+ tester . bounds (priority = 2 , expected_power = - 150.0 , expected_bounds = (- 200.0 , - 50.0 ))
141+ tester . bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , - 50.0 ))
142+
143+ tester . tgt_power (priority = 4 , power = - 180.0 , bounds = (- 200.0 , - 50.0 ), expected = None )
144+ tester . bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , - 50.0 ))
145+
146+ tester . tgt_power (priority = 4 , power = 50.0 , bounds = (50.0 , None ), expected = 50.0 )
147+ tester . bounds (priority = 4 , expected_power = 50.0 , expected_bounds = (- 200.0 , 200.0 ))
148+ tester . bounds (priority = 3 , expected_power = 50.0 , expected_bounds = (50.0 , 200.0 ))
149+ tester . bounds (priority = 2 , expected_power = 50.0 , expected_bounds = (50.0 , 200.0 ))
150+ tester . bounds (priority = 1 , expected_power = 50.0 , expected_bounds = (50.0 , 200.0 ))
151+
152+ tester . tgt_power (priority = 4 , power = 0.0 , bounds = (- 200.0 , 200.0 ), expected = - 150.0 )
153+ tester . bounds (priority = 4 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
154+ tester . bounds (priority = 3 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
155+ tester . bounds (priority = 2 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
156+ tester . bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
157+
158+ tester . tgt_power (priority = 3 , power = 0.0 , bounds = (- 200.0 , 200.0 ), expected = None )
159+ tester . bounds (priority = 3 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
160+ tester . bounds (priority = 2 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
161+ tester . bounds (priority = 1 , expected_power = - 150.0 , expected_bounds = (- 200.0 , 200.0 ))
162+
163+ tester . tgt_power (priority = 2 , power = 50.0 , bounds = (- 100 , 100 ), expected = - 100.0 )
164+ tester . bounds (priority = 3 , expected_power = - 100.0 , expected_bounds = (- 200.0 , 200.0 ))
165+ tester . bounds (priority = 2 , expected_power = - 100.0 , expected_bounds = (- 200.0 , 200.0 ))
166+ tester . bounds (priority = 1 , expected_power = - 100.0 , expected_bounds = (- 100.0 , 100.0 ))
167+
168+ tester . tgt_power (priority = 1 , power = 100.0 , bounds = (100 , 200 ), expected = 100.0 )
169+ tester . bounds (priority = 1 , expected_power = 100.0 , expected_bounds = (- 100.0 , 100.0 ))
170+
171+ tester . tgt_power (priority = 1 , power = 50.0 , bounds = (50 , 200 ), expected = 50.0 )
172+ tester . bounds (priority = 1 , expected_power = 50.0 , expected_bounds = (- 100.0 , 100.0 ))
173+
174+ tester . tgt_power (priority = 1 , power = 200.0 , bounds = (50 , 200 ), expected = 100.0 )
175+ tester . bounds (priority = 1 , expected_power = 100.0 , expected_bounds = (- 100.0 , 100.0 ))
176+
177+ tester . tgt_power (priority = 1 , power = 0.0 , bounds = (- 200 , 200 ), expected = 0.0 )
178+ tester . bounds (priority = 1 , expected_power = 0.0 , expected_bounds = (- 100.0 , 100.0 ))
179+
180+ tester . tgt_power (priority = 1 , power = None , bounds = (- 200 , 200 ), expected = 50.0 )
181+ tester . bounds (priority = 1 , expected_power = 50.0 , expected_bounds = (- 100.0 , 100.0 ))
0 commit comments