forked from youknowone/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_test_func_async_def.py
91 lines (69 loc) · 2.22 KB
/
_test_func_async_def.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
import pytest
from typing import Optional
import ring
@pytest.mark.asyncio
async def test_async_def_vanilla_function(aiomcache_client):
storage, storage_ring = aiomcache_client
with pytest.raises(TypeError):
@storage_ring(storage)
def vanilla_function():
pass
@pytest.mark.asyncio
async def test_async_def_func_method():
cache = {}
async def async_func(n):
return n
class A(object):
def __str__(self):
return 'A'
@ring.dict(cache)
async def method(self, a, b):
x = await async_func(100)
return base + a * x + b
@ring.dict(cache)
@classmethod
async def cmethod(cls, a, b):
x = await async_func(200)
return base + a * x + b
obj = A()
base = 10000
await obj.method.delete(1, 2)
value = await obj.method(1, 2)
assert value == 10102, value
await obj.cmethod.delete(1, 2)
value = await obj.cmethod(1, 2)
assert value == 10202, value
@pytest.mark.parametrize('field,expected', [
(None, {'a': int, 'b': str, 'return': float}),
('__call__', {'a': int, 'b': str, 'return': float}),
('execute', {'a': int, 'b': str, 'return': float}),
('get', {'a': int, 'b': str, 'return': Optional[float]}),
('get_or_update', {'a': int, 'b': str, 'return': float}),
('update', {'a': int, 'b': str, 'return': float}),
('key', {'a': int, 'b': str, 'return': str}),
('set', {'a': int, 'b': str, 'return': None}),
('delete', {'a': int, 'b': str, 'return': None}),
('touch', {'a': int, 'b': str, 'return': None}),
])
@pytest.mark.asyncio
async def test_annotation(field, expected):
@ring.dict({})
def f(a: int, b: str) -> float:
pass
@ring.dict({})
async def g(a: int, b: str) -> float:
pass
if field is not None:
owner = getattr(f, field)
else:
owner = f
print('actual:', owner.__annotations__)
print('expected:', expected)
assert owner.__annotations__ == expected
if field is not None:
owner = getattr(g, field)
else:
owner = g
print('actual:', owner.__annotations__)
print('expected:', expected)
assert owner.__annotations__ == expected