Coverage for tests / test_tutorial / test_extra_models / test_tutorial001_tutorial002.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from dirty_equals import IsList 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import snapshot 1abdc

7 

8from ...utils import needs_py310 1abdc

9 

10 

11@pytest.fixture( 1abdc

12 name="client", 

13 params=[ 

14 pytest.param("tutorial001_py310", marks=needs_py310), 

15 pytest.param("tutorial002_py310", marks=needs_py310), 

16 ], 

17) 

18def get_client(request: pytest.FixtureRequest): 1abdc

19 mod = importlib.import_module(f"docs_src.extra_models.{request.param}") 1abc

20 

21 client = TestClient(mod.app) 1abc

22 return client 1abc

23 

24 

25def test_post(client: TestClient): 1abdc

26 response = client.post( 1efg

27 "/user/", 

28 json={ 

29 "username": "johndoe", 

30 "password": "secret", 

31 "email": "johndoe@example.com", 

32 "full_name": "John Doe", 

33 }, 

34 ) 

35 assert response.status_code == 200, response.text 1efg

36 assert response.json() == { 1efg

37 "username": "johndoe", 

38 "email": "johndoe@example.com", 

39 "full_name": "John Doe", 

40 } 

41 

42 

43def test_openapi_schema(client: TestClient): 1abdc

44 response = client.get("/openapi.json") 1hij

45 assert response.status_code == 200, response.text 1hij

46 assert response.json() == snapshot( 1hij

47 { 

48 "openapi": "3.1.0", 

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

50 "paths": { 

51 "/user/": { 

52 "post": { 

53 "responses": { 

54 "200": { 

55 "description": "Successful Response", 

56 "content": { 

57 "application/json": { 

58 "schema": { 

59 "$ref": "#/components/schemas/UserOut", 

60 } 

61 } 

62 }, 

63 }, 

64 "422": { 

65 "description": "Validation Error", 

66 "content": { 

67 "application/json": { 

68 "schema": { 

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

70 } 

71 } 

72 }, 

73 }, 

74 }, 

75 "summary": "Create User", 

76 "operationId": "create_user_user__post", 

77 "requestBody": { 

78 "content": { 

79 "application/json": { 

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

81 } 

82 }, 

83 "required": True, 

84 }, 

85 } 

86 } 

87 }, 

88 "components": { 

89 "schemas": { 

90 "UserIn": { 

91 "title": "UserIn", 

92 "required": IsList( 

93 "username", "password", "email", check_order=False 

94 ), 

95 "type": "object", 

96 "properties": { 

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

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

99 "email": { 

100 "title": "Email", 

101 "type": "string", 

102 "format": "email", 

103 }, 

104 "full_name": { 

105 "title": "Full Name", 

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

107 }, 

108 }, 

109 }, 

110 "UserOut": { 

111 "title": "UserOut", 

112 "required": ["username", "email"], 

113 "type": "object", 

114 "properties": { 

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

116 "email": { 

117 "title": "Email", 

118 "type": "string", 

119 "format": "email", 

120 }, 

121 "full_name": { 

122 "title": "Full Name", 

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

124 }, 

125 }, 

126 }, 

127 "ValidationError": { 

128 "title": "ValidationError", 

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

130 "type": "object", 

131 "properties": { 

132 "loc": { 

133 "title": "Location", 

134 "type": "array", 

135 "items": { 

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

137 }, 

138 }, 

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

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

141 "input": {"title": "Input"}, 

142 "ctx": {"title": "Context", "type": "object"}, 

143 }, 

144 }, 

145 "HTTPValidationError": { 

146 "title": "HTTPValidationError", 

147 "type": "object", 

148 "properties": { 

149 "detail": { 

150 "title": "Detail", 

151 "type": "array", 

152 "items": { 

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

154 }, 

155 } 

156 }, 

157 }, 

158 } 

159 }, 

160 } 

161 )