-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fastapi_bootstrap.py
More file actions
112 lines (86 loc) · 4.07 KB
/
test_fastapi_bootstrap.py
File metadata and controls
112 lines (86 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import dataclasses
import logging
import fastapi
import pytest
import structlog
from starlette import status
from starlette.testclient import TestClient
from lite_bootstrap import FastAPIBootstrapper, FastAPIConfig
from tests.conftest import CustomInstrumentor, SentryTestTransport, emulate_package_missing
logger = structlog.getLogger(__name__)
std_logger = logging.getLogger()
@pytest.fixture
def fastapi_config() -> FastAPIConfig:
return FastAPIConfig(
service_name="microservice",
service_version="2.0.0",
service_environment="test",
service_debug=False,
cors_allowed_origins=["http://test"],
health_checks_path="/custom-health/",
logging_buffer_capacity=0,
opentelemetry_instrumentors=[CustomInstrumentor()],
opentelemetry_log_traces=True,
opentelemetry_generate_health_check_spans=False,
prometheus_metrics_path="/custom-metrics/",
sentry_dsn="https://testdsn@localhost/1",
sentry_additional_params={"transport": SentryTestTransport()},
swagger_offline_docs=True,
)
def test_fastapi_bootstrap(fastapi_config: FastAPIConfig) -> None:
bootstrapper = FastAPIBootstrapper(bootstrap_config=fastapi_config)
application = bootstrapper.bootstrap()
assert bootstrapper.is_bootstrapped
logger.info("testing logging", key="value")
with TestClient(application) as test_client:
response = test_client.get(fastapi_config.health_checks_path)
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"health_status": True, "service_name": "microservice", "service_version": "2.0.0"}
response = test_client.get(fastapi_config.prometheus_metrics_path)
assert response.status_code == status.HTTP_200_OK
assert response.text
response = test_client.get(str(application.docs_url))
assert response.status_code == status.HTTP_200_OK
assert response.text
response = test_client.get(str(application.redoc_url))
assert response.status_code == status.HTTP_200_OK
assert response.text
assert not bootstrapper.is_bootstrapped
def test_fastapi_bootstrap_std_logger(fastapi_config: FastAPIConfig, capsys: pytest.CaptureFixture[str]) -> None:
bootstrapper = FastAPIBootstrapper(bootstrap_config=fastapi_config)
application = bootstrapper.bootstrap()
@application.get("/")
async def home() -> str:
std_logger.info("std logger")
logger.info("structlog logger")
return ""
with TestClient(application) as test_client:
test_client.get("/")
stdout = capsys.readouterr().out
assert '"event":"std logger","level":"info","logger":"root"' in stdout
assert stdout.count("std logger") == 1
def test_fastapi_bootstrapper_not_ready() -> None:
with emulate_package_missing("fastapi"), pytest.raises(RuntimeError, match="fastapi is not installed"):
FastAPIBootstrapper(bootstrap_config=FastAPIConfig())
def test_fastapi_bootstrapper_docs_url_differ(fastapi_config: FastAPIConfig) -> None:
new_config = dataclasses.replace(fastapi_config, application=fastapi.FastAPI(docs_url="/custom-docs/"))
bootstrapper = FastAPIBootstrapper(bootstrap_config=new_config)
with pytest.warns(UserWarning, match="swagger_path is differ from docs_url"):
bootstrapper.bootstrap()
def test_fastapi_bootstrapper_apps_and_kwargs_warning(fastapi_config: FastAPIConfig) -> None:
with pytest.warns(UserWarning, match="application_kwargs must be used without application"):
dataclasses.replace(fastapi_config, application=fastapi.FastAPI(), application_kwargs={"title": "some title"})
@pytest.mark.parametrize(
"package_name",
[
"opentelemetry",
"sentry_sdk",
"structlog",
"prometheus_fastapi_instrumentator",
],
)
def test_fastapi_bootstrapper_with_missing_instrument_dependency(
fastapi_config: FastAPIConfig, package_name: str
) -> None:
with emulate_package_missing(package_name), pytest.warns(UserWarning, match=package_name):
FastAPIBootstrapper(bootstrap_config=fastapi_config)