|
| 1 | +# Copyright (C) 2026 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import shutil |
| 5 | +import zipfile |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | +import requests |
| 10 | + |
| 11 | +S3_URL = "https://storage.geti.intel.com/test-data/geti/datasets" |
| 12 | +OBJECT_NAME = "regression.zip" |
| 13 | +PARENT_DIR = Path(__file__).parent |
| 14 | +DATASETS_DIR = PARENT_DIR / "regression" |
| 15 | + |
| 16 | + |
| 17 | +def _download_regression_datasets(dest_dir: Path) -> None: |
| 18 | + """Download and unpack the regression dataset archive from public location.""" |
| 19 | + archive = dest_dir / OBJECT_NAME |
| 20 | + |
| 21 | + if not archive.exists(): |
| 22 | + response = requests.get(f"{S3_URL}/{OBJECT_NAME}", stream=True) |
| 23 | + with open(archive, "wb") as f: |
| 24 | + shutil.copyfileobj(response.raw, f) |
| 25 | + |
| 26 | + with zipfile.ZipFile(archive, "r") as zf: |
| 27 | + zf.extractall(dest_dir) |
| 28 | + |
| 29 | + |
| 30 | +def pytest_configure() -> None: |
| 31 | + """Session-wide hook - download datasets before collection begins.""" |
| 32 | + _download_regression_datasets(PARENT_DIR) |
| 33 | + |
| 34 | + |
| 35 | +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: |
| 36 | + """Parametrize after datasets are already downloaded.""" |
| 37 | + if "archive" in metafunc.fixturenames: |
| 38 | + zip_files = sorted(DATASETS_DIR.glob("*.zip")) |
| 39 | + if not zip_files: |
| 40 | + raise pytest.UsageError( |
| 41 | + f"No regression dataset archives were found in '{DATASETS_DIR}'. The dataset download/extraction may " |
| 42 | + f"have failed, or the archive structure may have changed." |
| 43 | + ) |
| 44 | + metafunc.parametrize("archive", zip_files, ids=[p.stem for p in zip_files]) |
| 45 | + |
| 46 | + |
| 47 | +def pytest_unconfigure() -> None: |
| 48 | + """Session-wide hook - remove downloaded archive and unpacked files after tests complete.""" |
| 49 | + archive = PARENT_DIR / OBJECT_NAME |
| 50 | + if archive.exists(): |
| 51 | + archive.unlink() |
| 52 | + if DATASETS_DIR.exists(): |
| 53 | + shutil.rmtree(DATASETS_DIR) |
0 commit comments