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

25 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 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 pytest.param("tutorial006_py310"), 

12 pytest.param("tutorial006_an_py310"), 

13 ], 

14) 

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

16 mod = importlib.import_module( 1abc

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

18 ) 

19 

20 client = TestClient(mod.app) 1abc

21 return client 1abc

22 

23 

24def test_query_params_str_validations_no_query(client: TestClient): 1abdc

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

26 assert response.status_code == 422 1efg

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

28 "detail": [ 

29 { 

30 "type": "missing", 

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

32 "msg": "Field required", 

33 "input": None, 

34 } 

35 ] 

36 } 

37 

38 

39def test_query_params_str_validations_q_fixedquery(client: TestClient): 1abdc

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

41 assert response.status_code == 200 1hij

42 assert response.json() == { 1hij

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

44 "q": "fixedquery", 

45 } 

46 

47 

48def test_query_params_str_validations_q_fixedquery_too_short(client: TestClient): 1abdc

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

50 assert response.status_code == 422 1klm

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

52 "detail": [ 

53 { 

54 "type": "string_too_short", 

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

56 "msg": "String should have at least 3 characters", 

57 "input": "fa", 

58 "ctx": {"min_length": 3}, 

59 } 

60 ] 

61 } 

62 

63 

64def test_openapi_schema(client: TestClient): 1abdc

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

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

67 assert response.json() == snapshot( 1nop

68 { 

69 "openapi": "3.1.0", 

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

71 "paths": { 

72 "/items/": { 

73 "get": { 

74 "responses": { 

75 "200": { 

76 "description": "Successful Response", 

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

78 }, 

79 "422": { 

80 "description": "Validation Error", 

81 "content": { 

82 "application/json": { 

83 "schema": { 

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

85 } 

86 } 

87 }, 

88 }, 

89 }, 

90 "summary": "Read Items", 

91 "operationId": "read_items_items__get", 

92 "parameters": [ 

93 { 

94 "required": True, 

95 "schema": { 

96 "type": "string", 

97 "minLength": 3, 

98 "title": "Q", 

99 }, 

100 "name": "q", 

101 "in": "query", 

102 } 

103 ], 

104 } 

105 } 

106 }, 

107 "components": { 

108 "schemas": { 

109 "ValidationError": { 

110 "title": "ValidationError", 

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

112 "type": "object", 

113 "properties": { 

114 "loc": { 

115 "title": "Location", 

116 "type": "array", 

117 "items": { 

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

119 }, 

120 }, 

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

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

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

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

125 }, 

126 }, 

127 "HTTPValidationError": { 

128 "title": "HTTPValidationError", 

129 "type": "object", 

130 "properties": { 

131 "detail": { 

132 "title": "Detail", 

133 "type": "array", 

134 "items": { 

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

136 }, 

137 } 

138 }, 

139 }, 

140 } 

141 }, 

142 } 

143 )