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_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(): """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 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_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_requires_context_io_aliases(): """Ensures that ReaderIOResult correctly typecast.""" container: ReaderIOResultE[int, float] = _function(1) container2: ReaderIOResult[int, float, Exception] = _function(1) container3: ReaderIOResultE[int, float] = ReaderIOResultE.from_value(10.0, ) container4: ReaderIOResultE[int, float] = ReaderIOResult.from_value(10.0) assert container(0) == container2(0) == container3(0) == container4(0) assert container(0) == RequiresContextIOResult.from_value(10.0)(0)
def factory(inner_value) -> RCR[int, str, int]: return RCR.from_value(inner_value * 2)
def _function(arg: int) -> RequiresContextIOResultE[int, float]: if arg == 0: return RequiresContextIOResult.from_failure( ZeroDivisionError('Divided by 0'), ) return RequiresContextIOResult.from_value(10 / arg)
def test_requires_context_ioresulte(): """Ensures that RequiresContextIOResultE correctly typecast.""" container: RequiresContextIOResult[int, float, Exception] = _function(1) assert container(0) == RequiresContextIOResult.from_value(10.0)(0)
def test_requires_context_result_immutable_deepcopy(): # noqa: WPS118 """Ensures that helper returns it self when passed to deepcopy function.""" requires_context = RequiresContextIOResult.from_value(1) assert requires_context is deepcopy(requires_context)
def test_requires_context_result_immutable(): """Ensures that container is immutable.""" with pytest.raises(ImmutableStateError): RequiresContextIOResult.from_value(1).abc = 1
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_value(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, Unitable,
def test_requires_context_result_immutable_copy(): """Ensures that helper returns it self when passed to copy function.""" context_ioresult = RequiresContextIOResult.from_value(1) assert context_ioresult is copy(context_ioresult)
def test_bind_with_context_ioresult(): """Ensures that functions can be composed and return type is correct.""" bound = bind(_context_ioresult_function) assert bound(RequiresContextIOResult.from_value(3))(5) == IOSuccess(8)
def test_apply_with_context_ioresult(): """Ensures that functions can be composed and return type is correct.""" applied = apply(RequiresContextIOResult.from_value(_function)) assert applied(RequiresContextIOResult.from_value(1), )( ...) == IOSuccess('2')