-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_flask_redislite.py
231 lines (177 loc) · 5.8 KB
/
test_flask_redislite.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import flask
import pytest
import time
import flask_redislite
from os import remove, path
from redislite import Redis, StrictRedis
@pytest.fixture
def app():
app = flask.Flask(__name__)
app.config.update({
'REDISLITE_PATH': 'frl.rdb',
'REDISLITE_WORKER_PID': 'worker.pid',
'RL_PATH': 'frl1.rdb'
})
try:
remove(app.config.get('REDISLITE_PATH'))
except OSError:
pass
try:
remove(app.config.get('RL_PATH'))
except OSError:
pass
return app
@pytest.fixture
def simple_job():
return '224a-ff43-6a2gF'
def test_constructor(app):
"""
- Test if the connection is created and using Redis class
"""
rdb = flask_redislite.FlaskRedis(app)
with app.app_context():
assert isinstance(rdb.connection, Redis)
assert rdb.connection.socket_file is not None
def test_constructor_strict(app):
"""
- Test if the connection is created and using StrictRedis class
"""
rdb = flask_redislite.FlaskRedis(app, strict=True)
with app.app_context():
assert isinstance(rdb.connection, StrictRedis)
assert rdb.connection.socket_file is not None
def test_constructor_redis_collections(app):
"""
- Test if the connection is created and using StrictRedis class
- Check if the collection is created
"""
rdb = flask_redislite.FlaskRedis(app, collections=True)
with app.app_context():
assert isinstance(rdb.connection, StrictRedis)
assert rdb.connection.socket_file is not None
assert rdb.collection is not None
def test_constructor_rq(app):
"""
- Test if the connection is created and using Redis class
- Check if the worker process is created
- Check if the worker pid file is created
- Check if the worker pid file is cleaned up after terminated
"""
rdb = flask_redislite.FlaskRedis(app, rq=True)
assert not path.isfile(app.config.get('REDISLITE_WORKER_PID'))
with app.app_context():
assert isinstance(rdb.connection, Redis)
assert rdb.connection.socket_file is not None
rdb.start_worker()
assert rdb.worker_process.is_alive()
assert path.isfile(app.config.get('REDISLITE_WORKER_PID'))
# Simulate the app termination
time.sleep(0.5)
rdb.worker_process.terminate()
time.sleep(0.5)
assert not path.isfile(app.config.get('REDISLITE_WORKER_PID'))
def test_config_prefix(app):
"""
- If the constructor is called without app object, no connection
"""
rdb = flask_redislite.FlaskRedis(config_prefix='RL')
with app.app_context():
assert isinstance(rdb.connection, Redis)
assert rdb.connection.socket_file is not None
def test_commit(app):
"""
- If commit, check if rdb file exists
"""
rdb = flask_redislite.FlaskRedis()
with app.app_context():
rdb.commit()
time.sleep(0.5)
assert path.isfile(flask.current_app.config.get('REDISLITE_PATH'))
def test_redis_set_get(app):
"""
- Test if we can set and get values from Redislite
"""
rdb = flask_redislite.FlaskRedis(app)
with app.app_context():
rdb.connection.set('foo1', 'aiKjq91jUyq3r')
assert rdb.connection.get('foo1').decode('utf8') == 'aiKjq91jUyq3r'
def test_redis_collections(app):
"""
- Test if we can set and get values using Redis-Collections
"""
rdb = flask_redislite.FlaskRedis(app, collections=True)
with app.app_context():
collection = rdb.collection
# Dict
d = collection.dict('foo_dict')
d['foo2'] = '28bH-76R-26A'
assert not isinstance(d, dict)
assert d == {'foo2': '28bH-76R-26A'}
# List
l = collection.list('foo_list')
l.append('foo3 is kua3D')
assert not isinstance(l, list)
assert l[0] == 'foo3 is kua3D'
time.sleep(0.5)
with app.app_context():
collection = rdb.collection
# Get Dict
d = collection.get_dict('foo_dict')
assert not isinstance(d, dict)
assert d == {'foo2': '28bH-76R-26A'}
# Get List
l = collection.get_list('foo_list')
assert not isinstance(l, list)
assert l[0] == 'foo3 is kua3D'
def test_rq(app):
"""
- Test if we can enqueue a simple job and get result from it
"""
rdb = flask_redislite.FlaskRedis(app, rq=True)
with app.app_context():
rdb.start_worker()
# Simulate the request
with app.app_context():
queue = rdb.queue
job = queue['default'].enqueue(simple_job, ttl=60,
result_ttl=60, job_id='foo4'
)
assert job.result is None
time.sleep(1)
assert job.result == simple_job()
# Another request
time.sleep(0.5)
with app.app_context():
queue = rdb.queue
assert queue['default'].fetch_job('foo4').result == simple_job()
# Clean up
rdb.worker_process.terminate()
def test_non_collection(app):
"""
- Test if collection can created or not. Normally it should not be.
"""
rdb = flask_redislite.FlaskRedis(app)
with app.app_context():
assert rdb.collection is None
def test_non_rq(app):
"""
- Test if rq can created or not. Normally it should not be.
"""
rdb = flask_redislite.FlaskRedis(app)
with app.app_context():
assert rdb.start_worker() is None
assert rdb.queue is None
def test_existed_worker(app):
"""
- Test if another worker will pawned if it's already started.
"""
rdb = flask_redislite.FlaskRedis(app, rq=True)
with app.app_context():
pid = rdb.start_worker()
assert pid > 0
time.sleep(0.5)
with app.app_context():
new_pid = rdb.start_worker()
assert pid == new_pid
# Clean up
rdb.worker_process.terminate()