|
1 | 1 | import os
|
2 |
| -from unittest.mock import patch, mock_open |
| 2 | +from unittest.mock import patch |
3 | 3 | from code_mage.loadConfig import load_config # Adjust the import path accordingly
|
4 | 4 |
|
5 | 5 |
|
6 | 6 | class TestLoadConfig:
|
7 | 7 | @patch("os.path.exists")
|
8 | 8 | @patch("toml.load")
|
9 |
| - @patch("builtins.open", new_callable=mock_open) |
10 |
| - def test_load_config_file_exists(self, mock_file, mock_toml_load, mock_os_exists): |
11 |
| - # Simulate that the config file exists |
| 9 | + def test_load_config_file_exists(self, mock_toml_load, mock_os_exists): |
| 10 | + # Simulate that the file exists |
12 | 11 | mock_os_exists.return_value = True
|
13 | 12 |
|
14 |
| - # Simulate the contents of the config file |
15 |
| - mock_toml_load.side_effect = [ |
16 |
| - { |
17 |
| - "language": "Python", |
18 |
| - "OPENROUTER_API_KEY": "mock_openrouter_api_key", |
19 |
| - }, # First call (config file) |
20 |
| - {"tool": {"poetry": {"version": "0.9.0"}}}, # Second call (pyproject.toml) |
21 |
| - ] |
22 |
| - |
23 |
| - # Call load_config function |
24 |
| - config = load_config() |
25 |
| - |
26 |
| - # Assertions |
27 |
| - mock_os_exists.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml")) |
28 |
| - mock_file.assert_called_once_with("pyproject.toml", "r") |
29 |
| - assert config == { |
| 13 | + # Simulate the contents of the file |
| 14 | + mock_toml_load.return_value = { |
30 | 15 | "language": "Python",
|
31 | 16 | "OPENROUTER_API_KEY": "mock_openrouter_api_key",
|
32 |
| - "version": "0.9.0", |
33 | 17 | }
|
34 | 18 |
|
35 |
| - @patch("os.path.exists") |
36 |
| - @patch("toml.load") |
37 |
| - @patch("builtins.open", new_callable=mock_open) |
38 |
| - def test_load_config_file_with_no_content(self, mock_file, mock_toml_load, mock_os_exists): |
39 |
| - # Simulate that the file does not exist |
40 |
| - mock_os_exists.return_value = True |
41 |
| - |
42 |
| - # Call the function under test |
43 |
| - |
44 |
| - mock_toml_load.side_effect = [ |
45 |
| - {}, |
46 |
| - {"tool": {"poetry": {"version": "0.9.0"}}}, # Second call (pyproject.toml) |
47 |
| - ] |
48 |
| - |
| 19 | + # Call load_config function |
49 | 20 | config = load_config()
|
50 | 21 |
|
51 | 22 | # Assertions
|
52 | 23 | mock_os_exists.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml"))
|
53 |
| - mock_file.assert_called_once_with("pyproject.toml", "r") |
| 24 | + mock_toml_load.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml")) |
| 25 | + assert config == {"language": "Python", "OPENROUTER_API_KEY": "mock_openrouter_api_key"} |
54 | 26 |
|
55 |
| - # Assert the default config with version added |
56 |
| - assert config == {"version": "0.9.0"} |
57 |
| - |
58 |
| - @patch("os.path.exists") # Third argument |
59 |
| - @patch("builtins.open") # Second argument |
60 |
| - @patch("toml.load") # First argument |
61 |
| - def test_load_config_file_not_exists(self, mock_toml_load, mock_file, mock_os_exists): |
62 |
| - # Simulate that the config file does not exist |
| 27 | + @patch("os.path.exists") |
| 28 | + def test_load_config_file_not_exists(self, mock_os_exists): |
| 29 | + # Simulate that the file does not exist |
63 | 30 | mock_os_exists.return_value = False
|
64 | 31 |
|
65 |
| - # Simulate the content of the pyproject.toml file |
66 |
| - mock_toml_load.return_value = {"tool": {"poetry": {"version": "0.9.0"}}} |
67 |
| - |
68 | 32 | # Call the function under test
|
69 | 33 | config = load_config()
|
70 | 34 |
|
71 | 35 | # Assertions
|
72 | 36 | mock_os_exists.assert_called_once_with(os.path.expanduser("~/.codemage-config.toml"))
|
73 |
| - mock_file.assert_called_once_with("pyproject.toml", "r") |
74 |
| - assert config == {"version": "0.9.0"} |
| 37 | + assert config == {} |
0 commit comments