def test_rescue_failure(): """Ensures that rescue works for Failure container.""" def factory(inner_value) -> RCR[int, str, int]: return RCR.from_failure(inner_value * 2) assert RCR.from_value(5).rescue(factory, )(0) == RCR.from_value(5)(0) assert RCR.from_failure(5).rescue(factory, )(0) == RCR.from_failure(10)(0)
def test_lash_success(): """Ensures that lash works for Success container.""" def factory(inner_value) -> RCR[int, str, int]: return RCR.from_value(inner_value * 2) assert RCR.from_value(5).lash(factory, )(0) == RCR.from_value(5)(0) assert RCR.from_failure(5).lash(factory, )(0) == RCR.from_value(10)(0)
def test_rescue_with_context_result(): """Ensures that functions can be composed and return type is correct.""" rescued = rescue(_context_result_function) assert rescued(RequiresContextResult.from_value(1), )(1) == Success(1) assert rescued(RequiresContextResult.from_failure(1), )(1) == Success(2) assert rescued( RequiresContextResult.from_failure(0), )(1) == Failure('nope')
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) == Success(0.5) assert RCR.from_value(2).bind_context(factory, )(1) == Success(2.0) assert third.bind_context(factory)(1) == Failure('a')
def test_rescue_success(): """Ensures that rescue works for Success container.""" def factory(inner_value) -> RCR[int, int, str]: return RCR.from_success(inner_value * 2) assert RCR.from_success(5).rescue( factory, )(0) == RCR.from_success(5)(0) assert RCR.from_failure(5).rescue( factory, )(0) == RCR.from_success(10)(0)
def test_bind_regular_result(): """Ensures that regular ``Result`` can be bound.""" def factory(inner_value: int) -> Result[int, str]: if inner_value > 0: return Success(inner_value + 1) return Failure('nope') first: RCR[int, str, int] = RCR.from_value(1) third: RCR[int, str, int] = RCR.from_failure('a') assert first.bind_result(factory)(RCR.empty) == Success(2) assert RCR.from_value(0).bind_result(factory, )( RCR.empty) == Failure('nope') assert third.bind_result(factory)(RCR.empty) == Failure('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: Success(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) == Success(2.5) assert RCR.from_value(0).bind( factory, )(2) == factory(0)(2) == Failure('0')
def test_requires_context_aliases(): """Ensures that ReaderResult correctly typecast.""" container: ReaderResultE[float, int] = _function(1) container2: ReaderResult[float, Exception, int] = _function(1) container3: ReaderResultE[float, int] = ReaderResultE.from_value(10.0, ) container4: ReaderResultE[float, int] = ReaderResult.from_value(10.0) assert container(0) == container2(0) == container3(0) == container4(0) assert container(0) == RequiresContextResult.from_value(10.0)(0)
def test_requires_context_resulte(): """Ensures that RequiresContextResultE correctly typecast.""" container: RequiresContextResult[float, Exception, int] = _function(1) assert container(0) == RequiresContextResult.from_value(10.0)(0)
def test_requires_context_result_immutable(): """Ensures that container is immutable.""" with pytest.raises(ImmutableStateError): RequiresContextResult.from_success(1).abc = 1
from returns.context import ContextResult, RequiresContextResult 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', [ RequiresContextResult(lambda _: Success(1)), RequiresContextResult(lambda _: Failure(1)), RequiresContextResult.from_success(1), RequiresContextResult.from_failure(1), RequiresContextResult.from_result(Success(1)), RequiresContextResult.from_result(Failure(1)), ContextResult.ask(), ]) @pytest.mark.parametrize('protocol', [ Bindable, Mappable, Rescueable, Unwrapable, Altable, Fixable, Unitable,
# Nope: (Nothing, Nothing), (Failure(Failure('a')), Failure(Failure('a'))), (Failure(Success('a')), Failure(Success('a'))), (IOFailure(IOFailure('a')), IOFailure(IOFailure('a'))), (IOFailure(IOSuccess('a')), IOFailure(IOSuccess('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_value( RequiresContextResult.from_value(1), ), RequiresContextResult.from_value(1), ), ( RequiresContextIOResult.from_value( RequiresContextIOResult.from_value(1), ), RequiresContextIOResult.from_value(1), ), ( RequiresContext.from_value(RequiresContext.from_value(1)), RequiresContext.from_value(1), ), ]) def test_flatten_context(container, merged): """Ensures that `flatten` is always returning the correct type.""" assert flatten(container)(...) == merged(...)
def test_apply_with_context_result(): """Ensures that functions can be composed and return type is correct.""" applied = apply(RequiresContextResult.from_value(_function)) assert applied(RequiresContextResult.from_value(1), )(...) == Success('2')
def test_bind_with_context_result(): """Ensures that functions can be composed and return type is correct.""" binded = bind(_context_result_function) assert binded(RequiresContextResult.from_success(3))(5) == Success(8)
def factory(inner_value: int) -> RequiresContextResult[float, str, int]: return RequiresContextResult(lambda deps: Success(inner_value / deps))
def _context_result_function( argument: int, ) -> RequiresContextResult[int, int, str]: if argument > 0: return RequiresContextResult(lambda deps: Success(argument + deps)) return RequiresContextResult.from_failure('nope')
def _function(arg: int) -> RequiresContextResultE[float, int]: if arg == 0: return RequiresContextResult.from_failure( ZeroDivisionError('Divided by 0'), ) return RequiresContextResult.from_value(10 / arg)
def test_immutable_copy(): """Ensures that helper returns it self when passed to copy function.""" context_result = RequiresContextResult.from_value(1) assert context_result is copy(context_result)
def factory(inner_value) -> RCR[int, str, int]: return RCR.from_value(inner_value * 2)
def test_return_false_with_requires_context_result_container(): # noqa: WPS118 """Ensures `is_io` function will return False for RequiresContextResult.""" assert is_io(RequiresContextResult.from_success(Success(True))) is False
def factory(inner_value) -> RCR[int, int, str]: return RCR.from_success(inner_value * 2)
def factory(inner_value) -> RCR[int, int, str]: return RCR.from_failure(inner_value * 2)
def factory(inner_value: int) -> RCR[int, float, str]: if inner_value > 0: return RCR(lambda deps: Success(inner_value / deps)) return RCR.from_failure(str(inner_value))
def _context_result_function( argument: int, ) -> RequiresContextResult[int, int, str]: return RequiresContextResult(lambda other: Success(argument + other))
): if should_lash: return container.lash(lambda inner: container.from_failure(inner)) return container.bind(lambda inner: container.from_value(inner)) @pytest.mark.parametrize('container', [ Success(1), Failure(1), IOSuccess(1), IOFailure(1), RequiresContextIOResult.from_value(1), RequiresContextIOResult.from_failure(1), RequiresContextFutureResult.from_value(1), RequiresContextFutureResult.from_failure(1), RequiresContextResult.from_value(1), RequiresContextResult.from_failure(1), FutureResult.from_value(1), FutureResult.from_failure(1), ]) @pytest.mark.parametrize('kwargs', [ { 'should_lash': True }, ]) def test_error_handled(container, returns, kwargs): """Demo on how to use ``pytest`` helpers to work with error handling.""" error_handled = _under_test(container, **kwargs) assert returns.is_error_handled(error_handled) assert returns.is_error_handled(error_handled.map(identity))
(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), ), RequiresContextIOResult.from_success(1), ), ( RequiresContext.from_value(RequiresContext.from_value(1)), RequiresContext.from_value(1), ), ]) def test_flatten_context(container, merged): """Ensures that `flatten` is always returning the correct type.""" assert flatten(container)(...) == merged(...)
): if should_rescue: return container.rescue(lambda inner: container.from_failure(inner)) if should_fix: return container.fix(identity) return container.bind(lambda inner: container.from_success(inner)) @pytest.mark.parametrize('container', [ Success(1), Failure(1), IOSuccess(1), IOFailure(1), RequiresContextIOResult.from_success(1), RequiresContextIOResult.from_failure(1), RequiresContextResult.from_success(1), RequiresContextResult.from_failure(1), ]) @pytest.mark.parametrize('kwargs', [ {'should_rescue': True}, {'should_fix': True}, {'should_rescue': True, 'should_fix': True}, ]) def test_error_handled(container, returns, kwargs): """Demo on how to use ``pytest`` helpers to work with error handling.""" error_handled = _under_test(container, **kwargs) assert returns.is_error_handled(error_handled) assert returns.is_error_handled(error_handled.map(identity)) assert returns.is_error_handled(error_handled.alt(identity))