-
Notifications
You must be signed in to change notification settings - Fork 10
feat: iterate over shortest path #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
28f8e3e
102c280
ff85f14
5baeaf7
1761bb8
a4a1c0e
5cdbb67
d5e2c8f
c98798a
42ec97a
8baba8d
0e681c6
441e2c5
5c3734b
6e15083
5277f52
06ab70f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from power_grid_model_ds import Grid | ||||||||||||||||||||||||||||
| from power_grid_model_ds._core.model.arrays import LineArray, ThreeWindingTransformerArray, TransformerArray | ||||||||||||||||||||||||||||
| from power_grid_model_ds._core.model.arrays.base.errors import RecordDoesNotExist | ||||||||||||||||||||||||||||
| from power_grid_model_ds._core.model.enums.nodes import NodeType | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -56,6 +57,36 @@ def test_get_branches_in_path_empty_path(self, basic_grid): | |||||||||||||||||||||||||||
| assert 0 == branches.size | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class TestIterBranchesInShortestPath: | ||||||||||||||||||||||||||||
| def test_iter_branches_in_shortest_path_returns_branch_arrays(self, basic_grid): | ||||||||||||||||||||||||||||
| branches = list(basic_grid.iter_branches_in_shortest_path(101, 106)) | ||||||||||||||||||||||||||||
| assert len(branches) == 2 | ||||||||||||||||||||||||||||
| branch_nodes = [(branch.from_node.item(), branch.to_node.item()) for branch in branches] | ||||||||||||||||||||||||||||
| assert branch_nodes == [(101, 102), (102, 106)] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_iter_branches_in_shortest_path_three_winding_transformer(self, grid_with_3wt): | ||||||||||||||||||||||||||||
| branches = list(grid_with_3wt.iter_branches_in_shortest_path(101, 104)) | ||||||||||||||||||||||||||||
| assert len(branches) == 2 | ||||||||||||||||||||||||||||
| branch_nodes = [(branch.from_node.item(), branch.to_node.item()) for branch in branches] | ||||||||||||||||||||||||||||
| assert branch_nodes == [(101, 102), (102, 104)] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_iter_branches_same_node_returns_empty(self, basic_grid): | ||||||||||||||||||||||||||||
| assert [] == list(basic_grid.iter_branches_in_shortest_path(101, 101)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_iter_branches_in_shortest_path_typed(self, basic_grid): | ||||||||||||||||||||||||||||
| branches = list(basic_grid.iter_branches_in_shortest_path(101, 106, typed=True)) | ||||||||||||||||||||||||||||
| assert len(branches) == 2 | ||||||||||||||||||||||||||||
| assert isinstance(branches[0], LineArray) | ||||||||||||||||||||||||||||
| assert isinstance(branches[1], TransformerArray) | ||||||||||||||||||||||||||||
| assert [branch.id.item() for branch in branches] == [201, 301] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_iter_branches_in_shortest_path_three_winding_transformer_typed(self, grid_with_3wt): | ||||||||||||||||||||||||||||
| branches = list(grid_with_3wt.iter_branches_in_shortest_path(101, 104, typed=True)) | ||||||||||||||||||||||||||||
| assert len(branches) == 2 | ||||||||||||||||||||||||||||
| assert isinstance(branches[0], ThreeWindingTransformerArray) | ||||||||||||||||||||||||||||
| assert isinstance(branches[1], LineArray) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+97
to
+98
|
||||||||||||||||||||||||||||
| def test_iter_branches_in_shortest_path_missing_branch_raises(self, basic_grid, monkeypatch): | |
| # Simulate an inconsistent state where the graph reports a path but | |
| # there is no active branch between consecutive nodes in that path. | |
| def fake_get_shortest_path(*args, **kwargs): | |
| # Return a path with a node pair that has no active branch | |
| return [101, 999], 1 | |
| monkeypatch.object(basic_grid.graphs.active_graph, "get_shortest_path", fake_get_shortest_path) | |
| with pytest.raises(Exception): | |
| list(basic_grid.iter_branches_in_shortest_path(101, 999)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer having both
_privatefunction below the public one.(This is also recommended in the book Clean Clode)