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

26 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("tutorial001_py310", marks=needs_py310), 

14 ], 

15) 

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

17 mod = importlib.import_module( 1abc

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

19 ) 

20 

21 client = TestClient(mod.app) 1abc

22 return client 1abc

23 

24 

25def test_query_params_str_validations_no_query(client: TestClient): 1abdc

26 response = client.get("/items/") 1efg

27 assert response.status_code == 200 1efg

28 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1efg

29 

30 

31def test_query_params_str_validations_q_empty_str(client: TestClient): 1abdc

32 response = client.get("/items/", params={"q": ""}) 1hij

33 assert response.status_code == 200 1hij

34 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1hij

35 

36 

37def test_query_params_str_validations_q_query(client: TestClient): 1abdc

38 response = client.get("/items/", params={"q": "query"}) 1klm

39 assert response.status_code == 200 1klm

40 assert response.json() == { 1klm

41 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], 

42 "q": "query", 

43 } 

44 

45 

46def test_openapi_schema(client: TestClient): 1abdc

47 response = client.get("/openapi.json") 1nop

48 assert response.status_code == 200, response.text 1nop

49 assert response.json() == snapshot( 1nop

50 { 

51 "openapi": "3.1.0", 

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

53 "paths": { 

54 "/items/": { 

55 "get": { 

56 "responses": { 

57 "200": { 

58 "description": "Successful Response", 

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

60 }, 

61 "422": { 

62 "description": "Validation Error", 

63 "content": { 

64 "application/json": { 

65 "schema": { 

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

67 } 

68 } 

69 }, 

70 }, 

71 }, 

72 "summary": "Read Items", 

73 "operationId": "read_items_items__get", 

74 "parameters": [ 

75 { 

76 "required": False, 

77 "schema": { 

78 "anyOf": [ 

79 {"type": "string"}, 

80 {"type": "null"}, 

81 ], 

82 "title": "Q", 

83 }, 

84 "name": "q", 

85 "in": "query", 

86 } 

87 ], 

88 } 

89 } 

90 }, 

91 "components": { 

92 "schemas": { 

93 "ValidationError": { 

94 "title": "ValidationError", 

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

96 "type": "object", 

97 "properties": { 

98 "loc": { 

99 "title": "Location", 

100 "type": "array", 

101 "items": { 

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

103 }, 

104 }, 

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

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

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

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

109 }, 

110 }, 

111 "HTTPValidationError": { 

112 "title": "HTTPValidationError", 

113 "type": "object", 

114 "properties": { 

115 "detail": { 

116 "title": "Detail", 

117 "type": "array", 

118 "items": { 

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

120 }, 

121 } 

122 }, 

123 }, 

124 } 

125 }, 

126 } 

127 )