Coverage for tests / test_multi_query_errors.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI, Query 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8@app.get("/items/") 1abcd

9def read_items(q: list[int] = Query(default=None)): 1abcd

10 return {"q": q} 1efg

11 

12 

13client = TestClient(app) 1abcd

14 

15 

16def test_multi_query(): 1abcd

17 response = client.get("/items/?q=5&q=6") 1efg

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

19 assert response.json() == {"q": [5, 6]} 1efg

20 

21 

22def test_multi_query_incorrect(): 1abcd

23 response = client.get("/items/?q=five&q=six") 1hij

24 assert response.status_code == 422, response.text 1hij

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

26 "detail": [ 

27 { 

28 "type": "int_parsing", 

29 "loc": ["query", "q", 0], 

30 "msg": "Input should be a valid integer, unable to parse string as an integer", 

31 "input": "five", 

32 }, 

33 { 

34 "type": "int_parsing", 

35 "loc": ["query", "q", 1], 

36 "msg": "Input should be a valid integer, unable to parse string as an integer", 

37 "input": "six", 

38 }, 

39 ] 

40 } 

41 

42 

43def test_openapi_schema(): 1abcd

44 response = client.get("/openapi.json") 1klm

45 assert response.status_code == 200, response.text 1klm

46 assert response.json() == snapshot( 1klm

47 { 

48 "openapi": "3.1.0", 

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

50 "paths": { 

51 "/items/": { 

52 "get": { 

53 "responses": { 

54 "200": { 

55 "description": "Successful Response", 

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

57 }, 

58 "422": { 

59 "description": "Validation Error", 

60 "content": { 

61 "application/json": { 

62 "schema": { 

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

64 } 

65 } 

66 }, 

67 }, 

68 }, 

69 "summary": "Read Items", 

70 "operationId": "read_items_items__get", 

71 "parameters": [ 

72 { 

73 "required": False, 

74 "schema": { 

75 "title": "Q", 

76 "type": "array", 

77 "items": {"type": "integer"}, 

78 }, 

79 "name": "q", 

80 "in": "query", 

81 } 

82 ], 

83 } 

84 } 

85 }, 

86 "components": { 

87 "schemas": { 

88 "ValidationError": { 

89 "title": "ValidationError", 

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

91 "type": "object", 

92 "properties": { 

93 "loc": { 

94 "title": "Location", 

95 "type": "array", 

96 "items": { 

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

98 }, 

99 }, 

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

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

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

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

104 }, 

105 }, 

106 "HTTPValidationError": { 

107 "title": "HTTPValidationError", 

108 "type": "object", 

109 "properties": { 

110 "detail": { 

111 "title": "Detail", 

112 "type": "array", 

113 "items": { 

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

115 }, 

116 } 

117 }, 

118 }, 

119 } 

120 }, 

121 } 

122 )