forked from fastapiutils/fastapi-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timing.py
More file actions
109 lines (74 loc) · 2.58 KB
/
test_timing.py
File metadata and controls
109 lines (74 loc) · 2.58 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
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any
import pytest
from fastapi import FastAPI
from starlette.requests import Request
from starlette.staticfiles import StaticFiles
from starlette.testclient import TestClient
from fastapi_utils.timing import add_timing_middleware, record_timing
if TYPE_CHECKING:
from pytest.capture import CaptureFixture
else:
CaptureFixture = Any
app = FastAPI()
add_timing_middleware(app, exclude="untimed")
static_files_app = StaticFiles(directory=".")
app.mount(path="/static", app=static_files_app, name="static")
@app.get("/timed")
def get_timed() -> None:
pass
@app.get("/untimed")
def get_untimed() -> None:
pass
client = TestClient(app)
def test_timing(capsys: CaptureFixture[str]) -> None:
client.get("/timed")
out, err = capsys.readouterr()
assert err == ""
assert out.startswith("TIMING: Wall")
assert "CPU:" in out
assert out.endswith("test_timing.get_timed\n")
def test_silent_timing(capsys: CaptureFixture[str]) -> None:
client.get("/untimed")
out, err = capsys.readouterr()
assert err == ""
assert out == ""
def test_mount(capsys: CaptureFixture[str]) -> None:
basename = Path(__file__).name
client.get(f"/static/{basename}")
out, err = capsys.readouterr()
assert err == ""
assert out.startswith("TIMING:")
assert out.endswith("StaticFiles<'static'>\n")
def test_missing(capsys: CaptureFixture[str]) -> None:
client.get("/will-404")
out, err = capsys.readouterr()
assert err == ""
assert out.startswith("TIMING:")
assert out.endswith("<Path: /will-404>\n")
app2 = FastAPI()
add_timing_middleware(app2, prefix="app2")
@app2.get("/")
def get_with_intermediate_timing(request: Request) -> None:
record_timing(request, note="hello")
client2 = TestClient(app2)
def test_intermediate(capsys: CaptureFixture[str]) -> None:
client2.get("/")
out, err = capsys.readouterr()
assert err == ""
outs = out.strip().split("\n")
assert len(outs) == 2
assert outs[0].startswith("TIMING:")
assert outs[0].endswith("test_timing.get_with_intermediate_timing (hello)")
assert outs[1].startswith("TIMING:")
assert outs[1].endswith("test_timing.get_with_intermediate_timing")
app3 = FastAPI()
@app3.get("/")
def fail_to_record(request: Request) -> None:
record_timing(request)
client3 = TestClient(app3)
def test_recording_fails_without_middleware() -> None:
with pytest.raises(ValueError) as exc_info:
client3.get("/")
assert str(exc_info.value) == "No timer present on request"