Skip to content

Commit a3305a5

Browse files
committed
Add unittests for utils.ichunked()
1 parent 6c78bfc commit a3305a5

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

tests/ichunked_test.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# This software may be modified and distributed under the terms
4+
# of the MIT license. See the LICENSE file for details.
5+
6+
from random import randint
7+
import unittest
8+
9+
from logstash_async.utils import ichunked
10+
11+
CHUNK_SIZE_SMALL = 1
12+
CHUNK_SIZE_NORMAL = 100
13+
CHUNK_SIZE_BIG = 750
14+
CHUNK_ITERATIONS = 5
15+
16+
17+
class IChunkedTest(unittest.TestCase):
18+
19+
# ----------------------------------------------------------------------
20+
def _test_chunking(self, chunk_size, chunk_iterations):
21+
# test data
22+
random_extra_chunk_size = randint(0, chunk_size - 1)
23+
test_sequence_size = chunk_size * chunk_iterations + random_extra_chunk_size
24+
test_sequence = list(range(test_sequence_size))
25+
# keep results for assertions
26+
iterations = 0
27+
iterated_elements = list()
28+
# test
29+
for sequence_subset in ichunked(test_sequence, chunk_size):
30+
iterations += 1
31+
iterated_elements.extend(sequence_subset)
32+
self.assertLessEqual(len(sequence_subset), chunk_size)
33+
34+
expected_iterations = chunk_iterations
35+
if random_extra_chunk_size > 0:
36+
expected_iterations += 1 # add 1 because of 'random_extra_chunk_size'
37+
self.assertListEqual(iterated_elements, test_sequence)
38+
self.assertEqual(iterations, expected_iterations)
39+
40+
# ----------------------------------------------------------------------
41+
def test_chunks_big_iterations_fixed(self):
42+
self._test_chunking(CHUNK_SIZE_BIG, CHUNK_ITERATIONS)
43+
44+
# ----------------------------------------------------------------------
45+
def test_chunks_big_iterations_random(self):
46+
chunk_iterations = randint(3, 20)
47+
self._test_chunking(CHUNK_SIZE_BIG, chunk_iterations)
48+
49+
# ----------------------------------------------------------------------
50+
def test_chunks_normal_iterations_fixed(self):
51+
self._test_chunking(CHUNK_SIZE_NORMAL, CHUNK_ITERATIONS)
52+
53+
# ----------------------------------------------------------------------
54+
def test_chunks_normal_iterations_random(self):
55+
chunk_iterations = randint(3, 20)
56+
self._test_chunking(CHUNK_SIZE_NORMAL, chunk_iterations)
57+
58+
# ----------------------------------------------------------------------
59+
def test_chunks_small_iterations_fixed(self):
60+
self._test_chunking(CHUNK_SIZE_SMALL, CHUNK_ITERATIONS)
61+
62+
# ----------------------------------------------------------------------
63+
def test_chunks_small_iterations_random(self):
64+
chunk_iterations = randint(3, 20)
65+
self._test_chunking(CHUNK_SIZE_SMALL, chunk_iterations)
66+
67+
# ----------------------------------------------------------------------
68+
def test_empty_sequence(self):
69+
chunk_size = 5
70+
test_sequence = list()
71+
# keep results for assertions
72+
iterations = 0
73+
iterated_elements = list()
74+
# test
75+
for sequence_subset in ichunked(test_sequence, chunk_size):
76+
iterations += 1
77+
iterated_elements.extend(sequence_subset)
78+
self.assertLessEqual(len(sequence_subset), chunk_size)
79+
80+
expected_iterations = 0
81+
self.assertListEqual(iterated_elements, test_sequence)
82+
self.assertEqual(iterations, expected_iterations)
83+
84+
85+
if __name__ == '__main__':
86+
unittest.main()

0 commit comments

Comments
 (0)