Skip to content

Commit 180eab8

Browse files
author
mongkok
committed
Added TestClient.get_stats
1 parent 7f9f07a commit 180eab8

12 files changed

Lines changed: 58 additions & 86 deletions
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from fastapi import status
2-
31
from ...mark import override_panels, skip_py
42
from ...testclient import TestClient
53

@@ -8,12 +6,9 @@
86
@override_panels(["debug_toolbar.panels.sqlalchemy.SQLAlchemyPanel"])
97
def test_sqlalchemy(client: TestClient) -> None:
108
store_id = client.get_store_id("/sql")
11-
response = client.render_panel(store_id, "SQLAlchemyPanel")
12-
13-
assert response.status_code == status.HTTP_200_OK
9+
stats = client.get_stats(store_id, "SQLAlchemyPanel")
10+
queries = stats["queries"]
1411

15-
content = response.json()["content"]
16-
assert "3 queries" in content
17-
assert "2 similar" in content
18-
assert "INSERT" in content
19-
assert "SELECT" in content
12+
assert len(queries) == 3
13+
assert queries[0][1]["sql"].startswith("INSERT")
14+
assert queries[1][1]["dup_count"] == 2

tests/panels/test_headers.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from fastapi import status
2-
31
from ..mark import override_panels
42
from ..testclient import TestClient
53

64

75
@override_panels(["debug_toolbar.panels.headers.HeadersPanel"])
86
def test_headers(client: TestClient) -> None:
9-
store_id = client.get_store_id("/async")
10-
response = client.render_panel(store_id, "HeadersPanel")
11-
12-
assert response.status_code == status.HTTP_200_OK
13-
assert "accept" in response.json()["content"]
7+
headers = {
8+
"cookie": "",
9+
}
10+
store_id = client.get_store_id("/async", headers=headers)
11+
stats = client.get_stats(store_id, "HeadersPanel")
12+
request_headers = stats["request_headers"]
13+
14+
assert request_headers["cookie"]

tests/panels/test_logging.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import typing as t
33

44
import pytest
5-
from fastapi import FastAPI, Request, status
5+
from fastapi import FastAPI, Request
66
from fastapi.logger import logger
77
from fastapi.responses import HTMLResponse
88

@@ -29,7 +29,6 @@ async def get_log_async(request: Request, level: str) -> str:
2929
@override_panels(["debug_toolbar.panels.logging.LoggingPanel"])
3030
def test_logging(client: TestClient, path: str, level: str) -> None:
3131
store_id = client.get_store_id(f"/log/{path}?level={level}")
32-
response = client.render_panel(store_id, "LoggingPanel")
32+
stats = client.get_stats(store_id, "LoggingPanel")
3333

34-
assert response.status_code == status.HTTP_200_OK
35-
assert level in response.json()["content"]
34+
assert stats["records"][0]["level"] == level

tests/panels/test_profiling.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from fastapi import status
32

43
from ..mark import override_panels
54
from ..testclient import TestClient
@@ -9,7 +8,6 @@
98
@override_panels(["debug_toolbar.panels.profiling.ProfilingPanel"])
109
def test_profiling(client: TestClient, path: str) -> None:
1110
store_id = client.get_store_id(f"/{path}")
12-
response = client.render_panel(store_id, "ProfilingPanel")
11+
stats = client.get_stats(store_id, "ProfilingPanel")
1312

14-
assert response.status_code == status.HTTP_200_OK
15-
assert "profileSession" in response.json()["content"]
13+
assert "profileSession" in stats["content"]

tests/panels/test_pydantic.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
1-
import typing as t
2-
3-
import pytest
4-
from fastapi import Depends, FastAPI, Request, status
5-
from fastapi.responses import HTMLResponse
6-
from pydantic import BaseModel
7-
81
from ..mark import override_panels
92
from ..testclient import TestClient
103

114

12-
class Model(BaseModel):
13-
test: bool
14-
15-
16-
@pytest.fixture
17-
def client(app: FastAPI, get_index: t.Callable) -> TestClient:
18-
@app.get("/pydantic", response_class=HTMLResponse)
19-
async def get_pydantic(request: Request, model: Model = Depends()) -> str:
20-
return get_index(request)
21-
22-
return TestClient(app)
23-
24-
255
@override_panels(["debug_toolbar.panels.pydantic.PydanticPanel"])
266
def test_pydantic(client: TestClient) -> None:
27-
store_id = client.get_store_id("/pydantic", params={"test": True})
28-
response = client.render_panel(store_id, "PydanticPanel")
7+
store_id = client.get_store_id("/openapi.json")
8+
stats = client.get_stats(store_id, "PydanticPanel")
299

30-
assert response.status_code == status.HTTP_200_OK
31-
assert "Model.test" in response.json()["content"]
10+
assert stats["validations"]

tests/panels/test_request.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing as t
22

33
import pytest
4-
from fastapi import FastAPI, Request, status
4+
from fastapi import FastAPI, Request
55
from fastapi.responses import HTMLResponse
66
from starlette.middleware.sessions import SessionMiddleware
77

@@ -24,7 +24,6 @@ async def get_session(request: Request) -> str:
2424
@override_panels(["debug_toolbar.panels.request.RequestPanel"])
2525
def test_session(client: TestClient) -> None:
2626
store_id = client.get_store_id("/session")
27-
response = client.render_panel(store_id, "RequestPanel")
27+
stats = client.get_stats(store_id, "RequestPanel")
2828

29-
assert response.status_code == status.HTTP_200_OK
30-
assert "debug" in response.json()["content"]
29+
assert stats["session"]["debug"]

tests/panels/test_routes.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
from fastapi import status
2-
31
from ..mark import override_panels
42
from ..testclient import TestClient
53

64

75
@override_panels(["debug_toolbar.panels.routes.RoutesPanel"])
86
def test_routes(client: TestClient) -> None:
97
store_id = client.get_store_id("/async")
10-
response = client.render_panel(store_id, "RoutesPanel")
8+
stats = client.get_stats(store_id, "RoutesPanel")
119

12-
assert response.status_code == status.HTTP_200_OK
13-
assert "openapi" in response.json()["content"]
10+
assert any(route.name == "openapi" for route in stats["routes"])

tests/panels/test_settings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import status
1+
import pytest
22

33
from ..mark import override_panels
44
from ..testclient import TestClient
@@ -7,7 +7,6 @@
77
@override_panels(["debug_toolbar.panels.settings.SettingsPanel"])
88
def test_settings(client: TestClient) -> None:
99
store_id = client.get_store_id("/async")
10-
response = client.render_panel(store_id, "SettingsPanel")
1110

12-
assert response.status_code == status.HTTP_200_OK
13-
assert "PANELS" in response.json()["content"]
11+
with pytest.raises(KeyError):
12+
client.get_stats(store_id, "SettingsPanel")

tests/panels/test_timer.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import re
2-
3-
from fastapi import status
4-
51
from ..mark import override_panels
62
from ..testclient import TestClient
73

84

95
@override_panels(["debug_toolbar.panels.timer.TimerPanel"])
106
def test_timer(client: TestClient) -> None:
117
store_id = client.get_store_id("/async")
12-
response = client.render_panel(store_id, "TimerPanel")
8+
stats = client.get_stats(store_id, "TimerPanel")
139

14-
assert response.status_code == status.HTTP_200_OK
15-
assert re.findall(r"(\d+) msec", response.json()["content"])
10+
assert "total" in stats

tests/panels/test_versions.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
from fastapi import status
2-
31
from ..mark import override_panels
42
from ..testclient import TestClient
53

64

75
@override_panels(["debug_toolbar.panels.versions.VersionsPanel"])
86
def test_versions(client: TestClient) -> None:
97
store_id = client.get_store_id("/async")
10-
response = client.render_panel(store_id, "VersionsPanel")
8+
stats = client.get_stats(store_id, "VersionsPanel")
119

12-
assert response.status_code == status.HTTP_200_OK
13-
assert "fastapi" in response.json()["content"]
10+
assert stats["packages"]

0 commit comments

Comments
 (0)