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

28 statements  

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

1import importlib 1abdc

2import os 1abdc

3import shutil 1abdc

4 

5import pytest 1abdc

6from fastapi.testclient import TestClient 1abdc

7from inline_snapshot import snapshot 1abdc

8 

9from tests.utils import needs_py310 1abdc

10 

11 

12@pytest.fixture( 1abdc

13 name="client", 

14 params=[ 

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

16 ], 

17) 

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

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

20 

21 client = TestClient(mod.app) 1abc

22 client.headers.clear() 1abc

23 return client 1abc

24 

25 

26def test_path_operation(client: TestClient): 1abdc

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

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

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

30 

31 

32def test_path_operation_img(client: TestClient): 1abdc

33 shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") 1efg

34 response = client.get("/items/foo?img=1") 1efg

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

36 assert response.headers["Content-Type"] == "image/png" 1efg

37 assert len(response.content) 1efg

38 os.remove("./image.png") 1efg

39 

40 

41def test_openapi_schema(client: TestClient): 1abdc

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

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

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

45 { 

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/items/{item_id}": { 

50 "get": { 

51 "responses": { 

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

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

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

55 "200": { 

56 "description": "Successful Response", 

57 "content": { 

58 "image/png": {}, 

59 "application/json": { 

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

61 }, 

62 }, 

63 }, 

64 "422": { 

65 "description": "Validation Error", 

66 "content": { 

67 "application/json": { 

68 "schema": { 

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

70 } 

71 } 

72 }, 

73 }, 

74 }, 

75 "summary": "Read Item", 

76 "operationId": "read_item_items__item_id__get", 

77 "parameters": [ 

78 { 

79 "required": True, 

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

81 "name": "item_id", 

82 "in": "path", 

83 }, 

84 { 

85 "required": False, 

86 "schema": { 

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

88 "title": "Img", 

89 }, 

90 "name": "img", 

91 "in": "query", 

92 }, 

93 ], 

94 } 

95 } 

96 }, 

97 "components": { 

98 "schemas": { 

99 "Item": { 

100 "title": "Item", 

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

102 "type": "object", 

103 "properties": { 

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

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

106 }, 

107 }, 

108 "ValidationError": { 

109 "title": "ValidationError", 

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

111 "type": "object", 

112 "properties": { 

113 "loc": { 

114 "title": "Location", 

115 "type": "array", 

116 "items": { 

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

118 }, 

119 }, 

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

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

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

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

124 }, 

125 }, 

126 "HTTPValidationError": { 

127 "title": "HTTPValidationError", 

128 "type": "object", 

129 "properties": { 

130 "detail": { 

131 "title": "Detail", 

132 "type": "array", 

133 "items": { 

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

135 }, 

136 } 

137 }, 

138 }, 

139 } 

140 }, 

141 } 

142 )