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

24 statements  

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

1import pytest 1deabc

2from dirty_equals import IsOneOf 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

8@pytest.fixture(name="client") 1deabc

9def get_client(): 1deabc

10 from docs_src.extra_models.tutorial003_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17def test_get_car(client: TestClient): 1deabc

18 response = client.get("/items/item1") 1abc

19 assert response.status_code == 200, response.text 1abc

20 assert response.json() == { 1abc

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

22 "type": "car", 

23 } 

24 

25 

26@needs_py310 1deabc

27def test_get_plane(client: TestClient): 1deabc

28 response = client.get("/items/item2") 1abc

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

30 assert response.json() == { 1abc

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

32 "type": "plane", 

33 "size": 5, 

34 } 

35 

36 

37@needs_py310 1deabc

38def test_openapi_schema(client: TestClient): 1deabc

39 response = client.get("/openapi.json") 1abc

40 assert response.status_code == 200, response.text 1abc

41 assert response.json() == { 1abc

42 "openapi": "3.1.0", 

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

44 "paths": { 

45 "/items/{item_id}": { 

46 "get": { 

47 "responses": { 

48 "200": { 

49 "description": "Successful Response", 

50 "content": { 

51 "application/json": { 

52 "schema": { 

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

54 "anyOf": [ 

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

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

57 ], 

58 } 

59 } 

60 }, 

61 }, 

62 "422": { 

63 "description": "Validation Error", 

64 "content": { 

65 "application/json": { 

66 "schema": { 

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

68 } 

69 } 

70 }, 

71 }, 

72 }, 

73 "summary": "Read Item", 

74 "operationId": "read_item_items__item_id__get", 

75 "parameters": [ 

76 { 

77 "required": True, 

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

79 "name": "item_id", 

80 "in": "path", 

81 } 

82 ], 

83 } 

84 } 

85 }, 

86 "components": { 

87 "schemas": { 

88 "PlaneItem": { 

89 "title": "PlaneItem", 

90 "required": IsOneOf( 

91 ["description", "type", "size"], 

92 # TODO: remove when deprecating Pydantic v1 

93 ["description", "size"], 

94 ), 

95 "type": "object", 

96 "properties": { 

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

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

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

100 }, 

101 }, 

102 "CarItem": { 

103 "title": "CarItem", 

104 "required": IsOneOf( 

105 ["description", "type"], 

106 # TODO: remove when deprecating Pydantic v1 

107 ["description"], 

108 ), 

109 "type": "object", 

110 "properties": { 

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

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

113 }, 

114 }, 

115 "ValidationError": { 

116 "title": "ValidationError", 

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

118 "type": "object", 

119 "properties": { 

120 "loc": { 

121 "title": "Location", 

122 "type": "array", 

123 "items": { 

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

125 }, 

126 }, 

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

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

129 }, 

130 }, 

131 "HTTPValidationError": { 

132 "title": "HTTPValidationError", 

133 "type": "object", 

134 "properties": { 

135 "detail": { 

136 "title": "Detail", 

137 "type": "array", 

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

139 } 

140 }, 

141 }, 

142 } 

143 }, 

144 }