Commit 99a4ac1a authored by David Mendez's avatar David Mendez

Add more tests for chunk generation function

parent 19957e55
Pipeline #92116 passed with stages
in 2 minutes and 37 seconds
......@@ -17,12 +17,11 @@ class TestUtilFunctions(unittest.TestCase):
"""
list_size = 100
original_list = list(range(0, list_size))
original_list_size = len(original_list)
for chunk_size in range(1, list_size + 10):
chunks_obtained = list(utils.get_list_chunks(chunk_size, original_list))
total_size_obtained = sum([len(chunk) for chunk in chunks_obtained])
self.assertTrue(total_size_obtained == original_list_size,
self.assertTrue(total_size_obtained == list_size,
msg='The total size of the chunks do not match the size of the original list!')
items_found = set()
......@@ -33,3 +32,26 @@ class TestUtilFunctions(unittest.TestCase):
for item in original_list:
self.assertIn(item, items_found, msg=f'The item {item} is missing in the chunks!')
def test_list_chunk_fails_with_nonsense_numbers(self):
"""
Tests that the chunks function fails when nonsense numbers are given (negative and zero)
"""
list_size = 100
original_list = list(range(0, list_size))
with self.assertRaises(utils.ChunkSizeNonSenseError,
msg='It should raise an error with a nonsense chunk size!'):
utils.get_list_chunks(0, original_list)
utils.get_list_chunks(-1, original_list)
def test_list_chunk_works_with_empty_list(self):
"""
test chunks function works with an empty list. It should create an empty chunk list
"""
list_size = 0
original_list = list(range(0, list_size))
chunks_obtained = list(utils.get_list_chunks(10, original_list))
self.assertEqual(chunks_obtained, [], msg=f'No chunks must have been produced!')
......@@ -3,12 +3,35 @@ Utils functions for handling the subset creation
"""
class ChunkSizeNonSenseError(Exception):
"""
Error raised when the chunk size does not make sense
"""
def get_list_chunks(chunk_size, original_list):
"""
:param chunk_size: size of chunk to obtain
:param original_list: list to split
:return: a generator object with the chunks of the original list with the given size
"""
if chunk_size <= 0:
raise ChunkSizeNonSenseError(f'A chunk size of {chunk_size} does not make sense!')
if len(original_list) == 0:
return []
return prepare_list_chunks_generator(chunk_size, original_list)
def prepare_list_chunks_generator(chunk_size, original_list):
"""
Prepares the generator to get the list chunks
:param chunk_size: size of chunk to obta
:param original_list: list to split
:return: a generator object with the chunks of the original list with the given size
"""
start_index = 0
end_index = chunk_size
original_list_size = len(original_list)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment