Files
g2s-aggregator/tests/smoke/test_app_import.py
T
Раис Юсупалиев 798b6e38bd
Deploy / deploy (push) Successful in 52s
пофикшен redis и логи redis
2026-06-20 19:35:23 +03:00

89 lines
3.4 KiB
Python

import importlib
import sys
from pathlib import Path
from fastapi import FastAPI
from app import config as config_module
from app.config import Settings, get_settings
from app.runtime import logging as logging_module
from app.runtime import metrics as metrics_module
from app.runtime import tracing as tracing_module
TEST_CONFIG_FILE = Path(__file__).resolve().parents[2] / "config.test.yaml"
def _import_main_module(monkeypatch):
get_settings.cache_clear()
monkeypatch.setattr(config_module, "TEST_CONFIG_FILE", str(TEST_CONFIG_FILE))
sys.modules.pop("app.main", None)
return importlib.import_module("app.main")
def test_app_import_smoke(monkeypatch) -> None:
logging_bootstrap_calls: list[None] = []
metrics_bootstrap_calls: list[object] = []
tracing_bootstrap_calls: list[tuple[FastAPI, object]] = []
def fake_configure_logging() -> None:
logging_bootstrap_calls.append(None)
def fake_configure_tracing(app: FastAPI, observability: object) -> None:
tracing_bootstrap_calls.append((app, observability))
def fake_configure_metrics(observability: object) -> None:
metrics_bootstrap_calls.append(observability)
monkeypatch.setattr(logging_module, "configure_logging", fake_configure_logging)
monkeypatch.setattr(metrics_module, "configure_metrics", fake_configure_metrics)
monkeypatch.setattr(tracing_module, "configure_tracing", fake_configure_tracing)
main_module = _import_main_module(monkeypatch)
assert isinstance(main_module.app, FastAPI)
assert isinstance(main_module.app.state.settings, Settings)
assert "/api/v1/delivery/tbank/notifications" in {
route.path for route in main_module.app.routes
}
assert logging_bootstrap_calls == [None]
assert metrics_bootstrap_calls == [main_module.app.state.settings.observability]
assert tracing_bootstrap_calls == [
(main_module.app, main_module.app.state.settings.observability)
]
get_settings.cache_clear()
def test_app_created_with_provided_settings(monkeypatch) -> None:
logging_bootstrap_calls: list[None] = []
metrics_bootstrap_calls: list[object] = []
tracing_bootstrap_calls: list[tuple[FastAPI, object]] = []
def fake_configure_logging() -> None:
logging_bootstrap_calls.append(None)
def fake_configure_tracing(app: FastAPI, observability: object) -> None:
tracing_bootstrap_calls.append((app, observability))
def fake_configure_metrics(observability: object) -> None:
metrics_bootstrap_calls.append(observability)
monkeypatch.setattr(logging_module, "configure_logging", fake_configure_logging)
monkeypatch.setattr(metrics_module, "configure_metrics", fake_configure_metrics)
monkeypatch.setattr(tracing_module, "configure_tracing", fake_configure_tracing)
main_module = _import_main_module(monkeypatch)
monkeypatch.setattr(config_module, "TEST_CONFIG_FILE", str(TEST_CONFIG_FILE))
settings = Settings()
instance = main_module.create_app(settings=settings)
assert instance.state.settings is settings
assert logging_bootstrap_calls == [None, None]
assert metrics_bootstrap_calls == [
main_module.app.state.settings.observability,
settings.observability,
]
assert tracing_bootstrap_calls == [
(main_module.app, main_module.app.state.settings.observability),
(instance, settings.observability),
]
get_settings.cache_clear()