forked from yeshsurya/python-diskcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_query_only.py
More file actions
64 lines (47 loc) · 1.53 KB
/
Copy pathtest_query_only.py
File metadata and controls
64 lines (47 loc) · 1.53 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
import contextlib
import os
import shutil
import sqlite3
import stat
import tempfile
import pytest
import diskcache as dc
from diskcache.core import DBNAME, ReadOnlyError
@pytest.fixture
def cache_directory():
with contextlib.nullcontext(
tempfile.mkdtemp(prefix='diskcache-')
) as directory:
yield directory
shutil.rmtree(directory, ignore_errors=True)
def test_cannot_create(cache_directory):
with pytest.raises(sqlite3.OperationalError):
dc.Cache(directory=cache_directory, sqlite_query_only=True)
def test_can_read_only(cache_directory):
key = 'some'
obj1 = [5, 6, 7]
# create the cache, must be in read write mode
rw = dc.Cache(directory=cache_directory)
rw[key] = obj1
rw = None
# make the file RO
os.chmod(os.path.join(cache_directory, DBNAME), stat.S_IREAD)
# with sqlite_query_only=True we can read the DB
ro = dc.Cache(directory=cache_directory, sqlite_query_only=True)
obj2 = ro[key]
ro = None
assert obj2 == obj1
# default cache cannot read a ro file
with pytest.raises(sqlite3.OperationalError):
dc.Cache(directory=cache_directory)
def test_cannot_update(cache_directory):
# create the cache, must be in read write mode
rw = dc.Cache(directory=cache_directory)
rw['key'] = 'old'
rw = None
# re-open ro: cannot update
ro = dc.Cache(directory=cache_directory, sqlite_query_only=True)
with pytest.raises(ReadOnlyError):
ro['key'] = 'new'
with pytest.raises(ReadOnlyError):
ro.clear()