-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_operation_cache.py
More file actions
114 lines (81 loc) · 2.56 KB
/
Copy pathtest_operation_cache.py
File metadata and controls
114 lines (81 loc) · 2.56 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
from time import sleep
from ellar.common import ModuleRouter, cache
from ellar.core import TestClientFactory
from ellar.core.response import PlainTextResponse
def test_cache_route_function_return_data():
called_count = 0
mr = ModuleRouter()
@mr.get("/index")
@cache(timeout=0.12) # cache for 3sec
def homepage():
nonlocal called_count
called_count += 1
return dict(message="Response Information cached.")
client = TestClientFactory.create_test_module(
routers=[
mr,
]
).get_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(timeout=3) # cache for 3sec
async def homepage():
nonlocal called_count
called_count += 1
return dict(message="Response Information cached Async")
client = TestClientFactory.create_test_module(
routers=[
mr,
]
).get_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(timeout=3) # cache for 3sec
async def homepage():
nonlocal called_count
called_count += 1
return PlainTextResponse("Response Information cached Async")
client = TestClientFactory.create_test_module(
routers=[
mr,
]
).get_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(timeout=2) # cache for 3sec
def homepage():
nonlocal called_count
called_count += 1
return PlainTextResponse("Response Information cached")
client = TestClientFactory.create_test_module(
routers=[
mr,
]
).get_client()
res = client.get("/index")
assert res.text == "Response Information cached"
assert res.status_code == 200
assert called_count == 1