Skip to content

Commit 60fd837

Browse files
committed
Improve finalization of voltage streamer tests
Use the `MockMicrogrid` as a context manager so it gets automatically finalized. Before no finalization was done in the tests, except for only one. This commit can be reviewed much more easily hiding differences in spaces (with `diff -w`, or appending `?w=1` to the GitHub URL). Signed-off-by: Leandro Lucarella <[email protected]>
1 parent af2005d commit 60fd837

File tree

1 file changed

+76
-82
lines changed

1 file changed

+76
-82
lines changed

tests/timeseries/test_voltage_streamer.py

Lines changed: 76 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,108 +17,102 @@
1717

1818
async def test_voltage_1(mocker: MockerFixture) -> None:
1919
"""Test the phase-to-neutral voltage with a grid side meter."""
20-
mockgrid = MockMicrogrid(grid_meter=True)
20+
mockgrid = MockMicrogrid(grid_meter=True, mocker=mocker)
2121
mockgrid.add_batteries(1, no_meter=True)
2222
mockgrid.add_batteries(1, no_meter=False)
23-
await mockgrid.start(mocker)
2423

25-
voltage = microgrid.voltage()
26-
voltage_recv = voltage.new_receiver()
24+
async with mockgrid:
25+
voltage = microgrid.voltage()
26+
voltage_recv = voltage.new_receiver()
2727

28-
assert voltage._task is not None
29-
# Wait for voltage requests to be sent, one request per phase.
30-
await asyncio.sleep(0)
31-
await asyncio.sleep(0)
32-
await asyncio.sleep(0)
28+
assert voltage._task is not None
29+
# Wait for voltage requests to be sent, one request per phase.
30+
await asyncio.sleep(0)
31+
await asyncio.sleep(0)
32+
await asyncio.sleep(0)
3333

34-
for count in range(10):
35-
volt_delta = 1 if count % 2 == 0 else -1
36-
volt_phases: list[float | None] = [
37-
220.0 * volt_delta,
38-
219.8 * volt_delta,
39-
220.2 * volt_delta,
40-
]
34+
for count in range(10):
35+
volt_delta = 1 if count % 2 == 0 else -1
36+
volt_phases: list[float | None] = [
37+
220.0 * volt_delta,
38+
219.8 * volt_delta,
39+
220.2 * volt_delta,
40+
]
4141

42-
await mockgrid.mock_resampler.send_meter_voltage([volt_phases, volt_phases])
42+
await mockgrid.mock_resampler.send_meter_voltage([volt_phases, volt_phases])
4343

44-
val = await voltage_recv.receive()
45-
assert val is not None
46-
assert val.value_p1 and val.value_p2 and val.value_p3
47-
assert val.value_p1.as_volts() == volt_phases[0]
48-
assert val.value_p2.as_volts() == volt_phases[1]
49-
assert val.value_p3.as_volts() == volt_phases[2]
50-
51-
await mockgrid.cleanup()
44+
val = await voltage_recv.receive()
45+
assert val is not None
46+
assert val.value_p1 and val.value_p2 and val.value_p3
47+
assert val.value_p1.as_volts() == volt_phases[0]
48+
assert val.value_p2.as_volts() == volt_phases[1]
49+
assert val.value_p3.as_volts() == volt_phases[2]
5250

5351

5452
async def test_voltage_2(mocker: MockerFixture) -> None:
5553
"""Test the phase-to-neutral voltage without a grid side meter."""
56-
mockgrid = MockMicrogrid(grid_meter=False)
54+
mockgrid = MockMicrogrid(grid_meter=False, mocker=mocker)
5755
mockgrid.add_batteries(1, no_meter=False)
5856
mockgrid.add_batteries(1, no_meter=True)
59-
await mockgrid.start(mocker)
60-
61-
voltage = microgrid.voltage()
62-
voltage_recv = voltage.new_receiver()
6357

64-
assert voltage._task is not None
65-
# Wait for voltage requests to be sent, one request per phase.
66-
await asyncio.sleep(0)
67-
await asyncio.sleep(0)
68-
await asyncio.sleep(0)
58+
async with mockgrid:
59+
voltage = microgrid.voltage()
60+
voltage_recv = voltage.new_receiver()
6961

70-
for count in range(10):
71-
volt_delta = 1 if count % 2 == 0 else -1
72-
volt_phases: list[float | None] = [
73-
220.0 * volt_delta,
74-
219.8 * volt_delta,
75-
220.2 * volt_delta,
76-
]
62+
assert voltage._task is not None
63+
# Wait for voltage requests to be sent, one request per phase.
64+
await asyncio.sleep(0)
65+
await asyncio.sleep(0)
66+
await asyncio.sleep(0)
7767

78-
await mockgrid.mock_resampler.send_meter_voltage([volt_phases])
68+
for count in range(10):
69+
volt_delta = 1 if count % 2 == 0 else -1
70+
volt_phases: list[float | None] = [
71+
220.0 * volt_delta,
72+
219.8 * volt_delta,
73+
220.2 * volt_delta,
74+
]
7975

80-
val = await voltage_recv.receive()
81-
assert val is not None
82-
assert val.value_p1 and val.value_p2 and val.value_p3
83-
assert val.value_p1.as_volts() == volt_phases[0]
84-
assert val.value_p2.as_volts() == volt_phases[1]
85-
assert val.value_p3.as_volts() == volt_phases[2]
76+
await mockgrid.mock_resampler.send_meter_voltage([volt_phases])
8677

87-
await mockgrid.cleanup()
78+
val = await voltage_recv.receive()
79+
assert val is not None
80+
assert val.value_p1 and val.value_p2 and val.value_p3
81+
assert val.value_p1.as_volts() == volt_phases[0]
82+
assert val.value_p2.as_volts() == volt_phases[1]
83+
assert val.value_p3.as_volts() == volt_phases[2]
8884

8985

9086
async def test_voltage_3(mocker: MockerFixture) -> None:
9187
"""Test the phase-to-neutral voltage with None values."""
92-
mockgrid = MockMicrogrid(grid_meter=True)
88+
mockgrid = MockMicrogrid(grid_meter=True, mocker=mocker)
9389
mockgrid.add_batteries(2, no_meter=False)
94-
await mockgrid.start(mocker)
95-
96-
voltage = microgrid.voltage()
97-
voltage_recv = voltage.new_receiver()
98-
99-
assert voltage._task is not None
100-
# Wait for voltage requests to be sent, one request per phase.
101-
await asyncio.sleep(0)
102-
await asyncio.sleep(0)
103-
await asyncio.sleep(0)
104-
105-
for count in range(10):
106-
volt_delta = 1 if count % 2 == 0 else -1
107-
volt_phases: list[float | None] = [
108-
220.0 * volt_delta,
109-
219.8 * volt_delta,
110-
220.2 * volt_delta,
111-
]
112-
113-
await mockgrid.mock_resampler.send_meter_voltage(
114-
[volt_phases, [None, None, None], [None, 219.8, 220.2]]
115-
)
116-
117-
val = await voltage_recv.receive()
118-
assert val is not None
119-
assert val.value_p1 and val.value_p2 and val.value_p3
120-
assert val.value_p1.as_volts() == volt_phases[0]
121-
assert val.value_p2.as_volts() == volt_phases[1]
122-
assert val.value_p3.as_volts() == volt_phases[2]
123-
124-
await mockgrid.cleanup()
90+
91+
async with mockgrid:
92+
voltage = microgrid.voltage()
93+
voltage_recv = voltage.new_receiver()
94+
95+
assert voltage._task is not None
96+
# Wait for voltage requests to be sent, one request per phase.
97+
await asyncio.sleep(0)
98+
await asyncio.sleep(0)
99+
await asyncio.sleep(0)
100+
101+
for count in range(10):
102+
volt_delta = 1 if count % 2 == 0 else -1
103+
volt_phases: list[float | None] = [
104+
220.0 * volt_delta,
105+
219.8 * volt_delta,
106+
220.2 * volt_delta,
107+
]
108+
109+
await mockgrid.mock_resampler.send_meter_voltage(
110+
[volt_phases, [None, None, None], [None, 219.8, 220.2]]
111+
)
112+
113+
val = await voltage_recv.receive()
114+
assert val is not None
115+
assert val.value_p1 and val.value_p2 and val.value_p3
116+
assert val.value_p1.as_volts() == volt_phases[0]
117+
assert val.value_p2.as_volts() == volt_phases[1]
118+
assert val.value_p3.as_volts() == volt_phases[2]

0 commit comments

Comments
 (0)