Coverage for tests/test_union_body.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import Optional, Union 1abcde

2 

3from dirty_equals import IsDict 1abcde

4from fastapi import FastAPI 1abcde

5from fastapi.testclient import TestClient 1abcde

6from pydantic import BaseModel 1abcde

7 

8app = FastAPI() 1abcde

9 

10 

11class Item(BaseModel): 1abcde

12 name: Optional[str] = None 1abcde

13 

14 

15class OtherItem(BaseModel): 1abcde

16 price: int 1abcde

17 

18 

19@app.post("/items/") 1abcde

20def save_union_body(item: Union[OtherItem, Item]): 1abcde

21 return {"item": item} 1abcde

22 

23 

24client = TestClient(app) 1abcde

25 

26 

27def test_post_other_item(): 1abcde

28 response = client.post("/items/", json={"price": 100}) 1abcde

29 assert response.status_code == 200, response.text 1abcde

30 assert response.json() == {"item": {"price": 100}} 1abcde

31 

32 

33def test_post_item(): 1abcde

34 response = client.post("/items/", json={"name": "Foo"}) 1abcde

35 assert response.status_code == 200, response.text 1abcde

36 assert response.json() == {"item": {"name": "Foo"}} 1abcde

37 

38 

39def test_openapi_schema(): 1abcde

40 response = client.get("/openapi.json") 1abcde

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

42 assert response.json() == { 1abcde

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/items/": { 

47 "post": { 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

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

52 }, 

53 "422": { 

54 "description": "Validation Error", 

55 "content": { 

56 "application/json": { 

57 "schema": { 

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

59 } 

60 } 

61 }, 

62 }, 

63 }, 

64 "summary": "Save Union Body", 

65 "operationId": "save_union_body_items__post", 

66 "requestBody": { 

67 "content": { 

68 "application/json": { 

69 "schema": { 

70 "title": "Item", 

71 "anyOf": [ 

72 {"$ref": "#/components/schemas/OtherItem"}, 

73 {"$ref": "#/components/schemas/Item"}, 

74 ], 

75 } 

76 } 

77 }, 

78 "required": True, 

79 }, 

80 } 

81 } 

82 }, 

83 "components": { 

84 "schemas": { 

85 "OtherItem": { 

86 "title": "OtherItem", 

87 "required": ["price"], 

88 "type": "object", 

89 "properties": {"price": {"title": "Price", "type": "integer"}}, 

90 }, 

91 "Item": { 

92 "title": "Item", 

93 "type": "object", 

94 "properties": IsDict( 

95 { 

96 "name": { 

97 "title": "Name", 

98 "anyOf": [{"type": "string"}, {"type": "null"}], 

99 } 

100 } 

101 ) 

102 | IsDict( 

103 # TODO: remove when deprecating Pydantic v1 

104 {"name": {"title": "Name", "type": "string"}} 

105 ), 

106 }, 

107 "ValidationError": { 

108 "title": "ValidationError", 

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

110 "type": "object", 

111 "properties": { 

112 "loc": { 

113 "title": "Location", 

114 "type": "array", 

115 "items": { 

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

117 }, 

118 }, 

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

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

121 }, 

122 }, 

123 "HTTPValidationError": { 

124 "title": "HTTPValidationError", 

125 "type": "object", 

126 "properties": { 

127 "detail": { 

128 "title": "Detail", 

129 "type": "array", 

130 "items": {"$ref": "#/components/schemas/ValidationError"}, 

131 } 

132 }, 

133 }, 

134 } 

135 }, 

136 }