Coverage for tests/test_multi_query_errors.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import List 1abcde

2 

3from dirty_equals import IsDict 1abcde

4from fastapi import FastAPI, Query 1abcde

5from fastapi.testclient import TestClient 1abcde

6 

7app = FastAPI() 1abcde

8 

9 

10@app.get("/items/") 1abcde

11def read_items(q: List[int] = Query(default=None)): 1abcde

12 return {"q": q} 1abcde

13 

14 

15client = TestClient(app) 1abcde

16 

17 

18def test_multi_query(): 1abcde

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

20 assert response.status_code == 200, response.text 1abcde

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

22 

23 

24def test_multi_query_incorrect(): 1abcde

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

26 assert response.status_code == 422, response.text 1abcde

27 assert response.json() == IsDict( 1abcde

28 { 

29 "detail": [ 

30 { 

31 "type": "int_parsing", 

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

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

34 "input": "five", 

35 }, 

36 { 

37 "type": "int_parsing", 

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

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

40 "input": "six", 

41 }, 

42 ] 

43 } 

44 ) | IsDict( 

45 # TODO: remove when deprecating Pydantic v1 

46 { 

47 "detail": [ 

48 { 

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

50 "msg": "value is not a valid integer", 

51 "type": "type_error.integer", 

52 }, 

53 { 

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

55 "msg": "value is not a valid integer", 

56 "type": "type_error.integer", 

57 }, 

58 ] 

59 } 

60 ) 

61 

62 

63def test_openapi_schema(): 1abcde

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

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

66 assert response.json() == { 1abcde

67 "openapi": "3.1.0", 

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

69 "paths": { 

70 "/items/": { 

71 "get": { 

72 "responses": { 

73 "200": { 

74 "description": "Successful Response", 

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

76 }, 

77 "422": { 

78 "description": "Validation Error", 

79 "content": { 

80 "application/json": { 

81 "schema": { 

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

83 } 

84 } 

85 }, 

86 }, 

87 }, 

88 "summary": "Read Items", 

89 "operationId": "read_items_items__get", 

90 "parameters": [ 

91 { 

92 "required": False, 

93 "schema": { 

94 "title": "Q", 

95 "type": "array", 

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

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 }, 

122 }, 

123 "HTTPValidationError": { 

124 "title": "HTTPValidationError", 

125 "type": "object", 

126 "properties": { 

127 "detail": { 

128 "title": "Detail", 

129 "type": "array", 

130 "items": {"$ref": "#/components/schemas/ValidationError"}, 

131 } 

132 }, 

133 }, 

134 } 

135 }, 

136 }