forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch.py
More file actions
89 lines (68 loc) · 2.4 KB
/
test_batch.py
File metadata and controls
89 lines (68 loc) · 2.4 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
import asyncio
import math
from ..base import DBTestCase
from ..example_app.tables import Manager
class TestBatchSelect(DBTestCase):
def _check_results(self, batch):
"""
Make sure the data is returned in the correct format.
"""
self.assertTrue(type(batch) == list)
if len(batch) > 0:
row = batch[0]
self.assertTrue(type(row) == dict)
self.assertTrue("name" in row.keys())
self.assertTrue("id" in row.keys())
async def run_batch(self, batch_size):
row_count = 0
iterations = 0
async with await Manager.select().batch(
batch_size=batch_size
) as batch:
async for _batch in batch:
self._check_results(_batch)
_row_count = len(_batch)
row_count += _row_count
iterations += 1
return row_count, iterations
def test_batch(self):
row_count = 1000
self.insert_many_rows(row_count)
batch_size = 10
_row_count, iterations = asyncio.run(
self.run_batch(batch_size=batch_size), debug=True
)
_iterations = math.ceil(row_count / batch_size)
self.assertTrue(_row_count == row_count)
self.assertTrue(iterations == _iterations)
class TestBatchObjects(DBTestCase):
def _check_results(self, batch):
"""
Make sure the data is returned in the correct format.
"""
self.assertTrue(type(batch) == list)
if len(batch) > 0:
row = batch[0]
self.assertTrue(isinstance(row, Manager))
async def run_batch(self, batch_size):
row_count = 0
iterations = 0
async with await Manager.objects().batch(
batch_size=batch_size
) as batch:
async for _batch in batch:
self._check_results(_batch)
_row_count = len(_batch)
row_count += _row_count
iterations += 1
return row_count, iterations
def test_batch(self):
row_count = 1000
self.insert_many_rows(row_count)
batch_size = 10
_row_count, iterations = asyncio.run(
self.run_batch(batch_size=batch_size), debug=True
)
_iterations = math.ceil(row_count / batch_size)
self.assertTrue(_row_count == row_count)
self.assertTrue(iterations == _iterations)