forked from fsspec/filesystem_spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_spec.py
372 lines (311 loc) · 11.1 KB
/
test_spec.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
365
366
367
368
369
370
371
372
import json
import pickle
import numpy as np
import pytest
import fsspec
from fsspec.implementations.ftp import FTPFileSystem
from fsspec.implementations.http import HTTPFileSystem
from fsspec.spec import AbstractBufferedFile, AbstractFileSystem
class DummyTestFS(AbstractFileSystem):
protocol = "mock"
_fs_contents = (
{"name": "top_level", "type": "directory"},
{"name": "top_level/second_level", "type": "directory"},
{"name": "top_level/second_level/date=2019-10-01", "type": "directory"},
{
"name": "top_level/second_level/date=2019-10-01/a.parquet",
"type": "file",
"size": 100,
},
{
"name": "top_level/second_level/date=2019-10-01/b.parquet",
"type": "file",
"size": 100,
},
{"name": "top_level/second_level/date=2019-10-02", "type": "directory"},
{
"name": "top_level/second_level/date=2019-10-02/a.parquet",
"type": "file",
"size": 100,
},
{"name": "top_level/second_level/date=2019-10-04", "type": "directory"},
{
"name": "top_level/second_level/date=2019-10-04/a.parquet",
"type": "file",
"size": 100,
},
{"name": "misc", "type": "directory"},
{"name": "misc/foo.txt", "type": "file", "size": 100},
{"name": "glob_test", "type": "directory", "size": 0},
{"name": "glob_test/hat", "type": "directory", "size": 0},
{"name": "glob_test/hat/^foo.txt", "type": "file", "size": 100},
{"name": "glob_test/dollar", "type": "directory", "size": 0},
{"name": "glob_test/dollar/$foo.txt", "type": "file", "size": 100},
{"name": "glob_test/lbrace", "type": "directory", "size": 0},
{"name": "glob_test/lbrace/{foo.txt", "type": "file", "size": 100},
{"name": "glob_test/rbrace", "type": "directory", "size": 0},
{"name": "glob_test/rbrace/}foo.txt", "type": "file", "size": 100},
)
def __getitem__(self, name):
for item in self._fs_contents:
if item["name"] == name:
return item
raise IndexError("{name} not found!".format(name=name))
def ls(self, path, detail=True, refresh=True, **kwargs):
if kwargs.pop("strip_proto", True):
path = self._strip_protocol(path)
files = not refresh and self._ls_from_cache(path)
if not files:
files = [
file for file in self._fs_contents if path == self._parent(file["name"])
]
files.sort(key=lambda file: file["name"])
self.dircache[path.rstrip("/")] = files
if detail:
return files
return [file["name"] for file in files]
@classmethod
def get_test_paths(cls, start_with=""):
"""Helper to return directory and file paths with no details"""
all = [
file["name"]
for file in cls._fs_contents
if file["name"].startswith(start_with)
]
return all
@pytest.mark.parametrize(
"test_path, expected",
[
(
"mock://top_level/second_level/date=2019-10-01/a.parquet",
["top_level/second_level/date=2019-10-01/a.parquet"],
),
(
"mock://top_level/second_level/date=2019-10-01/*",
[
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-01/b.parquet",
],
),
("mock://top_level/second_level/date=2019-10", []),
(
"mock://top_level/second_level/date=2019-10-0[1-4]",
[
"top_level/second_level/date=2019-10-01",
"top_level/second_level/date=2019-10-02",
"top_level/second_level/date=2019-10-04",
],
),
(
"mock://top_level/second_level/date=2019-10-0[1-4]/*",
[
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-01/b.parquet",
"top_level/second_level/date=2019-10-02/a.parquet",
"top_level/second_level/date=2019-10-04/a.parquet",
],
),
(
"mock://top_level/second_level/date=2019-10-0[1-4]/[a].*",
[
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-02/a.parquet",
"top_level/second_level/date=2019-10-04/a.parquet",
],
),
("mock://glob_test/hat/^foo.*", ["glob_test/hat/^foo.txt"]),
("mock://glob_test/dollar/$foo.*", ["glob_test/dollar/$foo.txt"]),
("mock://glob_test/lbrace/{foo.*", ["glob_test/lbrace/{foo.txt"]),
("mock://glob_test/rbrace/}foo.*", ["glob_test/rbrace/}foo.txt"]),
],
)
def test_glob(test_path, expected):
test_fs = DummyTestFS()
res = test_fs.glob(test_path)
res = sorted(res) # FIXME: py35 back-compat
assert res == expected
res = test_fs.glob(test_path, detail=True)
assert isinstance(res, dict)
assert sorted(res) == expected # FIXME: py35 back-compat
for name, info in res.items():
assert info == test_fs[name]
@pytest.mark.parametrize(
["test_paths", "expected"],
[
(
("top_level/second_level", "top_level/sec*", "top_level/*"),
[
"top_level/second_level",
"top_level/second_level/date=2019-10-01",
"top_level/second_level/date=2019-10-01/a.parquet",
"top_level/second_level/date=2019-10-01/b.parquet",
"top_level/second_level/date=2019-10-02",
"top_level/second_level/date=2019-10-02/a.parquet",
"top_level/second_level/date=2019-10-04",
"top_level/second_level/date=2019-10-04/a.parquet",
],
),
(("misc/foo.txt", "misc/*.txt"), ["misc/foo.txt"]),
(
("",),
DummyTestFS.get_test_paths() + [DummyTestFS.root_marker],
),
],
# ids=["all_second_level", "single_file"],
)
def test_expand_path_recursive(test_paths, expected):
"""Test a number of paths and then their combination which should all yield
the same set of expanded paths"""
test_fs = DummyTestFS()
# test single query
for test_path in test_paths:
paths = test_fs.expand_path(test_path, recursive=True)
assert sorted(paths) == sorted(expected)
# test with all queries
paths = test_fs.expand_path(list(test_paths), recursive=True)
assert sorted(paths) == sorted(expected)
def test_find():
""" Test .find() method on debian server (ftp, https) with constant folder """
filesystem, host, test_path = (
FTPFileSystem,
"ftp.fau.de",
"ftp://ftp.fau.de/debian-cd/current/amd64/log/success",
)
test_fs = filesystem(host)
filenames_ftp = test_fs.find(test_path)
assert filenames_ftp
filesystem, host, test_path = (
HTTPFileSystem,
"https://ftp.fau.de",
"https://ftp.fau.de/debian-cd/current/amd64/log/success",
)
test_fs = filesystem()
filenames_http = test_fs.find(test_path)
roots = [f.rsplit("/", 1)[-1] for f in filenames_http]
assert all(f.rsplit("/", 1)[-1] in roots for f in filenames_ftp)
def test_find_details():
test_fs = DummyTestFS()
filenames = test_fs.find("/")
details = test_fs.find("/", detail=True)
for filename in filenames:
assert details[filename] == test_fs.info(filename)
def test_cache():
fs = DummyTestFS()
fs2 = DummyTestFS()
assert fs is fs2
assert len(fs._cache) == 1
del fs2
assert len(fs._cache) == 1
del fs
assert len(DummyTestFS._cache) == 1
DummyTestFS.clear_instance_cache()
assert len(DummyTestFS._cache) == 0
def test_alias():
with pytest.warns(FutureWarning, match="add_aliases"):
DummyTestFS(add_aliases=True)
def test_add_docs_warns():
with pytest.warns(FutureWarning, match="add_docs"):
AbstractFileSystem(add_docs=True)
def test_cache_options():
fs = DummyTestFS()
f = AbstractBufferedFile(fs, "misc/foo.txt", cache_type="bytes")
assert f.cache.trim
# TODO: dummy buffered file
f = AbstractBufferedFile(
fs, "misc/foo.txt", cache_type="bytes", cache_options=dict(trim=False)
)
assert f.cache.trim is False
f = fs.open("misc/foo.txt", cache_type="bytes", cache_options=dict(trim=False))
assert f.cache.trim is False
def test_trim_kwarg_warns():
fs = DummyTestFS()
with pytest.warns(FutureWarning, match="cache_options"):
AbstractBufferedFile(fs, "misc/foo.txt", cache_type="bytes", trim=False)
def test_eq():
fs = DummyTestFS()
result = fs == 1
assert result is False
def test_pickle_multiple():
a = DummyTestFS(1)
b = DummyTestFS(2, bar=1)
x = pickle.dumps(a)
y = pickle.dumps(b)
del a, b
DummyTestFS.clear_instance_cache()
result = pickle.loads(x)
assert result.storage_args == (1,)
assert result.storage_options == {}
result = pickle.loads(y)
assert result.storage_args == (2,)
assert result.storage_options == dict(bar=1)
def test_json():
a = DummyTestFS(1)
b = DummyTestFS(2, bar=1)
outa = a.to_json()
outb = b.to_json()
assert json.loads(outb) # is valid JSON
assert a != b
assert "bar" in outb
assert DummyTestFS.from_json(outa) is a
assert DummyTestFS.from_json(outb) is b
def test_ls_from_cache():
fs = DummyTestFS()
uncached_results = fs.ls("top_level/second_level/", refresh=True)
assert fs.ls("top_level/second_level/", refresh=False) == uncached_results
# _strip_protocol removes everything by default though
# for the sake of testing the _ls_from_cache interface
# directly, we need run one time more without that call
# to actually verify that our stripping in the client
# function works.
assert (
fs.ls("top_level/second_level/", refresh=False, strip_proto=False)
== uncached_results
)
@pytest.mark.parametrize(
"dt",
[
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.float32,
np.float64,
],
)
def test_readinto_with_numpy(tmpdir, dt):
store_path = str(tmpdir / "test_arr.npy")
arr = np.arange(10, dtype=dt)
arr.tofile(store_path)
arr2 = np.empty_like(arr)
with fsspec.open(store_path, "rb") as f:
f.readinto(arr2)
assert np.array_equal(arr, arr2)
@pytest.mark.parametrize(
"dt",
[
np.int8,
np.int16,
np.int32,
np.int64,
np.uint8,
np.uint16,
np.uint32,
np.uint64,
np.float32,
np.float64,
],
)
def test_readinto_with_multibyte(ftp_writable, tmpdir, dt):
host, port, user, pw = ftp_writable
ftp = FTPFileSystem(host=host, port=port, username=user, password=pw)
with ftp.open("/out", "wb") as fp:
arr = np.arange(10, dtype=dt)
fp.write(arr.tobytes())
with ftp.open("/out", "rb") as fp:
arr2 = np.empty_like(arr)
fp.readinto(arr2)
assert np.array_equal(arr, arr2)