Skip to content

Commit 4b456df

Browse files
Add tests for graph DFS search
Signed-off-by: Matthias Wende <[email protected]>
1 parent 1b6c74b commit 4b456df

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

tests/microgrid/test_graph.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,116 @@ def test_connection_filters(self) -> None:
403403
Connection(2, 6),
404404
}
405405

406+
def test_dfs_search_two_grid_meters(self) -> None:
407+
"""Test DFS searching PV components in a graph with two grid meters."""
408+
grid = Component(1, ComponentCategory.GRID)
409+
pv_inverters = {
410+
Component(4, ComponentCategory.INVERTER, InverterType.SOLAR),
411+
Component(5, ComponentCategory.INVERTER, InverterType.SOLAR),
412+
}
413+
414+
graph = gr._MicrogridComponentGraph(
415+
components={
416+
grid,
417+
Component(2, ComponentCategory.METER),
418+
Component(3, ComponentCategory.METER),
419+
}.union(pv_inverters),
420+
connections={
421+
Connection(1, 2),
422+
Connection(1, 3),
423+
Connection(2, 4),
424+
Connection(2, 5),
425+
},
426+
)
427+
428+
result = graph.dfs(grid, set(), graph.is_pv_inverter)
429+
assert result == pv_inverters
430+
431+
def test_dfs_search_grid_meter(self) -> None:
432+
"""Test DFS searching PV components in a graph with a single grid meter."""
433+
grid = Component(1, ComponentCategory.GRID)
434+
pv_meters = {
435+
Component(3, ComponentCategory.METER),
436+
Component(4, ComponentCategory.METER),
437+
}
438+
439+
graph = gr._MicrogridComponentGraph(
440+
components={
441+
grid,
442+
Component(2, ComponentCategory.METER),
443+
Component(5, ComponentCategory.INVERTER, InverterType.SOLAR),
444+
Component(6, ComponentCategory.INVERTER, InverterType.SOLAR),
445+
}.union(pv_meters),
446+
connections={
447+
Connection(1, 2),
448+
Connection(2, 3),
449+
Connection(2, 4),
450+
Connection(3, 5),
451+
Connection(4, 6),
452+
},
453+
)
454+
455+
result = graph.dfs(grid, set(), graph.is_pv_chain)
456+
assert result == pv_meters
457+
458+
def test_dfs_search_no_grid_meter(self) -> None:
459+
"""Test DFS searching PV components in a graph with no grid meter."""
460+
grid = Component(1, ComponentCategory.GRID)
461+
pv_meters = {
462+
Component(3, ComponentCategory.METER),
463+
Component(4, ComponentCategory.METER),
464+
}
465+
466+
graph = gr._MicrogridComponentGraph(
467+
components={
468+
grid,
469+
Component(2, ComponentCategory.METER),
470+
Component(5, ComponentCategory.INVERTER, InverterType.SOLAR),
471+
Component(6, ComponentCategory.INVERTER, InverterType.SOLAR),
472+
}.union(pv_meters),
473+
connections={
474+
Connection(1, 2),
475+
Connection(1, 3),
476+
Connection(1, 4),
477+
Connection(3, 5),
478+
Connection(4, 6),
479+
},
480+
)
481+
482+
result = graph.dfs(grid, set(), graph.is_pv_chain)
483+
assert result == pv_meters
484+
485+
def test_dfs_search_nested_components(self) -> None:
486+
"""Test DFS searching PV components in a graph with nested components."""
487+
grid = Component(1, ComponentCategory.GRID)
488+
battery_components = {
489+
Component(4, ComponentCategory.METER),
490+
Component(5, ComponentCategory.METER),
491+
Component(6, ComponentCategory.INVERTER, InverterType.BATTERY),
492+
}
493+
494+
graph = gr._MicrogridComponentGraph(
495+
components={
496+
grid,
497+
Component(2, ComponentCategory.METER),
498+
Component(3, ComponentCategory.METER),
499+
Component(7, ComponentCategory.INVERTER, InverterType.BATTERY),
500+
Component(8, ComponentCategory.INVERTER, InverterType.BATTERY),
501+
}.union(battery_components),
502+
connections={
503+
Connection(1, 2),
504+
Connection(2, 3),
505+
Connection(2, 6),
506+
Connection(3, 4),
507+
Connection(3, 5),
508+
Connection(4, 7),
509+
Connection(5, 8),
510+
},
511+
)
512+
513+
assert set() == graph.dfs(grid, set(), graph.is_pv_chain)
514+
assert battery_components == graph.dfs(grid, set(), graph.is_battery_chain)
515+
406516

407517
class Test_MicrogridComponentGraph:
408518
"""Test cases for the package-internal implementation of the ComponentGraph.

0 commit comments

Comments
 (0)