Coverage for tests / test_tutorial / test_additional_responses / test_tutorial002.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("tutorial002_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 "200": { 

53 "description": "Return the JSON item or an image.", 

54 "content": { 

55 "image/png": {}, 

56 "application/json": { 

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

58 }, 

59 }, 

60 }, 

61 "422": { 

62 "description": "Validation Error", 

63 "content": { 

64 "application/json": { 

65 "schema": { 

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

67 } 

68 } 

69 }, 

70 }, 

71 }, 

72 "summary": "Read Item", 

73 "operationId": "read_item_items__item_id__get", 

74 "parameters": [ 

75 { 

76 "required": True, 

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

78 "name": "item_id", 

79 "in": "path", 

80 }, 

81 { 

82 "required": False, 

83 "schema": { 

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

85 "title": "Img", 

86 }, 

87 "name": "img", 

88 "in": "query", 

89 }, 

90 ], 

91 } 

92 } 

93 }, 

94 "components": { 

95 "schemas": { 

96 "Item": { 

97 "title": "Item", 

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

99 "type": "object", 

100 "properties": { 

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

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

103 }, 

104 }, 

105 "ValidationError": { 

106 "title": "ValidationError", 

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

108 "type": "object", 

109 "properties": { 

110 "loc": { 

111 "title": "Location", 

112 "type": "array", 

113 "items": { 

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

115 }, 

116 }, 

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

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

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

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

121 }, 

122 }, 

123 "HTTPValidationError": { 

124 "title": "HTTPValidationError", 

125 "type": "object", 

126 "properties": { 

127 "detail": { 

128 "title": "Detail", 

129 "type": "array", 

130 "items": { 

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

132 }, 

133 } 

134 }, 

135 }, 

136 } 

137 }, 

138 } 

139 )