-
Notifications
You must be signed in to change notification settings - Fork 3
/
file_test.py
376 lines (304 loc) · 12.4 KB
/
file_test.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
373
374
375
376
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 SciCat Project (https://github.com/SciCatProject/scitacean)
import hashlib
from dataclasses import replace
from datetime import datetime, timedelta, timezone
from pathlib import Path
import pytest
from dateutil.parser import parse as parse_date
from scitacean import File, IntegrityError, RemotePath
from scitacean.file import checksum_of_file
from scitacean.logging import logger_name
from scitacean.model import DataFile
from .common.files import make_file
@pytest.fixture
def fake_file(fs):
return make_file(fs, path=Path("local", "dir", "events.nxs"))
def test_file_from_local(fake_file):
file = replace(File.from_local(fake_file["path"]), checksum_algorithm="md5")
assert file.remote_access_path("/remote") is None
assert file.local_path == fake_file["path"]
assert file.remote_path == fake_file["path"].as_posix()
assert file.checksum() == fake_file["checksum"]
assert file.size == fake_file["size"]
assert file.remote_uid is None
assert file.remote_gid is None
assert file.remote_perm is None
assert abs(fake_file["creation_time"] - file.creation_time) < timedelta(seconds=1)
def test_file_from_local_with_base_path(fake_file):
assert fake_file["path"] == Path("local", "dir", "events.nxs") # used below
file = replace(
File.from_local(fake_file["path"], base_path="local"), checksum_algorithm="md5"
)
assert file.remote_access_path("/remote") is None
assert file.local_path == fake_file["path"]
assert file.remote_path == "dir/events.nxs"
assert file.checksum() == fake_file["checksum"]
assert file.size == fake_file["size"]
assert file.remote_uid is None
assert file.remote_gid is None
assert file.remote_perm is None
assert abs(fake_file["creation_time"] - file.creation_time) < timedelta(seconds=1)
def test_file_from_local_set_remote_path(fake_file):
file = replace(
File.from_local(fake_file["path"], remote_path="remote/location/file.nxs"),
checksum_algorithm="md5",
)
assert file.remote_access_path("/remote") is None
assert file.local_path == fake_file["path"]
assert file.remote_path == "remote/location/file.nxs"
assert file.checksum() == fake_file["checksum"]
assert file.size == fake_file["size"]
assert file.remote_uid is None
assert file.remote_gid is None
assert file.remote_perm is None
assert abs(fake_file["creation_time"] - file.creation_time) < timedelta(seconds=1)
def test_file_from_local_set_many_args(fake_file):
file = replace(
File.from_local(
fake_file["path"],
base_path="local",
remote_uid="user-usy",
remote_gid="groupy-group",
remote_perm="wrx",
),
checksum_algorithm="md5",
)
assert file.remote_access_path("/remote") is None
assert file.local_path == fake_file["path"]
assert file.checksum() == fake_file["checksum"]
assert file.size == fake_file["size"]
assert file.remote_uid == "user-usy"
assert file.remote_gid == "groupy-group"
assert file.remote_perm == "wrx"
assert abs(fake_file["creation_time"] - file.creation_time) < timedelta(seconds=1)
@pytest.mark.parametrize("alg", ("md5", "sha256", "blake2s"))
def test_file_from_local_select_checksum_algorithm(fake_file, alg):
file = replace(File.from_local(fake_file["path"]), checksum_algorithm=alg)
expected = checksum_of_file(fake_file["path"], algorithm=alg)
assert file.checksum() == expected
def test_file_from_local_remote_path_uses_forward_slash(fs):
fs.create_file(Path("data", "subdir", "file.dat"))
file = File.from_local(Path("data", "subdir", "file.dat"))
assert file.remote_path == RemotePath("data/subdir/file.dat")
assert file.make_model().path == "data/subdir/file.dat"
file = File.from_local(Path("data", "subdir", "file.dat"), base_path=Path("data"))
assert file.remote_path == RemotePath("subdir/file.dat")
assert file.make_model().path == "subdir/file.dat"
file = File.from_local(
Path("data", "subdir", "file.dat"), base_path=Path("data") / "subdir"
)
assert file.remote_path == RemotePath("file.dat")
assert file.make_model().path == "file.dat"
def test_file_from_scicat():
model = DataFile(
path="dir/image.jpg", size=12345, time=parse_date("2022-06-22T15:42:53.123Z")
)
file = File.from_scicat(model)
assert file.remote_access_path("/remote/folder") == "/remote/folder/dir/image.jpg"
assert file.local_path is None
assert file.checksum() is None
assert file.size == 12345
assert file.creation_time == parse_date("2022-06-22T15:42:53.123Z")
def test_file_from_scicat_remote_path_uses_forward_slash():
file = File.from_scicat(
DataFile(
path="data/subdir/file.dat",
size=0,
time=parse_date("2022-06-22T15:42:53.123Z"),
),
local_path=None,
)
assert file.remote_path == RemotePath("data/subdir/file.dat")
file = File.from_scicat(
DataFile(
path="data/subdir/file.dat",
size=0,
time=parse_date("2022-06-22T15:42:53.123Z"),
),
local_path=Path("data") / "subdir",
)
assert file.remote_path == RemotePath("data/subdir/file.dat")
def test_make_model_local_file(fake_file):
file = replace(
File.from_local(
fake_file["path"],
remote_uid="user-usy",
remote_gid="groupy-group",
remote_perm="wrx",
),
checksum_algorithm="blake2s",
)
model = file.make_model()
assert model.path == fake_file["path"].as_posix()
assert model.size == fake_file["size"]
assert model.chk == checksum_of_file(fake_file["path"], algorithm="blake2s")
assert model.gid == "groupy-group"
assert model.perm == "wrx"
assert model.uid == "user-usy"
assert abs(fake_file["creation_time"] - model.time) < timedelta(seconds=1)
def test_uploaded(fake_file):
file = replace(
File.from_local(
fake_file["path"],
remote_uid="the-user",
),
checksum_algorithm="sha256",
)
uploaded = file.uploaded(
remote_gid="the-group", remote_creation_time=parse_date("2100-09-07T11:34:51")
)
assert uploaded.is_on_local
assert uploaded.is_on_remote
assert uploaded.remote_path == fake_file["path"].as_posix()
assert uploaded.local_path == fake_file["path"]
assert uploaded.checksum() == checksum_of_file(
fake_file["path"], algorithm="sha256"
)
assert uploaded.remote_uid == "the-user"
assert uploaded.remote_gid == "the-group"
assert uploaded._remote_creation_time == parse_date("2100-09-07T11:34:51")
def test_downloaded():
model = DataFile(
path="dir/stream.s",
size=55123,
time=parse_date("2025-01-09T21:00:21.421Z"),
perm="xrw",
createdBy="creator-id",
)
file = File.from_scicat(model)
downloaded = file.downloaded(local_path="/local/stream.s")
assert downloaded.is_on_local
assert downloaded.is_on_remote
assert downloaded.remote_path == RemotePath("dir/stream.s")
assert downloaded.local_path == Path("/local/stream.s")
assert downloaded.remote_perm == "xrw"
assert downloaded.created_by == "creator-id"
def test_creation_time_is_always_local_time(fake_file):
file = File.from_local(path=fake_file["path"])
model = file.make_model()
model.time = parse_date("2105-04-01T04:52:23")
uploaded = file.uploaded()
assert abs(fake_file["creation_time"] - uploaded.creation_time) < timedelta(
seconds=1
)
def test_size_is_always_local_size(fake_file):
file = File.from_local(path=fake_file["path"])
model = file.make_model()
model.size = 999999999
uploaded = file.uploaded()
assert uploaded.size == fake_file["size"]
def test_checksum_is_always_local_checksum(fake_file):
file = File.from_local(path=fake_file["path"])
uploaded = replace(
file.uploaded(), _remote_checksum="6e9eb73953231aebbbc8788f39f08618"
)
assert replace(uploaded, checksum_algorithm="md5").checksum() == checksum_of_file(
fake_file["path"], algorithm="md5"
)
assert replace(
uploaded, checksum_algorithm="sha256"
).checksum() == checksum_of_file(fake_file["path"], algorithm="sha256")
def test_creation_time_is_up_to_date(fs, fake_file):
file = File.from_local(path=fake_file["path"])
with open(fake_file["path"], "wb") as f:
f.write(b"some new content to update time stamp")
new_creation_time = datetime.fromtimestamp(
fake_file["path"].stat().st_mtime
).astimezone(timezone.utc)
assert file.creation_time == new_creation_time
def test_size_is_up_to_date(fs, fake_file):
file = File.from_local(path=fake_file["path"])
new_contents = b"content with a new size"
assert len(new_contents) != fake_file["size"]
with open(fake_file["path"], "wb") as f:
f.write(new_contents)
assert file.size == len(new_contents)
def test_checksum_is_up_to_date(fs, fake_file):
file = replace(File.from_local(path=fake_file["path"]), checksum_algorithm="md5")
new_contents = b"content a different checksum"
checksum = hashlib.new("md5")
checksum.update(new_contents)
checksum = checksum.hexdigest()
assert checksum != fake_file["size"]
with open(fake_file["path"], "wb") as f:
f.write(new_contents)
assert file.checksum() == checksum
def test_validate_after_download_detects_bad_checksum(fake_file):
model = DataFile(
path=fake_file["path"].name,
size=fake_file["size"],
time=parse_date("2022-06-22T15:42:53.123Z"),
chk="incorrect-checksum",
)
file = replace(
File.from_scicat(model),
checksum_algorithm="md5",
)
downloaded = file.downloaded(local_path=fake_file["path"])
with pytest.raises(IntegrityError):
downloaded.validate_after_download()
def test_validate_after_download_ignores_checksum_if_no_algorithm(fake_file):
model = DataFile(
path=fake_file["path"].name,
size=fake_file["size"],
time=parse_date("2022-06-22T15:42:53.123Z"),
chk="incorrect-checksum",
)
file = File.from_scicat(model)
downloaded = file.downloaded(local_path=fake_file["path"])
# does not raise
downloaded.validate_after_download()
def test_validate_after_download_detects_size_mismatch(fake_file, caplog):
model = DataFile(
path=fake_file["path"].name,
size=fake_file["size"] + 100,
time=parse_date("2022-06-22T15:42:53.123Z"),
chk="incorrect-checksum",
)
file = replace(
File.from_scicat(model),
checksum_algorithm=None,
)
downloaded = file.downloaded(local_path=fake_file["path"])
with caplog.at_level("INFO", logger=logger_name()):
downloaded.validate_after_download()
assert "does not match size reported in dataset" in caplog.text
@pytest.mark.parametrize("chk", ("sha256", None))
def test_local_is_not_up_to_date_for_remote_file(chk):
file = File.from_scicat(DataFile(path="data.csv", size=65178, chk=chk))
assert not file.local_is_up_to_date()
def test_local_is_up_to_date_for_local_file():
# Note that the file does not actually exist on disk but the test still works.
file = File.from_local(path="image.jpg")
assert file.local_is_up_to_date()
def test_local_is_not_up_to_date_without_checksum_alg():
file = File.from_scicat(
DataFile(path="data.csv", size=65178, chk="sha256")
).downloaded(local_path="data.csv")
with pytest.warns(UserWarning, match="checksum"):
assert not file.local_is_up_to_date()
def test_local_is_up_to_date_matching_checksum(fake_file):
model = DataFile(
path=fake_file["path"].name,
size=fake_file["size"],
time=parse_date("2022-06-22T15:42:53.123Z"),
chk=fake_file["checksum"],
)
file = replace(
File.from_scicat(model).downloaded(local_path=fake_file["path"]),
checksum_algorithm="md5",
)
assert file.local_is_up_to_date()
def test_local_is_not_up_to_date_differing_checksum(fake_file):
model = DataFile(
path=fake_file["path"].name,
size=fake_file["size"],
time=parse_date("2022-06-22T15:42:53.123Z"),
chk="a-different-checksum",
)
file = replace(
File.from_scicat(model).downloaded(local_path=fake_file["path"]),
checksum_algorithm="md5",
)
assert not file.local_is_up_to_date()