async def test_pipe_complex_pipe(): xs = rx.from_iterable([1, 2, 3]) result = [] def mapper(value: int) -> int: return value * 10 async def predicate(value: int) -> bool: await asyncio.sleep(0.1) return value > 1 def long_running(value: int) -> AsyncObservable[int]: return rx.from_iterable([value]) ys = pipe( xs, rx.filter_async(predicate), rx.map(mapper), rx.flat_map(long_running), rx.to_async_iterable, ) async for value in ys: result.append(value) assert result == [20, 30]
async def test_filter_happy() -> None: xs = rx.from_iterable([1, 2, 3]) result = [] async def asend(value: int) -> None: result.append(value) async def predicate(value: int) -> bool: await asyncio.sleep(0.1) return value > 1 ys = pipe(xs, rx.filter_async(predicate)) value = await rx.run(ys, rx.AsyncAwaitableObserver(asend)) assert value == 3 assert result == [2, 3]
async def test_filter_predicate_throws() -> None: xs = rx.from_iterable([1, 2, 3]) err = MyException("err") result = [] async def asend(value: int) -> None: result.append(value) async def predicate(value: int) -> bool: await asyncio.sleep(0.1) raise err ys = pipe(xs, rx.filter_async(predicate)) with pytest.raises(MyException): await rx.run(ys, rx.AsyncAwaitableObserver(asend)) assert result == []
async def test_pipe_simple_pipe(): xs = rx.from_iterable([1, 2, 3]) def mapper(value: int) -> int: return value * 10 async def predicate(value: int) -> bool: await asyncio.sleep(0.1) return value > 1 ys = pipe(xs, rx.filter_async(predicate), rx.map(mapper)) obv: AsyncTestObserver[int] = AsyncTestObserver() await rx.run(ys, obv) assert obv.values == [ (approx(0.2), OnNext(20)), (approx(0.3), OnNext(30)), (approx(0.3), OnCompleted), ]