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

29 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2import os 1abcd

3import shutil 1abcd

4 

5import pytest 1abcd

6from fastapi.testclient import TestClient 1abcd

7from inline_snapshot import snapshot 1abcd

8 

9from tests.utils import needs_py310, workdir_lock 1abcd

10 

11 

12@pytest.fixture( 1abcd

13 name="client", 

14 params=[ 

15 pytest.param("tutorial004_py310", marks=needs_py310), 

16 ], 

17) 

18def get_client(request: pytest.FixtureRequest): 1abcd

19 mod = importlib.import_module(f"docs_src.additional_responses.{request.param}") 1ijklmnopqrs

20 

21 client = TestClient(mod.app) 1ijklmnopqrs

22 client.headers.clear() 1ijklmnopqrs

23 return client 1ijklmnopqrs

24 

25 

26def test_path_operation(client: TestClient): 1abcd

27 response = client.get("/items/foo") 1tuvw

28 assert response.status_code == 200, response.text 1tuvw

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

30 

31 

32@workdir_lock 1abcd

33def test_path_operation_img(client: TestClient): 1abcd

34 shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") 1efgh

35 response = client.get("/items/foo?img=1") 1efgh

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

37 assert response.headers["Content-Type"] == "image/png" 1efgh

38 assert len(response.content) 1efgh

39 os.remove("./image.png") 1efgh

40 

41 

42def test_openapi_schema(client: TestClient): 1abcd

43 response = client.get("/openapi.json") 1xyzA

44 assert response.status_code == 200, response.text 1xyzA

45 assert response.json() == snapshot( 1xyzA

46 { 

47 "openapi": "3.1.0", 

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

49 "paths": { 

50 "/items/{item_id}": { 

51 "get": { 

52 "responses": { 

53 "404": {"description": "Item not found"}, 

54 "302": {"description": "The item was moved"}, 

55 "403": {"description": "Not enough privileges"}, 

56 "200": { 

57 "description": "Successful Response", 

58 "content": { 

59 "image/png": {}, 

60 "application/json": { 

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

62 }, 

63 }, 

64 }, 

65 "422": { 

66 "description": "Validation Error", 

67 "content": { 

68 "application/json": { 

69 "schema": { 

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

71 } 

72 } 

73 }, 

74 }, 

75 }, 

76 "summary": "Read Item", 

77 "operationId": "read_item_items__item_id__get", 

78 "parameters": [ 

79 { 

80 "required": True, 

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

82 "name": "item_id", 

83 "in": "path", 

84 }, 

85 { 

86 "required": False, 

87 "schema": { 

88 "anyOf": [{"type": "boolean"}, {"type": "null"}], 

89 "title": "Img", 

90 }, 

91 "name": "img", 

92 "in": "query", 

93 }, 

94 ], 

95 } 

96 } 

97 }, 

98 "components": { 

99 "schemas": { 

100 "Item": { 

101 "title": "Item", 

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

103 "type": "object", 

104 "properties": { 

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

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

107 }, 

108 }, 

109 "ValidationError": { 

110 "title": "ValidationError", 

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

112 "type": "object", 

113 "properties": { 

114 "loc": { 

115 "title": "Location", 

116 "type": "array", 

117 "items": { 

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

119 }, 

120 }, 

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

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

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

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

125 }, 

126 }, 

127 "HTTPValidationError": { 

128 "title": "HTTPValidationError", 

129 "type": "object", 

130 "properties": { 

131 "detail": { 

132 "title": "Detail", 

133 "type": "array", 

134 "items": { 

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

136 }, 

137 } 

138 }, 

139 }, 

140 } 

141 }, 

142 } 

143 )