Coverage for tests/test_tutorial/test_separate_openapi_schemas/test_tutorial002_py39.py: 100%

24 statements  

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

1import pytest 1eabcd

2from fastapi.testclient import TestClient 1eabcd

3 

4from ...utils import needs_py39, needs_pydanticv2 1eabcd

5 

6 

7@pytest.fixture(name="client") 1eabcd

8def get_client() -> TestClient: 1eabcd

9 from docs_src.separate_openapi_schemas.tutorial002_py39 import app 1abcd

10 

11 client = TestClient(app) 1abcd

12 return client 1abcd

13 

14 

15@needs_py39 1eabcd

16def test_create_item(client: TestClient) -> None: 1eabcd

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

18 assert response.status_code == 200, response.text 1abcd

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

20 

21 

22@needs_py39 1eabcd

23def test_read_items(client: TestClient) -> None: 1eabcd

24 response = client.get("/items/") 1abcd

25 assert response.status_code == 200, response.text 1abcd

26 assert response.json() == [ 1abcd

27 { 

28 "name": "Portal Gun", 

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

30 }, 

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

32 ] 

33 

34 

35@needs_py39 1eabcd

36@needs_pydanticv2 1eabcd

37def test_openapi_schema(client: TestClient) -> None: 1eabcd

38 response = client.get("/openapi.json") 1abcd

39 assert response.status_code == 200, response.text 1abcd

40 assert response.json() == { 1abcd

41 "openapi": "3.1.0", 

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

43 "paths": { 

44 "/items/": { 

45 "get": { 

46 "summary": "Read Items", 

47 "operationId": "read_items_items__get", 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

51 "content": { 

52 "application/json": { 

53 "schema": { 

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

55 "type": "array", 

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

57 } 

58 } 

59 }, 

60 } 

61 }, 

62 }, 

63 "post": { 

64 "summary": "Create Item", 

65 "operationId": "create_item_items__post", 

66 "requestBody": { 

67 "content": { 

68 "application/json": { 

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

70 } 

71 }, 

72 "required": True, 

73 }, 

74 "responses": { 

75 "200": { 

76 "description": "Successful Response", 

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

78 }, 

79 "422": { 

80 "description": "Validation Error", 

81 "content": { 

82 "application/json": { 

83 "schema": { 

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

85 } 

86 } 

87 }, 

88 }, 

89 }, 

90 }, 

91 } 

92 }, 

93 "components": { 

94 "schemas": { 

95 "HTTPValidationError": { 

96 "properties": { 

97 "detail": { 

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

99 "type": "array", 

100 "title": "Detail", 

101 } 

102 }, 

103 "type": "object", 

104 "title": "HTTPValidationError", 

105 }, 

106 "Item": { 

107 "properties": { 

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

109 "description": { 

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

111 "title": "Description", 

112 }, 

113 }, 

114 "type": "object", 

115 "required": ["name"], 

116 "title": "Item", 

117 }, 

118 "ValidationError": { 

119 "properties": { 

120 "loc": { 

121 "items": { 

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

123 }, 

124 "type": "array", 

125 "title": "Location", 

126 }, 

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

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

129 }, 

130 "type": "object", 

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

132 "title": "ValidationError", 

133 }, 

134 } 

135 }, 

136 }