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-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 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 pytest.param("tutorial012_py310"), 

12 pytest.param("tutorial012_an_py310"), 

13 ], 

14) 

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

16 mod = importlib.import_module( 1abc

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

18 ) 

19 

20 client = TestClient(mod.app) 1abc

21 return client 1abc

22 

23 

24def test_default_query_values(client: TestClient): 1abdc

25 url = "/items/" 1efg

26 response = client.get(url) 1efg

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

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

29 

30 

31def test_multi_query_values(client: TestClient): 1abdc

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

33 response = client.get(url) 1hij

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

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

36 

37 

38def test_openapi_schema(client: TestClient): 1abdc

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

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

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

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 )