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

23 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 

7 

8@pytest.fixture( 1abcd

9 name="client", 

10 params=[ 

11 pytest.param("tutorial012_py310"), 

12 pytest.param("tutorial012_an_py310"), 

13 ], 

14) 

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

16 mod = importlib.import_module( 1stuvwxyzABCDEFGHIJK

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

18 ) 

19 

20 client = TestClient(mod.app) 1stuvwxyzABCDEFGHIJK

21 return client 1stuvwxyzABCDEFGHIJK

22 

23 

24def test_default_query_values(client: TestClient): 1abcd

25 url = "/items/" 1efghijk

26 response = client.get(url) 1efghijk

27 assert response.status_code == 200, response.text 1efghijk

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

29 

30 

31def test_multi_query_values(client: TestClient): 1abcd

32 url = "/items/?q=baz&q=foobar" 1lmnopqr

33 response = client.get(url) 1lmnopqr

34 assert response.status_code == 200, response.text 1lmnopqr

35 assert response.json() == {"q": ["baz", "foobar"]} 1lmnopqr

36 

37 

38def test_openapi_schema(client: TestClient): 1abcd

39 response = client.get("/openapi.json") 1LMNOPQR

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

41 assert response.json() == snapshot( 1LMNOPQR

42 { 

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/items/": { 

47 "get": { 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

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

52 }, 

53 "422": { 

54 "description": "Validation Error", 

55 "content": { 

56 "application/json": { 

57 "schema": { 

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

59 } 

60 } 

61 }, 

62 }, 

63 }, 

64 "summary": "Read Items", 

65 "operationId": "read_items_items__get", 

66 "parameters": [ 

67 { 

68 "required": False, 

69 "schema": { 

70 "title": "Q", 

71 "type": "array", 

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

73 "default": ["foo", "bar"], 

74 }, 

75 "name": "q", 

76 "in": "query", 

77 } 

78 ], 

79 } 

80 } 

81 }, 

82 "components": { 

83 "schemas": { 

84 "ValidationError": { 

85 "title": "ValidationError", 

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

87 "type": "object", 

88 "properties": { 

89 "loc": { 

90 "title": "Location", 

91 "type": "array", 

92 "items": { 

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

94 }, 

95 }, 

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

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

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

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

100 }, 

101 }, 

102 "HTTPValidationError": { 

103 "title": "HTTPValidationError", 

104 "type": "object", 

105 "properties": { 

106 "detail": { 

107 "title": "Detail", 

108 "type": "array", 

109 "items": { 

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

111 }, 

112 } 

113 }, 

114 }, 

115 } 

116 }, 

117 } 

118 )