Coverage for tests / test_tutorial / test_fastapi / test_response_model / test_tutorial001.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-01-06 21:09 +0000

1import importlib 1jklmnopqr

2from types import ModuleType 1jklmnopqr

3 

4import pytest 1jklmnopqr

5from fastapi.testclient import TestClient 1jklmnopqr

6from sqlmodel import create_engine 1jklmnopqr

7from sqlmodel.pool import StaticPool 1jklmnopqr

8 

9from tests.conftest import needs_py310 1jklmnopqr

10 

11 

12@pytest.fixture( 1jklmnopqr

13 name="module", 

14 params=[ 

15 pytest.param("tutorial001_py39"), 

16 pytest.param("tutorial001_py310", marks=needs_py310), 

17 ], 

18) 

19def get_module(request: pytest.FixtureRequest) -> ModuleType: 1jklmnopqr

20 mod = importlib.import_module( 1jklmnopqr

21 f"docs_src.tutorial.fastapi.response_model.{request.param}" 

22 ) 

23 mod.sqlite_url = "sqlite://" 1jklmnopqr

24 mod.engine = create_engine( 1jklmnopqr

25 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool 

26 ) 

27 return mod 1jklmnopqr

28 

29 

30def test_tutorial(module: ModuleType): 1jklmnopqr

31 with TestClient(module.app) as client: 1abcdefghi

32 hero_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefghi

33 response = client.post("/heroes/", json=hero_data) 1abcdefghi

34 data = response.json() 1abcdefghi

35 

36 assert response.status_code == 200, response.text 1abcdefghi

37 assert data["name"] == hero_data["name"] 1abcdefghi

38 assert data["secret_name"] == hero_data["secret_name"] 1abcdefghi

39 assert data["id"] is not None 1abcdefghi

40 assert data["age"] is None 1abcdefghi

41 

42 response = client.get("/heroes/") 1abcdefghi

43 data = response.json() 1abcdefghi

44 

45 assert response.status_code == 200, response.text 1abcdefghi

46 assert len(data) == 1 1abcdefghi

47 assert data[0]["name"] == hero_data["name"] 1abcdefghi

48 assert data[0]["secret_name"] == hero_data["secret_name"] 1abcdefghi

49 

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

51 

52 assert response.status_code == 200, response.text 1abcdefghi

53 

54 assert response.json() == { 1abcdefghi

55 "openapi": "3.1.0", 

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

57 "paths": { 

58 "/heroes/": { 

59 "get": { 

60 "summary": "Read Heroes", 

61 "operationId": "read_heroes_heroes__get", 

62 "responses": { 

63 "200": { 

64 "description": "Successful Response", 

65 "content": { 

66 "application/json": { 

67 "schema": { 

68 "title": "Response Read Heroes Heroes Get", 

69 "type": "array", 

70 "items": { 

71 "$ref": "#/components/schemas/Hero" 

72 }, 

73 } 

74 } 

75 }, 

76 } 

77 }, 

78 }, 

79 "post": { 

80 "summary": "Create Hero", 

81 "operationId": "create_hero_heroes__post", 

82 "requestBody": { 

83 "content": { 

84 "application/json": { 

85 "schema": {"$ref": "#/components/schemas/Hero"} 

86 } 

87 }, 

88 "required": True, 

89 }, 

90 "responses": { 

91 "200": { 

92 "description": "Successful Response", 

93 "content": { 

94 "application/json": { 

95 "schema": {"$ref": "#/components/schemas/Hero"} 

96 } 

97 }, 

98 }, 

99 "422": { 

100 "description": "Validation Error", 

101 "content": { 

102 "application/json": { 

103 "schema": { 

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

105 } 

106 } 

107 }, 

108 }, 

109 }, 

110 }, 

111 } 

112 }, 

113 "components": { 

114 "schemas": { 

115 "HTTPValidationError": { 

116 "title": "HTTPValidationError", 

117 "type": "object", 

118 "properties": { 

119 "detail": { 

120 "title": "Detail", 

121 "type": "array", 

122 "items": { 

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

124 }, 

125 } 

126 }, 

127 }, 

128 "Hero": { 

129 "title": "Hero", 

130 "required": ["name", "secret_name"], 

131 "type": "object", 

132 "properties": { 

133 "id": { 

134 "title": "Id", 

135 "anyOf": [{"type": "integer"}, {"type": "null"}], 

136 }, 

137 "name": {"title": "Name", "type": "string"}, 

138 "secret_name": {"title": "Secret Name", "type": "string"}, 

139 "age": { 

140 "title": "Age", 

141 "anyOf": [{"type": "integer"}, {"type": "null"}], 

142 }, 

143 }, 

144 }, 

145 "ValidationError": { 

146 "title": "ValidationError", 

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

148 "type": "object", 

149 "properties": { 

150 "loc": { 

151 "title": "Location", 

152 "type": "array", 

153 "items": { 

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

155 }, 

156 }, 

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

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

159 }, 

160 }, 

161 } 

162 }, 

163 }