Coverage for tests/test_tutorial/test_response_model/test_tutorial003_01.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_01 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 "summary": "Create User", 

37 "operationId": "create_user_user__post", 

38 "requestBody": { 

39 "content": { 

40 "application/json": { 

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

42 } 

43 }, 

44 "required": True, 

45 }, 

46 "responses": { 

47 "200": { 

48 "description": "Successful Response", 

49 "content": { 

50 "application/json": { 

51 "schema": {"$ref": "#/components/schemas/BaseUser"} 

52 } 

53 }, 

54 }, 

55 "422": { 

56 "description": "Validation Error", 

57 "content": { 

58 "application/json": { 

59 "schema": { 

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

61 } 

62 } 

63 }, 

64 }, 

65 }, 

66 } 

67 } 

68 }, 

69 "components": { 

70 "schemas": { 

71 "BaseUser": { 

72 "title": "BaseUser", 

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 "HTTPValidationError": { 

99 "title": "HTTPValidationError", 

100 "type": "object", 

101 "properties": { 

102 "detail": { 

103 "title": "Detail", 

104 "type": "array", 

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

106 } 

107 }, 

108 }, 

109 "UserIn": { 

110 "title": "UserIn", 

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

112 "type": "object", 

113 "properties": { 

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

115 "email": { 

116 "title": "Email", 

117 "type": "string", 

118 "format": "email", 

119 }, 

120 "full_name": IsDict( 

121 { 

122 "title": "Full Name", 

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

124 } 

125 ) 

126 | IsDict( 

127 # TODO: remove when deprecating Pydantic v1 

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

129 ), 

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

131 }, 

132 }, 

133 "ValidationError": { 

134 "title": "ValidationError", 

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

136 "type": "object", 

137 "properties": { 

138 "loc": { 

139 "title": "Location", 

140 "type": "array", 

141 "items": { 

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

143 }, 

144 }, 

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

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

147 }, 

148 }, 

149 } 

150 }, 

151 }