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

26 statements  

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

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.query_params_str_validations.tutorial011_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17def test_multi_query_values(client: TestClient): 1eabcd

18 url = "/items/?q=foo&q=bar" 1abcd

19 response = client.get(url) 1abcd

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

21 assert response.json() == {"q": ["foo", "bar"]} 1abcd

22 

23 

24@needs_py39 1eabcd

25def test_query_no_values(client: TestClient): 1eabcd

26 url = "/items/" 1abcd

27 response = client.get(url) 1abcd

28 assert response.status_code == 200, response.text 1abcd

29 assert response.json() == {"q": None} 1abcd

30 

31 

32@needs_py39 1eabcd

33def test_openapi_schema(client: TestClient): 1eabcd

34 response = client.get("/openapi.json") 1abcd

35 assert response.status_code == 200, response.text 1abcd

36 assert response.json() == { 1abcd

37 "openapi": "3.1.0", 

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

39 "paths": { 

40 "/items/": { 

41 "get": { 

42 "responses": { 

43 "200": { 

44 "description": "Successful Response", 

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

46 }, 

47 "422": { 

48 "description": "Validation Error", 

49 "content": { 

50 "application/json": { 

51 "schema": { 

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

53 } 

54 } 

55 }, 

56 }, 

57 }, 

58 "summary": "Read Items", 

59 "operationId": "read_items_items__get", 

60 "parameters": [ 

61 { 

62 "required": False, 

63 "schema": IsDict( 

64 { 

65 "anyOf": [ 

66 {"type": "array", "items": {"type": "string"}}, 

67 {"type": "null"}, 

68 ], 

69 "title": "Q", 

70 } 

71 ) 

72 | IsDict( 

73 # TODO: remove when deprecating Pydantic v1 

74 { 

75 "title": "Q", 

76 "type": "array", 

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

78 } 

79 ), 

80 "name": "q", 

81 "in": "query", 

82 } 

83 ], 

84 } 

85 } 

86 }, 

87 "components": { 

88 "schemas": { 

89 "ValidationError": { 

90 "title": "ValidationError", 

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

92 "type": "object", 

93 "properties": { 

94 "loc": { 

95 "title": "Location", 

96 "type": "array", 

97 "items": { 

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

99 }, 

100 }, 

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

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

103 }, 

104 }, 

105 "HTTPValidationError": { 

106 "title": "HTTPValidationError", 

107 "type": "object", 

108 "properties": { 

109 "detail": { 

110 "title": "Detail", 

111 "type": "array", 

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

113 } 

114 }, 

115 }, 

116 } 

117 }, 

118 }