-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_cache_decorator.py
More file actions
119 lines (86 loc) · 2.73 KB
/
Copy pathtest_cache_decorator.py
File metadata and controls
119 lines (86 loc) · 2.73 KB
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
116
117
118
119
from time import sleep
from ellar.cache import CacheModule, cache
from ellar.common import ModuleRouter
from ellar.core.response import PlainTextResponse
from ellar.testing import Test
def test_cache_route_function_return_data():
called_count = 0
mr = ModuleRouter()
@mr.get("/index")
@cache(ttl=0.12) # cache for 3sec
def homepage():
nonlocal called_count
called_count += 1
return dict(message="Response Information cached.")
client = Test.create_test_module(
modules=[CacheModule.register_setup()],
routers=[
mr,
],
).get_test_client()
for i in range(2):
client.get("/index")
sleep(0.22)
res = client.get("/index")
assert res.json() == dict(message="Response Information cached.")
assert res.status_code == 200
assert called_count == 2
def test_cache_for_async_route_function_return_data():
called_count = 0
mr = ModuleRouter()
@mr.get("/index")
@cache(ttl=3) # cache for 3sec
async def homepage():
nonlocal called_count
called_count += 1
return dict(message="Response Information cached Async")
client = Test.create_test_module(
modules=[CacheModule.register_setup()],
routers=[
mr,
],
).get_test_client()
res = client.get("/index")
for i in range(2):
res = client.get("/index")
assert res.json() == dict(message="Response Information cached Async")
assert res.status_code == 200
assert called_count == 1
def test_cache_for_async_route_function_return_response():
called_count = 0
mr = ModuleRouter()
@mr.get("/index")
@cache(ttl=3) # cache for 3sec
async def homepage():
nonlocal called_count
called_count += 1
return PlainTextResponse("Response Information cached Async")
client = Test.create_test_module(
modules=[CacheModule.register_setup()],
routers=[
mr,
],
).get_test_client()
res = client.get("/index")
assert res.text == "Response Information cached Async"
assert res.status_code == 200
assert called_count == 1
def test_cache_route_function_return_response():
called_count = 0
mr = ModuleRouter()
@mr.get("/index")
@cache(ttl=2) # cache for 3sec
def homepage():
nonlocal called_count
called_count += 1
return PlainTextResponse("Response Information cached")
client = Test.create_test_module(
modules=[CacheModule.register_setup()],
routers=[
mr,
],
).get_test_client()
res = client.get("/index")
assert res.text == "Response Information cached"
assert res.status_code == 200
assert called_count == 1