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

26 statements  

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

1from dirty_equals import IsDict 1ghijkl

2from fastapi.testclient import TestClient 1ghijkl

3from sqlmodel import create_engine 1ghijkl

4from sqlmodel.pool import StaticPool 1ghijkl

5 

6 

7def test_tutorial(clear_sqlmodel): 1ghijkl

8 from docs_src.tutorial.fastapi.response_model import tutorial001 as mod 1abcdef

9 

10 mod.sqlite_url = "sqlite://" 1abcdef

11 mod.engine = create_engine( 1abcdef

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

13 ) 

14 

15 with TestClient(mod.app) as client: 1abcdef

16 hero_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdef

17 response = client.post("/heroes/", json=hero_data) 1abcdef

18 data = response.json() 1abcdef

19 

20 assert response.status_code == 200, response.text 1abcdef

21 assert data["name"] == hero_data["name"] 1abcdef

22 assert data["secret_name"] == hero_data["secret_name"] 1abcdef

23 assert data["id"] is not None 1abcdef

24 assert data["age"] is None 1abcdef

25 

26 response = client.get("/heroes/") 1abcdef

27 data = response.json() 1abcdef

28 

29 assert response.status_code == 200, response.text 1abcdef

30 assert len(data) == 1 1abcdef

31 assert data[0]["name"] == hero_data["name"] 1abcdef

32 assert data[0]["secret_name"] == hero_data["secret_name"] 1abcdef

33 

34 response = client.get("/openapi.json") 1abcdef

35 

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

37 

38 assert response.json() == { 1abcdef

39 "openapi": "3.1.0", 

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

41 "paths": { 

42 "/heroes/": { 

43 "get": { 

44 "summary": "Read Heroes", 

45 "operationId": "read_heroes_heroes__get", 

46 "responses": { 

47 "200": { 

48 "description": "Successful Response", 

49 "content": { 

50 "application/json": { 

51 "schema": { 

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

53 "type": "array", 

54 "items": { 

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

56 }, 

57 } 

58 } 

59 }, 

60 } 

61 }, 

62 }, 

63 "post": { 

64 "summary": "Create Hero", 

65 "operationId": "create_hero_heroes__post", 

66 "requestBody": { 

67 "content": { 

68 "application/json": { 

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

70 } 

71 }, 

72 "required": True, 

73 }, 

74 "responses": { 

75 "200": { 

76 "description": "Successful Response", 

77 "content": { 

78 "application/json": { 

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

80 } 

81 }, 

82 }, 

83 "422": { 

84 "description": "Validation Error", 

85 "content": { 

86 "application/json": { 

87 "schema": { 

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

89 } 

90 } 

91 }, 

92 }, 

93 }, 

94 }, 

95 } 

96 }, 

97 "components": { 

98 "schemas": { 

99 "HTTPValidationError": { 

100 "title": "HTTPValidationError", 

101 "type": "object", 

102 "properties": { 

103 "detail": { 

104 "title": "Detail", 

105 "type": "array", 

106 "items": { 

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

108 }, 

109 } 

110 }, 

111 }, 

112 "Hero": { 

113 "title": "Hero", 

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

115 "type": "object", 

116 "properties": { 

117 "id": IsDict( 

118 { 

119 "title": "Id", 

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

121 } 

122 ) 

123 | IsDict( 

124 # TODO: remove when deprecating Pydantic v1 

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

126 ), 

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

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

129 "age": IsDict( 

130 { 

131 "title": "Age", 

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

133 } 

134 ) 

135 | IsDict( 

136 # TODO: remove when deprecating Pydantic v1 

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

138 ), 

139 }, 

140 }, 

141 "ValidationError": { 

142 "title": "ValidationError", 

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

144 "type": "object", 

145 "properties": { 

146 "loc": { 

147 "title": "Location", 

148 "type": "array", 

149 "items": { 

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

151 }, 

152 }, 

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

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

155 }, 

156 }, 

157 } 

158 }, 

159 }