Coverage for tests / test_tutorial / test_custom_response / test_tutorial002_tutorial003_tutorial004.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import Is, snapshot 1abdc

6 

7 

8@pytest.fixture( 1abdc

9 name="mod_name", 

10 params=[ 

11 pytest.param("tutorial002_py310"), 

12 pytest.param("tutorial003_py310"), 

13 pytest.param("tutorial004_py310"), 

14 ], 

15) 

16def get_mod_name(request: pytest.FixtureRequest) -> str: 1abdc

17 return request.param 1abc

18 

19 

20@pytest.fixture(name="client") 1abdc

21def get_client(mod_name: str) -> TestClient: 1abdc

22 mod = importlib.import_module(f"docs_src.custom_response.{mod_name}") 1abc

23 return TestClient(mod.app) 1abc

24 

25 

26html_contents = """ 1abdc

27 <html> 

28 <head> 

29 <title>Some HTML in here</title> 

30 </head> 

31 <body> 

32 <h1>Look ma! HTML!</h1> 

33 </body> 

34 </html> 

35 """ 

36 

37 

38def test_get_custom_response(client: TestClient): 1abdc

39 response = client.get("/items/") 1hij

40 assert response.status_code == 200, response.text 1hij

41 assert response.text == html_contents 1hij

42 

43 

44def test_openapi_schema(client: TestClient, mod_name: str): 1abdc

45 if mod_name.startswith("tutorial003"): 1efg

46 response_content = {"application/json": {"schema": {}}} 1efg

47 else: 

48 response_content = {"text/html": {"schema": {"type": "string"}}} 1efg

49 

50 response = client.get("/openapi.json") 1efg

51 assert response.status_code == 200, response.text 1efg

52 assert response.json() == snapshot( 1efg

53 { 

54 "openapi": "3.1.0", 

55 "info": {"title": "FastAPI", "version": "0.1.0"}, 

56 "paths": { 

57 "/items/": { 

58 "get": { 

59 "responses": { 

60 "200": { 

61 "description": "Successful Response", 

62 "content": Is(response_content), 

63 } 

64 }, 

65 "summary": "Read Items", 

66 "operationId": "read_items_items__get", 

67 } 

68 } 

69 }, 

70 } 

71 )