File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,34 @@ def close(self):
51
51
# to make it easier to use.
52
52
53
53
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
+ '''
54
82
# An example to use while prototyping
55
83
class ExampleCache:
56
84
# Why is ExampleCache.data not a dict[str, io.BytesIO]?
@@ -88,3 +116,4 @@ def delete(self, key):
88
116
89
117
def close(self):
90
118
pass
119
+ '''
Original file line number Diff line number Diff line change
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'
You can’t perform that action at this time.
0 commit comments