Coverage for tests / test_tutorial / test_response_model / test_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 fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

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

14 ], 

15) 

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

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

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_post_user(client: TestClient): 1abdc

24 user_data = { 1efg

25 "username": "foo", 

26 "password": "fighter", 

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

28 "full_name": "Grave Dohl", 

29 } 

30 response = client.post( 1efg

31 "/user/", 

32 json=user_data, 

33 ) 

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

35 assert response.json() == user_data 1efg

36 

37 

38def test_openapi_schema(client: TestClient): 1abdc

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

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

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

42 { 

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/user/": { 

47 "post": { 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

51 "content": { 

52 "application/json": { 

53 "schema": { 

54 "$ref": "#/components/schemas/UserIn" 

55 } 

56 } 

57 }, 

58 }, 

59 "422": { 

60 "description": "Validation Error", 

61 "content": { 

62 "application/json": { 

63 "schema": { 

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

65 } 

66 } 

67 }, 

68 }, 

69 }, 

70 "summary": "Create User", 

71 "operationId": "create_user_user__post", 

72 "requestBody": { 

73 "content": { 

74 "application/json": { 

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

76 } 

77 }, 

78 "required": True, 

79 }, 

80 } 

81 } 

82 }, 

83 "components": { 

84 "schemas": { 

85 "UserIn": { 

86 "title": "UserIn", 

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

88 "type": "object", 

89 "properties": { 

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

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

92 "email": { 

93 "title": "Email", 

94 "type": "string", 

95 "format": "email", 

96 }, 

97 "full_name": { 

98 "title": "Full Name", 

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

100 }, 

101 }, 

102 }, 

103 "ValidationError": { 

104 "title": "ValidationError", 

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

106 "type": "object", 

107 "properties": { 

108 "loc": { 

109 "title": "Location", 

110 "type": "array", 

111 "items": { 

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

113 }, 

114 }, 

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

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

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

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

119 }, 

120 }, 

121 "HTTPValidationError": { 

122 "title": "HTTPValidationError", 

123 "type": "object", 

124 "properties": { 

125 "detail": { 

126 "title": "Detail", 

127 "type": "array", 

128 "items": { 

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

130 }, 

131 } 

132 }, 

133 }, 

134 } 

135 }, 

136 } 

137 )