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

21 statements  

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

1import pytest 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from ...utils import needs_pydanticv2 1abcde

5 

6 

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

8def get_client() -> TestClient: 1abcde

9 from docs_src.separate_openapi_schemas.tutorial001 import app 1abcde

10 

11 client = TestClient(app) 1abcde

12 return client 1abcde

13 

14 

15def test_create_item(client: TestClient) -> None: 1abcde

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

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

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

19 

20 

21def test_read_items(client: TestClient) -> None: 1abcde

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

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

24 assert response.json() == [ 1abcde

25 { 

26 "name": "Portal Gun", 

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

28 }, 

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

30 ] 

31 

32 

33@needs_pydanticv2 1abcde

34def test_openapi_schema(client: TestClient) -> None: 1abcde

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

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

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

38 "openapi": "3.1.0", 

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

40 "paths": { 

41 "/items/": { 

42 "get": { 

43 "summary": "Read Items", 

44 "operationId": "read_items_items__get", 

45 "responses": { 

46 "200": { 

47 "description": "Successful Response", 

48 "content": { 

49 "application/json": { 

50 "schema": { 

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

52 "type": "array", 

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

54 } 

55 } 

56 }, 

57 } 

58 }, 

59 }, 

60 "post": { 

61 "summary": "Create Item", 

62 "operationId": "create_item_items__post", 

63 "requestBody": { 

64 "content": { 

65 "application/json": { 

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

67 } 

68 }, 

69 "required": True, 

70 }, 

71 "responses": { 

72 "200": { 

73 "description": "Successful Response", 

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

75 }, 

76 "422": { 

77 "description": "Validation Error", 

78 "content": { 

79 "application/json": { 

80 "schema": { 

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

82 } 

83 } 

84 }, 

85 }, 

86 }, 

87 }, 

88 } 

89 }, 

90 "components": { 

91 "schemas": { 

92 "HTTPValidationError": { 

93 "properties": { 

94 "detail": { 

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

96 "type": "array", 

97 "title": "Detail", 

98 } 

99 }, 

100 "type": "object", 

101 "title": "HTTPValidationError", 

102 }, 

103 "Item": { 

104 "properties": { 

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

106 "description": { 

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

108 "title": "Description", 

109 }, 

110 }, 

111 "type": "object", 

112 "required": ["name"], 

113 "title": "Item", 

114 }, 

115 "ValidationError": { 

116 "properties": { 

117 "loc": { 

118 "items": { 

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

120 }, 

121 "type": "array", 

122 "title": "Location", 

123 }, 

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

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

126 }, 

127 "type": "object", 

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

129 "title": "ValidationError", 

130 }, 

131 } 

132 }, 

133 }