32
32
33
33
from servicex .minio_adapter import MinioAdapter
34
34
from servicex .models import ResultFile
35
+ from pathlib import Path
35
36
36
37
DOWNLOAD_PATCH_COUNTER = 0
37
38
38
39
39
- def mock_downloader (** args ):
40
- global DOWNLOAD_PATCH_COUNTER
41
- DOWNLOAD_PATCH_COUNTER += 1
42
- if DOWNLOAD_PATCH_COUNTER == 1 :
43
- raise Exception ("lol" )
44
- elif DOWNLOAD_PATCH_COUNTER == 2 :
45
- open ("/tmp/foo/test.txt" , "wb" ).write (b"\x01 " * 5 )
46
- elif DOWNLOAD_PATCH_COUNTER == 3 :
47
- open ("/tmp/foo/test.txt" , "wb" ).write (b"\x01 " * 10 )
40
+ def make_mock_downloader (target : Path ):
41
+ def mock_downloader (** args ):
42
+ global DOWNLOAD_PATCH_COUNTER
43
+ DOWNLOAD_PATCH_COUNTER += 1
44
+ if DOWNLOAD_PATCH_COUNTER == 1 :
45
+ raise Exception ("lol" )
46
+ elif DOWNLOAD_PATCH_COUNTER == 2 :
47
+ open (target , "wb" ).write (b"\x01 " * 5 )
48
+ elif DOWNLOAD_PATCH_COUNTER == 3 :
49
+ open (target , "wb" ).write (b"\x01 " * 10 )
50
+
51
+ return mock_downloader
48
52
49
53
50
54
@fixture
@@ -65,7 +69,6 @@ async def populate_bucket(request, minio_adapter):
65
69
Bucket = minio_adapter .bucket , Key = request .param , Body = b"\x01 " * 10
66
70
)
67
71
yield
68
- await s3 .delete_object (Bucket = minio_adapter .bucket , Key = request .param )
69
72
70
73
71
74
@fixture
@@ -95,8 +98,8 @@ async def test_list_bucket(minio_adapter, populate_bucket):
95
98
96
99
@pytest .mark .parametrize ("populate_bucket" , ["test.txt" ], indirect = True )
97
100
@pytest .mark .asyncio
98
- async def test_download_file (minio_adapter , populate_bucket ):
99
- result = await minio_adapter .download_file ("test.txt" , local_dir = "/tmp/foo" )
101
+ async def test_download_file (minio_adapter , populate_bucket , tmp_path ):
102
+ result = await minio_adapter .download_file ("test.txt" , local_dir = tmp_path )
100
103
assert str (result ).endswith ("test.txt" )
101
104
assert result .exists ()
102
105
assert result .read_bytes () == (b"\x01 " * 10 )
@@ -105,10 +108,12 @@ async def test_download_file(minio_adapter, populate_bucket):
105
108
106
109
@pytest .mark .parametrize ("populate_bucket" , ["test.txt" ], indirect = True )
107
110
@pytest .mark .asyncio
108
- async def test_download_file_with_expected_size (minio_adapter , populate_bucket ):
111
+ async def test_download_file_with_expected_size (
112
+ minio_adapter , populate_bucket , tmp_path
113
+ ):
109
114
info = await minio_adapter .list_bucket ()
110
115
result = await minio_adapter .download_file (
111
- "test.txt" , local_dir = "/tmp/foo" , expected_size = info [0 ].size
116
+ "test.txt" , local_dir = tmp_path , expected_size = info [0 ].size
112
117
)
113
118
assert str (result ).endswith ("test.txt" )
114
119
assert result .exists ()
@@ -118,8 +123,8 @@ async def test_download_file_with_expected_size(minio_adapter, populate_bucket):
118
123
119
124
@pytest .mark .parametrize ("populate_bucket" , ["t::est.txt" ], indirect = True )
120
125
@pytest .mark .asyncio
121
- async def test_download_bad_filename (minio_adapter , populate_bucket ):
122
- result = await minio_adapter .download_file ("t::est.txt" , local_dir = "/tmp/foo" )
126
+ async def test_download_bad_filename (minio_adapter , populate_bucket , tmp_path ):
127
+ result = await minio_adapter .download_file ("t::est.txt" , local_dir = tmp_path )
123
128
assert str (result ).endswith ("t__est.txt" )
124
129
assert result .exists ()
125
130
assert result .read_bytes () == (b"\x01 " * 10 )
@@ -128,9 +133,11 @@ async def test_download_bad_filename(minio_adapter, populate_bucket):
128
133
129
134
@pytest .mark .parametrize ("populate_bucket" , ["test.txt" ], indirect = True )
130
135
@pytest .mark .asyncio
131
- async def test_download_short_filename_no_change (minio_adapter , populate_bucket ):
136
+ async def test_download_short_filename_no_change (
137
+ minio_adapter , populate_bucket , tmp_path
138
+ ):
132
139
result = await minio_adapter .download_file (
133
- "test.txt" , local_dir = "/tmp/foo" , shorten_filename = True
140
+ "test.txt" , local_dir = tmp_path , shorten_filename = True
134
141
)
135
142
assert str (result ).endswith ("test.txt" )
136
143
assert result .exists ()
@@ -146,10 +153,10 @@ async def test_download_short_filename_no_change(minio_adapter, populate_bucket)
146
153
indirect = True ,
147
154
)
148
155
@pytest .mark .asyncio
149
- async def test_download_short_filename_change (minio_adapter , populate_bucket ):
156
+ async def test_download_short_filename_change (minio_adapter , populate_bucket , tmp_path ):
150
157
result = await minio_adapter .download_file (
151
158
"test12345678901234567890123456789012345678901234567898012345678901234567890.txt" ,
152
- local_dir = "/tmp/foo" ,
159
+ local_dir = tmp_path ,
153
160
shorten_filename = True ,
154
161
)
155
162
@@ -164,19 +171,18 @@ async def test_download_short_filename_change(minio_adapter, populate_bucket):
164
171
result .unlink () # it should exist, from above ...
165
172
166
173
167
- @pytest .mark .parametrize ("populate_bucket" , ["test2 .txt" ], indirect = True )
174
+ @pytest .mark .parametrize ("populate_bucket" , ["test .txt" ], indirect = True )
168
175
@pytest .mark .asyncio
169
- async def test_download_repeat (minio_adapter , populate_bucket ):
176
+ async def test_download_repeat (minio_adapter , populate_bucket , tmp_path ):
170
177
import asyncio
171
178
172
- print (await minio_adapter .list_bucket ())
173
- result = await minio_adapter .download_file ("test2.txt" , local_dir = "/tmp/foo" )
174
- assert str (result ).endswith ("test2.txt" )
179
+ result = await minio_adapter .download_file ("test.txt" , local_dir = tmp_path )
180
+ assert str (result ).endswith ("test.txt" )
175
181
assert result .exists ()
176
182
t0 = result .stat ().st_mtime_ns
177
183
await asyncio .sleep (4 ) # hopefully long enough for Windows/FAT32 ... ?
178
184
179
- result2 = await minio_adapter .download_file ("test2 .txt" , local_dir = "/tmp/foo" )
185
+ result2 = await minio_adapter .download_file ("test .txt" , local_dir = tmp_path )
180
186
assert result2 .exists ()
181
187
assert result2 == result
182
188
assert t0 == result2 .stat ().st_mtime_ns
@@ -192,11 +198,12 @@ async def test_get_signed_url(minio_adapter, moto_services, populate_bucket):
192
198
193
199
@pytest .mark .parametrize ("populate_bucket" , ["test.txt" ], indirect = True )
194
200
@pytest .mark .asyncio
195
- async def test_download_file_retry (minio_adapter , populate_bucket , mocker ):
201
+ async def test_download_file_retry (minio_adapter , populate_bucket , mocker , tmp_path ):
196
202
download_patch = mocker .patch (
197
- "aioboto3.s3.inject.download_file" , side_effect = mock_downloader
203
+ "aioboto3.s3.inject.download_file" ,
204
+ side_effect = make_mock_downloader (tmp_path / "test.txt" ),
198
205
)
199
- result = await minio_adapter .download_file ("test.txt" , local_dir = "/tmp/foo" )
206
+ result = await minio_adapter .download_file ("test.txt" , local_dir = tmp_path )
200
207
assert str (result ).endswith ("test.txt" )
201
208
assert result .exists ()
202
209
assert download_patch .call_count == 3
0 commit comments