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-06-10 09:40 +0000

1import importlib 1ijklmnop

2from types import ModuleType 1ijklmnop

3 

4import pytest 1ijklmnop

5from dirty_equals import IsOneOf 1ijklmnop

6from fastapi.testclient import TestClient 1ijklmnop

7from sqlmodel import create_engine 1ijklmnop

8from sqlmodel.pool import StaticPool 1ijklmnop

9 

10 

11@pytest.fixture( 1ijklmnop

12 name="module", 

13 params=[ 

14 pytest.param("tutorial001_py310"), 

15 ], 

16) 

17def get_module(request: pytest.FixtureRequest) -> ModuleType: 1ijklmnop

18 mod = importlib.import_module( 1ijklmnop

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

20 ) 

21 mod.sqlite_url = "sqlite://" 1ijklmnop

22 mod.engine = create_engine( 1ijklmnop

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

24 ) 

25 return mod 1ijklmnop

26 

27 

28def test_tutorial(module: ModuleType): 1ijklmnop

29 with TestClient(module.app) as client: 1abcdefgh

30 hero_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefgh

31 response = client.post("/heroes/", json=hero_data) 1abcdefgh

32 data = response.json() 1abcdefgh

33 

34 assert response.status_code == 200, response.text 1abcdefgh

35 assert data["name"] == hero_data["name"] 1abcdefgh

36 assert data["secret_name"] == hero_data["secret_name"] 1abcdefgh

37 assert data["id"] is not None 1abcdefgh

38 assert data["age"] is None 1abcdefgh

39 

40 response = client.get("/heroes/") 1abcdefgh

41 data = response.json() 1abcdefgh

42 

43 assert response.status_code == 200, response.text 1abcdefgh

44 assert len(data) == 1 1abcdefgh

45 assert data[0]["name"] == hero_data["name"] 1abcdefgh

46 assert data[0]["secret_name"] == hero_data["secret_name"] 1abcdefgh

47 

48 response = client.get("/openapi.json") 1abcdefgh

49 

50 assert response.status_code == 200, response.text 1abcdefgh

51 

52 assert response.json() == { 1abcdefgh

53 "openapi": "3.1.0", 

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

55 "paths": { 

56 "/heroes/": { 

57 "get": { 

58 "summary": "Read Heroes", 

59 "operationId": "read_heroes_heroes__get", 

60 "responses": { 

61 "200": { 

62 "description": "Successful Response", 

63 "content": { 

64 "application/json": { 

65 "schema": { 

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

67 "type": "array", 

68 "items": { 

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

70 }, 

71 } 

72 } 

73 }, 

74 } 

75 }, 

76 }, 

77 "post": { 

78 "summary": "Create Hero", 

79 "operationId": "create_hero_heroes__post", 

80 "requestBody": { 

81 "content": { 

82 "application/json": { 

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

84 } 

85 }, 

86 "required": True, 

87 }, 

88 "responses": { 

89 "200": { 

90 "description": "Successful Response", 

91 "content": { 

92 "application/json": { 

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

94 } 

95 }, 

96 }, 

97 "422": { 

98 "description": "Validation Error", 

99 "content": { 

100 "application/json": { 

101 "schema": { 

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

103 } 

104 } 

105 }, 

106 }, 

107 }, 

108 }, 

109 } 

110 }, 

111 "components": { 

112 "schemas": { 

113 "HTTPValidationError": { 

114 "title": "HTTPValidationError", 

115 "type": "object", 

116 "properties": { 

117 "detail": { 

118 "title": "Detail", 

119 "type": "array", 

120 "items": { 

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

122 }, 

123 } 

124 }, 

125 }, 

126 "Hero": { 

127 "title": "Hero", 

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

129 "type": "object", 

130 "properties": { 

131 "id": { 

132 "title": "Id", 

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

134 }, 

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

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

137 "age": { 

138 "title": "Age", 

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

140 }, 

141 }, 

142 }, 

143 "ValidationError": { 

144 "title": "ValidationError", 

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

146 "type": "object", 

147 "properties": IsOneOf( 

148 { 

149 "loc": { 

150 "title": "Location", 

151 "type": "array", 

152 "items": { 

153 "anyOf": [ 

154 {"type": "string"}, 

155 {"type": "integer"}, 

156 ] 

157 }, 

158 }, 

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

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

161 }, 

162 { 

163 "loc": { 

164 "title": "Location", 

165 "type": "array", 

166 "items": { 

167 "anyOf": [ 

168 {"type": "string"}, 

169 {"type": "integer"}, 

170 ] 

171 }, 

172 }, 

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

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

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

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

177 }, 

178 ), 

179 }, 

180 } 

181 }, 

182 }