|
23 | 23 | # License: BSD 3-Clause |
24 | 24 | from __future__ import annotations |
25 | 25 |
|
| 26 | +from collections.abc import Iterator |
26 | 27 | import logging |
27 | 28 | import os |
28 | 29 | import shutil |
@@ -195,55 +196,90 @@ def pytest_addoption(parser): |
195 | 196 | def _expected_static_cache_state(root_dir: Path) -> list[Path]: |
196 | 197 | _c_root_dir = root_dir / "org" / "openml" / "test" |
197 | 198 | res_paths = [root_dir, _c_root_dir] |
198 | | - |
| 199 | + |
199 | 200 | for _d in ["datasets", "tasks", "runs", "setups"]: |
200 | 201 | res_paths.append(_c_root_dir / _d) |
201 | 202 |
|
202 | | - for _id in ["-1","2"]: |
| 203 | + for _id in ["-1", "2"]: |
203 | 204 | tmp_p = _c_root_dir / "datasets" / _id |
204 | | - res_paths.extend([ |
205 | | - tmp_p / "dataset.arff", |
206 | | - tmp_p / "features.xml", |
207 | | - tmp_p / "qualities.xml", |
208 | | - tmp_p / "description.xml", |
209 | | - ]) |
| 205 | + res_paths.extend( |
| 206 | + [ |
| 207 | + tmp_p / "dataset.arff", |
| 208 | + tmp_p / "features.xml", |
| 209 | + tmp_p / "qualities.xml", |
| 210 | + tmp_p / "description.xml", |
| 211 | + ] |
| 212 | + ) |
210 | 213 |
|
211 | 214 | res_paths.append(_c_root_dir / "datasets" / "30" / "dataset_30.pq") |
212 | 215 | res_paths.append(_c_root_dir / "runs" / "1" / "description.xml") |
213 | 216 | res_paths.append(_c_root_dir / "setups" / "1" / "description.xml") |
214 | | - |
| 217 | + |
215 | 218 | for _id in ["1", "3", "1882"]: |
216 | 219 | tmp_p = _c_root_dir / "tasks" / _id |
217 | | - res_paths.extend([ |
218 | | - tmp_p / "datasplits.arff", |
219 | | - tmp_p / "task.xml", |
220 | | - ]) |
221 | | - |
| 220 | + res_paths.extend( |
| 221 | + [ |
| 222 | + tmp_p / "datasplits.arff", |
| 223 | + tmp_p / "task.xml", |
| 224 | + ] |
| 225 | + ) |
| 226 | + |
222 | 227 | return res_paths |
223 | 228 |
|
224 | 229 |
|
225 | 230 | def assert_static_test_cache_correct(root_dir: Path) -> None: |
226 | 231 | for p in _expected_static_cache_state(root_dir): |
227 | | - assert p.exists(), f"Expected path {p} does not exist" |
228 | | - |
| 232 | + assert p.exists(), f"Expected path {p} exists" |
| 233 | + |
229 | 234 |
|
230 | 235 | @pytest.fixture(scope="class") |
231 | 236 | def long_version(request): |
232 | 237 | request.cls.long_version = request.config.getoption("--long") |
233 | 238 |
|
234 | 239 |
|
235 | | -@pytest.fixture() |
| 240 | +@pytest.fixture(scope="session") |
236 | 241 | def test_files_directory() -> Path: |
237 | 242 | return Path(__file__).parent / "files" |
238 | 243 |
|
239 | 244 |
|
240 | | -@pytest.fixture() |
| 245 | +@pytest.fixture(scope="session") |
241 | 246 | def test_api_key() -> str: |
242 | 247 | return "c0c42819af31e706efe1f4b88c23c6c1" |
243 | 248 |
|
244 | 249 |
|
245 | | -@pytest.fixture(autouse=True) |
246 | | -def verify_cache_state(test_files_directory) -> None: |
| 250 | +@pytest.fixture(autouse=True, scope="function") |
| 251 | +def verify_cache_state(test_files_directory) -> Iterator[None]: |
247 | 252 | assert_static_test_cache_correct(test_files_directory) |
248 | 253 | yield |
249 | 254 | assert_static_test_cache_correct(test_files_directory) |
| 255 | + |
| 256 | + |
| 257 | +@pytest.fixture(autouse=True, scope="session") |
| 258 | +def as_robot() -> Iterator[None]: |
| 259 | + policy = openml.config.retry_policy |
| 260 | + n_retries = openml.config.connection_n_retries |
| 261 | + openml.config.set_retry_policy("robot", n_retries=20) |
| 262 | + yield |
| 263 | + openml.config.set_retry_policy(policy, n_retries) |
| 264 | + |
| 265 | + |
| 266 | +@pytest.fixture(autouse=True, scope="session") |
| 267 | +def with_test_server(): |
| 268 | + openml.config.start_using_configuration_for_example() |
| 269 | + yield |
| 270 | + openml.config.stop_using_configuration_for_example() |
| 271 | + |
| 272 | + |
| 273 | +@pytest.fixture(autouse=True) |
| 274 | +def with_test_cache(test_files_directory, request): |
| 275 | + if not test_files_directory.exists(): |
| 276 | + raise ValueError( |
| 277 | + f"Cannot find test cache dir, expected it to be {test_files_directory!s}!", |
| 278 | + ) |
| 279 | + _root_cache_directory = openml.config._root_cache_directory |
| 280 | + tmp_cache = test_files_directory / request.node.name |
| 281 | + openml.config.set_root_cache_directory(tmp_cache) |
| 282 | + yield |
| 283 | + openml.config.set_root_cache_directory(_root_cache_directory) |
| 284 | + if tmp_cache.exists(): |
| 285 | + shutil.rmtree(tmp_cache) |
0 commit comments