Skip to content

Commit 3d30237

Browse files
TDD
1 parent 507bfb0 commit 3d30237

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

cachecontrol/streaming_cache.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ def close(self):
5151
# to make it easier to use.
5252

5353

54+
CACHE = []
55+
56+
57+
class ExampleCache:
58+
class WriteHandle:
59+
def write(self, b):
60+
global CACHE
61+
CACHE.append(b)
62+
63+
def commit(self):
64+
pass
65+
66+
def close(self):
67+
pass
68+
69+
def open_read(self, key):
70+
return io.BytesIO(CACHE.pop(0))
71+
72+
def open_write(self, key, expires=None):
73+
return self.WriteHandle()
74+
75+
def delete(self, key):
76+
pass
77+
78+
def close(self):
79+
'''Cleanup any temporary files, database connections etc.'''
80+
pass
81+
'''
5482
# An example to use while prototyping
5583
class ExampleCache:
5684
# Why is ExampleCache.data not a dict[str, io.BytesIO]?
@@ -88,3 +116,4 @@ def delete(self, key):
88116
89117
def close(self):
90118
pass
119+
'''

tests/test_streaming_cache.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from contextlib import closing
2+
from cachecontrol.streaming_cache import ExampleCache
3+
import pytest
4+
5+
6+
@pytest.mark.parametrize('v', [b'fizz', b'buzz'])
7+
def test_read_returns_what_you_wrote(v):
8+
with closing(ExampleCache()) as cache:
9+
with closing(cache.open_write('foo')) as w:
10+
w.write(v)
11+
w.commit()
12+
with closing(cache.open_read('foo')) as r:
13+
got = r.read()
14+
assert got == v
15+
16+
17+
def test_cache_remembers_more_than_one_value():
18+
with closing(ExampleCache()) as cache:
19+
with closing(cache.open_write('foo')) as w:
20+
w.write(b'one')
21+
w.commit()
22+
with closing(cache.open_write('bar')) as w:
23+
w.write(b'two')
24+
w.commit()
25+
with closing(cache.open_read('foo')) as r:
26+
got = r.read()
27+
assert got == b'one'
28+
with closing(cache.open_read('bar')) as r:
29+
got = r.read()
30+
assert got == b'two'

0 commit comments

Comments
 (0)