Ejemplo n.º 1
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
Ejemplo n.º 2
0
 def __call__(
     self,
     inner_value: str,
     use_result: Result[str, str],
 ) -> FutureResult[None, str]:
     self._logs.append((inner_value, use_result))
     return FutureResult.from_value(None)
Ejemplo n.º 3
0
async def test_inner_value(subtests):
    """Ensure that coroutine correct value is preserved for all units."""
    containers: List[Awaitable[Any]] = [
        # 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_value(1),
        Future.from_io(IO(1)),
        Future.from_future_result(FutureResult.from_value(1)),
        Future.from_future_result(FutureResult.from_failure(1)),
    ]
    for container in containers:
        with subtests.test(container=container):
            assert isinstance(await container, IO)
Ejemplo n.º 4
0
async def test_non_flatten_future(subtests):
    """Ensures that `flatten` is always returning the correct type."""
    futures = [
        # Not flattens:
        FutureResult.from_failure(FutureResult.from_failure(1)),
        FutureResult.from_failure(FutureResult.from_value(1)),
    ]

    for cont in futures:
        with subtests.test(container=cont):
            assert isinstance(
                (await flatten(cont)).failure()._inner_value,  # noqa: WPS437
                cont.__class__,
            )
    def from_value(
        cls,
        inner_value: _FirstType,
    ) -> 'RequiresContextFutureResult[_FirstType, Any, NoDeps]':
        """
        Creates new container with successful ``FutureResult`` as a unit value.

        .. code:: python

          >>> import anyio
          >>> from returns.context import RequiresContextFutureResult
          >>> from returns.io import IOSuccess

          >>> assert anyio.run(RequiresContextFutureResult.from_value(1)(
          ...    RequiresContextFutureResult.empty,
          ... ).awaitable) == IOSuccess(1)

        """
        return RequiresContextFutureResult(
            lambda _: FutureResult.from_value(inner_value), )
Ejemplo n.º 6
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
    def from_context(
        cls,
        inner_value: 'RequiresContext[_NewValueType, _NewEnvType]',
    ) -> 'RequiresContextFutureResult[_NewValueType, Any, _NewEnvType]':
        """
        Creates new container from ``RequiresContext`` as a success unit.

        .. code:: python

          >>> import anyio
          >>> from returns.context import RequiresContext
          >>> from returns.io import IOSuccess

          >>> assert anyio.run(
          ...     RequiresContextFutureResult.from_context(
          ...         RequiresContext.from_value(1),
          ...     ),
          ...     RequiresContextFutureResult.empty,
          ... ) == IOSuccess(1)

        """
        return RequiresContextFutureResult(
            lambda deps: FutureResult.from_value(inner_value(deps)), )
Ejemplo n.º 8
0
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."""
    assert_equal(container, container, backend=anyio_backend_name)
Ejemplo n.º 9
0
def _use_success(inner_value: str) -> FutureResult[str, str]:
    return FutureResult.from_value('use success')
Ejemplo n.º 10
0
def _acquire_success() -> FutureResult[str, str]:
    return FutureResult.from_value('acquire success')
Ejemplo n.º 11
0
 def factory(deps):
     return _managed(
         lambda inner: self._use(inner)(deps),
         lambda inner, pure: self._release(inner, pure)(deps),
     )(FutureResult.from_value(inner_value))
def test_nonequality():
    """Ensures that containers can be compared."""
    assert FutureResult.from_value(1) != FutureResult.from_value(1)
    assert hash(FutureResult.from_value(1))
Ejemplo n.º 13
0
def _future_result_function(argument: int) -> FutureResult[str, float]:
    return FutureResult.from_value(str(argument + 1))
Ejemplo n.º 14
0
async def test_bind_future_result():
    """Ensures that functions can be composed and return type is correct."""
    assert await bind(_future_result_function)(
        FutureResult.from_value(1), ) == IOSuccess('2')
    assert await bind(_future_result_function)(
        FutureResult.from_failure(1.0), ) == IOFailure(1.0)