async def test_async_iteration_inception() -> None: # iterable to async source to async iterator to async source ys = await subscribe(AsyncObservable.from_iterable([1, 2, 3])) xs = AsyncObservable.from_iterable(ys) result = [] async with subscribe(xs) as stream: async for x in stream: result.append(x) assert result == [1, 2, 3]
async def test_concat_special_iadd(): xs = AsyncObservable.from_iterable(range(5)) ys = AsyncObservable.from_iterable(range(5, 10)) result = [] async def asend(value): log.debug("test_merge_done:asend(%s)", value) result.append(value) xs += ys await run(xs, AsyncAnonymousObserver(asend)) assert result == list(range(10))
async def test_chain_complex_pipe(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value) -> int: return value * 10 async def predicate(value) -> bool: await asyncio.sleep(0.1) return value > 1 async def long_running(value) -> AsyncObservable: return AsyncObservable.from_iterable([value]) ys = (xs .where(predicate) .select(mapper) .select_many(long_running)) async def on_next(value): result.append(value) obv = AsyncAnonymousObserver(on_next) sub = await ys.subscribe(obv) await obv assert result == [20, 30]
async def test_pipe_complex_pipe(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 async def predicate(value): await asyncio.sleep(0.1) return value > 1 async def long_running(value): return AsyncObservable.from_iterable([value]) ys = pipe(xs, op.filter(predicate), op.map(mapper), op.flat_map(long_running) ) async with subscribe(ys) as stream: async for value in stream: result.append(value) assert result == [20, 30]
def test_accepts_type_definition_with_sync_subscribe_function(): SubscriptionType = GraphQLObjectType( name='Subscription', fields=OrderedDict([ ('importantEmail', GraphQLField( EmailEventType, resolver=lambda *_: AsyncObservable.from_iterable([None]))), ])) test_schema = GraphQLSchema(query=QueryType, subscription=SubscriptionType) stream = AsyncSingleStream() send_important_email, subscription = create_subscription( stream, test_schema) email = Email( from_='*****@*****.**', subject='Alright', message='Tests are good', unread=True, ) l = [] fut1 = asyncio.ensure_future(p_subscribe(l, stream)) fut2 = asyncio.ensure_future(send_important_email(email)) asyncio.get_event_loop().run_until_complete(asyncio.gather(fut1, fut2)) assert l # [0].data == {'importantEmail': None}
async def test_forward_pipe_complex_pipe() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value) -> int: return value * 10 async def predicate(value) -> bool: await asyncio.sleep(0.1) return value > 1 async def long_running(value) -> AsyncObservable[int]: return AsyncObservable.from_iterable([value]) ys = (xs | _.filter(predicate) | _.map(mapper) | _.flat_map(long_running) | _.to_async_iterable() ) async for value in ys: result.append(value) assert result == [20, 30]
async def test_async_comprehension() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) async with subscribe(xs) as stream: result = [x async for x in stream] assert result == [1, 2, 3]
async def test_async_iteration() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] async for x in to_async_iterable(xs): result.append(x) assert result == [1, 2, 3]
async def test_take_negative() -> None: xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) values = [] async def asend(value) -> None: values.append(value) with pytest.raises(ValueError): take(-1, xs)
async def test_async_iteration_aync_with() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] async with subscribe(xs) as stream: async for x in stream: result.append(x) assert result == [1, 2, 3]
async def test_async_iteration() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] stream = await subscribe(xs) async for x in stream: result.append(x) stream.cancel() assert result == [1, 2, 3]
async def test_async_iteration_aync_with() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] obv = AsyncIteratorObserver() async with subscribe(xs, obv): async for x in obv: result.append(x) assert result == [1, 2, 3]
async def main() -> None: xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) async def mapper(value) -> AsyncObservable: fut = executor.submit(long_running, value) return AsyncObservable.unit(wrap_future(fut)) ys = xs | op.flat_map(mapper) | op.to_async_iterable() async for x in ys: print(x)
async def main(): xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) async def mapper(value): fut = executor.submit(long_running, value) return AsyncObservable.unit(wrap_future(fut)) ys = xs | op.flat_map(mapper) async with subscribe(ys) as stream: async for x in stream: print(x)
async def main(): xs = AsyncObservable.from_iterable(range(10)) # Split into odds and evens odds = xs | op.filter(lambda x: x % 2 == 1) evens = xs | op.filter(lambda x: x % 2 == 0) async def mysink(value): print(value) await subscribe(odds, AsyncAnonymousObserver(mysink)) await subscribe(evens, AsyncAnonymousObserver(mysink))
async def test_async_iteration_inception() -> None: # iterable to async source to async iterator to async source obv = AsyncIteratorObserver() xs = AsyncObservable.from_iterable([1, 2, 3]) await subscribe(xs, obv) ys = from_async_iterable(obv) result = [] async for y in to_async_iterable(ys): result.append(y) assert result == [1, 2, 3]
async def test_slice_step(): xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) values = [] async def asend(value): values.append(value) ys = xs[::2] result = await run(ys, AsyncAnonymousObserver(asend)) assert result == 5 assert values == [1, 3, 5]
async def test_take_zero() -> None: xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) values = [] async def asend(value) -> None: values.append(value) ys = take(0, xs) with pytest.raises(CancelledError): await run(ys, AnonymousAsyncObserver(asend)) assert values == []
async def test_slice_special(): xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) values = [] async def asend(value): values.append(value) ys = xs[1:-1] result = await run(ys, AnonymousAsyncObserver(asend)) assert result == 4 assert values == [2, 3, 4]
async def test_take_normal() -> None: xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5]) values = [] async def asend(value) -> None: values.append(value) ys = take(2, xs) result = await run(ys, AnonymousAsyncObserver(asend)) assert result == 2 assert values == [1, 2]
async def test_pipe_map(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 ys = pipe(xs, op.map(mapper)) async def asend(value): result.append(value) await run(ys, AnonymousAsyncObserver(asend)) assert result == [10, 20, 30]
async def test_forward_pipe_map() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value) -> int: return value * 10 ys = xs | _.map(mapper) async def asend(value) -> None: result.append(value) await run(ys, AsyncAnonymousObserver(asend)) assert result == [10, 20, 30]
async def test_chain_map(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 ys = chain(xs).select(mapper) async def on_next(value): result.append(value) sub = await ys.subscribe(AnonymousAsyncObserver(on_next)) await sub assert result == [10, 20, 30]
async def test_pipe_simple_pipe(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 async def predicate(value): await asyncio.sleep(0.1) return value > 1 ys = pipe(xs, _.filter(predicate), _.map(mapper)) async def asend(value): result.append(value) await run(ys, AsyncAnonymousObserver(asend)) assert result == [20, 30]
async def test_forward_pipe_simple_pipe() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value) -> int: return value * 10 async def predicate(value) -> bool: await asyncio.sleep(0.1) return value > 1 ys = xs | _.filter(predicate) | _.map(mapper) async def asend(value) -> None: result.append(value) await run(ys, AsyncAnonymousObserver(asend)) assert result == [20, 30]
async def test_chain_simple_pipe(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 async def predicate(value): await asyncio.sleep(0.1) return value > 1 ys = chain(xs).where(predicate).select(mapper) async def on_next(value): result.append(value) sub = await ys.subscribe(AnonymousAsyncObserver(on_next)) await sub assert result == [20, 30]
async def test_chain_simple_pipe(): xs = AsyncObservable.from_iterable([1, 2, 3]) result = [] def mapper(value): return value * 10 async def predicate(value) -> bool: await asyncio.sleep(0.1) return value > 1 ys = xs.where(predicate).select(mapper) async def on_next(value) -> None: result.append(value) obv = AsyncAnonymousObserver(on_next) async with ys.subscribe(obv): await obv assert result == [20, 30]
def from_iterable(cls, iter): return ChainedAsyncObservable(AsyncObservable.from_iterable(iter))
async def test_async_comprehension() -> None: xs = AsyncObservable.from_iterable([1, 2, 3]) result = [x async for x in to_async_iterable(xs)] assert result == [1, 2, 3]
async def long_running(value): return AsyncObservable.from_iterable([value])