Coverage for tests / test_tutorial / test_body_nested_models / test_tutorial008.py: 100%

28 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("tutorial008_py310"), 

12 ], 

13) 

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

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

16 

17 client = TestClient(mod.app) 1abc

18 return client 1abc

19 

20 

21def test_post_body(client: TestClient): 1abdc

22 data = [ 1efg

23 {"url": "http://example.com/", "name": "Example"}, 

24 {"url": "http://fastapi.tiangolo.com/", "name": "FastAPI"}, 

25 ] 

26 response = client.post("/images/multiple", json=data) 1efg

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

28 assert response.json() == data 1efg

29 

30 

31def test_post_invalid_list_item(client: TestClient): 1abdc

32 data = [{"url": "not a valid url", "name": "Example"}] 1hij

33 response = client.post("/images/multiple", json=data) 1hij

34 assert response.status_code == 422, response.text 1hij

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

36 "detail": [ 

37 { 

38 "loc": ["body", 0, "url"], 

39 "input": "not a valid url", 

40 "msg": "Input should be a valid URL, relative URL without a base", 

41 "type": "url_parsing", 

42 "ctx": {"error": "relative URL without a base"}, 

43 }, 

44 ] 

45 } 

46 

47 

48def test_post_not_a_list(client: TestClient): 1abdc

49 data = {"url": "http://example.com/", "name": "Example"} 1klm

50 response = client.post("/images/multiple", json=data) 1klm

51 assert response.status_code == 422, response.text 1klm

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

53 "detail": [ 

54 { 

55 "loc": ["body"], 

56 "input": { 

57 "name": "Example", 

58 "url": "http://example.com/", 

59 }, 

60 "msg": "Input should be a valid list", 

61 "type": "list_type", 

62 } 

63 ] 

64 } 

65 

66 

67def test_openapi_schema(client: TestClient): 1abdc

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

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

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

71 { 

72 "openapi": "3.1.0", 

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

74 "paths": { 

75 "/images/multiple/": { 

76 "post": { 

77 "responses": { 

78 "200": { 

79 "description": "Successful Response", 

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

81 }, 

82 "422": { 

83 "description": "Validation Error", 

84 "content": { 

85 "application/json": { 

86 "schema": { 

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

88 } 

89 } 

90 }, 

91 }, 

92 }, 

93 "summary": "Create Multiple Images", 

94 "operationId": "create_multiple_images_images_multiple__post", 

95 "requestBody": { 

96 "content": { 

97 "application/json": { 

98 "schema": { 

99 "title": "Images", 

100 "type": "array", 

101 "items": {"$ref": "#/components/schemas/Image"}, 

102 } 

103 } 

104 }, 

105 "required": True, 

106 }, 

107 } 

108 } 

109 }, 

110 "components": { 

111 "schemas": { 

112 "Image": { 

113 "properties": { 

114 "url": { 

115 "title": "Url", 

116 "type": "string", 

117 "format": "uri", 

118 "maxLength": 2083, 

119 "minLength": 1, 

120 }, 

121 "name": { 

122 "title": "Name", 

123 "type": "string", 

124 }, 

125 }, 

126 "required": ["url", "name"], 

127 "title": "Image", 

128 "type": "object", 

129 }, 

130 "ValidationError": { 

131 "title": "ValidationError", 

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

133 "type": "object", 

134 "properties": { 

135 "loc": { 

136 "title": "Location", 

137 "type": "array", 

138 "items": { 

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

140 }, 

141 }, 

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

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

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

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

146 }, 

147 }, 

148 "HTTPValidationError": { 

149 "title": "HTTPValidationError", 

150 "type": "object", 

151 "properties": { 

152 "detail": { 

153 "title": "Detail", 

154 "type": "array", 

155 "items": { 

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

157 }, 

158 } 

159 }, 

160 }, 

161 } 

162 }, 

163 } 

164 )