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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1deabc

2from dirty_equals import IsDict, IsOneOf 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

8@pytest.fixture(name="client") 1deabc

9def get_client(): 1deabc

10 from docs_src.response_model.tutorial003_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17def test_post_user(client: TestClient): 1deabc

18 response = client.post( 1abc

19 "/user/", 

20 json={ 

21 "username": "foo", 

22 "password": "fighter", 

23 "email": "foo@example.com", 

24 "full_name": "Grave Dohl", 

25 }, 

26 ) 

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

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

29 "username": "foo", 

30 "email": "foo@example.com", 

31 "full_name": "Grave Dohl", 

32 } 

33 

34 

35@needs_py310 1deabc

36def test_openapi_schema(client: TestClient): 1deabc

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

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

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

40 "openapi": "3.1.0", 

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

42 "paths": { 

43 "/user/": { 

44 "post": { 

45 "responses": { 

46 "200": { 

47 "description": "Successful Response", 

48 "content": { 

49 "application/json": { 

50 "schema": {"$ref": "#/components/schemas/UserOut"} 

51 } 

52 }, 

53 }, 

54 "422": { 

55 "description": "Validation Error", 

56 "content": { 

57 "application/json": { 

58 "schema": { 

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

60 } 

61 } 

62 }, 

63 }, 

64 }, 

65 "summary": "Create User", 

66 "operationId": "create_user_user__post", 

67 "requestBody": { 

68 "content": { 

69 "application/json": { 

70 "schema": {"$ref": "#/components/schemas/UserIn"} 

71 } 

72 }, 

73 "required": True, 

74 }, 

75 } 

76 } 

77 }, 

78 "components": { 

79 "schemas": { 

80 "UserOut": { 

81 "title": "UserOut", 

82 "required": IsOneOf( 

83 ["username", "email", "full_name"], 

84 # TODO: remove when deprecating Pydantic v1 

85 ["username", "email"], 

86 ), 

87 "type": "object", 

88 "properties": { 

89 "username": {"title": "Username", "type": "string"}, 

90 "email": { 

91 "title": "Email", 

92 "type": "string", 

93 "format": "email", 

94 }, 

95 "full_name": IsDict( 

96 { 

97 "title": "Full Name", 

98 "anyOf": [{"type": "string"}, {"type": "null"}], 

99 } 

100 ) 

101 | IsDict( 

102 # TODO: remove when deprecating Pydantic v1 

103 {"title": "Full Name", "type": "string"} 

104 ), 

105 }, 

106 }, 

107 "UserIn": { 

108 "title": "UserIn", 

109 "required": ["username", "password", "email"], 

110 "type": "object", 

111 "properties": { 

112 "username": {"title": "Username", "type": "string"}, 

113 "password": {"title": "Password", "type": "string"}, 

114 "email": { 

115 "title": "Email", 

116 "type": "string", 

117 "format": "email", 

118 }, 

119 "full_name": IsDict( 

120 { 

121 "title": "Full Name", 

122 "anyOf": [{"type": "string"}, {"type": "null"}], 

123 } 

124 ) 

125 | IsDict( 

126 # TODO: remove when deprecating Pydantic v1 

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

128 ), 

129 }, 

130 }, 

131 "ValidationError": { 

132 "title": "ValidationError", 

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

134 "type": "object", 

135 "properties": { 

136 "loc": { 

137 "title": "Location", 

138 "type": "array", 

139 "items": { 

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

141 }, 

142 }, 

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

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

145 }, 

146 }, 

147 "HTTPValidationError": { 

148 "title": "HTTPValidationError", 

149 "type": "object", 

150 "properties": { 

151 "detail": { 

152 "title": "Detail", 

153 "type": "array", 

154 "items": {"$ref": "#/components/schemas/ValidationError"}, 

155 } 

156 }, 

157 }, 

158 } 

159 }, 

160 }