Exemplo n.º 1
0
async def test_inner_value(subtests):
    """Ensure that coroutine correct value is preserved for all units."""
    containers = [
        # We have to define these values inside the test, because
        # otherwise `anyio` will `await` reused coroutines.
        # And they have to be fresh. That's why we use subtests for it.
        Future.from_futureresult(FutureResult.from_success(1)),
        Future.from_futureresult(FutureResult.from_failure(1)),
    ]
    for container in containers:
        with subtests.test(container=container):
            assert isinstance(await container, IO)
Exemplo n.º 2
0
async def test_flatten_future(subtests):
    """Ensures that `flatten` is always returning the correct type."""
    futures = [
        # Flattens:
        (Future.from_value(Future.from_value(1)), Future.from_value(1)),
        (
            FutureResult.from_value(FutureResult.from_value(1)),
            FutureResult.from_value(1),
        ),
    ]

    for container, merged in futures:
        with subtests.test(container=container, merged=merged):
            assert await flatten(container) == await merged  # type: ignore
Exemplo n.º 3
0
async def test_inner_value(subtests):
    """Ensure that coroutine correct value is preserved for all units."""
    containers = [
        # We have to define these values inside the test, because
        # otherwise `anyio` will `await` reused coroutines.
        # And they have to be fresh. That's why we use subtests for it.
        FutureResult.from_value(1),
        FutureResult.from_failure(1),
        FutureResult.from_io(IO(1)),
        FutureResult.from_failed_io(IO(1)),
        FutureResult.from_ioresult(IOSuccess(1)),
        FutureResult.from_ioresult(IOFailure(1)),
        FutureResult.from_result(Success(1)),
        FutureResult.from_result(Failure(1)),
        FutureResult.from_future(Future.from_value(1)),
        FutureResult.from_failed_future(Future.from_value(1)),
        FutureResult.from_typecast(Future.from_value(Success(1))),
    ]
    for container in containers:
        with subtests.test(container=container):
            result_inst = await container
            assert result_inst._inner_value._inner_value == 1  # noqa: WPS437
Exemplo n.º 4
0
async def test_fold_collect_future(subtests):
    """Iterable for ``Future`` and ``Fold``."""
    containers: List[Tuple[  # noqa: WPS234
        Iterable[Future[int]], Future[Sequence[int]], ]] = [
            ([], Future.from_value(())),
            ([Future.from_value(1)], Future.from_value((1, ))),
            (
                [Future.from_value(1),
                 Future.from_value(2)],
                Future.from_value((1, 2)),
            ),
        ]
    for iterable, sequence in containers:
        with subtests.test(iterable=iterable, sequence=sequence):
            assert await Fold.collect(
                iterable,
                sequence.from_value(()),
            ) == await sequence
Exemplo n.º 5
0
def test_nonequality():
    """Ensures that containers can be compared."""
    assert Future.from_value(1) != Future.from_value(1)
    assert hash(Future.from_value(1))
Exemplo n.º 6
0
from returns.io import IO, IOResult
from returns.maybe import Maybe
from returns.primitives.asserts import assert_equal
from returns.primitives.container import BaseContainer
from returns.result import Result

_containers: Sequence[BaseContainer] = (
    Result.from_failure(1),
    Result.from_value(1),
    IO(1),
    IOResult.from_failure(1),
    IOResult.from_value(1),
    Maybe.from_value(1),
    Maybe.from_value(None),
    Maybe.from_optional(None),
    Future.from_value(1),
    FutureResult.from_value(1),
    FutureResult.from_failure(1),
    Reader.from_value(1),
    ReaderResult.from_value(1),
    ReaderResult.from_failure(1),
    ReaderIOResult.from_value(1),
    ReaderIOResult.from_failure(1),
    ReaderFutureResult.from_value(1),
    ReaderFutureResult.from_failure(1),
)


@pytest.mark.parametrize('container', _containers)
def test_assert_equal(container, anyio_backend_name: str):
    """Ensure that containers can be equal."""
Exemplo n.º 7
0
import pytest

from returns.future import Future
from returns.primitives.interfaces import Applicative, Bindable, Mappable


@pytest.mark.parametrize('container', [
    Future.from_value(''),
])
@pytest.mark.parametrize('protocol', [
    Bindable,
    Mappable,
    Applicative,
])
def test_protocols(container, protocol):
    """Ensures that Future has all the right protocols."""
    assert isinstance(container, protocol)
Exemplo n.º 8
0
def test_return_false_with_future_container():
    """Ensures `is_io` function will return False for Future."""
    assert is_io(Future.from_value('future')) is False
Exemplo n.º 9
0
def _future_function(argument: int) -> Future[str]:
    return Future.from_value(str(argument + 1))
Exemplo n.º 10
0
async def test_bind_future():
    """Ensures that functions can be composed and return type is correct."""
    assert await bind(_future_function)(Future.from_value(1)) == IO('2')