|
| 1 | +"""Pytest fixtures""" |
| 2 | +import pathlib |
| 3 | +from typing import Callable, Iterable |
| 4 | + |
| 5 | +import pytest |
| 6 | +from databento import Schema |
| 7 | + |
| 8 | +from tests import TESTS_ROOT |
| 9 | + |
| 10 | + |
| 11 | +def pytest_addoption(parser: pytest.Parser) -> None: |
| 12 | + """ |
| 13 | + Function to customize pytest cli options. |
| 14 | + This should not be invoked directly. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + parser : pytest.Parser |
| 19 | + The pytest argument parser. |
| 20 | +
|
| 21 | + See Also |
| 22 | + -------- |
| 23 | + pytest.addoption |
| 24 | +
|
| 25 | + """ |
| 26 | + # Add a --release flag |
| 27 | + parser.addoption( |
| 28 | + "--release", |
| 29 | + action="store_true", |
| 30 | + help="indicates release tests should be run", |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def pytest_configure(config: pytest.Config) -> None: |
| 35 | + """ |
| 36 | + Function to configure pytest. |
| 37 | + This should not be invoked directly. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + config : pytest.Config |
| 42 | + The pytest configuration. |
| 43 | +
|
| 44 | + """ |
| 45 | + # Add custom mark for `release` |
| 46 | + config.addinivalue_line( |
| 47 | + "markers", |
| 48 | + "release: mark tests as release tests (run with --release)", |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def pytest_collection_modifyitems( |
| 53 | + config: pytest.Config, |
| 54 | + items: Iterable[pytest.Item], |
| 55 | +) -> None: |
| 56 | + """ |
| 57 | + Function to customize test items. |
| 58 | + This should not be invoked directly. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + config : pytest.Config |
| 63 | + The pytest configuration. |
| 64 | + items : Iterable[pytest.Item] |
| 65 | + The pytest test item. |
| 66 | +
|
| 67 | + """ |
| 68 | + skip_release = pytest.mark.skip( |
| 69 | + reason="skipping release test (invoke pytest with --release to execute)", |
| 70 | + ) |
| 71 | + |
| 72 | + for item in items: |
| 73 | + # Skip release tests if `--release` was not specified |
| 74 | + if "release" in item.keywords and not config.getoption("--release"): |
| 75 | + item.add_marker(skip_release) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture(name="test_data_path") |
| 79 | +def fixture_test_data_path() -> Callable[[Schema], pathlib.Path]: |
| 80 | + """ |
| 81 | + Factory fixture for retrieving stub data paths. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + schema : Schema |
| 86 | + The schema of the stub data path to request. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + Callable |
| 91 | +
|
| 92 | + See Also |
| 93 | + -------- |
| 94 | + test_data |
| 95 | +
|
| 96 | + """ |
| 97 | + |
| 98 | + def func(schema: Schema) -> pathlib.Path: |
| 99 | + return pathlib.Path(TESTS_ROOT) / "data" / f"test_data.{schema}.dbn.zst" |
| 100 | + |
| 101 | + return func |
| 102 | + |
| 103 | + |
| 104 | +@pytest.fixture(name="test_data") |
| 105 | +def fixture_test_data( |
| 106 | + test_data_path: Callable[[Schema], pathlib.Path], |
| 107 | +) -> Callable[[Schema], bytes]: |
| 108 | + """ |
| 109 | + Factory fixture for retrieving stub test data. |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + test_data_path : Callable |
| 114 | + The test_data_path fixture. |
| 115 | +
|
| 116 | + Returns |
| 117 | + ------- |
| 118 | + Callable |
| 119 | +
|
| 120 | + See Also |
| 121 | + -------- |
| 122 | + test_data_path |
| 123 | +
|
| 124 | + """ |
| 125 | + |
| 126 | + def func(schema: Schema) -> bytes: |
| 127 | + return test_data_path(schema).read_bytes() |
| 128 | + |
| 129 | + return func |
0 commit comments