|
| 1 | +"""Unit-test conftest: install lightweight stubs for Azure Functions packages. |
| 2 | +
|
| 3 | +``azure-functions`` and ``azure-durable-functions`` are Azure Functions |
| 4 | +runtime packages that are not installed in the library's dev dependencies — |
| 5 | +they're only needed when *running* the function app, not when testing the |
| 6 | +helper logic inside it. This conftest inserts minimal stubs into |
| 7 | +``sys.modules`` before any test module is imported so that |
| 8 | +``tests/unit/test_changefeed.py`` (and any other test that imports from |
| 9 | +``azure_functions/``) can be collected without the runtime packages present. |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +import types |
| 14 | + |
| 15 | + |
| 16 | +def _install_azure_functions_stubs() -> None: |
| 17 | + # Inject stub sub-modules directly into sys.modules. Do NOT create or |
| 18 | + # overwrite the top-level ``azure`` entry — that is a real namespace |
| 19 | + # package provided by azure-cosmos (a core dependency), and replacing it |
| 20 | + # with a plain module breaks its sub-package imports. |
| 21 | + |
| 22 | + # ----- azure.functions ----- |
| 23 | + if "azure.functions" not in sys.modules: |
| 24 | + af = types.ModuleType("azure.functions") |
| 25 | + |
| 26 | + class _AuthLevel: |
| 27 | + FUNCTION = "function" |
| 28 | + |
| 29 | + af.AuthLevel = _AuthLevel |
| 30 | + af.DocumentList = list |
| 31 | + af.HttpRequest = object |
| 32 | + af.HttpResponse = object |
| 33 | + sys.modules["azure.functions"] = af |
| 34 | + |
| 35 | + # ----- azure.durable_functions ----- |
| 36 | + if "azure.durable_functions" not in sys.modules: |
| 37 | + adf = types.ModuleType("azure.durable_functions") |
| 38 | + |
| 39 | + class _Noop: |
| 40 | + """A do-nothing stand-in for DFApp / Blueprint decorators.""" |
| 41 | + |
| 42 | + def __init__(self, *args, **kwargs): |
| 43 | + pass |
| 44 | + |
| 45 | + def register_functions(self, *args, **kwargs): |
| 46 | + pass |
| 47 | + |
| 48 | + def route(self, *args, **kwargs): |
| 49 | + return lambda fn: fn |
| 50 | + |
| 51 | + def durable_client_input(self, *args, **kwargs): |
| 52 | + return lambda fn: fn |
| 53 | + |
| 54 | + def orchestration_trigger(self, *args, **kwargs): |
| 55 | + return lambda fn: fn |
| 56 | + |
| 57 | + def cosmos_db_trigger(self, *args, **kwargs): |
| 58 | + return lambda fn: fn |
| 59 | + |
| 60 | + def activity_trigger(self, *args, **kwargs): |
| 61 | + return lambda fn: fn |
| 62 | + |
| 63 | + adf.Blueprint = _Noop |
| 64 | + adf.DFApp = _Noop |
| 65 | + adf.DurableOrchestrationContext = object |
| 66 | + sys.modules["azure.durable_functions"] = adf |
| 67 | + |
| 68 | + |
| 69 | +_install_azure_functions_stubs() |
0 commit comments