Coverage for tests / unit / cli / test_enhancements.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-16 21:32 +0000

1from pathlib import Path 

2from unittest.mock import MagicMock, patch 

3 

4from graphable.cli.commands.core import info_command, load_graph, paths_command 

5from graphable.graph import Graph 

6from graphable.graphable import Graphable 

7 

8 

9def test_load_graph_slicing(): 

10 a = Graphable("A") 

11 b = Graphable("B") 

12 c = Graphable("C") 

13 g = Graph() 

14 g.add_edge(a, b) 

15 g.add_edge(b, c) 

16 

17 with patch("graphable.cli.commands.core.get_parser") as mock_get_parser: 

18 mock_parser = MagicMock(return_value=g) 

19 mock_get_parser.return_value = mock_parser 

20 

21 # Upstream of B should be {A, B} 

22 g_up = load_graph(Path("test.json"), upstream_of="B") 

23 assert len(g_up) == 2 

24 assert "A" in g_up 

25 assert "B" in g_up 

26 assert "C" not in g_up 

27 

28 # Downstream of B should be {B, C} 

29 g_down = load_graph(Path("test.json"), downstream_of="B") 

30 assert len(g_down) == 2 

31 assert "B" in g_down 

32 assert "C" in g_down 

33 assert "A" not in g_down 

34 

35 

36def test_paths_command(tmp_path): 

37 graph_file = tmp_path / "test.json" 

38 graph_file.write_text( 

39 '{"nodes": [{"id": "A"}, {"id": "B"}, {"id": "C"}], "edges": [{"source": "A", "target": "B"}, {"source": "B", "target": "C"}]}' 

40 ) 

41 

42 paths = paths_command(graph_file, "A", "C") 

43 assert paths == [["A", "B", "C"]] 

44 

45 

46def test_info_command_slicing(tmp_path): 

47 graph_file = tmp_path / "test.json" 

48 graph_file.write_text( 

49 '{"nodes": [{"id": "A"}, {"id": "B"}], "edges": [{"source": "A", "target": "B"}]}' 

50 ) 

51 

52 # Full graph 

53 data = info_command(graph_file) 

54 assert data["nodes"] == 2 

55 

56 # Sliced graph (upstream of A is just A) 

57 data_sliced = info_command(graph_file, upstream_of="A") 

58 assert data_sliced["nodes"] == 1 

59 assert data_sliced["sources"] == ["A"]