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

12 statements  

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

1from dirty_equals import IsDict, IsOneOf 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.response_model.tutorial003 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_post_user(): 1abcde

10 response = client.post( 1abcde

11 "/user/", 

12 json={ 

13 "username": "foo", 

14 "password": "fighter", 

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

16 "full_name": "Grave Dohl", 

17 }, 

18 ) 

19 assert response.status_code == 200, response.text 1abcde

20 assert response.json() == { 1abcde

21 "username": "foo", 

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

23 "full_name": "Grave Dohl", 

24 } 

25 

26 

27def test_openapi_schema(): 1abcde

28 response = client.get("/openapi.json") 1abcde

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

30 assert response.json() == { 1abcde

31 "openapi": "3.1.0", 

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

33 "paths": { 

34 "/user/": { 

35 "post": { 

36 "responses": { 

37 "200": { 

38 "description": "Successful Response", 

39 "content": { 

40 "application/json": { 

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

42 } 

43 }, 

44 }, 

45 "422": { 

46 "description": "Validation Error", 

47 "content": { 

48 "application/json": { 

49 "schema": { 

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

51 } 

52 } 

53 }, 

54 }, 

55 }, 

56 "summary": "Create User", 

57 "operationId": "create_user_user__post", 

58 "requestBody": { 

59 "content": { 

60 "application/json": { 

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

62 } 

63 }, 

64 "required": True, 

65 }, 

66 } 

67 } 

68 }, 

69 "components": { 

70 "schemas": { 

71 "UserOut": { 

72 "title": "UserOut", 

73 "required": IsOneOf( 

74 ["username", "email", "full_name"], 

75 # TODO: remove when deprecating Pydantic v1 

76 ["username", "email"], 

77 ), 

78 "type": "object", 

79 "properties": { 

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

81 "email": { 

82 "title": "Email", 

83 "type": "string", 

84 "format": "email", 

85 }, 

86 "full_name": IsDict( 

87 { 

88 "title": "Full Name", 

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

90 } 

91 ) 

92 | IsDict( 

93 # TODO: remove when deprecating Pydantic v1 

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

95 ), 

96 }, 

97 }, 

98 "UserIn": { 

99 "title": "UserIn", 

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

101 "type": "object", 

102 "properties": { 

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

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

105 "email": { 

106 "title": "Email", 

107 "type": "string", 

108 "format": "email", 

109 }, 

110 "full_name": IsDict( 

111 { 

112 "title": "Full Name", 

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

114 } 

115 ) 

116 | IsDict( 

117 # TODO: remove when deprecating Pydantic v1 

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

119 ), 

120 }, 

121 }, 

122 "ValidationError": { 

123 "title": "ValidationError", 

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

125 "type": "object", 

126 "properties": { 

127 "loc": { 

128 "title": "Location", 

129 "type": "array", 

130 "items": { 

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

132 }, 

133 }, 

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

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

136 }, 

137 }, 

138 "HTTPValidationError": { 

139 "title": "HTTPValidationError", 

140 "type": "object", 

141 "properties": { 

142 "detail": { 

143 "title": "Detail", 

144 "type": "array", 

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

146 } 

147 }, 

148 }, 

149 } 

150 }, 

151 }