|
| 1 | +from contextlib import suppress |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from app.tests import conftest |
| 8 | + |
| 9 | +# the fixture decorator hides the generator function; drive the raw one |
| 10 | +seed_baseline_fn = conftest.seed_baseline.__wrapped__ |
| 11 | + |
| 12 | + |
| 13 | +def _tmp_path_factory(root: Path) -> MagicMock: |
| 14 | + # fixture reads getbasetemp().parent, so hand back a child of root |
| 15 | + factory = MagicMock(spec=pytest.TempPathFactory) |
| 16 | + factory.getbasetemp.return_value = root / "popen-gw0" |
| 17 | + return factory |
| 18 | + |
| 19 | + |
| 20 | +class TestSeedBaselineMaster: |
| 21 | + def test_seeds_once_without_filelock(self, tmp_path: Path) -> None: |
| 22 | + with ( |
| 23 | + patch.object(conftest, "Session"), |
| 24 | + patch.object(conftest, "seed_database") as seed_database, |
| 25 | + patch.object(conftest, "FileLock") as file_lock, |
| 26 | + ): |
| 27 | + gen = seed_baseline_fn(_tmp_path_factory(tmp_path), "master") |
| 28 | + next(gen) |
| 29 | + with suppress(StopIteration): |
| 30 | + next(gen) |
| 31 | + |
| 32 | + assert seed_database.call_count == 1 |
| 33 | + file_lock.assert_not_called() |
| 34 | + |
| 35 | + |
| 36 | +class TestSeedBaselineXdistWorker: |
| 37 | + def test_seeds_and_touches_flag_when_absent(self, tmp_path: Path) -> None: |
| 38 | + with ( |
| 39 | + patch.object(conftest, "Session"), |
| 40 | + patch.object(conftest, "seed_database") as seed_database, |
| 41 | + patch.object(conftest, "FileLock") as file_lock, |
| 42 | + ): |
| 43 | + gen = seed_baseline_fn(_tmp_path_factory(tmp_path), "gw0") |
| 44 | + next(gen) |
| 45 | + with suppress(StopIteration): |
| 46 | + next(gen) |
| 47 | + |
| 48 | + assert seed_database.call_count == 1 |
| 49 | + assert (tmp_path / "seeded").exists() |
| 50 | + file_lock.assert_called_once_with(tmp_path / "seed.lock") |
| 51 | + |
| 52 | + def test_skips_seeding_when_flag_exists(self, tmp_path: Path) -> None: |
| 53 | + (tmp_path / "seeded").touch() |
| 54 | + |
| 55 | + with ( |
| 56 | + patch.object(conftest, "Session"), |
| 57 | + patch.object(conftest, "seed_database") as seed_database, |
| 58 | + patch.object(conftest, "FileLock") as file_lock, |
| 59 | + ): |
| 60 | + gen = seed_baseline_fn(_tmp_path_factory(tmp_path), "gw1") |
| 61 | + next(gen) |
| 62 | + with suppress(StopIteration): |
| 63 | + next(gen) |
| 64 | + |
| 65 | + seed_database.assert_not_called() |
| 66 | + file_lock.assert_called_once_with(tmp_path / "seed.lock") |
0 commit comments