Coverage for tests / test_tutorial / test_response_directly / 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 

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(f"docs_src.response_directly.{request.param}") 1abc

18 

19 client = TestClient(mod.app) 1abc

20 return client 1abc

21 

22 

23def test_path_operation(client: TestClient): 1abdc

24 response = client.put( 1efg

25 "/items/1", 

26 json={ 

27 "title": "Foo", 

28 "timestamp": "2023-01-01T12:00:00", 

29 "description": "A test item", 

30 }, 

31 ) 

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

33 assert response.json() == { 1efg

34 "description": "A test item", 

35 "timestamp": "2023-01-01T12:00:00", 

36 "title": "Foo", 

37 } 

38 

39 

40def test_openapi_schema_pv2(client: TestClient): 1abdc

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

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

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

44 { 

45 "info": { 

46 "title": "FastAPI", 

47 "version": "0.1.0", 

48 }, 

49 "openapi": "3.1.0", 

50 "paths": { 

51 "/items/{id}": { 

52 "put": { 

53 "operationId": "update_item_items__id__put", 

54 "parameters": [ 

55 { 

56 "in": "path", 

57 "name": "id", 

58 "required": True, 

59 "schema": {"title": "Id", "type": "string"}, 

60 }, 

61 ], 

62 "requestBody": { 

63 "content": { 

64 "application/json": { 

65 "schema": { 

66 "$ref": "#/components/schemas/Item", 

67 }, 

68 }, 

69 }, 

70 "required": True, 

71 }, 

72 "responses": { 

73 "200": { 

74 "content": { 

75 "application/json": {"schema": {}}, 

76 }, 

77 "description": "Successful Response", 

78 }, 

79 "422": { 

80 "content": { 

81 "application/json": { 

82 "schema": { 

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

84 }, 

85 }, 

86 }, 

87 "description": "Validation Error", 

88 }, 

89 }, 

90 "summary": "Update Item", 

91 }, 

92 }, 

93 }, 

94 "components": { 

95 "schemas": { 

96 "HTTPValidationError": { 

97 "properties": { 

98 "detail": { 

99 "items": { 

100 "$ref": "#/components/schemas/ValidationError", 

101 }, 

102 "title": "Detail", 

103 "type": "array", 

104 }, 

105 }, 

106 "title": "HTTPValidationError", 

107 "type": "object", 

108 }, 

109 "Item": { 

110 "properties": { 

111 "description": { 

112 "anyOf": [ 

113 {"type": "string"}, 

114 {"type": "null"}, 

115 ], 

116 "title": "Description", 

117 }, 

118 "timestamp": { 

119 "format": "date-time", 

120 "title": "Timestamp", 

121 "type": "string", 

122 }, 

123 "title": {"title": "Title", "type": "string"}, 

124 }, 

125 "required": [ 

126 "title", 

127 "timestamp", 

128 ], 

129 "title": "Item", 

130 "type": "object", 

131 }, 

132 "ValidationError": { 

133 "properties": { 

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

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

136 "loc": { 

137 "items": { 

138 "anyOf": [ 

139 {"type": "string"}, 

140 {"type": "integer"}, 

141 ], 

142 }, 

143 "title": "Location", 

144 "type": "array", 

145 }, 

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

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

148 }, 

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

150 "title": "ValidationError", 

151 "type": "object", 

152 }, 

153 }, 

154 }, 

155 } 

156 )