Coverage for tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002.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 fastapi.testclient import TestClient 1abcdef

5 

6from ...utils import needs_py39, needs_py310, needs_pydanticv2 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial002", 

13 pytest.param("tutorial002_py310", marks=needs_py310), 

14 pytest.param("tutorial002_py39", marks=needs_py39), 

15 ], 

16) 

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

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

19 

20 client = TestClient(mod.app) 1abcdef

21 return client 1abcdef

22 

23 

24def test_create_item(client: TestClient) -> None: 1abcdef

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

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

27 assert response.json() == {"name": "Foo", "description": None} 1ghijkl

28 

29 

30def test_read_items(client: TestClient) -> None: 1abcdef

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

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

33 assert response.json() == [ 1mnopqr

34 { 

35 "name": "Portal Gun", 

36 "description": "Device to travel through the multi-rick-verse", 

37 }, 

38 {"name": "Plumbus", "description": None}, 

39 ] 

40 

41 

42@needs_pydanticv2 1abcdef

43def test_openapi_schema(client: TestClient) -> None: 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/": { 

51 "get": { 

52 "summary": "Read Items", 

53 "operationId": "read_items_items__get", 

54 "responses": { 

55 "200": { 

56 "description": "Successful Response", 

57 "content": { 

58 "application/json": { 

59 "schema": { 

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

61 "type": "array", 

62 "title": "Response Read Items Items Get", 

63 } 

64 } 

65 }, 

66 } 

67 }, 

68 }, 

69 "post": { 

70 "summary": "Create Item", 

71 "operationId": "create_item_items__post", 

72 "requestBody": { 

73 "content": { 

74 "application/json": { 

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

76 } 

77 }, 

78 "required": True, 

79 }, 

80 "responses": { 

81 "200": { 

82 "description": "Successful Response", 

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

84 }, 

85 "422": { 

86 "description": "Validation Error", 

87 "content": { 

88 "application/json": { 

89 "schema": { 

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

91 } 

92 } 

93 }, 

94 }, 

95 }, 

96 }, 

97 } 

98 }, 

99 "components": { 

100 "schemas": { 

101 "HTTPValidationError": { 

102 "properties": { 

103 "detail": { 

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

105 "type": "array", 

106 "title": "Detail", 

107 } 

108 }, 

109 "type": "object", 

110 "title": "HTTPValidationError", 

111 }, 

112 "Item": { 

113 "properties": { 

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

115 "description": { 

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

117 "title": "Description", 

118 }, 

119 }, 

120 "type": "object", 

121 "required": ["name"], 

122 "title": "Item", 

123 }, 

124 "ValidationError": { 

125 "properties": { 

126 "loc": { 

127 "items": { 

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

129 }, 

130 "type": "array", 

131 "title": "Location", 

132 }, 

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

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

135 }, 

136 "type": "object", 

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

138 "title": "ValidationError", 

139 }, 

140 } 

141 }, 

142 }