Coverage for tests / test_tutorial / test_query_params / test_tutorial004.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("tutorial004_py310", marks=needs_py310), 

14 ], 

15) 

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

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

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23@pytest.mark.parametrize( 1abdc

24 ("path", "expected_json"), 

25 [ 

26 ( 

27 "/users/123/items/foo", 

28 { 

29 "item_id": "foo", 

30 "owner_id": 123, 

31 "description": "This is an amazing item that has a long description", 

32 }, 

33 ), 

34 ( 

35 "/users/1/items/bar?q=somequery", 

36 { 

37 "item_id": "bar", 

38 "owner_id": 1, 

39 "q": "somequery", 

40 "description": "This is an amazing item that has a long description", 

41 }, 

42 ), 

43 ( 

44 "/users/42/items/baz?short=true", 

45 {"item_id": "baz", "owner_id": 42}, 

46 ), 

47 ], 

48) 

49def test_read_user_item(client: TestClient, path, expected_json): 1abdc

50 response = client.get(path) 1efg

51 assert response.status_code == 200 1efg

52 assert response.json() == expected_json 1efg

53 

54 

55def test_openapi_schema(client: TestClient): 1abdc

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

57 assert response.status_code == 200 1hij

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

59 { 

60 "openapi": "3.1.0", 

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

62 "paths": { 

63 "/users/{user_id}/items/{item_id}": { 

64 "get": { 

65 "summary": "Read User Item", 

66 "operationId": "read_user_item_users__user_id__items__item_id__get", 

67 "parameters": [ 

68 { 

69 "required": True, 

70 "schema": {"title": "User Id", "type": "integer"}, 

71 "name": "user_id", 

72 "in": "path", 

73 }, 

74 { 

75 "required": True, 

76 "schema": {"title": "Item Id", "type": "string"}, 

77 "name": "item_id", 

78 "in": "path", 

79 }, 

80 { 

81 "required": False, 

82 "schema": { 

83 "title": "Q", 

84 "anyOf": [ 

85 { 

86 "type": "string", 

87 }, 

88 { 

89 "type": "null", 

90 }, 

91 ], 

92 }, 

93 "name": "q", 

94 "in": "query", 

95 }, 

96 { 

97 "required": False, 

98 "schema": { 

99 "title": "Short", 

100 "type": "boolean", 

101 "default": False, 

102 }, 

103 "name": "short", 

104 "in": "query", 

105 }, 

106 ], 

107 "responses": { 

108 "200": { 

109 "description": "Successful Response", 

110 "content": {"application/json": {"schema": {}}}, 

111 }, 

112 "422": { 

113 "content": { 

114 "application/json": { 

115 "schema": { 

116 "$ref": "#/components/schemas/HTTPValidationError", 

117 }, 

118 }, 

119 }, 

120 "description": "Validation Error", 

121 }, 

122 }, 

123 } 

124 } 

125 }, 

126 "components": { 

127 "schemas": { 

128 "ValidationError": { 

129 "title": "ValidationError", 

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

131 "type": "object", 

132 "properties": { 

133 "loc": { 

134 "title": "Location", 

135 "type": "array", 

136 "items": { 

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

138 }, 

139 }, 

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

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

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

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

144 }, 

145 }, 

146 "HTTPValidationError": { 

147 "title": "HTTPValidationError", 

148 "type": "object", 

149 "properties": { 

150 "detail": { 

151 "title": "Detail", 

152 "type": "array", 

153 "items": { 

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

155 }, 

156 } 

157 }, 

158 }, 

159 } 

160 }, 

161 } 

162 )