|
| 1 | +""" |
| 2 | +Shared test fixtures for pmxt unit tests. |
| 3 | +
|
| 4 | +Mocks the auto-generated pmxt_internal module so tests can run without |
| 5 | +having to generate the OpenAPI client first. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +import types |
| 10 | +from unittest.mock import MagicMock |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# Ensure pmxt_internal is importable even when the generated/ dir is absent. |
| 14 | +# The real module lives in sdks/python/generated/pmxt_internal/ and is |
| 15 | +# produced by `npm run generate`. For pure-unit tests we mock the entire |
| 16 | +# package so that `from pmxt_internal import ...` succeeds. |
| 17 | +# --------------------------------------------------------------------------- |
| 18 | + |
| 19 | + |
| 20 | +def _ensure_pmxt_internal_mock(): |
| 21 | + """Insert a mock pmxt_internal package into sys.modules if not present.""" |
| 22 | + if "pmxt_internal" in sys.modules: |
| 23 | + return # already available (maybe the real one or a prior mock) |
| 24 | + |
| 25 | + # Top-level package |
| 26 | + pkg = types.ModuleType("pmxt_internal") |
| 27 | + pkg.ApiClient = MagicMock |
| 28 | + pkg.Configuration = MagicMock |
| 29 | + |
| 30 | + # Sub-module: models |
| 31 | + models_mod = types.ModuleType("pmxt_internal.models") |
| 32 | + pkg.models = models_mod |
| 33 | + |
| 34 | + # Sub-module: api.default_api |
| 35 | + api_pkg = types.ModuleType("pmxt_internal.api") |
| 36 | + default_api_mod = types.ModuleType("pmxt_internal.api.default_api") |
| 37 | + default_api_mod.DefaultApi = MagicMock |
| 38 | + api_pkg.default_api = default_api_mod |
| 39 | + |
| 40 | + # Sub-module: exceptions |
| 41 | + exc_mod = types.ModuleType("pmxt_internal.exceptions") |
| 42 | + |
| 43 | + class _FakeApiException(Exception): |
| 44 | + """Stand-in for the generated ApiException.""" |
| 45 | + |
| 46 | + def __init__(self, status=None, reason=None, body=None, **kwargs): |
| 47 | + self.status = status |
| 48 | + self.reason = reason |
| 49 | + self.body = body |
| 50 | + super().__init__(reason) |
| 51 | + |
| 52 | + exc_mod.ApiException = _FakeApiException |
| 53 | + |
| 54 | + # Register everything in sys.modules |
| 55 | + sys.modules["pmxt_internal"] = pkg |
| 56 | + sys.modules["pmxt_internal.models"] = models_mod |
| 57 | + sys.modules["pmxt_internal.api"] = api_pkg |
| 58 | + sys.modules["pmxt_internal.api.default_api"] = default_api_mod |
| 59 | + sys.modules["pmxt_internal.exceptions"] = exc_mod |
| 60 | + |
| 61 | + |
| 62 | +# Run at import time so that conftest is processed before any test module |
| 63 | +# tries to `import pmxt`. |
| 64 | +_ensure_pmxt_internal_mock() |
0 commit comments