-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_cache.py
More file actions
139 lines (94 loc) · 3.44 KB
/
Copy pathtest_cache.py
File metadata and controls
139 lines (94 loc) · 3.44 KB
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
import os
import shutil
from datetime import timedelta
from pybiocfilecache import BiocFileCache, CacheConfig
from pathlib import Path
import pytest
import tempfile
__author__ = "jkanche"
__copyright__ = "jkanche"
__license__ = "MIT"
CACHE_DIR = tempfile.mkdtemp() + "/cache"
def test_create_cache():
bfc = BiocFileCache(CACHE_DIR)
assert os.path.exists(CACHE_DIR)
bfc.check_metadata_key("schema_version")
bfc.purge()
def test_add_get_list_operations():
bfc = BiocFileCache(CACHE_DIR)
rtrip = bfc.add("test1", os.getcwd() + "/tests/data/test1.txt")
rec1 = bfc.get("test1")
assert rec1 is not None
bfc.add("test2", os.getcwd() + "/tests/data/test2.txt")
rec2 = bfc.get("test2")
assert rec2 is not None
frec1 = open(rec1["rpath"], "r").read().strip()
assert frec1 == "test1"
frec2 = open(rec2["rpath"], "r").read().strip()
assert frec2 == "test2"
shutil.copy(os.getcwd() + "/tests/data/test2.txt", os.getcwd() + "/tests/data/test3.txt")
bfc.add("test3_asis", os.getcwd() + "/tests/data/test3.txt", action="asis")
rec3 = bfc.get("test3_asis")
assert rec3 is not None
assert rec3["rpath"] == os.getcwd() + "/tests/data/test3.txt"
frec3 = open(rec3["rpath"], "r").read().strip()
assert frec3 == "test2"
rtrip = bfc.list_resources()
assert len(rtrip) == 3
downurl = "https://bioconductor.org/packages/stats/bioc/BiocFileCache/BiocFileCache_2024_stats.tab"
add_url = bfc.add(rname="download_link", fpath=downurl, rtype="web")
row = bfc.get(rid=add_url["rid"])
assert row["fpath"] == downurl
rtrip = bfc.list_resources()
assert len(rtrip) == 4
bfc.purge()
def test_remove_operations():
bfc = BiocFileCache(CACHE_DIR)
bfc.add("test1", os.getcwd() + "/tests/data/test1.txt")
rec1 = bfc.get("test1")
assert rec1 is not None
bfc.add("test2", os.getcwd() + "/tests/data/test2.txt")
rec2 = bfc.get("test2")
assert rec2 is not None
bfc.remove("test1")
rec1 = bfc.get("test1")
assert rec1 is None
bfc.purge()
def test_meta_operations():
bfc = BiocFileCache(CACHE_DIR)
bfc.add("test1", os.getcwd() + "/tests/data/test1.txt")
rec1 = bfc.get("test1")
assert rec1 is not None
with pytest.raises(Exception):
bfc.add_metadata("schema_version", "something")
bfc.check_metadata_key("schema_version")
bfc.add_metadata("language", "python")
downurl = "https://bioconductor.org/packages/stats/bioc/BiocFileCache/BiocFileCache_2024_stats.tab"
add_url = bfc.add(rname="download_link", fpath=downurl, rtype="web")
rec = bfc.get_metadata("schema_version")
assert rec["value"] == "0.99.4"
rec = bfc.get_metadata("language")
assert rec["value"] == "python"
rtrip = bfc.list_resources()
assert len(rtrip) == 2
bfc.purge()
def test_cache_with_config():
# Create custom configuration
config = CacheConfig(
cache_dir=Path(CACHE_DIR),
cleanup_interval=timedelta(days=7),
)
bfc = BiocFileCache(config=config)
bfc.check_metadata_key("schema_version")
bfc.add("test1", os.getcwd() + "/tests/data/test1.txt")
rec1 = bfc.get("test1")
assert rec1 is not None
rtrip = bfc.list_resources()
assert len(rtrip) == 1
bfc.update("test1", os.getcwd() + "/tests/data/test2.txt")
rec1 = bfc.get("test1")
assert rec1 is not None
rtrip = bfc.list_resources()
assert len(rtrip) == 1
bfc.cleanup()
bfc.purge()