|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import os |
| 6 | +import uuid |
| 7 | + |
5 | 8 | import grpc |
6 | 9 | import grpc.aio |
| 10 | +import pytest |
| 11 | + |
| 12 | +from opendecree._generated.centralconfig.v1 import ( |
| 13 | + schema_service_pb2, |
| 14 | + schema_service_pb2_grpc, |
| 15 | + types_pb2, |
| 16 | +) |
| 17 | + |
| 18 | +# --------------------------------------------------------------------------- |
| 19 | +# Integration fixtures — skipped unless DECREE_TEST_ADDR is set |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | + |
| 22 | + |
| 23 | +def _integration_addr() -> str | None: |
| 24 | + return os.environ.get("DECREE_TEST_ADDR") |
| 25 | + |
| 26 | + |
| 27 | +def pytest_collection_modifyitems(items: list[pytest.Item]) -> None: |
| 28 | + """Auto-skip integration tests when DECREE_TEST_ADDR is not set.""" |
| 29 | + if _integration_addr(): |
| 30 | + return |
| 31 | + skip = pytest.mark.skip(reason="DECREE_TEST_ADDR not set") |
| 32 | + for item in items: |
| 33 | + if item.get_closest_marker("integration"): |
| 34 | + item.add_marker(skip) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(scope="session") |
| 38 | +def decree_addr() -> str: |
| 39 | + addr = _integration_addr() |
| 40 | + if not addr: |
| 41 | + pytest.skip("DECREE_TEST_ADDR not set") |
| 42 | + return addr |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(scope="session") |
| 46 | +def grpc_channel(decree_addr: str) -> grpc.Channel: |
| 47 | + channel = grpc.insecure_channel(decree_addr) |
| 48 | + yield channel |
| 49 | + channel.close() |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="session") |
| 53 | +def schema_stub(grpc_channel: grpc.Channel) -> schema_service_pb2_grpc.SchemaServiceStub: |
| 54 | + return schema_service_pb2_grpc.SchemaServiceStub(grpc_channel) |
| 55 | + |
| 56 | + |
| 57 | +def _superadmin_metadata() -> list[tuple[str, str]]: |
| 58 | + return [("x-decree-subject", "pytest"), ("x-decree-role", "superadmin")] |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture(scope="session") |
| 62 | +def live_schema( |
| 63 | + schema_stub: schema_service_pb2_grpc.SchemaServiceStub, |
| 64 | +) -> tuple[str, int]: |
| 65 | + """Create + publish a schema; return (schema_id, version). |
| 66 | +
|
| 67 | + Cleaned up after the session via DeleteSchema. |
| 68 | + """ |
| 69 | + meta = _superadmin_metadata() |
| 70 | + tag = uuid.uuid4().hex[:8] |
| 71 | + resp = schema_stub.CreateSchema( |
| 72 | + schema_service_pb2.CreateSchemaRequest( |
| 73 | + name=f"pytest-{tag}", |
| 74 | + description="Created by pytest integration suite", |
| 75 | + fields=[ |
| 76 | + types_pb2.SchemaField( |
| 77 | + path="greeting", |
| 78 | + type=types_pb2.FIELD_TYPE_STRING, |
| 79 | + ), |
| 80 | + types_pb2.SchemaField( |
| 81 | + path="count", |
| 82 | + type=types_pb2.FIELD_TYPE_INT, |
| 83 | + ), |
| 84 | + types_pb2.SchemaField( |
| 85 | + path="ratio", |
| 86 | + type=types_pb2.FIELD_TYPE_NUMBER, |
| 87 | + nullable=True, |
| 88 | + ), |
| 89 | + types_pb2.SchemaField( |
| 90 | + path="enabled", |
| 91 | + type=types_pb2.FIELD_TYPE_BOOL, |
| 92 | + ), |
| 93 | + ], |
| 94 | + ), |
| 95 | + metadata=meta, |
| 96 | + ) |
| 97 | + schema_id: str = resp.schema.id |
| 98 | + version: int = resp.schema.current_version |
| 99 | + |
| 100 | + schema_stub.PublishSchema( |
| 101 | + schema_service_pb2.PublishSchemaRequest(id=schema_id), |
| 102 | + metadata=meta, |
| 103 | + ) |
| 104 | + |
| 105 | + yield schema_id, version |
| 106 | + |
| 107 | + schema_stub.DeleteSchema( |
| 108 | + schema_service_pb2.DeleteSchemaRequest(id=schema_id), |
| 109 | + metadata=meta, |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@pytest.fixture(scope="session") |
| 114 | +def live_tenant( |
| 115 | + schema_stub: schema_service_pb2_grpc.SchemaServiceStub, |
| 116 | + live_schema: tuple[str, int], |
| 117 | +) -> str: |
| 118 | + """Create a tenant against the live schema; return tenant_id (name slug). |
| 119 | +
|
| 120 | + Cleaned up after the session via DeleteTenant. |
| 121 | + """ |
| 122 | + meta = _superadmin_metadata() |
| 123 | + schema_id, version = live_schema |
| 124 | + tag = uuid.uuid4().hex[:8] |
| 125 | + name = f"pytest-{tag}" |
| 126 | + resp = schema_stub.CreateTenant( |
| 127 | + schema_service_pb2.CreateTenantRequest( |
| 128 | + name=name, |
| 129 | + schema_id=schema_id, |
| 130 | + schema_version=version, |
| 131 | + ), |
| 132 | + metadata=meta, |
| 133 | + ) |
| 134 | + tenant_id: str = resp.tenant.id |
| 135 | + yield name |
| 136 | + schema_stub.DeleteTenant( |
| 137 | + schema_service_pb2.DeleteTenantRequest(id=tenant_id), |
| 138 | + metadata=meta, |
| 139 | + ) |
7 | 140 |
|
8 | 141 |
|
9 | 142 | class FakeRpcError(grpc.aio.AioRpcError): |
|
0 commit comments