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

24 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39, needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial011", 

14 pytest.param("tutorial011_py39", marks=needs_py310), 

15 pytest.param("tutorial011_py310", marks=needs_py310), 

16 "tutorial011_an", 

17 pytest.param("tutorial011_an_py39", marks=needs_py39), 

18 pytest.param("tutorial011_an_py310", marks=needs_py310), 

19 ], 

20) 

21def get_client(request: pytest.FixtureRequest): 1abcdef

22 mod = importlib.import_module( 1abcdef

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

24 ) 

25 

26 client = TestClient(mod.app) 1abcdef

27 return client 1abcdef

28 

29 

30def test_multi_query_values(client: TestClient): 1abcdef

31 url = "/items/?q=foo&q=bar" 1ghijkl

32 response = client.get(url) 1ghijkl

33 assert response.status_code == 200, response.text 1ghijkl

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

35 

36 

37def test_query_no_values(client: TestClient): 1abcdef

38 url = "/items/" 1mnopqr

39 response = client.get(url) 1mnopqr

40 assert response.status_code == 200, response.text 1mnopqr

41 assert response.json() == {"q": None} 1mnopqr

42 

43 

44def test_openapi_schema(client: TestClient): 1abcdef

45 response = client.get("/openapi.json") 1stuvwx

46 assert response.status_code == 200, response.text 1stuvwx

47 assert response.json() == { 1stuvwx

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": IsDict( 

75 { 

76 "anyOf": [ 

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

78 {"type": "null"}, 

79 ], 

80 "title": "Q", 

81 } 

82 ) 

83 | IsDict( 

84 # TODO: remove when deprecating Pydantic v1 

85 { 

86 "title": "Q", 

87 "type": "array", 

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

89 } 

90 ), 

91 "name": "q", 

92 "in": "query", 

93 } 

94 ], 

95 } 

96 } 

97 }, 

98 "components": { 

99 "schemas": { 

100 "ValidationError": { 

101 "title": "ValidationError", 

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

103 "type": "object", 

104 "properties": { 

105 "loc": { 

106 "title": "Location", 

107 "type": "array", 

108 "items": { 

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

110 }, 

111 }, 

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

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

114 }, 

115 }, 

116 "HTTPValidationError": { 

117 "title": "HTTPValidationError", 

118 "type": "object", 

119 "properties": { 

120 "detail": { 

121 "title": "Detail", 

122 "type": "array", 

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

124 } 

125 }, 

126 }, 

127 } 

128 }, 

129 }