forked from ResearchObject/ro-crate-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_readwrite.py
73 lines (64 loc) · 2.62 KB
/
test_readwrite.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
# Copyright 2019-2024 The University of Manchester, UK
# Copyright 2020-2024 Vlaams Instituut voor Biotechnologie (VIB), BE
# Copyright 2020-2024 Barcelona Supercomputing Center (BSC), ES
# Copyright 2020-2024 Center for Advanced Studies, Research and Development in Sardinia (CRS4), IT
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
# Copyright 2024 Data Centre, SciLifeLab, SE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from pathlib import Path
from rocrate.rocrate import ROCrate
def test_crate_update(test_data_dir, tmpdir, helpers):
crate_dir = test_data_dir / 'read_crate'
crate = ROCrate(crate_dir)
content_map = {}
for root, dirs, files in os.walk(crate_dir):
root = Path(root)
for name in files:
if not name.startswith("ro-crate"):
path = root / name
with open(path, "rb") as f:
content_map[path] = f.read()
# update an existing file
upd_file_id = "test_file_galaxy.txt"
upd_file = crate.dereference(upd_file_id)
with open(upd_file.source, "rb") as f:
content = f.read()
upd_content = content + b"foobar\n"
upd_source = tmpdir / "upd_source.txt"
with open(upd_source, "wb") as f:
f.write(upd_content)
crate.delete(upd_file)
crate.add_file(upd_source, upd_file_id)
# add a new file
new_file_id = "spam.txt"
new_content = b"enlarge your crate\n"
new_source = tmpdir / "new_source.txt"
with open(new_source, "wb") as f:
f.write(new_content)
crate.add_file(new_source, new_file_id)
crate.write(crate_dir)
for root, dirs, files in os.walk(crate_dir):
root = Path(root)
for name in files:
if not name.startswith("ro-crate"):
path = root / name
with open(path, "rb") as f:
content = f.read()
if path == crate_dir / upd_file_id:
assert content == upd_content
elif path == crate_dir / new_file_id:
assert content == new_content
else:
assert content == content_map[path]