|
17 | 17 |
|
18 | 18 | async def test_voltage_1(mocker: MockerFixture) -> None: |
19 | 19 | """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) |
21 | 21 | mockgrid.add_batteries(1, no_meter=True) |
22 | 22 | mockgrid.add_batteries(1, no_meter=False) |
23 | | - await mockgrid.start(mocker) |
24 | 23 |
|
25 | | - voltage = microgrid.voltage() |
26 | | - voltage_recv = voltage.new_receiver() |
| 24 | + async with mockgrid: |
| 25 | + voltage = microgrid.voltage() |
| 26 | + voltage_recv = voltage.new_receiver() |
27 | 27 |
|
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) |
33 | 33 |
|
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 | + ] |
41 | 41 |
|
42 | | - await mockgrid.mock_resampler.send_meter_voltage([volt_phases, volt_phases]) |
| 42 | + await mockgrid.mock_resampler.send_meter_voltage([volt_phases, volt_phases]) |
43 | 43 |
|
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] |
52 | 50 |
|
53 | 51 |
|
54 | 52 | async def test_voltage_2(mocker: MockerFixture) -> None: |
55 | 53 | """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) |
57 | 55 | mockgrid.add_batteries(1, no_meter=False) |
58 | 56 | mockgrid.add_batteries(1, no_meter=True) |
59 | | - await mockgrid.start(mocker) |
60 | | - |
61 | | - voltage = microgrid.voltage() |
62 | | - voltage_recv = voltage.new_receiver() |
63 | 57 |
|
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() |
69 | 61 |
|
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) |
77 | 67 |
|
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 | + ] |
79 | 75 |
|
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]) |
86 | 77 |
|
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] |
88 | 84 |
|
89 | 85 |
|
90 | 86 | async def test_voltage_3(mocker: MockerFixture) -> None: |
91 | 87 | """Test the phase-to-neutral voltage with None values.""" |
92 | | - mockgrid = MockMicrogrid(grid_meter=True) |
| 88 | + mockgrid = MockMicrogrid(grid_meter=True, mocker=mocker) |
93 | 89 | 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