Skip to content

Commit

Permalink
Add better tests for laziness of local operations
Browse files Browse the repository at this point in the history
  • Loading branch information
replomancer committed May 22, 2021
1 parent e6e72d7 commit 9cb4e9c
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions tests/pipeline_operations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ def test_local_map(self):
self.assertEqual(list(self.ops.map([], lambda x: x / 0)),
[])

some_mapped_list = self.ops.map([1, 2, 3], lambda x: x)
# some_mapped_list is its own consumable iterator
self.assertIs(some_mapped_list, iter(some_mapped_list))

self.assertEqual(list(self.ops.map([1, 2, 3], str)),
["1", "2", "3"])
self.assertEqual(list(self.ops.map(range(5), lambda x: x ** 2)),
Expand Down Expand Up @@ -111,12 +107,32 @@ def test_local_values(self):
def test_local_count_per_element(self):
example_list = [1, 2, 3, 4, 5, 6, 1, 4, 0, 1]
result = self.ops.count_per_element(example_list)
# result is its own consumable iterator
self.assertIs(result, iter(result))

self.assertEqual(dict(result),
{1: 3, 2: 1, 3: 1, 4: 2, 5: 1, 6: 1, 0: 1})

def test_laziness(self):
def exceptions_generator_function():
yield 1 / 0

def assert_laziness(operator, *args):
try:
operator(exceptions_generator_function(), *args)
except ZeroDivisionError:
self.fail(f"local {operator.__name__} is not lazy")

# reading from exceptions_generator_function() results in error:
self.assertRaises(ZeroDivisionError,
next, exceptions_generator_function())

# lazy operators accept exceptions_generator_function()
# as argument without raising errors:
assert_laziness(self.ops.map, str)
assert_laziness(self.ops.map_values, str)
assert_laziness(self.ops.filter, bool)
assert_laziness(self.ops.values)
assert_laziness(self.ops.count_per_element)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9cb4e9c

Please sign in to comment.