|
| 1 | +"""The generator of the sync integration suite: what it leaves out and what it must keep. |
| 2 | +
|
| 3 | +CI does not compare the sync suite with the aio one, so a test the generator |
| 4 | +drops by mistake just stops running, with nothing to say so. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import TYPE_CHECKING |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from scripts import sync_tests_mirror |
| 14 | +from scripts.sync_tests_mirror import transform |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from pathlib import Path |
| 18 | + |
| 19 | +pytestmark = pytest.mark.unit |
| 20 | + |
| 21 | + |
| 22 | +def test__transform__a_skipped_class__keeps_the_heading_and_the_test_after_it() -> None: |
| 23 | + # Arrange |
| 24 | + source = """\ |
| 25 | +# sync-mirror: skip |
| 26 | +class _Writer: |
| 27 | + async def write(self) -> None: |
| 28 | + await self.conn.execute(self.sql) |
| 29 | +
|
| 30 | +
|
| 31 | +# ── partition_data ────────────────────────────────────────────────────────────── |
| 32 | +
|
| 33 | +
|
| 34 | +async def test__drains(db_engine: AsyncEngine) -> None: |
| 35 | + assert await count(db_engine) == 0 |
| 36 | +""" |
| 37 | + |
| 38 | + expected = """\ |
| 39 | +# ── partition_data ────────────────────────────────────────────────────────────── |
| 40 | +
|
| 41 | +
|
| 42 | +def test__drains(sync_db_engine: Engine) -> None: |
| 43 | + assert count(sync_db_engine) == 0 |
| 44 | +""" |
| 45 | + |
| 46 | + # Act |
| 47 | + mirrored = transform(source) |
| 48 | + |
| 49 | + # Assert |
| 50 | + assert mirrored == expected |
| 51 | + |
| 52 | + |
| 53 | +def test__transform__a_skipped_decorated_test__keeps_the_test_after_it() -> None: |
| 54 | + # Arrange |
| 55 | + source = """\ |
| 56 | +# sync-mirror: skip |
| 57 | +@pytest.mark.parametrize("n", [1, 2]) |
| 58 | +async def test__races(db_engine: AsyncEngine, n: int) -> None: |
| 59 | + await asyncio.gather(work(db_engine), work(db_engine)) |
| 60 | +
|
| 61 | +
|
| 62 | +async def test__plain(db_engine: AsyncEngine) -> None: |
| 63 | + assert await count(db_engine) == 0 |
| 64 | +""" |
| 65 | + |
| 66 | + expected = """\ |
| 67 | +def test__plain(sync_db_engine: Engine) -> None: |
| 68 | + assert count(sync_db_engine) == 0 |
| 69 | +""" |
| 70 | + |
| 71 | + # Act |
| 72 | + mirrored = transform(source) |
| 73 | + |
| 74 | + # Assert |
| 75 | + assert mirrored == expected |
| 76 | + |
| 77 | + |
| 78 | +def test__transform__a_section_whose_tests_are_all_skipped__loses_its_heading_and_comment() -> None: |
| 79 | + # Arrange |
| 80 | + source = """\ |
| 81 | +# ── under a live writer ───────────────────────────────────────────────────────── |
| 82 | +# |
| 83 | +# Why these tests need two coroutines at once. |
| 84 | +
|
| 85 | +
|
| 86 | +# sync-mirror: skip |
| 87 | +async def test__races(db_engine: AsyncEngine) -> None: |
| 88 | + await asyncio.gather(work(db_engine), work(db_engine)) |
| 89 | +
|
| 90 | +
|
| 91 | +# ── retry ─────────────────────────────────────────────────────────────────────── |
| 92 | +
|
| 93 | +
|
| 94 | +async def test__retries(db_engine: AsyncEngine) -> None: |
| 95 | + assert await count(db_engine) == 0 |
| 96 | +""" |
| 97 | + |
| 98 | + expected = """\ |
| 99 | +# ── retry ─────────────────────────────────────────────────────────────────────── |
| 100 | +
|
| 101 | +
|
| 102 | +def test__retries(sync_db_engine: Engine) -> None: |
| 103 | + assert count(sync_db_engine) == 0 |
| 104 | +""" |
| 105 | + |
| 106 | + # Act |
| 107 | + mirrored = transform(source) |
| 108 | + |
| 109 | + # Assert |
| 110 | + assert mirrored == expected |
| 111 | + |
| 112 | + |
| 113 | +def test__main__the_cli_module__gets_no_sync_twin(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 114 | + # Arrange |
| 115 | + aio, sync = tmp_path / "aio", tmp_path / "sync" |
| 116 | + aio.mkdir() |
| 117 | + (aio / "test_cli.py").write_text("async def test__cli() -> None:\n pass\n", encoding="utf-8") |
| 118 | + (aio / "test_plan.py").write_text("async def test__plan() -> None:\n pass\n", encoding="utf-8") |
| 119 | + monkeypatch.setattr(sync_tests_mirror, "ROOT", tmp_path) |
| 120 | + monkeypatch.setattr(sync_tests_mirror, "AIO", aio) |
| 121 | + monkeypatch.setattr(sync_tests_mirror, "SYNC", sync) |
| 122 | + |
| 123 | + # Act |
| 124 | + sync_tests_mirror.main() |
| 125 | + |
| 126 | + # Assert |
| 127 | + assert sorted(path.name for path in sync.iterdir()) == ["test_plan.py"] |
0 commit comments