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

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsOneOf 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial003", 

14 pytest.param("tutorial003_py310", marks=needs_py310), 

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abcdef

18 mod = importlib.import_module(f"docs_src.extra_models.{request.param}") 1abcdef

19 

20 client = TestClient(mod.app) 1abcdef

21 return client 1abcdef

22 

23 

24def test_get_car(client: TestClient): 1abcdef

25 response = client.get("/items/item1") 1ghijkl

26 assert response.status_code == 200, response.text 1ghijkl

27 assert response.json() == { 1ghijkl

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

29 "type": "car", 

30 } 

31 

32 

33def test_get_plane(client: TestClient): 1abcdef

34 response = client.get("/items/item2") 1mnopqr

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

36 assert response.json() == { 1mnopqr

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

38 "type": "plane", 

39 "size": 5, 

40 } 

41 

42 

43def test_openapi_schema(client: TestClient): 1abcdef

44 response = client.get("/openapi.json") 1stuvwx

45 assert response.status_code == 200, response.text 1stuvwx

46 assert response.json() == { 1stuvwx

47 "openapi": "3.1.0", 

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

49 "paths": { 

50 "/items/{item_id}": { 

51 "get": { 

52 "responses": { 

53 "200": { 

54 "description": "Successful Response", 

55 "content": { 

56 "application/json": { 

57 "schema": { 

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

59 "anyOf": [ 

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

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

62 ], 

63 } 

64 } 

65 }, 

66 }, 

67 "422": { 

68 "description": "Validation Error", 

69 "content": { 

70 "application/json": { 

71 "schema": { 

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

73 } 

74 } 

75 }, 

76 }, 

77 }, 

78 "summary": "Read Item", 

79 "operationId": "read_item_items__item_id__get", 

80 "parameters": [ 

81 { 

82 "required": True, 

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

84 "name": "item_id", 

85 "in": "path", 

86 } 

87 ], 

88 } 

89 } 

90 }, 

91 "components": { 

92 "schemas": { 

93 "PlaneItem": { 

94 "title": "PlaneItem", 

95 "required": IsOneOf( 

96 ["description", "type", "size"], 

97 # TODO: remove when deprecating Pydantic v1 

98 ["description", "size"], 

99 ), 

100 "type": "object", 

101 "properties": { 

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

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

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

105 }, 

106 }, 

107 "CarItem": { 

108 "title": "CarItem", 

109 "required": IsOneOf( 

110 ["description", "type"], 

111 # TODO: remove when deprecating Pydantic v1 

112 ["description"], 

113 ), 

114 "type": "object", 

115 "properties": { 

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

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

118 }, 

119 }, 

120 "ValidationError": { 

121 "title": "ValidationError", 

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

123 "type": "object", 

124 "properties": { 

125 "loc": { 

126 "title": "Location", 

127 "type": "array", 

128 "items": { 

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

130 }, 

131 }, 

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

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

134 }, 

135 }, 

136 "HTTPValidationError": { 

137 "title": "HTTPValidationError", 

138 "type": "object", 

139 "properties": { 

140 "detail": { 

141 "title": "Detail", 

142 "type": "array", 

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

144 } 

145 }, 

146 }, 

147 } 

148 }, 

149 }