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

16 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi.testclient import TestClient 1abcd

2from inline_snapshot import snapshot 1abcd

3 

4from docs_src.additional_responses.tutorial003_py310 import app 1abcd

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_path_operation(): 1abcd

10 response = client.get("/items/foo") 1efg

11 assert response.status_code == 200, response.text 1efg

12 assert response.json() == {"id": "foo", "value": "there goes my hero"} 1efg

13 

14 

15def test_path_operation_not_found(): 1abcd

16 response = client.get("/items/bar") 1hij

17 assert response.status_code == 404, response.text 1hij

18 assert response.json() == {"message": "Item not found"} 1hij

19 

20 

21def test_openapi_schema(): 1abcd

22 response = client.get("/openapi.json") 1klm

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

24 assert response.json() == snapshot( 1klm

25 { 

26 "openapi": "3.1.0", 

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

28 "paths": { 

29 "/items/{item_id}": { 

30 "get": { 

31 "responses": { 

32 "404": { 

33 "description": "The item was not found", 

34 "content": { 

35 "application/json": { 

36 "schema": { 

37 "$ref": "#/components/schemas/Message" 

38 } 

39 } 

40 }, 

41 }, 

42 "200": { 

43 "description": "Item requested by ID", 

44 "content": { 

45 "application/json": { 

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

47 "example": { 

48 "id": "bar", 

49 "value": "The bar tenders", 

50 }, 

51 } 

52 }, 

53 }, 

54 "422": { 

55 "description": "Validation Error", 

56 "content": { 

57 "application/json": { 

58 "schema": { 

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

60 } 

61 } 

62 }, 

63 }, 

64 }, 

65 "summary": "Read Item", 

66 "operationId": "read_item_items__item_id__get", 

67 "parameters": [ 

68 { 

69 "required": True, 

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

71 "name": "item_id", 

72 "in": "path", 

73 } 

74 ], 

75 } 

76 } 

77 }, 

78 "components": { 

79 "schemas": { 

80 "Item": { 

81 "title": "Item", 

82 "required": ["id", "value"], 

83 "type": "object", 

84 "properties": { 

85 "id": {"title": "Id", "type": "string"}, 

86 "value": {"title": "Value", "type": "string"}, 

87 }, 

88 }, 

89 "Message": { 

90 "title": "Message", 

91 "required": ["message"], 

92 "type": "object", 

93 "properties": { 

94 "message": {"title": "Message", "type": "string"} 

95 }, 

96 }, 

97 "ValidationError": { 

98 "title": "ValidationError", 

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

100 "type": "object", 

101 "properties": { 

102 "loc": { 

103 "title": "Location", 

104 "type": "array", 

105 "items": { 

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

107 }, 

108 }, 

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

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

111 "input": {"title": "Input"}, 

112 "ctx": {"title": "Context", "type": "object"}, 

113 }, 

114 }, 

115 "HTTPValidationError": { 

116 "title": "HTTPValidationError", 

117 "type": "object", 

118 "properties": { 

119 "detail": { 

120 "title": "Detail", 

121 "type": "array", 

122 "items": { 

123 "$ref": "#/components/schemas/ValidationError" 

124 }, 

125 } 

126 }, 

127 }, 

128 } 

129 }, 

130 } 

131 )