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

18 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("tutorial001_py310"), 

12 ], 

13) 

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

15 mod = importlib.import_module(f"docs_src.query_params.{request.param}") 1abc

16 

17 client = TestClient(mod.app) 1abc

18 return client 1abc

19 

20 

21@pytest.mark.parametrize( 1abdc

22 ("path", "expected_json"), 

23 [ 

24 ( 

25 "/items/", 

26 [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}], 

27 ), 

28 ( 

29 "/items/?skip=1", 

30 [{"item_name": "Bar"}, {"item_name": "Baz"}], 

31 ), 

32 ( 

33 "/items/?skip=1&limit=1", 

34 [{"item_name": "Bar"}], 

35 ), 

36 ], 

37) 

38def test_read_user_item(client: TestClient, path, expected_json): 1abdc

39 response = client.get(path) 1efg

40 assert response.status_code == 200 1efg

41 assert response.json() == expected_json 1efg

42 

43 

44def test_openapi_schema(client: TestClient): 1abdc

45 response = client.get("/openapi.json") 1hij

46 assert response.status_code == 200 1hij

47 assert response.json() == snapshot( 1hij

48 { 

49 "openapi": "3.1.0", 

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

51 "paths": { 

52 "/items/": { 

53 "get": { 

54 "summary": "Read Item", 

55 "operationId": "read_item_items__get", 

56 "parameters": [ 

57 { 

58 "required": False, 

59 "schema": { 

60 "title": "Skip", 

61 "type": "integer", 

62 "default": 0, 

63 }, 

64 "name": "skip", 

65 "in": "query", 

66 }, 

67 { 

68 "required": False, 

69 "schema": { 

70 "title": "Limit", 

71 "type": "integer", 

72 "default": 10, 

73 }, 

74 "name": "limit", 

75 "in": "query", 

76 }, 

77 ], 

78 "responses": { 

79 "200": { 

80 "description": "Successful Response", 

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

82 }, 

83 "422": { 

84 "content": { 

85 "application/json": { 

86 "schema": { 

87 "$ref": "#/components/schemas/HTTPValidationError", 

88 }, 

89 }, 

90 }, 

91 "description": "Validation Error", 

92 }, 

93 }, 

94 } 

95 } 

96 }, 

97 "components": { 

98 "schemas": { 

99 "ValidationError": { 

100 "title": "ValidationError", 

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

102 "type": "object", 

103 "properties": { 

104 "loc": { 

105 "title": "Location", 

106 "type": "array", 

107 "items": { 

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

109 }, 

110 }, 

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

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

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

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

115 }, 

116 }, 

117 "HTTPValidationError": { 

118 "title": "HTTPValidationError", 

119 "type": "object", 

120 "properties": { 

121 "detail": { 

122 "title": "Detail", 

123 "type": "array", 

124 "items": { 

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

126 }, 

127 } 

128 }, 

129 }, 

130 } 

131 }, 

132 } 

133 )