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-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

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): 1abdc

18 mod = importlib.import_module( 1abc

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

20 ) 

21 

22 client = TestClient(mod.app) 1abc

23 return client 1abc

24 

25 

26def test_multi_query_values(client: TestClient): 1abdc

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

28 response = client.get(url) 1efg

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

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

31 

32 

33def test_query_no_values(client: TestClient): 1abdc

34 url = "/items/" 1hij

35 response = client.get(url) 1hij

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

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

38 

39 

40def test_openapi_schema(client: TestClient): 1abdc

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

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

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

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 )