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

28 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from dirty_equals import IsDict 1idefgh

2from fastapi.testclient import TestClient 1idefgh

3from sqlmodel import create_engine 1idefgh

4from sqlmodel.pool import StaticPool 1idefgh

5 

6from ....conftest import needs_py310 1idefgh

7 

8 

9@needs_py310 1idefgh

10def test_tutorial(clear_sqlmodel): 1defgh

11 from docs_src.tutorial.fastapi.response_model import tutorial001_py310 as mod 1abc

12 

13 mod.sqlite_url = "sqlite://" 1abc

14 mod.engine = create_engine( 1abc

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

16 ) 

17 

18 with TestClient(mod.app) as client: 1abc

19 hero_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abc

20 response = client.post("/heroes/", json=hero_data) 1abc

21 data = response.json() 1abc

22 

23 assert response.status_code == 200, response.text 1abc

24 assert data["name"] == hero_data["name"] 1abc

25 assert data["secret_name"] == hero_data["secret_name"] 1abc

26 assert data["id"] is not None 1abc

27 assert data["age"] is None 1abc

28 

29 response = client.get("/heroes/") 1abc

30 data = response.json() 1abc

31 

32 assert response.status_code == 200, response.text 1abc

33 assert len(data) == 1 1abc

34 assert data[0]["name"] == hero_data["name"] 1abc

35 assert data[0]["secret_name"] == hero_data["secret_name"] 1abc

36 

37 response = client.get("/openapi.json") 1abc

38 

39 assert response.status_code == 200, response.text 1abc

40 

41 assert response.json() == { 1abc

42 "openapi": "3.1.0", 

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

44 "paths": { 

45 "/heroes/": { 

46 "get": { 

47 "summary": "Read Heroes", 

48 "operationId": "read_heroes_heroes__get", 

49 "responses": { 

50 "200": { 

51 "description": "Successful Response", 

52 "content": { 

53 "application/json": { 

54 "schema": { 

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

56 "type": "array", 

57 "items": { 

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

59 }, 

60 } 

61 } 

62 }, 

63 } 

64 }, 

65 }, 

66 "post": { 

67 "summary": "Create Hero", 

68 "operationId": "create_hero_heroes__post", 

69 "requestBody": { 

70 "content": { 

71 "application/json": { 

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

73 } 

74 }, 

75 "required": True, 

76 }, 

77 "responses": { 

78 "200": { 

79 "description": "Successful Response", 

80 "content": { 

81 "application/json": { 

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

83 } 

84 }, 

85 }, 

86 "422": { 

87 "description": "Validation Error", 

88 "content": { 

89 "application/json": { 

90 "schema": { 

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

92 } 

93 } 

94 }, 

95 }, 

96 }, 

97 }, 

98 } 

99 }, 

100 "components": { 

101 "schemas": { 

102 "HTTPValidationError": { 

103 "title": "HTTPValidationError", 

104 "type": "object", 

105 "properties": { 

106 "detail": { 

107 "title": "Detail", 

108 "type": "array", 

109 "items": { 

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

111 }, 

112 } 

113 }, 

114 }, 

115 "Hero": { 

116 "title": "Hero", 

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

118 "type": "object", 

119 "properties": { 

120 "id": IsDict( 

121 { 

122 "title": "Id", 

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

124 } 

125 ) 

126 | IsDict( 

127 # TODO: remove when deprecating Pydantic v1 

128 {"title": "Id", "type": "integer"} 

129 ), 

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

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

132 "age": IsDict( 

133 { 

134 "title": "Age", 

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

136 } 

137 ) 

138 | IsDict( 

139 # TODO: remove when deprecating Pydantic v1 

140 {"title": "Age", "type": "integer"} 

141 ), 

142 }, 

143 }, 

144 "ValidationError": { 

145 "title": "ValidationError", 

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

147 "type": "object", 

148 "properties": { 

149 "loc": { 

150 "title": "Location", 

151 "type": "array", 

152 "items": { 

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

154 }, 

155 }, 

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

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

158 }, 

159 }, 

160 } 

161 }, 

162 }