1+ from pathlib import Path
12from typing import Any
23from unittest import mock
34
@@ -11,19 +12,17 @@ def test_using_gzip_if_header_present_and_file_available(loop: Any) -> None:
1112 "GET" , "http://python.org/logo.png" , headers = {hdrs .ACCEPT_ENCODING : "gzip" }
1213 )
1314
14- gz_filepath = mock .Mock ()
15- gz_filepath .open = mock .mock_open ()
15+ gz_filepath = mock .create_autospec (Path , spec_set = True )
1616 gz_filepath .is_file .return_value = True
17- gz_filepath .stat .return_value = mock .MagicMock ()
1817 gz_filepath .stat .return_value .st_size = 1024
1918 gz_filepath .stat .return_value .st_mtime_ns = 1603733507222449291
2019
21- filepath = mock .Mock ( )
20+ filepath = mock .create_autospec ( Path , spec_set = True )
2221 filepath .name = "logo.png"
23- filepath .open = mock .mock_open ()
2422 filepath .with_name .return_value = gz_filepath
2523
2624 file_sender = FileResponse (filepath )
25+ file_sender ._path = filepath
2726 file_sender ._sendfile = make_mocked_coro (None ) # type: ignore[assignment]
2827
2928 loop .run_until_complete (file_sender .prepare (request ))
@@ -35,19 +34,17 @@ def test_using_gzip_if_header_present_and_file_available(loop: Any) -> None:
3534def test_gzip_if_header_not_present_and_file_available (loop : Any ) -> None :
3635 request = make_mocked_request ("GET" , "http://python.org/logo.png" , headers = {})
3736
38- gz_filepath = mock .Mock ()
39- gz_filepath .open = mock .mock_open ()
37+ gz_filepath = mock .create_autospec (Path , spec_set = True )
4038 gz_filepath .is_file .return_value = True
4139
42- filepath = mock .Mock ( )
40+ filepath = mock .create_autospec ( Path , spec_set = True )
4341 filepath .name = "logo.png"
44- filepath .open = mock .mock_open ()
4542 filepath .with_name .return_value = gz_filepath
46- filepath .stat .return_value = mock .MagicMock ()
4743 filepath .stat .return_value .st_size = 1024
4844 filepath .stat .return_value .st_mtime_ns = 1603733507222449291
4945
5046 file_sender = FileResponse (filepath )
47+ file_sender ._path = filepath
5148 file_sender ._sendfile = make_mocked_coro (None ) # type: ignore[assignment]
5249
5350 loop .run_until_complete (file_sender .prepare (request ))
@@ -59,19 +56,17 @@ def test_gzip_if_header_not_present_and_file_available(loop: Any) -> None:
5956def test_gzip_if_header_not_present_and_file_not_available (loop : Any ) -> None :
6057 request = make_mocked_request ("GET" , "http://python.org/logo.png" , headers = {})
6158
62- gz_filepath = mock .Mock ()
63- gz_filepath .open = mock .mock_open ()
59+ gz_filepath = mock .create_autospec (Path , spec_set = True )
6460 gz_filepath .is_file .return_value = False
6561
66- filepath = mock .Mock ( )
62+ filepath = mock .create_autospec ( Path , spec_set = True )
6763 filepath .name = "logo.png"
68- filepath .open = mock .mock_open ()
6964 filepath .with_name .return_value = gz_filepath
70- filepath .stat .return_value = mock .MagicMock ()
7165 filepath .stat .return_value .st_size = 1024
7266 filepath .stat .return_value .st_mtime_ns = 1603733507222449291
7367
7468 file_sender = FileResponse (filepath )
69+ file_sender ._path = filepath
7570 file_sender ._sendfile = make_mocked_coro (None ) # type: ignore[assignment]
7671
7772 loop .run_until_complete (file_sender .prepare (request ))
@@ -85,19 +80,17 @@ def test_gzip_if_header_present_and_file_not_available(loop: Any) -> None:
8580 "GET" , "http://python.org/logo.png" , headers = {hdrs .ACCEPT_ENCODING : "gzip" }
8681 )
8782
88- gz_filepath = mock .Mock ()
89- gz_filepath .open = mock .mock_open ()
83+ gz_filepath = mock .create_autospec (Path , spec_set = True )
9084 gz_filepath .is_file .return_value = False
9185
92- filepath = mock .Mock ( )
86+ filepath = mock .create_autospec ( Path , spec_set = True )
9387 filepath .name = "logo.png"
94- filepath .open = mock .mock_open ()
9588 filepath .with_name .return_value = gz_filepath
96- filepath .stat .return_value = mock .MagicMock ()
9789 filepath .stat .return_value .st_size = 1024
9890 filepath .stat .return_value .st_mtime_ns = 1603733507222449291
9991
10092 file_sender = FileResponse (filepath )
93+ file_sender ._path = filepath
10194 file_sender ._sendfile = make_mocked_coro (None ) # type: ignore[assignment]
10295
10396 loop .run_until_complete (file_sender .prepare (request ))
@@ -109,14 +102,13 @@ def test_gzip_if_header_present_and_file_not_available(loop: Any) -> None:
109102def test_status_controlled_by_user (loop : Any ) -> None :
110103 request = make_mocked_request ("GET" , "http://python.org/logo.png" , headers = {})
111104
112- filepath = mock .Mock ( )
105+ filepath = mock .create_autospec ( Path , spec_set = True )
113106 filepath .name = "logo.png"
114- filepath .open = mock .mock_open ()
115- filepath .stat .return_value = mock .MagicMock ()
116107 filepath .stat .return_value .st_size = 1024
117108 filepath .stat .return_value .st_mtime_ns = 1603733507222449291
118109
119110 file_sender = FileResponse (filepath , status = 203 )
111+ file_sender ._path = filepath
120112 file_sender ._sendfile = make_mocked_coro (None ) # type: ignore[assignment]
121113
122114 loop .run_until_complete (file_sender .prepare (request ))
0 commit comments