|
3 | 3 | from typing import TYPE_CHECKING |
4 | 4 |
|
5 | 5 | if TYPE_CHECKING: |
| 6 | + from collections.abc import Callable |
6 | 7 | from pathlib import Path |
7 | 8 |
|
8 | 9 | from pytest_mock import MockerFixture |
9 | 10 |
|
10 | | -from pipelex.system.configuration.config_check import check_is_initialized |
| 11 | +from pipelex.system.configuration.config_check import CONFIG_NAME, PLXT_CONFIG_NAME, check_is_initialized |
| 12 | + |
| 13 | + |
| 14 | +def _make_resolve_config_file(config_dir: Path) -> Callable[[str], Path]: |
| 15 | + def resolve_config_file(name: str) -> Path: |
| 16 | + return config_dir / name |
| 17 | + |
| 18 | + return resolve_config_file |
11 | 19 |
|
12 | 20 |
|
13 | 21 | class TestPipelexCheckInitialization: |
14 | 22 | """Test the check_is_initialized function from config_check module.""" |
15 | 23 |
|
16 | | - def _mock_config_manager_paths(self, mocker: MockerFixture, backends_file: str, routing_file: str) -> None: |
| 24 | + def _setup_config_dir(self, tmp_path: Path, *, config_files: bool = False) -> Path: |
| 25 | + """Create the config directory structure and optionally create config files.""" |
| 26 | + config_dir = tmp_path / ".pipelex" |
| 27 | + inference_dir = config_dir / "inference" |
| 28 | + inference_dir.mkdir(parents=True) |
| 29 | + if config_files: |
| 30 | + (config_dir / CONFIG_NAME).write_text("[pipelex]\n") |
| 31 | + (config_dir / PLXT_CONFIG_NAME).write_text("[plxt]\n") |
| 32 | + return config_dir |
| 33 | + |
| 34 | + def _mock_config_manager_paths(self, mocker: MockerFixture, config_dir: Path, backends_file: str, routing_file: str) -> None: |
17 | 35 | """Mock config_manager properties used by config_check.""" |
18 | 36 | mock_manager = mocker.MagicMock() |
19 | 37 | mock_manager.backends_file_path = backends_file |
20 | 38 | mock_manager.routing_profiles_file_path = routing_file |
| 39 | + mock_manager.resolve_config_file = _make_resolve_config_file(config_dir) |
21 | 40 | mocker.patch("pipelex.system.configuration.config_check.config_manager", mock_manager) |
22 | 41 |
|
23 | 42 | def test_check_is_initialized_returns_true_when_all_files_exist(self, tmp_path: Path, mocker: MockerFixture) -> None: |
24 | 43 | """Test that check_is_initialized returns True when all required files exist.""" |
25 | | - # Setup test directories |
26 | | - config_dir = tmp_path / ".pipelex" / "inference" |
27 | | - config_dir.mkdir(parents=True) |
28 | | - backends_file = config_dir / "backends.toml" |
29 | | - routing_file = config_dir / "routing_profiles.toml" |
| 44 | + config_dir = self._setup_config_dir(tmp_path, config_files=True) |
| 45 | + inference_dir = config_dir / "inference" |
| 46 | + backends_file = inference_dir / "backends.toml" |
| 47 | + routing_file = inference_dir / "routing_profiles.toml" |
30 | 48 | backends_file.write_text("[backends]\nconfig = 'value'") |
31 | 49 | routing_file.write_text("[routing]\nconfig = 'value'") |
32 | 50 |
|
33 | | - # Mock config_manager to point to temp directory |
34 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
| 51 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
35 | 52 |
|
36 | | - # Test |
37 | 53 | result = check_is_initialized() |
38 | 54 |
|
39 | | - # Verify |
40 | 55 | assert result is True |
41 | 56 |
|
42 | 57 | def test_check_is_initialized_returns_false_when_backends_missing(self, tmp_path: Path, mocker: MockerFixture) -> None: |
43 | 58 | """Test that check_is_initialized returns False when backends.toml is missing.""" |
44 | | - # Setup test directories - only routing file exists |
45 | | - config_dir = tmp_path / ".pipelex" / "inference" |
46 | | - config_dir.mkdir(parents=True) |
47 | | - backends_file = config_dir / "backends.toml" |
48 | | - routing_file = config_dir / "routing_profiles.toml" |
| 59 | + config_dir = self._setup_config_dir(tmp_path, config_files=True) |
| 60 | + inference_dir = config_dir / "inference" |
| 61 | + backends_file = inference_dir / "backends.toml" |
| 62 | + routing_file = inference_dir / "routing_profiles.toml" |
49 | 63 | routing_file.write_text("[routing]\nconfig = 'value'") |
50 | 64 |
|
51 | | - # Mock config_manager to point to temp directory |
52 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
| 65 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
53 | 66 |
|
54 | | - # Test |
55 | 67 | result = check_is_initialized(print_warning_if_not=False) |
56 | 68 |
|
57 | | - # Verify |
58 | 69 | assert result is False |
59 | 70 |
|
60 | 71 | def test_check_is_initialized_returns_false_when_routing_missing(self, tmp_path: Path, mocker: MockerFixture) -> None: |
61 | 72 | """Test that check_is_initialized returns False when routing_profiles.toml is missing.""" |
62 | | - # Setup test directories - only backends file exists |
63 | | - config_dir = tmp_path / ".pipelex" / "inference" |
64 | | - config_dir.mkdir(parents=True) |
65 | | - backends_file = config_dir / "backends.toml" |
66 | | - routing_file = config_dir / "routing_profiles.toml" |
| 73 | + config_dir = self._setup_config_dir(tmp_path, config_files=True) |
| 74 | + inference_dir = config_dir / "inference" |
| 75 | + backends_file = inference_dir / "backends.toml" |
| 76 | + routing_file = inference_dir / "routing_profiles.toml" |
67 | 77 | backends_file.write_text("[backends]\nconfig = 'value'") |
68 | 78 |
|
69 | | - # Mock config_manager to point to temp directory |
70 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
| 79 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
71 | 80 |
|
72 | | - # Test |
73 | 81 | result = check_is_initialized(print_warning_if_not=False) |
74 | 82 |
|
75 | | - # Verify |
76 | 83 | assert result is False |
77 | 84 |
|
78 | 85 | def test_check_is_initialized_returns_false_when_all_files_missing(self, tmp_path: Path, mocker: MockerFixture) -> None: |
79 | | - """Test that check_is_initialized returns False when both required files are missing.""" |
80 | | - # Setup test directories - no files exist |
81 | | - config_dir = tmp_path / ".pipelex" / "inference" |
82 | | - config_dir.mkdir(parents=True) |
83 | | - backends_file = config_dir / "backends.toml" |
84 | | - routing_file = config_dir / "routing_profiles.toml" |
| 86 | + """Test that check_is_initialized returns False when all required files are missing.""" |
| 87 | + config_dir = self._setup_config_dir(tmp_path, config_files=False) |
| 88 | + inference_dir = config_dir / "inference" |
| 89 | + backends_file = inference_dir / "backends.toml" |
| 90 | + routing_file = inference_dir / "routing_profiles.toml" |
85 | 91 |
|
86 | | - # Mock config_manager to point to temp directory |
87 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
| 92 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
88 | 93 |
|
89 | | - # Test |
90 | 94 | result = check_is_initialized(print_warning_if_not=False) |
91 | 95 |
|
92 | | - # Verify |
93 | 96 | assert result is False |
94 | 97 |
|
95 | 98 | def test_check_is_initialized_prints_warning_when_not_initialized(self, tmp_path: Path, mocker: MockerFixture) -> None: |
96 | 99 | """Test that check_is_initialized prints warning and returns False when not initialized and print_warning_if_not is True.""" |
97 | | - # Setup test directories - no files exist |
98 | | - config_dir = tmp_path / ".pipelex" / "inference" |
99 | | - config_dir.mkdir(parents=True) |
100 | | - backends_file = config_dir / "backends.toml" |
101 | | - routing_file = config_dir / "routing_profiles.toml" |
| 100 | + config_dir = self._setup_config_dir(tmp_path, config_files=False) |
| 101 | + inference_dir = config_dir / "inference" |
| 102 | + backends_file = inference_dir / "backends.toml" |
| 103 | + routing_file = inference_dir / "routing_profiles.toml" |
102 | 104 |
|
103 | | - # Mock config_manager to point to temp directory |
104 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
| 105 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
105 | 106 |
|
106 | | - # Mock console.print to suppress output during test |
107 | 107 | mock_console = mocker.MagicMock() |
108 | 108 | mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mock_console) |
109 | 109 |
|
110 | | - # Test - should print warning and return False |
111 | 110 | result = check_is_initialized(print_warning_if_not=True) |
112 | 111 |
|
113 | | - # Verify |
114 | 112 | assert result is False |
115 | 113 | assert mock_console.print.called |
116 | 114 |
|
117 | 115 | def test_check_is_initialized_returns_true_when_initialized_with_print_warning(self, tmp_path: Path, mocker: MockerFixture) -> None: |
118 | 116 | """Test that check_is_initialized returns True when initialized with print_warning_if_not=True.""" |
119 | | - # Setup test directories - all files exist |
120 | | - config_dir = tmp_path / ".pipelex" / "inference" |
121 | | - config_dir.mkdir(parents=True) |
122 | | - backends_file = config_dir / "backends.toml" |
123 | | - routing_file = config_dir / "routing_profiles.toml" |
| 117 | + config_dir = self._setup_config_dir(tmp_path, config_files=True) |
| 118 | + inference_dir = config_dir / "inference" |
| 119 | + backends_file = inference_dir / "backends.toml" |
| 120 | + routing_file = inference_dir / "routing_profiles.toml" |
124 | 121 | backends_file.write_text("[backends]\nconfig = 'value'") |
125 | 122 | routing_file.write_text("[routing]\nconfig = 'value'") |
126 | 123 |
|
127 | | - # Mock config_manager to point to temp directory |
128 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
| 124 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
129 | 125 |
|
130 | | - # Test |
131 | 126 | result = check_is_initialized(print_warning_if_not=True) |
132 | 127 |
|
133 | | - # Verify |
134 | 128 | assert result is True |
135 | 129 |
|
136 | 130 | def test_check_is_initialized_returns_false_with_only_backends_missing(self, tmp_path: Path, mocker: MockerFixture) -> None: |
137 | 131 | """Test that check_is_initialized returns False when only backends file is missing.""" |
138 | | - # Setup test directories - only routing file exists |
139 | | - config_dir = tmp_path / ".pipelex" / "inference" |
140 | | - config_dir.mkdir(parents=True) |
141 | | - backends_file = config_dir / "backends.toml" |
142 | | - routing_file = config_dir / "routing_profiles.toml" |
| 132 | + config_dir = self._setup_config_dir(tmp_path, config_files=True) |
| 133 | + inference_dir = config_dir / "inference" |
| 134 | + backends_file = inference_dir / "backends.toml" |
| 135 | + routing_file = inference_dir / "routing_profiles.toml" |
143 | 136 | routing_file.write_text("[routing]\nconfig = 'value'") |
144 | 137 |
|
145 | | - # Mock config_manager to point to temp directory |
146 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
147 | | - |
148 | | - # Mock console.print to suppress output during test |
| 138 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
149 | 139 | mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mocker.MagicMock()) |
150 | 140 |
|
151 | | - # Test |
152 | 141 | result = check_is_initialized(print_warning_if_not=True) |
153 | 142 |
|
154 | | - # Verify |
155 | 143 | assert result is False |
156 | 144 |
|
157 | 145 | def test_check_is_initialized_returns_false_with_only_routing_missing(self, tmp_path: Path, mocker: MockerFixture) -> None: |
158 | 146 | """Test that check_is_initialized returns False when only routing file is missing.""" |
159 | | - # Setup test directories - only backends file exists |
160 | | - config_dir = tmp_path / ".pipelex" / "inference" |
161 | | - config_dir.mkdir(parents=True) |
162 | | - backends_file = config_dir / "backends.toml" |
163 | | - routing_file = config_dir / "routing_profiles.toml" |
| 147 | + config_dir = self._setup_config_dir(tmp_path, config_files=True) |
| 148 | + inference_dir = config_dir / "inference" |
| 149 | + backends_file = inference_dir / "backends.toml" |
| 150 | + routing_file = inference_dir / "routing_profiles.toml" |
164 | 151 | backends_file.write_text("[backends]\nconfig = 'value'") |
165 | 152 |
|
166 | | - # Mock config_manager to point to temp directory |
167 | | - self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file)) |
168 | | - |
169 | | - # Mock console.print to suppress output during test |
| 153 | + self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file)) |
170 | 154 | mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mocker.MagicMock()) |
171 | 155 |
|
172 | | - # Test |
173 | 156 | result = check_is_initialized(print_warning_if_not=True) |
174 | 157 |
|
175 | | - # Verify |
176 | 158 | assert result is False |
0 commit comments