Coverage for tests / test_tutorial / test_response_model / test_tutorial003_05.py: 100%

22 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 snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

13 pytest.param("tutorial003_05_py310", marks=needs_py310), 

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abc

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_get_portal(client: TestClient): 1abdc

24 response = client.get("/portal") 1efg

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

26 assert response.json() == {"message": "Here's your interdimensional portal."} 1efg

27 

28 

29def test_get_redirect(client: TestClient): 1abdc

30 response = client.get("/portal", params={"teleport": True}, follow_redirects=False) 1hij

31 assert response.status_code == 307, response.text 1hij

32 assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ" 1hij

33 

34 

35def test_openapi_schema(client: TestClient): 1abdc

36 response = client.get("/openapi.json") 1klm

37 assert response.status_code == 200, response.text 1klm

38 assert response.json() == snapshot( 1klm

39 { 

40 "openapi": "3.1.0", 

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

42 "paths": { 

43 "/portal": { 

44 "get": { 

45 "summary": "Get Portal", 

46 "operationId": "get_portal_portal_get", 

47 "parameters": [ 

48 { 

49 "required": False, 

50 "schema": { 

51 "title": "Teleport", 

52 "type": "boolean", 

53 "default": False, 

54 }, 

55 "name": "teleport", 

56 "in": "query", 

57 } 

58 ], 

59 "responses": { 

60 "200": { 

61 "description": "Successful Response", 

62 "content": {"application/json": {"schema": {}}}, 

63 }, 

64 "422": { 

65 "description": "Validation Error", 

66 "content": { 

67 "application/json": { 

68 "schema": { 

69 "$ref": "#/components/schemas/HTTPValidationError" 

70 } 

71 } 

72 }, 

73 }, 

74 }, 

75 } 

76 } 

77 }, 

78 "components": { 

79 "schemas": { 

80 "HTTPValidationError": { 

81 "title": "HTTPValidationError", 

82 "type": "object", 

83 "properties": { 

84 "detail": { 

85 "title": "Detail", 

86 "type": "array", 

87 "items": { 

88 "$ref": "#/components/schemas/ValidationError" 

89 }, 

90 } 

91 }, 

92 }, 

93 "ValidationError": { 

94 "title": "ValidationError", 

95 "required": ["loc", "msg", "type"], 

96 "type": "object", 

97 "properties": { 

98 "loc": { 

99 "title": "Location", 

100 "type": "array", 

101 "items": { 

102 "anyOf": [{"type": "string"}, {"type": "integer"}] 

103 }, 

104 }, 

105 "msg": {"title": "Message", "type": "string"}, 

106 "type": {"title": "Error Type", "type": "string"}, 

107 "input": {"title": "Input"}, 

108 "ctx": {"title": "Context", "type": "object"}, 

109 }, 

110 }, 

111 } 

112 }, 

113 } 

114 )