forked from youknowone/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_workflow.py
66 lines (46 loc) · 1.71 KB
/
test_workflow.py
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
import ring
class Object(object):
def __init__(self, **kwargs):
self._data = kwargs
def __getattr__(self, key):
if key in self._data:
return self._data[key]
return getattr(super(Object, self), key)
def test_workflow():
cache = {}
class User(Object):
def __ring_key__(self):
return 'User{self.user_id}'.format(self=self)
@ring.dict(cache)
def data(self):
return self._data.copy()
@ring.dict(cache)
def child(self, n):
return {'child_id': self.user_id * 10000 + n}
u1 = User(user_id=42, name='User 1')
# get cache or none
assert None is u1.data.get()
# get cache or create cache
assert u1.data() == {'user_id': 42, 'name': 'User 1'}
# do you want to access to cache directly?
key = u1.data.key()
assert u1.data() == cache[key] # whenever you want!
u1._data['name'] = 'User renamed'
assert u1.data() == {'user_id': 42, 'name': 'User 1'} # still cached
# force to update
assert u1.data.update() == {'user_id': 42, 'name': 'User renamed'}
# shared by user_id
u2 = User(user_id=42)
assert u1 != u2
assert u1.data.execute() != u2.data.execute() # value is different
assert u1.data() == u2.data() # but sharing the cache
# delete
assert u1.data.get()
u1.data.delete()
assert not u1.data.get()
# parametrized calling!
assert {'child_id': 420007} == u1.child(7)
assert {'child_id': 420007} == u1.child(n=7) # support keyword parameter too
assert u1.child.key(7) == u1.child.key(n=7) # generated cache keys are also same
# but distinguished from other parameters
assert u1.child.key(1) != u1.child.key(2)