Coverage for tests / unit / views / test_csv.py: 100%

31 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 mock_open, patch 

3 

4from pytest import fixture 

5 

6from graphable.graph import Graph 

7from graphable.graphable import Graphable 

8from graphable.views.csv import create_topology_csv, export_topology_csv 

9 

10 

11class TestCSV: 

12 @fixture 

13 def graph_fixture(self): 

14 a = Graphable("A") 

15 b = Graphable("B") 

16 g = Graph() 

17 g.add_edge(a, b) 

18 return g, a, b 

19 

20 def test_create_topology_csv_default(self, graph_fixture): 

21 g, a, b = graph_fixture 

22 csv_str = create_topology_csv(g) 

23 

24 assert "source,target" in csv_str 

25 assert "A,B" in csv_str 

26 

27 def test_create_topology_csv_no_header(self, graph_fixture): 

28 g, a, b = graph_fixture 

29 csv_str = create_topology_csv(g, include_header=False) 

30 

31 assert "source,target" not in csv_str 

32 assert "A,B" in csv_str 

33 

34 def test_export_topology_csv(self, graph_fixture): 

35 g, _, _ = graph_fixture 

36 output_path = Path("output.csv") 

37 

38 with patch("builtins.open", mock_open()) as mock_file: 

39 export_topology_csv(g, output_path) 

40 

41 mock_file.assert_called_with(output_path, "w+", newline="") 

42 mock_file().write.assert_called()