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

30 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 pytest.param("tutorial002_an_py310", marks=needs_py310), 

15 ], 

16) 

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

18 mod = importlib.import_module( 1abc

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

20 ) 

21 

22 client = TestClient(mod.app) 1abc

23 return client 1abc

24 

25 

26def test_query_params_str_validations_no_query(client: TestClient): 1abdc

27 response = client.get("/items/") 1efg

28 assert response.status_code == 200 1efg

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

30 

31 

32def test_query_params_str_validations_q_empty_str(client: TestClient): 1abdc

33 response = client.get("/items/", params={"q": ""}) 1hij

34 assert response.status_code == 200 1hij

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

36 

37 

38def test_query_params_str_validations_q_query(client: TestClient): 1abdc

39 response = client.get("/items/", params={"q": "query"}) 1klm

40 assert response.status_code == 200 1klm

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

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

43 "q": "query", 

44 } 

45 

46 

47def test_query_params_str_validations_q_too_long(client: TestClient): 1abdc

48 response = client.get("/items/", params={"q": "q" * 51}) 1nop

49 assert response.status_code == 422 1nop

50 assert response.json() == { 1nop

51 "detail": [ 

52 { 

53 "type": "string_too_long", 

54 "loc": ["query", "q"], 

55 "msg": "String should have at most 50 characters", 

56 "input": "q" * 51, 

57 "ctx": {"max_length": 50}, 

58 } 

59 ] 

60 } 

61 

62 

63def test_openapi_schema(client: TestClient): 1abdc

64 response = client.get("/openapi.json") 1qrs

65 assert response.status_code == 200, response.text 1qrs

66 assert response.json() == snapshot( 1qrs

67 { 

68 "openapi": "3.1.0", 

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

70 "paths": { 

71 "/items/": { 

72 "get": { 

73 "responses": { 

74 "200": { 

75 "description": "Successful Response", 

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

77 }, 

78 "422": { 

79 "description": "Validation Error", 

80 "content": { 

81 "application/json": { 

82 "schema": { 

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

84 } 

85 } 

86 }, 

87 }, 

88 }, 

89 "summary": "Read Items", 

90 "operationId": "read_items_items__get", 

91 "parameters": [ 

92 { 

93 "required": False, 

94 "schema": { 

95 "anyOf": [ 

96 { 

97 "type": "string", 

98 "maxLength": 50, 

99 }, 

100 {"type": "null"}, 

101 ], 

102 "title": "Q", 

103 }, 

104 "name": "q", 

105 "in": "query", 

106 } 

107 ], 

108 } 

109 } 

110 }, 

111 "components": { 

112 "schemas": { 

113 "ValidationError": { 

114 "title": "ValidationError", 

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

116 "type": "object", 

117 "properties": { 

118 "loc": { 

119 "title": "Location", 

120 "type": "array", 

121 "items": { 

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

123 }, 

124 }, 

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

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

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

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

129 }, 

130 }, 

131 "HTTPValidationError": { 

132 "title": "HTTPValidationError", 

133 "type": "object", 

134 "properties": { 

135 "detail": { 

136 "title": "Detail", 

137 "type": "array", 

138 "items": { 

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

140 }, 

141 } 

142 }, 

143 }, 

144 } 

145 }, 

146 } 

147 )