Coverage for tests / test_tutorial / test_additional_responses / test_tutorial001.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.tutorial001_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": "Not Found", 

34 "content": { 

35 "application/json": { 

36 "schema": { 

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

38 } 

39 } 

40 }, 

41 }, 

42 "200": { 

43 "description": "Successful Response", 

44 "content": { 

45 "application/json": { 

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

47 } 

48 }, 

49 }, 

50 "422": { 

51 "description": "Validation Error", 

52 "content": { 

53 "application/json": { 

54 "schema": { 

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

56 } 

57 } 

58 }, 

59 }, 

60 }, 

61 "summary": "Read Item", 

62 "operationId": "read_item_items__item_id__get", 

63 "parameters": [ 

64 { 

65 "required": True, 

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

67 "name": "item_id", 

68 "in": "path", 

69 } 

70 ], 

71 } 

72 } 

73 }, 

74 "components": { 

75 "schemas": { 

76 "Item": { 

77 "title": "Item", 

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

79 "type": "object", 

80 "properties": { 

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

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

83 }, 

84 }, 

85 "Message": { 

86 "title": "Message", 

87 "required": ["message"], 

88 "type": "object", 

89 "properties": { 

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

91 }, 

92 }, 

93 "ValidationError": { 

94 "title": "ValidationError", 

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

96 "type": "object", 

97 "properties": { 

98 "loc": { 

99 "title": "Location", 

100 "type": "array", 

101 "items": { 

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

103 }, 

104 }, 

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

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

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

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

109 }, 

110 }, 

111 "HTTPValidationError": { 

112 "title": "HTTPValidationError", 

113 "type": "object", 

114 "properties": { 

115 "detail": { 

116 "title": "Detail", 

117 "type": "array", 

118 "items": { 

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

120 }, 

121 } 

122 }, 

123 }, 

124 } 

125 }, 

126 } 

127 )