|
| 1 | +fromtypingimportCallable |
| 2 | + |
| 3 | +importpytest |
| 4 | + |
| 5 | + |
| 6 | +defpytest_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 | +defpytest_collection_modifyitems(config:pytest.Config,items:list[pytest.Item])->None: |
| 17 | +"""Skip integration tests unless --run-integration is given.""" |
| 18 | +ifconfig.getoption("--run-integration"): |
| 19 | +return |
| 20 | + |
| 21 | +skip_marker=pytest.mark.skip(reason="integration tests require --run-integration") |
| 22 | +foriteminitems: |
| 23 | +if"integration"initem.keywordsor"requires_postgres"initem.keywords: |
| 24 | +item.add_marker(skip_marker) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(name="prom_result") |
| 28 | +deffixture_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":rowsor [], |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | +return_builder |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(name="series_sample") |
| 43 | +deffixture_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=labelsor {} |
| 52 | +values=valuesor [] |
| 53 | +return { |
| 54 | +"metric": {"__name__":metric_name,**labels}, |
| 55 | +"values": [[ts,str(val)]forts,valinvalues], |
| 56 | + } |
| 57 | + |
| 58 | +return_builder |