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

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

39 "content": { 

40 "image/png": {}, 

41 "application/json": { 

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

43 }, 

44 }, 

45 }, 

46 "422": { 

47 "description": "Validation Error", 

48 "content": { 

49 "application/json": { 

50 "schema": { 

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

52 } 

53 } 

54 }, 

55 }, 

56 }, 

57 "summary": "Read Item", 

58 "operationId": "read_item_items__item_id__get", 

59 "parameters": [ 

60 { 

61 "required": True, 

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

63 "name": "item_id", 

64 "in": "path", 

65 }, 

66 { 

67 "required": False, 

68 "schema": IsDict( 

69 { 

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

71 "title": "Img", 

72 } 

73 ) 

74 | IsDict( 

75 # TODO: remove when deprecating Pydantic v1 

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

77 ), 

78 "name": "img", 

79 "in": "query", 

80 }, 

81 ], 

82 } 

83 } 

84 }, 

85 "components": { 

86 "schemas": { 

87 "Item": { 

88 "title": "Item", 

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

90 "type": "object", 

91 "properties": { 

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

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

94 }, 

95 }, 

96 "ValidationError": { 

97 "title": "ValidationError", 

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

99 "type": "object", 

100 "properties": { 

101 "loc": { 

102 "title": "Location", 

103 "type": "array", 

104 "items": { 

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

106 }, 

107 }, 

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

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

110 }, 

111 }, 

112 "HTTPValidationError": { 

113 "title": "HTTPValidationError", 

114 "type": "object", 

115 "properties": { 

116 "detail": { 

117 "title": "Detail", 

118 "type": "array", 

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

120 } 

121 }, 

122 }, 

123 } 

124 }, 

125 }