|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | import asyncio |
9 | | -import dataclasses |
10 | 9 | import math |
11 | 10 | import re |
12 | 11 | from collections import abc |
13 | | -from contextlib import asynccontextmanager |
14 | | -from datetime import timedelta |
15 | | -from typing import AsyncIterator, TypeVar |
| 12 | +from typing import TypeVar |
16 | 13 | from unittest.mock import MagicMock |
17 | 14 |
|
18 | | -from frequenz.channels import Broadcast, Sender |
| 15 | +from frequenz.channels import Broadcast |
19 | 16 | from frequenz.client.microgrid import ComponentCategory |
20 | 17 | from pytest_mock import MockerFixture |
21 | 18 |
|
22 | | -from frequenz.sdk import microgrid |
23 | | -from frequenz.sdk.actor import ResamplerConfig |
24 | 19 | from frequenz.sdk.actor.power_distributing import ( |
25 | 20 | ComponentPoolStatus, |
26 | 21 | PowerDistributingActor, |
|
40 | 35 | Result, |
41 | 36 | Success, |
42 | 37 | ) |
43 | | -from frequenz.sdk.microgrid.component_graph import _MicrogridComponentGraph |
44 | 38 | from frequenz.sdk.timeseries import Power |
45 | 39 |
|
46 | 40 | from ...conftest import SAFETY_TIMEOUT |
| 41 | +from ...microgrid.fixtures import _Mocks, _mocks |
47 | 42 | from ...timeseries.mock_microgrid import MockMicrogrid |
48 | | -from ...utils.component_data_streamer import MockComponentDataStreamer |
49 | 43 | from ...utils.graph_generator import GraphGenerator |
50 | 44 | from .test_battery_distribution_algorithm import ( |
51 | 45 | Bound, |
|
60 | 54 | T = TypeVar("T") # Declare type variable |
61 | 55 |
|
62 | 56 |
|
63 | | -@dataclasses.dataclass(frozen=True) |
64 | | -class _Mocks: |
65 | | - """Mocks for the tests.""" |
66 | | - |
67 | | - microgrid: MockMicrogrid |
68 | | - """A mock microgrid instance.""" |
69 | | - |
70 | | - streamer: MockComponentDataStreamer |
71 | | - """A mock component data streamer.""" |
72 | | - |
73 | | - component_status_sender: Sender[ComponentPoolStatus] |
74 | | - """Sender for sending status of the components being tested.""" |
75 | | - |
76 | | - @classmethod |
77 | | - async def new( |
78 | | - cls, |
79 | | - component_category: ComponentCategory, |
80 | | - mocker: MockerFixture, |
81 | | - graph: _MicrogridComponentGraph | None = None, |
82 | | - grid_meter: bool | None = None, |
83 | | - ) -> _Mocks: |
84 | | - """Initialize the mocks.""" |
85 | | - mockgrid = MockMicrogrid(graph=graph, grid_meter=grid_meter, mocker=mocker) |
86 | | - if not graph: |
87 | | - mockgrid.add_batteries(3) |
88 | | - await mockgrid.start() |
89 | | - |
90 | | - # pylint: disable=protected-access |
91 | | - if microgrid._data_pipeline._DATA_PIPELINE is not None: |
92 | | - microgrid._data_pipeline._DATA_PIPELINE = None |
93 | | - await microgrid._data_pipeline.initialize( |
94 | | - ResamplerConfig(resampling_period=timedelta(seconds=0.1)) |
95 | | - ) |
96 | | - streamer = MockComponentDataStreamer(mockgrid.mock_client) |
97 | | - |
98 | | - dp = microgrid._data_pipeline._DATA_PIPELINE |
99 | | - assert dp is not None |
100 | | - |
101 | | - if component_category == ComponentCategory.BATTERY: |
102 | | - return cls( |
103 | | - mockgrid, |
104 | | - streamer, |
105 | | - dp._battery_power_wrapper.status_channel.new_sender(), |
106 | | - ) |
107 | | - raise ValueError(f"Unsupported component category: {component_category}") |
108 | | - |
109 | | - async def stop(self) -> None: |
110 | | - """Stop the mocks.""" |
111 | | - # pylint: disable=protected-access |
112 | | - assert microgrid._data_pipeline._DATA_PIPELINE is not None |
113 | | - await asyncio.gather( |
114 | | - *[ |
115 | | - microgrid._data_pipeline._DATA_PIPELINE._stop(), |
116 | | - self.streamer.stop(), |
117 | | - self.microgrid.cleanup(), |
118 | | - ] |
119 | | - ) |
120 | | - # pylint: enable=protected-access |
121 | | - |
122 | | - |
123 | | -@asynccontextmanager |
124 | | -async def _mocks( |
125 | | - mocker: MockerFixture, |
126 | | - component_category: ComponentCategory, |
127 | | - *, |
128 | | - graph: _MicrogridComponentGraph | None = None, |
129 | | - grid_meter: bool | None = None, |
130 | | -) -> AsyncIterator[_Mocks]: |
131 | | - """Initialize the mocks.""" |
132 | | - mocks = await _Mocks.new( |
133 | | - component_category, mocker, graph=graph, grid_meter=grid_meter |
134 | | - ) |
135 | | - try: |
136 | | - yield mocks |
137 | | - finally: |
138 | | - await mocks.stop() |
139 | | - |
140 | | - |
141 | 57 | class TestPowerDistributingActor: |
142 | 58 | # pylint: disable=protected-access |
143 | 59 | # pylint: disable=too-many-public-methods |
|
0 commit comments