Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial010.py: 100%

32 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._compat import PYDANTIC_VERSION_MINOR_TUPLE 1abdc

5from fastapi.testclient import TestClient 1abdc

6from inline_snapshot import Is, snapshot 1abdc

7 

8from ...utils import needs_py310 1abdc

9 

10 

11@pytest.fixture( 1abdc

12 name="client", 

13 params=[ 

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

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

16 ], 

17) 

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

19 mod = importlib.import_module( 1abc

20 f"docs_src.query_params_str_validations.{request.param}" 

21 ) 

22 

23 client = TestClient(mod.app) 1abc

24 return client 1abc

25 

26 

27def test_query_params_str_validations_no_query(client: TestClient): 1abdc

28 response = client.get("/items/") 1hij

29 assert response.status_code == 200 1hij

30 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1hij

31 

32 

33def test_query_params_str_validations_item_query_fixedquery(client: TestClient): 1abdc

34 response = client.get("/items/", params={"item-query": "fixedquery"}) 1klm

35 assert response.status_code == 200 1klm

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

37 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], 

38 "q": "fixedquery", 

39 } 

40 

41 

42def test_query_params_str_validations_q_fixedquery(client: TestClient): 1abdc

43 response = client.get("/items/", params={"q": "fixedquery"}) 1nop

44 assert response.status_code == 200 1nop

45 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1nop

46 

47 

48def test_query_params_str_validations_item_query_nonregexquery(client: TestClient): 1abdc

49 response = client.get("/items/", params={"item-query": "nonregexquery"}) 1qrs

50 assert response.status_code == 422 1qrs

51 assert response.json() == { 1qrs

52 "detail": [ 

53 { 

54 "type": "string_pattern_mismatch", 

55 "loc": ["query", "item-query"], 

56 "msg": "String should match pattern '^fixedquery$'", 

57 "input": "nonregexquery", 

58 "ctx": {"pattern": "^fixedquery$"}, 

59 } 

60 ] 

61 } 

62 

63 

64def test_openapi_schema(client: TestClient): 1abdc

65 response = client.get("/openapi.json") 1efg

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

67 

68 parameters_schema = { 1efg

69 "anyOf": [ 

70 { 

71 "type": "string", 

72 "minLength": 3, 

73 "maxLength": 50, 

74 "pattern": "^fixedquery$", 

75 }, 

76 {"type": "null"}, 

77 ], 

78 "title": "Query string", 

79 "description": "Query string for the items to search in the database that have a good match", 

80 # See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34. 

81 **({"deprecated": True} if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10) else {}), 

82 } 

83 

84 assert response.json() == snapshot( 1efg

85 { 

86 "openapi": "3.1.0", 

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

88 "paths": { 

89 "/items/": { 

90 "get": { 

91 "responses": { 

92 "200": { 

93 "description": "Successful Response", 

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

95 }, 

96 "422": { 

97 "description": "Validation Error", 

98 "content": { 

99 "application/json": { 

100 "schema": { 

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

102 } 

103 } 

104 }, 

105 }, 

106 }, 

107 "summary": "Read Items", 

108 "operationId": "read_items_items__get", 

109 "parameters": [ 

110 { 

111 "description": "Query string for the items to search in the database that have a good match", 

112 "required": False, 

113 "deprecated": True, 

114 "schema": Is(parameters_schema), 

115 "name": "item-query", 

116 "in": "query", 

117 } 

118 ], 

119 } 

120 } 

121 }, 

122 "components": { 

123 "schemas": { 

124 "ValidationError": { 

125 "title": "ValidationError", 

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

127 "type": "object", 

128 "properties": { 

129 "loc": { 

130 "title": "Location", 

131 "type": "array", 

132 "items": { 

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

134 }, 

135 }, 

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

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

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

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

140 }, 

141 }, 

142 "HTTPValidationError": { 

143 "title": "HTTPValidationError", 

144 "type": "object", 

145 "properties": { 

146 "detail": { 

147 "title": "Detail", 

148 "type": "array", 

149 "items": { 

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

151 }, 

152 } 

153 }, 

154 }, 

155 } 

156 }, 

157 } 

158 )