|
1 | 1 | """Test cases for the config manager module.""" |
2 | | -from unittest.mock import Mock |
3 | | -from unittest.mock import patch |
4 | | - |
5 | 2 | import pytest |
| 3 | +from _pytest.tmpdir import Path |
| 4 | +from pytest_mock import MockerFixture |
6 | 5 |
|
7 | 6 | from git_portfolio import config_manager as cm |
8 | 7 |
|
9 | 8 |
|
10 | | -@patch("os.path.join") |
11 | | -class TestConfigManager: |
12 | | - """ConfigManager test class.""" |
| 9 | +@pytest.fixture |
| 10 | +def mock_os_join_path(mocker: MockerFixture) -> MockerFixture: |
| 11 | + """Fixture for mocking os.path.join.""" |
| 12 | + return mocker.patch("os.path.join") |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def mock_yaml_dump(mocker: MockerFixture) -> MockerFixture: |
| 17 | + """Fixture for mocking yaml.dump.""" |
| 18 | + return mocker.patch("yaml.dump") |
13 | 19 |
|
14 | | - def test_init_invalid_config(self, os_join_path: Mock, tmp_path: Mock) -> None: |
15 | | - """It trucantes the file.""" |
16 | | - filename = "config1.yaml" |
17 | | - d = tmp_path |
18 | | - p = d / filename |
19 | | - p.write_text("in:valid") |
20 | | - os_join_path.side_effect = [str(d), str(p)] |
21 | | - cm.ConfigManager() |
22 | | - # os_truncate.assert_called_once_with(0) |
23 | 20 |
|
24 | | - def test_save_invalid_yaml(self, os_join_path: Mock, tmp_path: Mock) -> None: |
25 | | - """It trucantes the file.""" |
26 | | - filename = "config.yaml" |
27 | | - content = ( |
28 | | - "github_access_token: aaaaabbbbbccccc12345" |
29 | | - "github_hostname: ''" |
30 | | - "github_selected_repos:" |
31 | | - " - staticdev/test" |
32 | | - ) |
33 | | - d = tmp_path |
34 | | - p = d / filename |
35 | | - p.write_text(content) |
36 | | - os_join_path.side_effect = [str(d), str(p)] |
37 | | - cm.ConfigManager() |
38 | | - # os_truncate.assert_called_once_with(0) |
| 21 | +def test_init_invalid_config(tmp_path: Path, mock_os_join_path: MockerFixture) -> None: |
| 22 | + """It trucantes the file.""" |
| 23 | + filename = "config1.yaml" |
| 24 | + d = tmp_path |
| 25 | + p = d / filename |
| 26 | + p.write_text("in:valid") |
| 27 | + mock_os_join_path.side_effect = [str(d), str(p)] |
| 28 | + cm.ConfigManager() |
| 29 | + # os_truncate.assert_called_once_with(0) |
39 | 30 |
|
40 | | - def test_save_config_no_file(self, os_join_path: Mock, tmp_path: Mock) -> None: |
41 | | - """It raises AttributeError.""" |
42 | | - d = tmp_path |
43 | | - os_join_path.side_effect = [str(d), str(d / "config.yaml")] |
44 | | - manager = cm.ConfigManager() |
45 | | - with pytest.raises(AttributeError): |
46 | | - manager.save_config() |
47 | 31 |
|
48 | | - def test_save_config_empty_file(self, os_join_path: Mock, tmp_path: Mock) -> None: |
49 | | - """It raises AttributeError.""" |
50 | | - filename = "config2.yaml" |
51 | | - d = tmp_path |
52 | | - p = d / filename |
53 | | - p.write_text("") |
54 | | - os_join_path.side_effect = [str(d), str(p)] |
55 | | - manager = cm.ConfigManager(filename) |
56 | | - with pytest.raises(AttributeError): |
57 | | - manager.save_config() |
| 32 | +def test_save_invalid_yaml(tmp_path: Path, mock_os_join_path: MockerFixture) -> None: |
| 33 | + """It trucantes the file.""" |
| 34 | + filename = "config.yaml" |
| 35 | + content = ( |
| 36 | + "github_access_token: aaaaabbbbbccccc12345" |
| 37 | + "github_hostname: ''" |
| 38 | + "github_selected_repos:" |
| 39 | + " - staticdev/test" |
| 40 | + ) |
| 41 | + d = tmp_path |
| 42 | + p = d / filename |
| 43 | + p.write_text(content) |
| 44 | + mock_os_join_path.side_effect = [str(d), str(p)] |
| 45 | + cm.ConfigManager() |
| 46 | + # os_truncate.assert_called_once_with(0) |
58 | 47 |
|
59 | | - @patch("yaml.dump") |
60 | | - def test_save_config_success( |
61 | | - self, yaml_dump: Mock, os_join_path: Mock, tmp_path: Mock |
62 | | - ) -> None: |
63 | | - """It dumps yaml config file.""" |
64 | | - filename = "config.yaml" |
65 | | - content = ( |
66 | | - "github_access_token: aaaaabbbbbccccc12345\n" |
67 | | - "github_hostname: ''\n" |
68 | | - "github_selected_repos:\n" |
69 | | - " - staticdev/test\n" |
70 | | - ) |
71 | | - d = tmp_path |
72 | | - p = d / filename |
73 | | - p.write_text(content) |
74 | | - os_join_path.side_effect = [str(d), str(p)] |
75 | | - manager = cm.ConfigManager() |
| 48 | + |
| 49 | +def test_save_config_no_file(tmp_path: Path, mock_os_join_path: MockerFixture) -> None: |
| 50 | + """It raises AttributeError.""" |
| 51 | + d = tmp_path |
| 52 | + mock_os_join_path.side_effect = [str(d), str(d / "config.yaml")] |
| 53 | + manager = cm.ConfigManager() |
| 54 | + with pytest.raises(AttributeError): |
76 | 55 | manager.save_config() |
77 | | - yaml_dump.assert_called_once() |
| 56 | + |
| 57 | + |
| 58 | +def test_save_config_empty_file( |
| 59 | + tmp_path: Path, mock_os_join_path: MockerFixture |
| 60 | +) -> None: |
| 61 | + """It raises AttributeError.""" |
| 62 | + filename = "config2.yaml" |
| 63 | + d = tmp_path |
| 64 | + p = d / filename |
| 65 | + p.write_text("") |
| 66 | + mock_os_join_path.side_effect = [str(d), str(p)] |
| 67 | + manager = cm.ConfigManager(filename) |
| 68 | + with pytest.raises(AttributeError): |
| 69 | + manager.save_config() |
| 70 | + |
| 71 | + |
| 72 | +def test_save_config_success( |
| 73 | + tmp_path: Path, |
| 74 | + mock_yaml_dump: MockerFixture, |
| 75 | + mock_os_join_path: MockerFixture, |
| 76 | +) -> None: |
| 77 | + """It dumps yaml config file.""" |
| 78 | + filename = "config.yaml" |
| 79 | + content = ( |
| 80 | + "github_access_token: aaaaabbbbbccccc12345\n" |
| 81 | + "github_hostname: ''\n" |
| 82 | + "github_selected_repos:\n" |
| 83 | + " - staticdev/test\n" |
| 84 | + ) |
| 85 | + d = tmp_path |
| 86 | + p = d / filename |
| 87 | + p.write_text(content) |
| 88 | + mock_os_join_path.side_effect = [str(d), str(p)] |
| 89 | + manager = cm.ConfigManager() |
| 90 | + manager.save_config() |
| 91 | + mock_yaml_dump.assert_called_once() |
0 commit comments