Coverage for tests/test_tutorial/test_extra_models/test_tutorial003.py: 100%

16 statements  

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

1from dirty_equals import IsOneOf 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.extra_models.tutorial003 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_get_car(): 1abcde

10 response = client.get("/items/item1") 1abcde

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

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

13 "description": "All my friends drive a low rider", 

14 "type": "car", 

15 } 

16 

17 

18def test_get_plane(): 1abcde

19 response = client.get("/items/item2") 1abcde

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

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

22 "description": "Music is my aeroplane, it's my aeroplane", 

23 "type": "plane", 

24 "size": 5, 

25 } 

26 

27 

28def test_openapi_schema(): 1abcde

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

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

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

32 "openapi": "3.1.0", 

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

34 "paths": { 

35 "/items/{item_id}": { 

36 "get": { 

37 "responses": { 

38 "200": { 

39 "description": "Successful Response", 

40 "content": { 

41 "application/json": { 

42 "schema": { 

43 "title": "Response Read Item Items Item Id Get", 

44 "anyOf": [ 

45 {"$ref": "#/components/schemas/PlaneItem"}, 

46 {"$ref": "#/components/schemas/CarItem"}, 

47 ], 

48 } 

49 } 

50 }, 

51 }, 

52 "422": { 

53 "description": "Validation Error", 

54 "content": { 

55 "application/json": { 

56 "schema": { 

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

58 } 

59 } 

60 }, 

61 }, 

62 }, 

63 "summary": "Read Item", 

64 "operationId": "read_item_items__item_id__get", 

65 "parameters": [ 

66 { 

67 "required": True, 

68 "schema": {"title": "Item Id", "type": "string"}, 

69 "name": "item_id", 

70 "in": "path", 

71 } 

72 ], 

73 } 

74 } 

75 }, 

76 "components": { 

77 "schemas": { 

78 "PlaneItem": { 

79 "title": "PlaneItem", 

80 "required": IsOneOf( 

81 ["description", "type", "size"], 

82 # TODO: remove when deprecating Pydantic v1 

83 ["description", "size"], 

84 ), 

85 "type": "object", 

86 "properties": { 

87 "description": {"title": "Description", "type": "string"}, 

88 "type": {"title": "Type", "type": "string", "default": "plane"}, 

89 "size": {"title": "Size", "type": "integer"}, 

90 }, 

91 }, 

92 "CarItem": { 

93 "title": "CarItem", 

94 "required": IsOneOf( 

95 ["description", "type"], 

96 # TODO: remove when deprecating Pydantic v1 

97 ["description"], 

98 ), 

99 "type": "object", 

100 "properties": { 

101 "description": {"title": "Description", "type": "string"}, 

102 "type": {"title": "Type", "type": "string", "default": "car"}, 

103 }, 

104 }, 

105 "ValidationError": { 

106 "title": "ValidationError", 

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

108 "type": "object", 

109 "properties": { 

110 "loc": { 

111 "title": "Location", 

112 "type": "array", 

113 "items": { 

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

115 }, 

116 }, 

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

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

119 }, 

120 }, 

121 "HTTPValidationError": { 

122 "title": "HTTPValidationError", 

123 "type": "object", 

124 "properties": { 

125 "detail": { 

126 "title": "Detail", 

127 "type": "array", 

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

129 } 

130 }, 

131 }, 

132 } 

133 }, 

134 }