async def test_slice(assert_run, event_loop): slice = stream.select.slice with event_loop.assert_cleanup(): xs = (stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(2)) await assert_run(xs, [10, 11]) with event_loop.assert_cleanup(): xs = (stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(8, None)) await assert_run(xs, [18, 19]) with event_loop.assert_cleanup(): xs = (stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(-3, -1)) await assert_run(xs, [17, 18]) with event_loop.assert_cleanup(): xs = (stream.range(10, 20) | add_resource.pipe(1) | slice.pipe(-5, -1, 2)) await assert_run(xs, [15, 17]) with pytest.raises(ValueError): xs = stream.range(10, 20) | slice.pipe(5, 1, -1) with pytest.raises(ValueError): xs = stream.range(10, 20) | slice.pipe(-8, 8)
async def test_groupby(assert_run, event_loop): async def odd_or_even(x): return await asyncio.sleep(1, result=x % 2) def block_odd_or_even(x): return x % 2 with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.group_by( odd_or_even) await assert_run(xs, [[(0, [0, 2, 4]), (1, [1, 3])]]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.group_by( odd_or_even) await assert_run(xs, [[]]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.group_by( block_odd_or_even) await assert_run(xs, [[(0, [0, 2, 4]), (1, [1, 3])]]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.group_by( block_odd_or_even) await assert_run(xs, [[]])
async def test_take(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.count() | add_resource.pipe(1) | pipe.take(3) await assert_run(xs, [0, 1, 2]) with event_loop.assert_cleanup(): xs = stream.count() | add_resource.pipe(1) | pipe.take(0) await assert_run(xs, [])
async def test_skiplast(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(10) | add_resource.pipe(1) | pipe.skiplast(8) await assert_run(xs, [0, 1]) with event_loop.assert_cleanup(): xs = stream.range(10) | add_resource.pipe(1) | pipe.skiplast(0) await assert_run(xs, list(range(10)))
async def test_list(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.list() await assert_run(xs, [[0, 1, 2, 3, 4]]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.list() await assert_run(xs, [[]])
async def test_chain(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(5) + stream.range(5, 10) await assert_run(xs, list(range(10))) with event_loop.assert_cleanup(): xs = stream.range(10, 15) | add_resource.pipe(1) xs += stream.range(15, 20) | add_resource.pipe(1) await assert_run(xs, list(range(10, 20)))
async def test_list(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(3) | add_resource.pipe(1) | pipe.list() # The same list object is yielded at each step await assert_run(xs, [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.list() await assert_run(xs, [[]])
async def test_reduce(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.reduce(min) await assert_run(xs, [0]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.reduce(max) await assert_run(xs, [4]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.reduce(max) await assert_run(xs, [], IndexError("Index out of range"))
async def test_action(assert_run, event_loop): with event_loop.assert_cleanup(): lst = [] xs = stream.range(3) | add_resource.pipe(1) | pipe.action(lst.append) await assert_run(xs, [0, 1, 2]) assert lst == [0, 1, 2] with event_loop.assert_cleanup(): queue = asyncio.Queue() xs = stream.range(3) | add_resource.pipe(1) | pipe.action(queue.put) await assert_run(xs, [0, 1, 2]) assert queue.get_nowait() == 0 assert queue.get_nowait() == 1 assert queue.get_nowait() == 2
async def test_cycle(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.empty() | pipe.cycle() | pipe.timeout(1) await assert_run(xs, [], asyncio.TimeoutError()) with event_loop.assert_cleanup(): xs = stream.empty() | add_resource.pipe( 1) | pipe.cycle() | pipe.timeout(1) await assert_run(xs, [], asyncio.TimeoutError()) with event_loop.assert_cleanup(): xs = stream.just(1) | add_resource.pipe(1) | pipe.cycle() await assert_run(xs[:5], [1] * 5) assert event_loop.steps == [1] * 5
async def test_print(assert_run, event_loop): with event_loop.assert_cleanup(): f = io.StringIO() xs = stream.range(3) | add_resource.pipe(1) | pipe.print(file=f) await assert_run(xs, [0, 1, 2]) assert f.getvalue() == '0\n1\n2\n' with event_loop.assert_cleanup(): f = io.StringIO() xs = (stream.range(3) | add_resource.pipe(1) | pipe.print('{:.1f}', end='|', file=f)) await assert_run(xs, [0, 1, 2]) assert f.getvalue() == '0.0|1.0|2.0|'
async def test_dropwhile(assert_run, event_loop): with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.dropwhile(lambda x: x < 7)) await assert_run(xs, [7, 8, 9]) async def afunc(x): await asyncio.sleep(1) return x < 7 with event_loop.assert_cleanup(): xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.dropwhile(afunc) await assert_run(xs, [7, 8, 9]) assert event_loop.steps == [1] * 8
async def test_until(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range( 1, 10) | add_resource.pipe(1) | pipe.until(lambda x: x == 3) await assert_run(xs, [1, 2, 3]) async def afunc(x): await asyncio.sleep(1) return x == 3 with event_loop.assert_cleanup(): xs = stream.range(1, 10) | add_resource.pipe(1) | pipe.until(afunc) await assert_run(xs, [1, 2, 3]) assert event_loop.steps == [1] * 4
async def test_filter(assert_run, event_loop): with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.filter(lambda x: x in [4, 7, 8])) await assert_run(xs, [4, 7, 8]) async def afunc(x): await asyncio.sleep(1) return x in [3, 6, 9] with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.filter(afunc)) await assert_run(xs, [3, 6, 9]) assert event_loop.steps == [1] * 10
async def test_print(assert_run, event_loop): with event_loop.assert_cleanup(): f = io.StringIO() xs = stream.range(3) | add_resource.pipe(1) | pipe.print(file=f) await assert_run(xs, [0, 1, 2]) assert f.getvalue() == '0\n1\n2\n' with event_loop.assert_cleanup(): f = io.StringIO() xs = ( stream.range(3) | add_resource.pipe(1) | pipe.print('{:.1f}', end='|', file=f) ) await assert_run(xs, [0, 1, 2]) assert f.getvalue() == '0.0|1.0|2.0|'
async def test_streamcontext(event_loop): with event_loop.assert_cleanup(): xs = stream.range(3) | add_resource.pipe(1) async with streamcontext(xs) as streamer: it = iter(range(3)) async for item in streamer: assert item == next(it) assert event_loop.steps == [1] with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) async with xs.stream() as streamer: it = iter(range(5)) async for item in streamer: assert item == next(it) assert event_loop.steps == [1]
async def test_dropwhile(assert_run, event_loop): with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.dropwhile(lambda x: x < 7)) await assert_run(xs, [7, 8, 9]) async def afunc(x): await asyncio.sleep(1) return x < 7 with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.dropwhile(afunc)) await assert_run(xs, [7, 8, 9]) assert event_loop.steps == [1]*8
async def test_filter(assert_run, event_loop): with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.filter(lambda x: x in [4, 7, 8])) await assert_run(xs, [4, 7, 8]) async def afunc(x): await asyncio.sleep(1) return x in [3, 6, 9] with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.filter(afunc)) await assert_run(xs, [3, 6, 9]) assert event_loop.steps == [1]*10
async def test_takewhile(assert_run, event_loop): with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.takewhile(lambda x: x < 4)) await assert_run(xs, [1, 2, 3]) async def afunc(x): await asyncio.sleep(1) return x < 4 with event_loop.assert_cleanup(): xs = (stream.range(1, 10) | add_resource.pipe(1) | pipe.takewhile(afunc)) await assert_run(xs, [1, 2, 3]) assert event_loop.steps == [1] * 5
async def test_cycle(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.empty() | pipe.cycle() | pipe.timeout(1) await assert_run(xs, [], asyncio.TimeoutError()) with event_loop.assert_cleanup(): xs = ( stream.empty() | add_resource.pipe(1) | pipe.cycle() | pipe.timeout(1) ) await assert_run(xs, [], asyncio.TimeoutError()) with event_loop.assert_cleanup(): xs = stream.just(1) | add_resource.pipe(1) | pipe.cycle() await assert_run(xs[:5], [1]*5) assert event_loop.steps == [1]*5
async def test_item(assert_run, event_loop): item = stream.select.item with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(2) await assert_run(xs, [2]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(-2) await assert_run(xs, [3]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(10) exception = IndexError('Index out of range', ) await assert_run(xs, [], exception) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(-10) exception = IndexError('Index out of range', ) await assert_run(xs, [], exception)
async def test_item(assert_run, event_loop): item = stream.select.item with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(2) await assert_run(xs, [2]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(-2) await assert_run(xs, [3]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(10) exception = IndexError('Index out of range',) await assert_run(xs, [], exception) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | item.pipe(-10) exception = IndexError('Index out of range',) await assert_run(xs, [], exception)
async def test_getitem(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.getitem(2) await assert_run(xs, [2]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) await assert_run(xs[2], [2]) with event_loop.assert_cleanup(): s = slice(1, 3) xs = stream.range(5) | add_resource.pipe(1) | pipe.getitem(s) await assert_run(xs, [1, 2]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) await assert_run(xs[1:3], [1, 2]) with event_loop.assert_cleanup(): s = slice(1, 5, 2) xs = stream.range(5) | add_resource.pipe(1) | pipe.getitem(s) await assert_run(xs, [1, 3]) with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) await assert_run(xs[1:5:2], [1, 3]) with pytest.raises(TypeError): xs = stream.range(5)[None]
async def test_aggregate(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.accumulate() await assert_run(xs, [0, 1, 3, 6, 10]) with event_loop.assert_cleanup(): xs = (stream.range(2, 4) | add_resource.pipe(1) | pipe.accumulate(func=operator.mul, initializer=2)) await assert_run(xs, [2, 4, 12]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.accumulate() await assert_run(xs, []) async def sleepmax(x, y): return await asyncio.sleep(1, result=max(x, y)) with event_loop.assert_cleanup(): xs = stream.range(3) | add_resource.pipe(1) | pipe.accumulate(sleepmax) await assert_run(xs, [0, 1, 2]) assert event_loop.steps == [1] * 3
async def test_aggregate(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(5) | add_resource.pipe(1) | pipe.accumulate() await assert_run(xs, [0, 1, 3, 6, 10]) with event_loop.assert_cleanup(): xs = (stream.range(2, 4) | add_resource.pipe(1) | pipe.accumulate(func=operator.mul, initializer=2)) await assert_run(xs, [2, 4, 12]) with event_loop.assert_cleanup(): xs = stream.range(0) | add_resource.pipe(1) | pipe.accumulate() await assert_run(xs, []) async def sleepmax(x, y): return await asyncio.sleep(1, result=max(x, y)) with event_loop.assert_cleanup(): xs = stream.range(3) | add_resource.pipe(1) | pipe.accumulate(sleepmax) await assert_run(xs, [0, 1, 2]) assert event_loop.steps == [1]*3
async def test_chunks(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(3, interval=1) | pipe.chunks(3) await assert_run(xs, [[0, 1, 2]]) with event_loop.assert_cleanup(): xs = stream.range(4, interval=1) | pipe.chunks(3) await assert_run(xs, [[0, 1, 2], [3]]) with event_loop.assert_cleanup(): xs = stream.range(5, interval=1) | pipe.chunks(3) await assert_run(xs, [[0, 1, 2], [3, 4]]) with event_loop.assert_cleanup(): xs = stream.count(interval=1) | add_resource.pipe(1) | pipe.chunks(3) await assert_run(xs[:1], [[0, 1, 2]])
async def test_chunks(assert_run, event_loop): with event_loop.assert_cleanup(): xs = stream.range(3, interval=1) | pipe.chunks(3) await assert_run(xs, [[0, 1, 2]]) with event_loop.assert_cleanup(): xs = stream.range(4, interval=1) | pipe.chunks(3) await assert_run(xs, [[0, 1, 2], [3]]) with event_loop.assert_cleanup(): xs = stream.range(5, interval=1) | pipe.chunks(3) await assert_run(xs, [[0, 1, 2], [3, 4]]) with event_loop.assert_cleanup(): xs = (stream.count(interval=1) | add_resource.pipe(1) | pipe.chunks(3)) await assert_run(xs[:1], [[0, 1, 2]])
async def test_zip(assert_run, event_loop): xs = stream.range(5) | add_resource.pipe(1.0) ys = xs | pipe.zip(xs, xs) expected = [(x,)*3 for x in range(5)] await assert_run(ys, expected)
async def test_zip(assert_run, event_loop): xs = stream.range(5) | add_resource.pipe(1.0) ys = xs | pipe.zip(xs, xs) expected = [(x, ) * 3 for x in range(5)] await assert_run(ys, expected)
async def test_takelast(assert_run, event_loop): xs = stream.range(10) | add_resource.pipe(1) | pipe.takelast(3) await assert_run(xs, [7, 8, 9])
async def test_filterindex(assert_run, event_loop): filterindex = stream.select.filterindex xs = (stream.range(10) | add_resource.pipe(1) | filterindex.pipe(lambda x: x in [4, 7, 8])) await assert_run(xs, [4, 7, 8])
async def test_skip(assert_run, event_loop): xs = stream.range(10) | add_resource.pipe(1) | pipe.skip(8) await assert_run(xs, [8, 9])