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

22 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 pytest.param("tutorial001_an_py310", marks=needs_py310), 

15 ], 

16) 

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

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

19 

20 client = TestClient(mod.app) 1abc

21 return client 1abc

22 

23 

24def test_extra_types(client: TestClient): 1abdc

25 item_id = "ff97dd87-a4a5-4a12-b412-cde99f33e00e" 1efg

26 data = { 1efg

27 "start_datetime": "2018-12-22T14:00:00+00:00", 

28 "end_datetime": "2018-12-24T15:00:00+00:00", 

29 "repeat_at": "15:30:00", 

30 "process_after": 300, 

31 } 

32 expected_response = data.copy() 1efg

33 expected_response.update( 1efg

34 { 

35 "start_process": "2018-12-22T14:05:00+00:00", 

36 "duration": 176_100, 

37 "item_id": item_id, 

38 } 

39 ) 

40 response = client.put(f"/items/{item_id}", json=data) 1efg

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

42 assert response.json() == expected_response 1efg

43 

44 

45def test_openapi_schema(client: TestClient): 1abdc

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

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

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

49 { 

50 "openapi": "3.1.0", 

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

52 "paths": { 

53 "/items/{item_id}": { 

54 "put": { 

55 "responses": { 

56 "200": { 

57 "description": "Successful Response", 

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

59 }, 

60 "422": { 

61 "description": "Validation Error", 

62 "content": { 

63 "application/json": { 

64 "schema": { 

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

66 } 

67 } 

68 }, 

69 }, 

70 }, 

71 "summary": "Read Items", 

72 "operationId": "read_items_items__item_id__put", 

73 "parameters": [ 

74 { 

75 "required": True, 

76 "schema": { 

77 "title": "Item Id", 

78 "type": "string", 

79 "format": "uuid", 

80 }, 

81 "name": "item_id", 

82 "in": "path", 

83 } 

84 ], 

85 "requestBody": { 

86 "required": True, 

87 "content": { 

88 "application/json": { 

89 "schema": { 

90 "$ref": "#/components/schemas/Body_read_items_items__item_id__put" 

91 } 

92 } 

93 }, 

94 }, 

95 } 

96 } 

97 }, 

98 "components": { 

99 "schemas": { 

100 "Body_read_items_items__item_id__put": { 

101 "title": "Body_read_items_items__item_id__put", 

102 "type": "object", 

103 "properties": { 

104 "start_datetime": { 

105 "title": "Start Datetime", 

106 "type": "string", 

107 "format": "date-time", 

108 }, 

109 "end_datetime": { 

110 "title": "End Datetime", 

111 "type": "string", 

112 "format": "date-time", 

113 }, 

114 "repeat_at": { 

115 "title": "Repeat At", 

116 "anyOf": [ 

117 {"type": "string", "format": "time"}, 

118 {"type": "null"}, 

119 ], 

120 }, 

121 "process_after": { 

122 "title": "Process After", 

123 "type": "string", 

124 "format": "duration", 

125 }, 

126 }, 

127 "required": ["start_datetime", "end_datetime", "process_after"], 

128 }, 

129 "ValidationError": { 

130 "title": "ValidationError", 

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

132 "type": "object", 

133 "properties": { 

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

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

136 "loc": { 

137 "title": "Location", 

138 "type": "array", 

139 "items": { 

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

141 }, 

142 }, 

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

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

145 }, 

146 }, 

147 "HTTPValidationError": { 

148 "title": "HTTPValidationError", 

149 "type": "object", 

150 "properties": { 

151 "detail": { 

152 "title": "Detail", 

153 "type": "array", 

154 "items": { 

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

156 }, 

157 } 

158 }, 

159 }, 

160 } 

161 }, 

162 } 

163 )