Skip to content

Commit 223e3b4

Browse files
committed
Use proper Request object to test other handlers too
1 parent c88b383 commit 223e3b4

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

tests/unit/app/endpoints/test_config.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from fastapi import HTTPException, status
5+
from fastapi import HTTPException, Request, status
66
from app.endpoints.config import config_endpoint_handler
77
from configuration import AppConfig
88

@@ -15,7 +15,11 @@ def test_config_endpoint_handler_configuration_not_loaded(mocker):
1515
)
1616
mocker.patch("app.endpoints.config.configuration", None)
1717

18-
request = None
18+
request = Request(
19+
scope={
20+
"type": "http",
21+
}
22+
)
1923
with pytest.raises(HTTPException) as e:
2024
config_endpoint_handler(request)
2125
assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR
@@ -49,7 +53,11 @@ def test_config_endpoint_handler_configuration_loaded():
4953
}
5054
cfg = AppConfig()
5155
cfg.init_from_dict(config_dict)
52-
request = None
56+
request = Request(
57+
scope={
58+
"type": "http",
59+
}
60+
)
5361
response = config_endpoint_handler(request)
5462
assert response is not None
5563
assert response == cfg.configuration

tests/unit/app/endpoints/test_info.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Unit tests for the /info REST API endpoint."""
22

3+
from fastapi import Request
4+
35
from app.endpoints.info import info_endpoint_handler
46
from configuration import AppConfig
57

@@ -28,7 +30,11 @@ def test_info_endpoint():
2830
}
2931
cfg = AppConfig()
3032
cfg.init_from_dict(config_dict)
31-
request = None
33+
request = Request(
34+
scope={
35+
"type": "http",
36+
}
37+
)
3238
response = info_endpoint_handler(request)
3339
assert response is not None
3440
assert response.name is not None

tests/unit/app/endpoints/test_metrics.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Unit tests for the /metrics REST API endpoint."""
22

3+
from fastapi import Request
4+
35
from app.endpoints.metrics import metrics_endpoint_handler
46

57

@@ -8,7 +10,12 @@ async def test_metrics_endpoint(mocker):
810
mock_setup_metrics = mocker.patch(
911
"app.endpoints.metrics.setup_model_metrics", return_value=None
1012
)
11-
response = await metrics_endpoint_handler(None)
13+
request = Request(
14+
scope={
15+
"type": "http",
16+
}
17+
)
18+
response = await metrics_endpoint_handler(request)
1219
assert response is not None
1320
assert response.status_code == 200
1421
assert "text/plain" in response.headers["Content-Type"]
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
"""Unit tests for the / endpoint handler."""
22

3+
from fastapi import Request
4+
35
from app.endpoints.root import root_endpoint_handler
46

57

68
def test_root_endpoint():
79
"""Test the root endpoint handler."""
8-
request = None
10+
request = Request(
11+
scope={
12+
"type": "http",
13+
}
14+
)
915
response = root_endpoint_handler(request)
1016
assert response is not None

0 commit comments

Comments
 (0)