|
| 1 | +"""Shared fixtures for SDK integration tests. |
| 2 | +
|
| 3 | +Tests run against production. See www.hotdata.dev/api/README.md for the |
| 4 | +contract — env vars, naming conventions, blast-radius rules. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import os |
| 10 | +import uuid |
| 11 | +from dataclasses import dataclass |
| 12 | +from typing import Iterator |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from hotdata import ApiClient, Configuration |
| 17 | +from hotdata.api.connections_api import ConnectionsApi |
| 18 | +from hotdata.api.datasets_api import DatasetsApi |
| 19 | +from hotdata.api.indexes_api import IndexesApi |
| 20 | +from hotdata.api.saved_queries_api import SavedQueriesApi |
| 21 | +from hotdata.api.secrets_api import SecretsApi |
| 22 | +from hotdata.api.workspaces_api import WorkspacesApi |
| 23 | + |
| 24 | + |
| 25 | +REQUIRED_ENV = ("HOTDATA_SDK_TEST_API_KEY", "HOTDATA_SDK_TEST_WORKSPACE_ID") |
| 26 | +DEFAULT_API_URL = "https://api.hotdata.dev" |
| 27 | + |
| 28 | + |
| 29 | +@dataclass(frozen=True) |
| 30 | +class TestEnv: |
| 31 | + api_url: str |
| 32 | + api_key: str |
| 33 | + workspace_id: str |
| 34 | + connection_id: str | None |
| 35 | + |
| 36 | + |
| 37 | +def _load_env() -> TestEnv: |
| 38 | + missing = [name for name in REQUIRED_ENV if not os.environ.get(name)] |
| 39 | + if missing: |
| 40 | + pytest.skip( |
| 41 | + "SDK integration tests require env vars: " + ", ".join(missing) |
| 42 | + ) |
| 43 | + # GitHub Actions sets `env:` keys even when the underlying secret/var is |
| 44 | + # unset, producing empty strings rather than absent keys. Use `or` to fall |
| 45 | + # back to the default for url and to None for the optional connection id. |
| 46 | + return TestEnv( |
| 47 | + api_url=os.environ.get("HOTDATA_SDK_TEST_API_URL") or DEFAULT_API_URL, |
| 48 | + api_key=os.environ["HOTDATA_SDK_TEST_API_KEY"], |
| 49 | + workspace_id=os.environ["HOTDATA_SDK_TEST_WORKSPACE_ID"], |
| 50 | + connection_id=os.environ.get("HOTDATA_SDK_TEST_CONNECTION_ID") or None, |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture(scope="session") |
| 55 | +def env() -> TestEnv: |
| 56 | + return _load_env() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture(scope="session") |
| 60 | +def api_client(env: TestEnv) -> Iterator[ApiClient]: |
| 61 | + config = Configuration( |
| 62 | + host=env.api_url, |
| 63 | + api_key=env.api_key, |
| 64 | + workspace_id=env.workspace_id, |
| 65 | + ) |
| 66 | + with ApiClient(config) as client: |
| 67 | + yield client |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture(scope="session") |
| 71 | +def workspace_id(env: TestEnv) -> str: |
| 72 | + return env.workspace_id |
| 73 | + |
| 74 | + |
| 75 | +@pytest.fixture(scope="session") |
| 76 | +def connection_id(env: TestEnv) -> str: |
| 77 | + if not env.connection_id: |
| 78 | + pytest.skip("HOTDATA_SDK_TEST_CONNECTION_ID required for this scenario") |
| 79 | + return env.connection_id |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def sdkci_name() -> "callable[[str], str]": |
| 84 | + """Returns `sdkci-<scenario>-<uuid8>` so orphans are identifiable. |
| 85 | +
|
| 86 | + See api/README.md — every test-created resource must use this prefix. |
| 87 | + """ |
| 88 | + |
| 89 | + def _make(scenario: str) -> str: |
| 90 | + return f"sdkci-{scenario}-{uuid.uuid4().hex[:8]}" |
| 91 | + |
| 92 | + return _make |
| 93 | + |
| 94 | + |
| 95 | +# Per-API client fixtures keep tests one-liner short and avoid every test |
| 96 | +# instantiating its own *Api(api_client). |
| 97 | +@pytest.fixture |
| 98 | +def datasets_api(api_client: ApiClient) -> DatasetsApi: |
| 99 | + return DatasetsApi(api_client) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def workspaces_api(api_client: ApiClient) -> WorkspacesApi: |
| 104 | + return WorkspacesApi(api_client) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.fixture |
| 108 | +def connections_api(api_client: ApiClient) -> ConnectionsApi: |
| 109 | + return ConnectionsApi(api_client) |
| 110 | + |
| 111 | + |
| 112 | +@pytest.fixture |
| 113 | +def indexes_api(api_client: ApiClient) -> IndexesApi: |
| 114 | + return IndexesApi(api_client) |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +def saved_queries_api(api_client: ApiClient) -> SavedQueriesApi: |
| 119 | + return SavedQueriesApi(api_client) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.fixture |
| 123 | +def secrets_api(api_client: ApiClient) -> SecretsApi: |
| 124 | + return SecretsApi(api_client) |
0 commit comments