Exemplo n.º 1
0
async def test_map_mapper_throws():
    error = Exception("ex")
    exception = None

    xs = rx.from_iterable([1])

    async def athrow(ex: Exception):
        nonlocal exception
        exception = ex

    def mapper(x: int):
        raise error

    ys = pipe(xs, rx.map(mapper))

    obv = rx.AsyncAwaitableObserver(athrow=athrow)

    await ys.subscribe_async(obv)

    try:
        await obv
    except Exception as ex:
        assert exception == ex
    else:
        assert False
Exemplo n.º 2
0
async def test_concat_happy():
    xs = rx.from_iterable(range(5))
    ys = rx.from_iterable(range(5, 10))
    result = []

    async def asend(value: int) -> None:
        result.append(value)

    zs = pipe(xs, rx.concat(ys))

    obv: rx.AsyncObserver[int] = rx.AsyncAwaitableObserver(asend)
    await rx.run(zs, obv)
    assert result == list(range(10))
Exemplo n.º 3
0
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]
Exemplo n.º 4
0
async def test_mapi_works():
    xs: AsyncObservable[int] = rx.from_iterable([1, 2, 3])
    values = []

    async def asend(value: int) -> None:
        values.append(value)

    def mapper(a: int, i: int) -> int:
        return a + i

    ys = pipe(xs, rx.mapi(mapper))

    obv: AsyncObserver[int] = rx.AsyncAwaitableObserver(asend)
    async with await ys.subscribe_async(obv):
        result = await obv
        assert result == 5
        assert values == [1, 3, 5]
Exemplo n.º 5
0
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 == []