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

21 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import os 1abcde

2import shutil 1abcde

3 

4from dirty_equals import IsDict 1abcde

5from fastapi.testclient import TestClient 1abcde

6 

7from docs_src.additional_responses.tutorial004 import app 1abcde

8 

9client = TestClient(app) 1abcde

10 

11 

12def test_path_operation(): 1abcde

13 response = client.get("/items/foo") 1abcde

14 assert response.status_code == 200, response.text 1abcde

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

16 

17 

18def test_path_operation_img(): 1abcde

19 shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") 1abcde

20 response = client.get("/items/foo?img=1") 1abcde

21 assert response.status_code == 200, response.text 1abcde

22 assert response.headers["Content-Type"] == "image/png" 1abcde

23 assert len(response.content) 1abcde

24 os.remove("./image.png") 1abcde

25 

26 

27def test_openapi_schema(): 1abcde

28 response = client.get("/openapi.json") 1abcde

29 assert response.status_code == 200, response.text 1abcde

30 assert response.json() == { 1abcde

31 "openapi": "3.1.0", 

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

33 "paths": { 

34 "/items/{item_id}": { 

35 "get": { 

36 "responses": { 

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

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

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

40 "200": { 

41 "description": "Successful Response", 

42 "content": { 

43 "image/png": {}, 

44 "application/json": { 

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

46 }, 

47 }, 

48 }, 

49 "422": { 

50 "description": "Validation Error", 

51 "content": { 

52 "application/json": { 

53 "schema": { 

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

55 } 

56 } 

57 }, 

58 }, 

59 }, 

60 "summary": "Read Item", 

61 "operationId": "read_item_items__item_id__get", 

62 "parameters": [ 

63 { 

64 "required": True, 

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

66 "name": "item_id", 

67 "in": "path", 

68 }, 

69 { 

70 "required": False, 

71 "schema": IsDict( 

72 { 

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

74 "title": "Img", 

75 } 

76 ) 

77 | IsDict( 

78 # TODO: remove when deprecating Pydantic v1 

79 {"title": "Img", "type": "boolean"} 

80 ), 

81 "name": "img", 

82 "in": "query", 

83 }, 

84 ], 

85 } 

86 } 

87 }, 

88 "components": { 

89 "schemas": { 

90 "Item": { 

91 "title": "Item", 

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

93 "type": "object", 

94 "properties": { 

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

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

97 }, 

98 }, 

99 "ValidationError": { 

100 "title": "ValidationError", 

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

102 "type": "object", 

103 "properties": { 

104 "loc": { 

105 "title": "Location", 

106 "type": "array", 

107 "items": { 

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

109 }, 

110 }, 

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

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

113 }, 

114 }, 

115 "HTTPValidationError": { 

116 "title": "HTTPValidationError", 

117 "type": "object", 

118 "properties": { 

119 "detail": { 

120 "title": "Detail", 

121 "type": "array", 

122 "items": {"$ref": "#/components/schemas/ValidationError"}, 

123 } 

124 }, 

125 }, 

126 } 

127 }, 

128 }