Coverage for tests / test_tutorial / test_conditional_openapi / test_tutorial001.py: 100%
30 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1lmno
3from fastapi.testclient import TestClient 1lmno
4from inline_snapshot import snapshot 1lmno
7def get_client() -> TestClient: 1lmno
8 from docs_src.conditional_openapi import tutorial001_py310 1eahbifcjgdk
10 importlib.reload(tutorial001_py310) 1eahbifcjgdk
12 client = TestClient(tutorial001_py310.app) 1eahbifcjgdk
13 return client 1eahbifcjgdk
16def test_disable_openapi(monkeypatch): 1lmno
17 monkeypatch.setenv("OPENAPI_URL", "") 1abcd
18 # Load the client after setting the env var
19 client = get_client() 1abcd
20 response = client.get("/openapi.json") 1abcd
21 assert response.status_code == 404, response.text 1abcd
22 response = client.get("/docs") 1abcd
23 assert response.status_code == 404, response.text 1abcd
24 response = client.get("/redoc") 1abcd
25 assert response.status_code == 404, response.text 1abcd
28def test_root(): 1lmno
29 client = get_client() 1hijk
30 response = client.get("/") 1hijk
31 assert response.status_code == 200 1hijk
32 assert response.json() == {"message": "Hello World"} 1hijk
35def test_default_openapi(): 1lmno
36 client = get_client() 1epfg
37 response = client.get("/docs") 1epfg
38 assert response.status_code == 200, response.text 1epfg
39 response = client.get("/redoc") 1epfg
40 assert response.status_code == 200, response.text 1epfg
41 response = client.get("/openapi.json") 1epfg
42 assert response.json() == snapshot( 1epfg
43 {
44 "openapi": "3.1.0",
45 "info": {"title": "FastAPI", "version": "0.1.0"},
46 "paths": {
47 "/": {
48 "get": {
49 "summary": "Root",
50 "operationId": "root__get",
51 "responses": {
52 "200": {
53 "description": "Successful Response",
54 "content": {"application/json": {"schema": {}}},
55 }
56 },
57 }
58 }
59 },
60 }
61 )