33
44"""Tests for the `Grid` module."""
55
6+ from pytest_mock import MockerFixture
7+
8+ import frequenz .sdk .microgrid .component_graph as gr
9+ from frequenz .sdk import microgrid
10+ from frequenz .sdk .microgrid .client import Connection
611from frequenz .sdk .microgrid .component import Component , ComponentCategory , GridMetadata
712from frequenz .sdk .timeseries import Current , Fuse
8- from frequenz .sdk .timeseries import grid as _grid
13+
14+ from ..timeseries .mock_microgrid import MockMicrogrid
915
1016
11- async def test_grid_1 () -> None :
17+ async def test_grid_1 (mocker : MockerFixture ) -> None :
1218 """Test the grid connection module."""
1319 # The tests here need to be in this exact sequence, because the grid connection
1420 # is a singleton. Once it gets created, it stays in memory for the duration of
1521 # the tests, unless we explicitly delete it.
1622
1723 # validate that islands with no grid connection are accepted.
18- components = [
24+ components = {
25+ Component (1 , ComponentCategory .NONE ),
1926 Component (2 , ComponentCategory .METER ),
20- ]
27+ }
28+ connections = {
29+ Connection (1 , 2 ),
30+ }
31+ # pylint: disable=protected-access
32+ graph = gr ._MicrogridComponentGraph (components = components , connections = connections )
33+
34+ mockgrid = MockMicrogrid (graph = graph )
35+ await mockgrid .start (mocker )
36+ grid = microgrid .grid ()
2137
22- _grid .initialize (components )
23- grid = _grid .get ()
2438 assert grid is None
39+ await mockgrid .cleanup ()
2540
2641
2742def _create_fuse () -> Fuse :
@@ -35,42 +50,26 @@ def _create_fuse() -> Fuse:
3550 return fuse
3651
3752
38- async def test_grid_2 () -> None :
39- """Test the grid connection module, slightly more complex.
40-
41- Validate that the microgrid initialization fails
42- when there are multiple grid connection points.
43- """
44- fuse = _create_fuse ()
45-
46- components = [
47- Component (1 , ComponentCategory .GRID , None , GridMetadata (fuse )),
48- Component (2 , ComponentCategory .GRID , None , GridMetadata (fuse )),
49- Component (3 , ComponentCategory .METER ),
50- ]
51-
52- try :
53- _grid .initialize (components )
54- assert False , "Expected microgrid.grid() to raise a RuntimeError."
55- except RuntimeError :
56- pass
57-
58-
59- async def test_grid_3 () -> None :
53+ async def test_grid_2 (mocker : MockerFixture ) -> None :
6054 """Validate that microgrids with one grid connection are accepted."""
61- components = [
55+ components = {
6256 Component (1 , ComponentCategory .GRID , None , GridMetadata (_create_fuse ())),
6357 Component (2 , ComponentCategory .METER ),
64- ]
58+ }
59+ connections = {
60+ Connection (1 , 2 ),
61+ }
6562
66- _grid .initialize (components )
67- grid = _grid .get ()
63+ # pylint: disable=protected-access
64+ graph = gr ._MicrogridComponentGraph (components = components , connections = connections )
65+
66+ mockgrid = MockMicrogrid (graph = graph )
67+ await mockgrid .start (mocker )
68+
69+ grid = microgrid .grid ()
6870 assert grid is not None
6971
7072 expected_fuse_current = Current .from_amperes (123.0 )
7173 expected_fuse = Fuse (expected_fuse_current )
7274
73- assert grid == _grid .Grid (fuse = expected_fuse )
74-
75- fuse_current = grid .fuse .max_current
76- assert fuse_current == expected_fuse_current
75+ assert grid .fuse == expected_fuse
0 commit comments