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

24 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7from ...utils import needs_py310 1abcd

8 

9 

10@pytest.fixture( 1abcd

11 name="client", 

12 params=[ 

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

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

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abcd

18 mod = importlib.import_module( 1tuvwxyzABCDEFGHIJKLM

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

20 ) 

21 

22 client = TestClient(mod.app) 1tuvwxyzABCDEFGHIJKLM

23 return client 1tuvwxyzABCDEFGHIJKLM

24 

25 

26def test_multi_query_values(client: TestClient): 1abcd

27 url = "/items/?q=foo&q=bar" 1efghijkl

28 response = client.get(url) 1efghijkl

29 assert response.status_code == 200, response.text 1efghijkl

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

31 

32 

33def test_query_no_values(client: TestClient): 1abcd

34 url = "/items/" 1mnopqrs

35 response = client.get(url) 1mnopqrs

36 assert response.status_code == 200, response.text 1mnopqrs

37 assert response.json() == {"q": None} 1mnopqrs

38 

39 

40def test_openapi_schema(client: TestClient): 1abcd

41 response = client.get("/openapi.json") 1NOPQRST

42 assert response.status_code == 200, response.text 1NOPQRST

43 assert response.json() == snapshot( 1NOPQRST

44 { 

45 "openapi": "3.1.0", 

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

47 "paths": { 

48 "/items/": { 

49 "get": { 

50 "responses": { 

51 "200": { 

52 "description": "Successful Response", 

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

54 }, 

55 "422": { 

56 "description": "Validation Error", 

57 "content": { 

58 "application/json": { 

59 "schema": { 

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

61 } 

62 } 

63 }, 

64 }, 

65 }, 

66 "summary": "Read Items", 

67 "operationId": "read_items_items__get", 

68 "parameters": [ 

69 { 

70 "required": False, 

71 "schema": { 

72 "anyOf": [ 

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

74 {"type": "null"}, 

75 ], 

76 "title": "Q", 

77 }, 

78 "name": "q", 

79 "in": "query", 

80 } 

81 ], 

82 } 

83 } 

84 }, 

85 "components": { 

86 "schemas": { 

87 "ValidationError": { 

88 "title": "ValidationError", 

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

90 "type": "object", 

91 "properties": { 

92 "loc": { 

93 "title": "Location", 

94 "type": "array", 

95 "items": { 

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

97 }, 

98 }, 

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

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

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

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

103 }, 

104 }, 

105 "HTTPValidationError": { 

106 "title": "HTTPValidationError", 

107 "type": "object", 

108 "properties": { 

109 "detail": { 

110 "title": "Detail", 

111 "type": "array", 

112 "items": { 

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

114 }, 

115 } 

116 }, 

117 }, 

118 } 

119 }, 

120 } 

121 )