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

26 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("tutorial007_py310", marks=needs_py310), 

14 pytest.param("tutorial007_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_fixedquery(client: TestClient): 1abdc

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

34 assert response.status_code == 200 1hij

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

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

37 "q": "fixedquery", 

38 } 

39 

40 

41def test_query_params_str_validations_q_fixedquery_too_short(client: TestClient): 1abdc

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

43 assert response.status_code == 422 1klm

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

45 "detail": [ 

46 { 

47 "type": "string_too_short", 

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

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

50 "input": "fa", 

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

52 } 

53 ] 

54 } 

55 

56 

57def test_openapi_schema(client: TestClient): 1abdc

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

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

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

61 { 

62 "openapi": "3.1.0", 

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

64 "paths": { 

65 "/items/": { 

66 "get": { 

67 "responses": { 

68 "200": { 

69 "description": "Successful Response", 

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

71 }, 

72 "422": { 

73 "description": "Validation Error", 

74 "content": { 

75 "application/json": { 

76 "schema": { 

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

78 } 

79 } 

80 }, 

81 }, 

82 }, 

83 "summary": "Read Items", 

84 "operationId": "read_items_items__get", 

85 "parameters": [ 

86 { 

87 "required": False, 

88 "schema": { 

89 "anyOf": [ 

90 { 

91 "type": "string", 

92 "minLength": 3, 

93 }, 

94 {"type": "null"}, 

95 ], 

96 "title": "Query string", 

97 }, 

98 "name": "q", 

99 "in": "query", 

100 } 

101 ], 

102 } 

103 } 

104 }, 

105 "components": { 

106 "schemas": { 

107 "ValidationError": { 

108 "title": "ValidationError", 

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

110 "type": "object", 

111 "properties": { 

112 "loc": { 

113 "title": "Location", 

114 "type": "array", 

115 "items": { 

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

117 }, 

118 }, 

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

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

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

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

123 }, 

124 }, 

125 "HTTPValidationError": { 

126 "title": "HTTPValidationError", 

127 "type": "object", 

128 "properties": { 

129 "detail": { 

130 "title": "Detail", 

131 "type": "array", 

132 "items": { 

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

134 }, 

135 } 

136 }, 

137 }, 

138 } 

139 }, 

140 } 

141 )