1
+ import os
2
+ import tempfile
3
+ from openpyxl import Workbook
1
4
import pytest
2
5
import sys
3
6
6
9
from unittest .mock import patch , MagicMock
7
10
8
11
12
+ @pytest .fixture
13
+ def create_excel_file (tmp_path ):
14
+ def _create_excel_file (data ):
15
+ file_path = tmp_path / "test.xlsx"
16
+ wb = Workbook ()
17
+ for sheet_name , sheet_data in data .items ():
18
+ ws = wb .create_sheet (title = sheet_name )
19
+ for row in sheet_data :
20
+ ws .append (row )
21
+ wb .save (file_path )
22
+ return file_path
23
+
24
+ return _create_excel_file
25
+
26
+
27
+ # test cli.main
28
+ def test_main ():
29
+ # assert that main with no arguments raises SystemExit error
30
+ test_args = ["graphedexcel" ]
31
+ with patch ("sys.argv" , test_args ):
32
+ with pytest .raises (SystemExit ):
33
+ main ()
34
+
35
+
36
+ def test_main_with_nonexistent_file (capsys ):
37
+ test_args = ["graphedexcel" , "nonexistent_file.xlsx" ]
38
+ with patch ("sys.argv" , test_args ):
39
+ with pytest .raises (SystemExit ) as exc_info :
40
+ main ()
41
+ assert exc_info .value .code != 0
42
+ captured = capsys .readouterr ()
43
+ assert "File not found:" in captured .err
44
+
45
+
46
+ def test_main_with_test_xlsx_file (capsys ):
47
+ """Test main with a test excel file to exercise the cli module"""
48
+ with tempfile .NamedTemporaryFile (delete = False , suffix = ".xlsx" ) as tmp_file :
49
+ test_file_path = tmp_file .name
50
+ try :
51
+ wb = Workbook ()
52
+ ws = wb .active
53
+ ws ["A1" ] = "Test Data"
54
+
55
+ wb .save (test_file_path )
56
+ wb .close ()
57
+ test_args = ["graphedexcel" , test_file_path ]
58
+ with patch ("sys.argv" , test_args ):
59
+ main ()
60
+ finally :
61
+ print ("here" )
62
+ wb .close ()
63
+ captured = capsys .readouterr ()
64
+ assert "Dependency graph image saved" in captured .out
65
+
66
+
9
67
def test_parse_arguments_required (monkeypatch ):
10
68
"""
11
69
Test that the required positional argument is parsed correctly.
@@ -20,12 +78,19 @@ def test_parse_arguments_optional_flags():
20
78
"""
21
79
Test that optional flags are parsed correctly.
22
80
"""
23
- test_args = ["graphedexcel" , "test.xlsx" , "--as-directed-graph" , "--no-visualize" ]
81
+ test_args = [
82
+ "graphedexcel" ,
83
+ "test.xlsx" ,
84
+ "--as-directed-graph" ,
85
+ "--no-visualize" ,
86
+ "--hide-legends" ,
87
+ ]
24
88
with patch ("sys.argv" , test_args ):
25
89
args = parse_arguments ()
26
90
assert args .path_to_excel == "test.xlsx"
27
91
assert args .as_directed_graph is True
28
92
assert args .no_visualize is True
93
+ assert args .hide_legends is True
29
94
30
95
31
96
def test_parse_arguments_optional_arguments ():
@@ -65,6 +130,7 @@ def test_parse_arguments_default_values():
65
130
assert args .as_directed_graph is False
66
131
assert args .no_visualize is False
67
132
assert args .open_image is False
133
+ assert args .hide_legends is None
68
134
69
135
70
136
def test_parse_arguments_invalid ():
0 commit comments