-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
382 lines (263 loc) · 10.6 KB
/
tests.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import os
import logging
import asyncio
from dotenv import load_dotenv
from unittest import TestCase
from peewee import Proxy
from peewee_async import MySQLDatabase as AsyncMySQLDatabase, Manager
from peewee import SqliteDatabase, Model, IntegerField
from peewee_syncer import SyncManager, get_sync_manager, Processor, AsyncProcessor, LastOffsetQueryIterator
logging.getLogger('peewee').setLevel(logging.INFO)
# used for async test as cannot support sqlite
load_dotenv()
log = logging.getLogger(__name__)
class BaseTestCase(TestCase):
def get_sqlite_db(self):
try:
os.remove('test.db')
except FileNotFoundError:
pass
return SqliteDatabase('test.db')
class SyncerTests(BaseTestCase):
"""
Syncer Tests
"""
def test(self):
db = self.get_sqlite_db()
# Re proxy to avoid previous test use
SyncManager._meta.database = Proxy()
SyncManager.init_db(db)
SyncManager.create_table()
class TestModel(Model):
value = IntegerField()
@classmethod
def get_value(cls, item):
return item.value
@classmethod
def get_key(cls, item):
return item.id
@classmethod
def select_since_id(cls, since, limit, offset):
q = cls.select().where(cls.id > since)
if limit:
q = q.limit(limit)
return q
class Meta:
database = db
TestModel.create_table()
sync_manager = get_sync_manager(app="test",
start=0,
test=None
)
output = []
def row_output(model):
data = {'id': model.id, 'value': model.value}
output.append(data)
return data
for i in range(25):
TestModel.create(id=i + 1, value=i + 1)
self.assertEqual(25, TestModel.select().count())
iteration = 0
def process(it):
nonlocal iteration
iteration += 1
for x in it:
log.debug("process it={} id={}".format(iteration, x['id']))
def it(since, limit, offset):
log.debug("it since={} limit={} offset={}".format(since, limit, offset))
q = TestModel.select_since_id(since, limit=limit, offset=offset)
return LastOffsetQueryIterator(q.iterator(), row_output_fun=row_output,
key_fun=TestModel.get_key, is_unique_key=True)
processor = Processor(
sync_manager=sync_manager,
it_function=it,
process_function=process,
sleep_duration=0
)
processor.process(limit=10, i=5)
self.assertEqual(len(output), 25)
self.assertEqual(output[0]['id'], 1)
self.assertEqual(output[-1]['id'], 25)
def test_offset_processing(self):
db = self.get_sqlite_db()
# Re proxy to avoid previous test use
SyncManager._meta.database = Proxy()
SyncManager.init_db(db)
SyncManager.create_table()
class TestModel(Model):
value = IntegerField()
@classmethod
def get_value(cls, item):
return item.value
@classmethod
def get_key(cls, item):
return item.value
@classmethod
def select_since_value(cls, since, limit, offset):
q = cls.select().where(cls.value > since)
if limit:
q = q.limit(limit)
if offset:
q = q.offset(offset)
log.debug(q.sql())
return q
class Meta:
database = db
TestModel.create_table()
sync_manager = get_sync_manager(app="test",
start=-1,
test=None
)
output = []
def row_output(model):
data = {'id': model.id, 'value': model.value}
output.append(data)
return data
# Create 15 regular records
for i in range(15):
TestModel.create(value=i)
# Now add 25 with same value (ie an "hump" that will require "offset" to get over)
for i in range(25):
TestModel.create(value=50)
# And a final few
for i in range(10):
TestModel.create(value=51+i)
self.assertEqual(50, TestModel.select().count())
iteration = 0
def process(it):
nonlocal iteration
iteration += 1
for x in it:
log.debug("process it={} id={} value={}".format(iteration, x['id'], x['value']))
# Note: is_unique_key=False (ie multiple same value may exist (eg same "lastModified" due to bulk update for example)
def it(since, limit, offset):
log.debug("it since={} limit={} offset={}".format(since, limit, offset))
q = TestModel.select_since_value(since, limit=limit, offset=offset)
return LastOffsetQueryIterator(q.iterator(), row_output_fun=row_output,
key_fun=TestModel.get_key, is_unique_key=False)
processor = Processor(
sync_manager=sync_manager,
it_function=it,
process_function=process,
sleep_duration=0
)
processor.process(limit=10, i=10)
# is_unique_key=False reduces in duplicate values when we hit the offset limit
# todo: cache to avoid dup values?
self.assertTrue(len(output), 56)
value_ids = list(set([x['value'] for x in output]))
# 0-14, 50, 51-60
self.assertEquals(value_ids, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60])
ids = list(set([x['id'] for x in output]))
self.assertEqual(len(ids), 50)
self.assertEqual(ids[0], 1)
self.assertEqual(ids[-1], 50)
class AsyncSyncerTests(BaseTestCase):
"""
Async Syncer Tests
Note: Not supported with SQLite yet: https://github.com/05bit/peewee-async/issues/126
"""
def get_mysql_db(self):
# uses .env via load_dotenv()
DB_NAME = os.environ.get("DB_NAME", "test")
DB_HOST = os.environ["DB_HOST"]
DB_PORT = int(os.environ.get("DB_port", "3306"))
DB_USERNAME = os.environ["DB_USERNAME"]
DB_PASSWORD = os.environ["DB_PASSWORD"]
db = AsyncMySQLDatabase(
database=DB_NAME,
user=DB_USERNAME,
host=DB_HOST,
port=DB_PORT,
password=DB_PASSWORD
)
return db
def test(self):
db = self.get_mysql_db()
# Re proxy to avoid previous test use
SyncManager._meta.database = Proxy()
# Init/Create in sync mode
SyncManager.init_db(db)
SyncManager.create_table()
# Clear out from previous test run
SyncManager.delete().execute()
sync_manager = get_sync_manager(app="test-async", start=0, db=db, set_async=True)
async def it(since=None, limit=None, offset=None):
log.debug("Getting iterator since={} limit={} offset={}".format(since, limit, offset))
def dummy():
for x in range(since+1, since+limit+1):
log.debug("yielded {}".format(x))
yield {"x": x}
return LastOffsetQueryIterator(dummy(), row_output_fun=lambda x:x, key_fun=lambda x:x['x'], is_unique_key=True)
output = []
async def process(it):
nonlocal output
for item in it:
output.append(item)
log.debug("process item: {}".format(item))
processor = AsyncProcessor(
sync_manager=sync_manager,
it_function=it,
process_function=process,
object=Manager(db, loop=None)
)
async def consume():
await processor.process(limit=10, i=3)
asyncio.get_event_loop().run_until_complete(consume())
self.assertEqual(len(output), 30)
#raise
def test_offset_processing(self):
db = self.get_mysql_db()
# Re proxy to avoid previous test use
SyncManager._meta.database = Proxy()
# Init/Create in sync mode
SyncManager.init_db(db)
SyncManager.create_table()
# Clear out from previous test run
SyncManager.delete().execute()
sync_manager = get_sync_manager(app="test-async", start=0, db=db, set_async=True)
# 15 regular, 25 @ 50 (ie the "hump"), 10 afterwards
items = list(range(15)) + list([50 for _ in range(25)]) + list(range(55, 65))
items = [{'id': i+1, 'x': x} for i,x in enumerate(items)]
async def it(since=0, limit=0, offset=0):
log.debug("Getting iterator since={} limit={} offset={}".format(since, limit, offset))
def dummy():
nonlocal items
nonlocal limit
nonlocal offset
for item in items:
if item['x'] < since:
continue
if offset > 0:
offset -=1
continue
limit -= 1
if limit < 0:
break
yield item
return LastOffsetQueryIterator(dummy(), row_output_fun=lambda x:x, key_fun=lambda x:x['x'], is_unique_key=False)
output = []
async def process(it):
nonlocal output
for item in it:
output.append(item)
log.debug("process item: {}".format(item))
processor = AsyncProcessor(
sync_manager=sync_manager,
it_function=it,
process_function=process,
object=Manager(db, loop=None)
)
async def consume():
await processor.process(limit=10, i=8)
asyncio.get_event_loop().run_until_complete(consume())
# todo: cache to avoid dup values?
self.assertTrue(len(output), 59)
unique_values = list(set([x['x'] for x in output]))
self.assertEquals(unique_values,
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 50, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64]
)
ids = list(set([x['id'] for x in output]))
self.assertEqual(len(ids), 50)
self.assertEqual(ids[0], 1)
self.assertEqual(ids[-1], 50)