|
| 1 | +from typing import Callable |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +def pytest_addoption(parser: pytest.Parser) -> None: |
| 7 | + """Add a flag for enabling integration tests that require services.""" |
| 8 | + parser.addoption( |
| 9 | + "--run-integration", |
| 10 | + action="store_true", |
| 11 | + default=False, |
| 12 | + help="Run tests marked as integration/requires_postgres.", |
| 13 | + ) |
| 14 | + |
| 15 | + |
| 16 | +def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: |
| 17 | + """Skip integration tests unless --run-integration is given.""" |
| 18 | + if config.getoption("--run-integration"): |
| 19 | + return |
| 20 | + |
| 21 | + skip_marker = pytest.mark.skip(reason="integration tests require --run-integration") |
| 22 | + for item in items: |
| 23 | + if "integration" in item.keywords or "requires_postgres" in item.keywords: |
| 24 | + item.add_marker(skip_marker) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(name="prom_result") |
| 28 | +def fixture_prom_result() -> Callable[[list[dict] | None, str], dict]: |
| 29 | + """Build a Prometheus-like payload for the happy-path tests.""" |
| 30 | + |
| 31 | + def _builder(rows: list[dict] | None = None, status: str = "success") -> dict: |
| 32 | + return { |
| 33 | + "status": status, |
| 34 | + "data": { |
| 35 | + "result": rows or [], |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + return _builder |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(name="series_sample") |
| 43 | +def fixture_series_sample() -> Callable[[str, dict | None, list[tuple[float | int, float | int | str]] | None], dict]: |
| 44 | + """Create metric entries (metric metadata + values array) for query_range tests.""" |
| 45 | + |
| 46 | + def _builder( |
| 47 | + metric_name: str, |
| 48 | + labels: dict | None = None, |
| 49 | + values: list[tuple[float | int, float | int | str]] | None = None, |
| 50 | + ) -> dict: |
| 51 | + labels = labels or {} |
| 52 | + values = values or [] |
| 53 | + return { |
| 54 | + "metric": {"__name__": metric_name, **labels}, |
| 55 | + "values": [[ts, str(val)] for ts, val in values], |
| 56 | + } |
| 57 | + |
| 58 | + return _builder |
0 commit comments