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

16 statements  

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

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.dataclasses.tutorial001 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_post_item(): 1abcde

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

11 assert response.status_code == 200 1abcde

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

13 "name": "Foo", 

14 "price": 3, 

15 "description": None, 

16 "tax": None, 

17 } 

18 

19 

20def test_post_invalid_item(): 1abcde

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

22 assert response.status_code == 422 1abcde

23 assert response.json() == IsDict( 1abcde

24 { 

25 "detail": [ 

26 { 

27 "type": "float_parsing", 

28 "loc": ["body", "price"], 

29 "msg": "Input should be a valid number, unable to parse string as a number", 

30 "input": "invalid price", 

31 } 

32 ] 

33 } 

34 ) | IsDict( 

35 # TODO: remove when deprecating Pydantic v1 

36 { 

37 "detail": [ 

38 { 

39 "loc": ["body", "price"], 

40 "msg": "value is not a valid float", 

41 "type": "type_error.float", 

42 } 

43 ] 

44 } 

45 ) 

46 

47 

48def test_openapi_schema(): 1abcde

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

50 assert response.status_code == 200 1abcde

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

52 "openapi": "3.1.0", 

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

54 "paths": { 

55 "/items/": { 

56 "post": { 

57 "summary": "Create Item", 

58 "operationId": "create_item_items__post", 

59 "requestBody": { 

60 "content": { 

61 "application/json": { 

62 "schema": {"$ref": "#/components/schemas/Item"} 

63 } 

64 }, 

65 "required": True, 

66 }, 

67 "responses": { 

68 "200": { 

69 "description": "Successful Response", 

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

71 }, 

72 "422": { 

73 "description": "Validation Error", 

74 "content": { 

75 "application/json": { 

76 "schema": { 

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

78 } 

79 } 

80 }, 

81 }, 

82 }, 

83 } 

84 } 

85 }, 

86 "components": { 

87 "schemas": { 

88 "HTTPValidationError": { 

89 "title": "HTTPValidationError", 

90 "type": "object", 

91 "properties": { 

92 "detail": { 

93 "title": "Detail", 

94 "type": "array", 

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

96 } 

97 }, 

98 }, 

99 "Item": { 

100 "title": "Item", 

101 "required": ["name", "price"], 

102 "type": "object", 

103 "properties": { 

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

105 "price": {"title": "Price", "type": "number"}, 

106 "description": IsDict( 

107 { 

108 "title": "Description", 

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

110 } 

111 ) 

112 | IsDict( 

113 # TODO: remove when deprecating Pydantic v1 

114 {"title": "Description", "type": "string"} 

115 ), 

116 "tax": IsDict( 

117 { 

118 "title": "Tax", 

119 "anyOf": [{"type": "number"}, {"type": "null"}], 

120 } 

121 ) 

122 | IsDict( 

123 # TODO: remove when deprecating Pydantic v1 

124 {"title": "Tax", "type": "number"} 

125 ), 

126 }, 

127 }, 

128 "ValidationError": { 

129 "title": "ValidationError", 

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

131 "type": "object", 

132 "properties": { 

133 "loc": { 

134 "title": "Location", 

135 "type": "array", 

136 "items": { 

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

138 }, 

139 }, 

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

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

142 }, 

143 }, 

144 } 

145 }, 

146 }