-
Notifications
You must be signed in to change notification settings - Fork 48
/
test_caching.py
364 lines (261 loc) · 10.4 KB
/
test_caching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import os
from time import sleep
import pytest
from cloudpathlib.enums import FileCacheMode
from cloudpathlib.exceptions import InvalidConfigurationException
from tests.conftest import CloudProviderTestRig
def test_defaults_work_as_expected(rig: CloudProviderTestRig):
# use client that we can delete rather than default
client = rig.client_class(**rig.required_client_kwargs)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
# default should be tmp_dir
assert cp.client.file_cache_mode == FileCacheMode.tmp_dir
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
# both exist
assert cp._local.exists()
assert cp.client._local_cache_dir.exists()
cache_path = cp._local
client_cache_dir = cp.client._local_cache_dir
del cp
# both exist
assert cache_path.exists()
assert client_cache_dir.exists()
del client
# cleaned up because client out of scope
assert not cache_path.exists()
assert not client_cache_dir.exists()
def test_close_file_mode(rig: CloudProviderTestRig):
# use client that we can delete rather than default
client = rig.client_class(
file_cache_mode=FileCacheMode.close_file, **rig.required_client_kwargs
)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
# default should be tmp_dir
assert cp.client.file_cache_mode == FileCacheMode.close_file
# download from cloud into the cache
# must use open for close_file mode
with cp.open("r") as f:
_ = f.read()
# file cache does not exist, but client folder may still be around
assert not cp._local.exists()
assert cp.client._local_cache_dir.exists()
methods_to_test = [
(cp.read_text, tuple()),
(cp.read_bytes, tuple()),
(cp.write_text, ("text",)),
(cp.write_bytes, (b"bytes",)),
]
# download from cloud into the cache with different methods
for method, method_args in methods_to_test:
assert not cp._local.exists()
method(*method_args)
# file cache does not exist, but client folder may still be around
assert not cp._local.exists()
assert cp.client._local_cache_dir.exists()
sleep(0.1) # writing twice in a row too quickly can trigger `OverwriteNewerCloudError`
def test_cloudpath_object_mode(rig: CloudProviderTestRig):
# use client that we can delete rather than default
client = rig.client_class(
file_cache_mode=FileCacheMode.cloudpath_object, **rig.required_client_kwargs
)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
assert cp.client.file_cache_mode == FileCacheMode.cloudpath_object
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
# both exist
assert cp._local.exists()
assert cp.client._local_cache_dir.exists()
cache_path = cp._local
client_cache_dir = cp.client._local_cache_dir
del cp
assert not cache_path.exists()
assert client_cache_dir.exists()
del client
assert not cache_path.exists()
assert not client_cache_dir.exists()
def test_tmp_dir_mode(rig: CloudProviderTestRig):
# use client that we can delete rather than default
client = rig.client_class(file_cache_mode=FileCacheMode.tmp_dir, **rig.required_client_kwargs)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
# default should be tmp_dir
assert cp.client.file_cache_mode == FileCacheMode.tmp_dir
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
# both exist
assert cp._local.exists()
assert cp.client._local_cache_dir.exists()
cache_path = cp._local
client_cache_dir = cp.client._local_cache_dir
del cp
# both exist
assert cache_path.exists()
assert client_cache_dir.exists()
del client
# cleaned up because client out of scope
assert not cache_path.exists()
assert not client_cache_dir.exists()
def test_persistent_mode(rig: CloudProviderTestRig, tmpdir):
client = rig.client_class(
file_cache_mode=FileCacheMode.persistent,
local_cache_dir=tmpdir,
**rig.required_client_kwargs,
)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
assert cp.client.file_cache_mode == FileCacheMode.persistent
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
# both exist
assert cp._local.exists()
assert cp.client._local_cache_dir.exists()
cache_path = cp._local
client_cache_dir = cp.client._local_cache_dir
del cp
# both exist
assert cache_path.exists()
assert client_cache_dir.exists()
del client
# both exist
assert cache_path.exists()
assert client_cache_dir.exists()
def test_interaction_with_local_cache_dir(rig: CloudProviderTestRig, tmpdir):
# cannot instantiate persistent without local file dir
with pytest.raises(InvalidConfigurationException):
client = rig.client_class(
file_cache_mode=FileCacheMode.persistent, **rig.required_client_kwargs
)
# automatically set to persitent if not specified
client = rig.client_class(local_cache_dir=tmpdir, **rig.required_client_kwargs)
assert client.file_cache_mode == FileCacheMode.persistent
# test setting close_file explicitly works
client = rig.client_class(
local_cache_dir=tmpdir,
file_cache_mode=FileCacheMode.close_file,
**rig.required_client_kwargs,
)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
assert cp.client.file_cache_mode == FileCacheMode.close_file
# download from cloud into the cache
# must use open for close_file mode
with cp.open("r") as f:
_ = f.read()
assert not cp._local.exists()
# setting cloudpath_object still works
client = rig.client_class(
local_cache_dir=tmpdir,
file_cache_mode=FileCacheMode.cloudpath_object,
**rig.required_client_kwargs,
)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
assert cp.client.file_cache_mode == FileCacheMode.cloudpath_object
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
assert cp._local.exists()
cache_path = cp._local
del cp
assert not cache_path.exists()
# setting tmp_dir still works
client = rig.client_class(
local_cache_dir=tmpdir, file_cache_mode=FileCacheMode.tmp_dir, **rig.required_client_kwargs
)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
assert cp.client.file_cache_mode == FileCacheMode.tmp_dir
# download from cloud into the cache
sleep(0.1) # test can be flaky saing that the cache dir doesn't exist yet
with cp.open("r") as f:
_ = f.read()
# both exist
assert cp._local.exists()
assert cp.client._local_cache_dir.exists()
cache_path = cp._local
client_cache_dir = cp.client._local_cache_dir
del cp
# both exist
assert cache_path.exists()
assert client_cache_dir.exists()
del client
# cleaned up because client out of scope
assert not cache_path.exists()
assert not client_cache_dir.exists()
def test_string_instantiation(rig: CloudProviderTestRig, tmpdir):
# string instantiation
for v in FileCacheMode:
local = tmpdir if v == FileCacheMode.persistent else None
client = rig.client_class(
file_cache_mode=v.value, local_cache_dir=local, **rig.required_client_kwargs
)
assert client.file_cache_mode == v
def test_environment_variable_instantiation(rig: CloudProviderTestRig, tmpdir):
# environment instantiation
original_env_setting = os.environ.get("CLOUPATHLIB_FILE_CACHE_MODE", "")
try:
for v in FileCacheMode:
os.environ["CLOUPATHLIB_FILE_CACHE_MODE"] = v.value
local = tmpdir if v == FileCacheMode.persistent else None
client = rig.client_class(local_cache_dir=local, **rig.required_client_kwargs)
assert client.file_cache_mode == v
finally:
os.environ["CLOUPATHLIB_FILE_CACHE_MODE"] = original_env_setting
def test_manual_cache_clearing(rig: CloudProviderTestRig):
# use client that we can delete rather than default
client = rig.client_class(**rig.required_client_kwargs)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
# default should be tmp_dir
assert cp.client.file_cache_mode == FileCacheMode.tmp_dir
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
# both exist
assert cp._local.exists()
assert cp.client._local_cache_dir.exists()
# clears the file itself, but not the containg folder
cp.clear_cache()
assert not cp._local.exists()
assert cp.client._local_cache_dir.exists()
# test removing parent directory
cp.fspath
assert cp._local.exists()
assert cp.parent._local.exists()
cp.parent.clear_cache()
assert not cp._local.exists()
assert not cp.parent._local.exists()
# download two files from cloud into the cache
cp.fspath
rig.create_cloud_path("dir_0/file0_1.txt", client=client).fspath
# 2 files present in cache folder
assert len(list(filter(lambda x: x.is_file(), client._local_cache_dir.rglob("*")))) == 2
# clears all files inside the folder, but containing folder still exists
client.clear_cache()
assert len(list(filter(lambda x: x.is_file(), client._local_cache_dir.rglob("*")))) == 0
# also removes containing folder on client cleanted up
local_cache_path = cp._local
client_cache_folder = client._local_cache_dir
del cp
del client
assert not local_cache_path.exists()
assert not client_cache_folder.exists()
def test_reuse_cache_after_manual_cache_clear(rig: CloudProviderTestRig):
# use client that we can delete rather than default
client = rig.client_class(**rig.required_client_kwargs)
cp = rig.create_cloud_path("dir_0/file0_0.txt", client=client)
# default should be tmp_dir
assert cp.client.file_cache_mode == FileCacheMode.tmp_dir
# download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
cp.clear_cache()
assert not cp._local.exists()
# re-download from cloud into the cache
with cp.open("r") as f:
_ = f.read()
client.clear_cache()
assert not cp._local.exists()
# re-download from cloud into the cache, no error
with cp.open("r") as f:
_ = f.read()
assert cp._local.exists()