forked from youknowone/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ring.py
115 lines (81 loc) · 1.92 KB
/
test_ring.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
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
import ring
import pytest
from .test_func_sync import pythonmemcache_client
__all__ = ('pythonmemcache_client', )
class hybridmethod(object):
def __init__(self, func):
self.__func__ = func
def __get__(self, obj, type=None):
bound = obj if obj is not None else type
return self.__func__.__get__(bound, type)
class A(object):
v = -10
def __init__(self, v):
self.v = v
def __ring_key__(self):
return str(self.v)
@ring.dict({})
def x(self):
return self.v
@ring.dict({})
@hybridmethod
def h(self):
return self.v
@ring.dict({})
@property
def p(self):
return self.v
def test_ring_wrapper():
a = A(10)
b = A(20)
print(a.x.key())
print(b.x.key())
print(a.x.storage)
print(b.x.storage)
assert a.x() == 10
assert b.x() == 20
assert a.x() == 10
assert b.x() == 20
def test_custom_method():
assert A.h() == -10
assert A(10).h() == 10
def test_ring_property():
a = A(15)
assert a.p == 15
@pytest.mark.parametrize('value', [
1,
0,
True,
False,
u'str',
b'bytes',
['list', 'with', 'values'],
{'dict': 'also', 'matters': '!'},
{'set', 'should', 'be', 'ordered'},
])
def test_ring_key(value):
# test only with real cache backends. dict doesn't help this test
@ring.memcache(pythonmemcache_client, expire=1)
def simple(key):
return key
assert simple(value) == value # cache miss
assert simple(value) == value # cache hit
def test_proxy_repr():
@ring.dict({})
def f():
pass
assert repr(ring.dict)
assert repr(ring.dict({}))
assert repr(f)
def test_proxy_cache():
dring = ring.dict({})
@dring
def f1():
pass
@dring
def f2():
pass
def test_package_version():
parts = ring.__version__.split('.')
assert len(parts) == 3
assert parts[0] == '0'