|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +import types |
| 5 | +from collections.abc import Callable |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from quant_platform_kit.common.health import register_health_endpoint |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class _FakeRule: |
| 14 | + rule: str |
| 15 | + |
| 16 | + |
| 17 | +class _FakeUrlMap: |
| 18 | + def __init__(self, app: _FakeFlaskApp): |
| 19 | + self._app = app |
| 20 | + |
| 21 | + def iter_rules(self): |
| 22 | + return iter(self._app.rules) |
| 23 | + |
| 24 | + |
| 25 | +class _FakeFlaskApp: |
| 26 | + def __init__(self): |
| 27 | + self.rules: list[_FakeRule] = [] |
| 28 | + self.view_functions: dict[str, Callable[..., Any]] = {} |
| 29 | + self.url_map = _FakeUrlMap(self) |
| 30 | + |
| 31 | + def add_url_rule( |
| 32 | + self, |
| 33 | + rule: str, |
| 34 | + endpoint: str, |
| 35 | + view_func: Callable[..., Any], |
| 36 | + methods: list[str], |
| 37 | + ) -> None: |
| 38 | + if endpoint in self.view_functions: |
| 39 | + raise AssertionError( |
| 40 | + f"View function mapping is overwriting an existing endpoint function: {endpoint}" |
| 41 | + ) |
| 42 | + self.rules.append(_FakeRule(rule)) |
| 43 | + self.view_functions[endpoint] = view_func |
| 44 | + |
| 45 | + |
| 46 | +def _install_fake_flask(monkeypatch): |
| 47 | + monkeypatch.setitem(sys.modules, "flask", types.SimpleNamespace(jsonify=lambda payload: payload)) |
| 48 | + |
| 49 | + |
| 50 | +def test_register_health_endpoint_uses_non_colliding_endpoint_names(monkeypatch): |
| 51 | + _install_fake_flask(monkeypatch) |
| 52 | + app = _FakeFlaskApp() |
| 53 | + register_health_endpoint(app) |
| 54 | + |
| 55 | + app.add_url_rule("/", endpoint="health", view_func=lambda: "service-info", methods=["GET"]) |
| 56 | + |
| 57 | + assert {rule.rule for rule in app.rules} == {"/health", "/healthz", "/"} |
| 58 | + assert {"qpk_health", "qpk_healthz", "health"} <= set(app.view_functions) |
| 59 | + |
| 60 | + |
| 61 | +def test_register_health_endpoint_preserves_existing_health_route(monkeypatch): |
| 62 | + _install_fake_flask(monkeypatch) |
| 63 | + app = _FakeFlaskApp() |
| 64 | + app.add_url_rule("/health", endpoint="platform_health", view_func=lambda: "platform-ok", methods=["GET"]) |
| 65 | + |
| 66 | + register_health_endpoint(app) |
| 67 | + |
| 68 | + assert [rule.rule for rule in app.rules] == ["/health", "/healthz"] |
| 69 | + assert "platform_health" in app.view_functions |
| 70 | + assert "qpk_health" not in app.view_functions |
| 71 | + assert "qpk_healthz" in app.view_functions |
0 commit comments