def test_rescue_with_ioresult(): """Ensures that functions can be composed and return type is correct.""" rescued = rescue(_ioresult_function) assert rescued(IOSuccess(1)) == IOSuccess(1) assert rescued(IOFailure(1)) == IOSuccess(2) assert rescued(IOFailure(0)) == IOFailure('nope')
def test_bind_with_ioresult(): """Ensures that functions can be composed and return type is correct.""" bound = bind(_ioresult_function) assert bound(IOSuccess(1)) == IOSuccess('2') assert bound(IOFailure('a')) == IOFailure('a') assert bound(IOSuccess(1)) == unify(_ioresult_function)(IOSuccess(1))
def test_not_equals(): """Ensures that ``.equals`` method works correctly.""" inner_value = 1 assert not IOSuccess(inner_value).equals(IOFailure(inner_value)) assert not IOSuccess(inner_value).equals(IOSuccess(0)) assert not IOFailure(inner_value).equals(IOSuccess(inner_value)) assert not IOFailure(inner_value).equals(IOFailure(0))
def test_left_identity_failure(): """Ensures that left identity works for IOFailure container.""" def factory(inner_value: int) -> IOResult[int, int]: return IOFailure(6) input_value = 5 bound: IOResult[int, int] = IOFailure(input_value) assert bound.bind(factory) == IOFailure(input_value)
def test_rescue_failure(): """Ensures that rescue works for IOFailure container.""" def factory(inner_value: int) -> IOResult[str, int]: return IOFailure(inner_value + 1) expected = 6 bound: IOResult[str, int] = IOFailure(5) assert bound.rescue(factory) == IOFailure(expected)
def test_bind_ioresult(): """Ensures that io ``Result`` can be bound.""" def factory(inner_value: int) -> IOResult[int, str]: if inner_value > 0: return IOSuccess(inner_value + 1) return IOFailure('nope') first: RCR[int, str, int] = RCR.from_value(1) third: RCR[int, str, int] = RCR.from_failure('a') assert first.bind_ioresult(factory)(RCR.empty) == IOSuccess(2) assert RCR.from_value(0).bind_ioresult(factory, )( RCR.empty) == IOFailure('nope') assert third.bind_ioresult(factory)(RCR.empty) == IOFailure('a')
def test_bind_regular_result(): """Ensures that regular ``Result`` can be bound to ``IOResult``.""" def factory(inner_value: int) -> Result[int, str]: if inner_value > 0: return Success(inner_value + 1) return Failure('nope') first: IOResult[int, str] = IOSuccess(1) second: IOResult[int, str] = IOSuccess(0) third: IOResult[int, str] = IOFailure('a') assert first.bind_result(factory) == IOSuccess(2) assert second.bind_result(factory) == IOFailure('nope') assert third.bind_result(factory) == IOFailure('a')
def test_rescue_with_context_ioresult(): """Ensures that functions can be composed and return type is correct.""" rescued = rescue(_context_ioresult_function) assert rescued(RequiresContextIOResult.from_value(1), )(1) == IOSuccess(1) assert rescued( RequiresContextIOResult.from_failure(1), )(1) == IOSuccess(2) assert rescued( RequiresContextIOResult.from_failure(0), )(1) == IOFailure('nope')
def test_unwrap_iofailure_with_exception(): """Ensures that unwrap raises from the original exception.""" expected_exception = ValueError('error') with pytest.raises(UnwrapFailedError) as excinfo: IOFailure(expected_exception).unwrap() assert 'ValueError: error' in str( excinfo.getrepr(), # noqa: WPS441 )
def test_bind_regular_context(): """Ensures that regular ``RequiresContext`` can be bound.""" def factory(inner_value: int) -> RequiresContext[float, int]: return RequiresContext(lambda deps: inner_value / deps) first: RCR[int, str, int] = RCR.from_value(1) third: RCR[int, str, int] = RCR.from_failure('a') assert first.bind_context(factory)(2) == IOSuccess(0.5) assert RCR.from_value(2).bind_context(factory, )(1) == IOSuccess(2.0) assert third.bind_context(factory)(1) == IOFailure('a')
def test_bind_result_context(): """Ensures that ``RequiresContextResult`` can be bound.""" def factory(inner_value: int) -> RequiresContextResult[int, float, str]: return RequiresContextResult(lambda deps: Success(inner_value / deps)) first: RCR[int, int, str] = RCR.from_success(1) third: RCR[int, int, str] = RCR.from_failure('a') assert first.bind_context_result(factory)(2) == IOSuccess(0.5) assert RCR.from_success(2).bind_context_result( factory, )(1) == IOSuccess(2.0) assert third.bind_context_result(factory)(1) == IOFailure('a')
def test_bind(): """Ensures that bind works.""" def factory(inner_value: int) -> RCR[float, str, int]: if inner_value > 0: return RCR(lambda deps: IOSuccess(inner_value / deps)) return RCR.from_failure(str(inner_value)) input_value = 5 bound: RCR[int, str, int] = RCR.from_value(input_value) assert bound.bind(factory)(2) == factory(input_value)(2) assert bound.bind(factory)(2) == IOSuccess(2.5) assert RCR.from_value(0).bind( factory, )(2) == factory(0)(2) == IOFailure('0')
def from_failure( cls, inner_value: _FirstType, ) -> 'RequiresContextIOResult[NoDeps, Any, _FirstType]': """ Creates new container with ``IOFailure(inner_value)`` as a unit value. .. code:: python >>> from returns.context import RequiresContextIOResult >>> from returns.io import IOFailure >>> assert RequiresContextIOResult.from_failure(1)( ... RequiresContextIOResult.empty, ... ) == IOFailure(1) """ return RequiresContextIOResult(lambda _: IOFailure(inner_value))
def from_failure( cls, inner_value: _NewErrorType, ) -> RequiresContextIOResult[Any, _NewErrorType, NoDeps]: """ Creates new container with ``IOFailure(inner_value)`` as a unit value. .. code:: python >>> from returns.context import RequiresContextIOResult >>> from returns.io import IOFailure >>> assert RequiresContextIOResult.from_failure(1)( ... RequiresContextIOResult.no_args, ... ) == IOFailure(1) """ return RequiresContextIOResult(lambda _: IOFailure(inner_value))
def from_failed_context( cls, inner_value: 'RequiresContext[_EnvType, _FirstType]', ) -> 'RequiresContextIOResult[_EnvType, Any, _FirstType]': """ Creates new container from ``RequiresContext`` as a failure unit. .. code:: python >>> from returns.context import RequiresContext >>> from returns.io import IOFailure >>> assert RequiresContextIOResult.from_failed_context( ... RequiresContext.from_value(1), ... )(...) == IOFailure(1) """ return RequiresContextIOResult( lambda deps: IOFailure(inner_value(deps)), )
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 test_ioresult_value_or(): """Ensures that ``value_or`` works correctly.""" assert IOSuccess(1).value_or(0) == IO(1) assert IOFailure(1).value_or(0) == IO(0)
def test_failure_iofailure(): """Ensures that failure works for IOFailure container.""" assert IOFailure(5).failure() == IO(5)
def test_unwrap_iofailure(): """Ensures that unwrap works for IOFailure container.""" with pytest.raises(UnwrapFailedError): assert IOFailure(5).unwrap()
from returns.result import Failure, Success @pytest.mark.parametrize( ('container', 'merged'), [ # Flattens: (IO(IO(1)), IO(1)), (Success(Success({})), Success({})), (IOSuccess(IOSuccess(1)), IOSuccess(1)), (Some(Some(None)), Nothing), (Some(Some([])), Some([])), # Nope: (Failure(Failure('a')), Failure(Failure('a'))), (IOFailure(IOFailure('a')), IOFailure(IOFailure('a'))), ]) def test_flatten(container, merged): """Ensures that `flatten` is always returning the correct type.""" assert flatten(container) == merged @pytest.mark.parametrize(('container', 'merged'), [ ( RequiresContextResult.from_success( RequiresContextResult.from_success(1), ), RequiresContextResult.from_success(1), ), ( RequiresContextIOResult.from_success( RequiresContextIOResult.from_success(1), ),
@pytest.mark.parametrize( ('acquire', 'use', 'release', 'final_result', 'log'), [ # Acquire success: ( _acquire_success, _use_success, _ReleaseSuccess, IOSuccess('use success'), [('acquire success', Success('use success'))], ), ( _acquire_success, _use_success, _ReleaseFailure, IOFailure('release failure'), [], ), ( _acquire_success, _use_failure, _ReleaseSuccess, IOFailure('use failure'), [('acquire success', Failure('use failure'))], ), ( _acquire_success, _use_failure, _ReleaseFailure, IOFailure('release failure'), [],
import pytest from returns.io import IOFailure, IOSuccess from returns.maybe import Nothing, Some from returns.pipeline import is_successful from returns.result import Failure, Success @pytest.mark.parametrize(('container', 'correct_result'), [ (Success('a'), True), (Failure('a'), False), (IOSuccess('a'), True), (IOFailure('a'), False), (Some('a'), True), (Some(None), True), (Nothing, False), ]) def test_is_successful(container, correct_result): """Ensures that successful state works correctly.""" assert is_successful(container) is correct_result
def __call__( self, inner_value: str, use_result: Result[str, str], ) -> IOResult[None, str]: return IOFailure('release failure')
def factory(inner_value: int) -> IOResult[int, str]: if inner_value > 0: return IOSuccess(inner_value * 2) return IOFailure(str(inner_value))
from returns.primitives.exceptions import ImmutableStateError from returns.primitives.interfaces import ( Altable, Bindable, Fixable, Mappable, Rescueable, Unitable, Unwrapable, ) from returns.result import Failure, Success @pytest.mark.parametrize('container', [ RequiresContextIOResult(lambda _: IOSuccess(1)), RequiresContextIOResult(lambda _: IOFailure(1)), RequiresContextIOResult.from_success(1), RequiresContextIOResult.from_failure(1), RequiresContextIOResult.from_result(Success(1)), RequiresContextIOResult.from_result(Failure(1)), RequiresContextIOResult.from_ioresult(IOSuccess(1)), RequiresContextIOResult.from_ioresult(IOFailure(1)), ContextIOResult.ask(), ]) @pytest.mark.parametrize('protocol', [ Bindable, Mappable, Rescueable, Unwrapable, Altable, Fixable,
def factory(inner_value: int) -> IOResult[int, int]: return IOFailure(6)
Failure('a'), ), ([Success(1), Failure('a')], Failure('a')), ([Failure('a'), Failure('b')], Failure('a')), ([], Some(())), ([Some(1)], Some((1, ))), ([Some(1), Some(2)], Some((1, 2))), ([Nothing, Some(1), Some(2)], Nothing), ([Some(1), Nothing, Some(2)], Nothing), ([Some(1), Some(2), Nothing], Nothing), ([Nothing], Nothing), ([], IOSuccess(())), ([IOSuccess(1)], IOSuccess((1, ))), ([IOSuccess(1), IOSuccess(2)], IOSuccess((1, 2))), ( [IOFailure('a'), IOSuccess(1), IOSuccess(2)], IOFailure('a'), ), ([IOSuccess(1), IOFailure('a')], IOFailure('a')), ([IOFailure('a'), IOFailure('b')], IOFailure('a')), ]) def test_fold_collect(iterable, sequence): """Iterable for regular types and ``Fold``.""" assert Fold.collect(iterable, sequence.from_value(())) == sequence @pytest.mark.parametrize( ('iterable', 'sequence'), [ # Regular types:
def factory(inner_value: int) -> IOResult[str, int]: return IOFailure(inner_value + 1)
def _function(arg: int) -> IOResultE[float]: if arg == 0: return IOFailure(ZeroDivisionError('Divided by 0')) return IOSuccess(10 / arg)
def factory(inner_value: int) -> IOResult[int, str]: if inner_value > 0: return IOSuccess(inner_value + 1) return IOFailure('nope')