Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial009.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("tutorial009_py310", marks=needs_py310), 

14 pytest.param("tutorial009_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_query_params_str_validations_no_query(client: TestClient): 1abdc

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

28 assert response.status_code == 200 1efg

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

30 

31 

32def test_query_params_str_validations_item_query_fixedquery(client: TestClient): 1abdc

33 response = client.get("/items/", params={"item-query": "fixedquery"}) 1hij

34 assert response.status_code == 200 1hij

35 assert response.json() == { 1hij

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

37 "q": "fixedquery", 

38 } 

39 

40 

41def test_query_params_str_validations_q_fixedquery(client: TestClient): 1abdc

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

43 assert response.status_code == 200 1klm

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

45 

46 

47def test_openapi_schema(client: TestClient): 1abdc

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

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

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

51 { 

52 "openapi": "3.1.0", 

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

54 "paths": { 

55 "/items/": { 

56 "get": { 

57 "responses": { 

58 "200": { 

59 "description": "Successful Response", 

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

61 }, 

62 "422": { 

63 "description": "Validation Error", 

64 "content": { 

65 "application/json": { 

66 "schema": { 

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

68 } 

69 } 

70 }, 

71 }, 

72 }, 

73 "summary": "Read Items", 

74 "operationId": "read_items_items__get", 

75 "parameters": [ 

76 { 

77 "schema": { 

78 "anyOf": [ 

79 {"type": "string"}, 

80 {"type": "null"}, 

81 ], 

82 "title": "Item-Query", 

83 }, 

84 "required": False, 

85 "name": "item-query", 

86 "in": "query", 

87 } 

88 ], 

89 } 

90 } 

91 }, 

92 "components": { 

93 "schemas": { 

94 "ValidationError": { 

95 "title": "ValidationError", 

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

97 "type": "object", 

98 "properties": { 

99 "loc": { 

100 "title": "Location", 

101 "type": "array", 

102 "items": { 

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

104 }, 

105 }, 

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

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

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

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

110 }, 

111 }, 

112 "HTTPValidationError": { 

113 "title": "HTTPValidationError", 

114 "type": "object", 

115 "properties": { 

116 "detail": { 

117 "title": "Detail", 

118 "type": "array", 

119 "items": { 

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

121 }, 

122 } 

123 }, 

124 }, 

125 } 

126 }, 

127 } 

128 )