-
-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathtest_cache.py
More file actions
155 lines (116 loc) · 5.11 KB
/
Copy pathtest_cache.py
File metadata and controls
155 lines (116 loc) · 5.11 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import datetime
import time
import freezegun
import pytest
from zeep import cache
def test_base_add():
base = cache.Base()
with pytest.raises(NotImplementedError):
base.add("test", b"test")
def test_base_get():
base = cache.Base()
with pytest.raises(NotImplementedError):
base.get("test")
class TestSqliteCache:
def test_in_memory(self):
with pytest.raises(ValueError):
cache.SqliteCache(path=":memory:")
def test_cache(self, tmpdir):
c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
def test_no_records(self, tmpdir):
c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result is None
def test_has_expired(self, tmpdir):
c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
freeze_dt = datetime.datetime.utcnow() + datetime.timedelta(seconds=7200)
with freezegun.freeze_time(freeze_dt):
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result is None
def test_has_not_expired(self, tmpdir):
c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath)
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
def test_memory_cache_timeout(tmpdir):
c = cache.InMemoryCache()
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
freeze_dt = datetime.datetime.utcnow() + datetime.timedelta(seconds=7200)
with freezegun.freeze_time(freeze_dt):
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result is None
def test_memory_cache_share_data(tmpdir):
a = cache.InMemoryCache()
b = cache.InMemoryCache()
a.add("http://tests.python-zeep.org/example.wsdl", b"content")
result = b.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
class TestIsExpired:
def test_timeout_none(self):
assert cache._is_expired(100, None) is False
def test_has_expired(self):
timeout = 7200
utcnow = datetime.datetime.utcnow()
value = utcnow + datetime.timedelta(seconds=timeout)
with freezegun.freeze_time(utcnow):
assert cache._is_expired(value, timeout) is False
def test_has_not_expired(self):
timeout = 7200
utcnow = datetime.datetime.utcnow()
value = utcnow - datetime.timedelta(seconds=timeout)
with freezegun.freeze_time(utcnow):
assert cache._is_expired(value, timeout) is False
def test_ttl_cache():
c = cache.TTLCache(maxsize=5, ttl=10)
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
cache._ttl_cache = None
def test_ttl_cache_no_records():
c = cache.TTLCache(maxsize=5, ttl=10)
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result is None
cache._ttl_cache = None
def test_ttl_cache_has_not_expired():
c = cache.TTLCache(maxsize=5, ttl=10)
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
freeze_dt = datetime.datetime.utcnow() + datetime.timedelta(seconds=2)
with freezegun.freeze_time(freeze_dt):
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
cache._ttl_cache = None
def test_ttl_cache_max_size_reached():
max_size = 3
c = cache.TTLCache(maxsize=max_size, ttl=2)
for i in range(0, 5):
c.add(f"http://tests.python-zeep.org/example{i}.wsdl", b"content")
result = c.get("http://tests.python-zeep.org/example0.wsdl")
assert result is None
result = c.get("http://tests.python-zeep.org/example1.wsdl")
assert result is None
cache._ttl_cache = None
def test_ttl_cache_share_data():
a = cache.TTLCache(maxsize=5, ttl=10)
b = cache.TTLCache(maxsize=5, ttl=10)
a.add("http://tests.python-zeep.org/example.wsdl", b"content")
result = b.get("http://tests.python-zeep.org/example.wsdl")
assert result == b"content"
cache._ttl_cache = None
def test_ttl_cache_invalid_type():
a = cache.TTLCache(maxsize=5, ttl=10)
with pytest.raises(TypeError):
a.add("http://tests.python-zeep.org/example.wsdl", 123456)
cache._ttl_cache = None
def test_ttl_cache_has_expired():
c = cache.TTLCache(maxsize=5, ttl=0.001)
c.add("http://tests.python-zeep.org/example.wsdl", b"content")
time.sleep(0.002) # Adding sleep because freezegun won't work here: https://github.com/spulec/freezegun/issues/477
result = c.get("http://tests.python-zeep.org/example.wsdl")
assert result is None
cache._ttl_cache = None