def test_should_be_the_same_a_result_with_an_error_failure_with_a_failure_class( ): result = Result(failure=Error()) failure = Failure() failure_with_error = Failure(Error()) assert result == failure assert result == failure_with_error assert result == isFailure
def test_should_raise_an_exception_when_unwrap_or_throw_with_a_failure_result( ): result = Failure(Error()) with pytest.raises(Error): _ = result.unwrap_or_throw()
def test_should_transform_a_failure_result_encapsulated_value(): def transform(domain_error): if isinstance(domain_error, Error): return "Error" else: return domain_error result = Failure(Error()) result.map(transform) assert result.value == "Error"
def test_should_call_on_failure_when_unwrap_or_else_with_a_result_failure_without_passing_arguments( ): global called_on_failure called_on_failure = False def on_failure(): global called_on_failure called_on_failure = True result = Failure(Error()) _ = result.unwrap_or_else(on_failure) assert called_on_failure
def test_should_call_on_failure_when_unwrap_or_else_with_a_result_failure(): global called_on_failure called_on_failure = False def on_failure(failure_value): global called_on_failure called_on_failure = True assert isinstance(failure_value, Error) result = Failure(Error()) _ = result.unwrap_or_else(on_failure) assert called_on_failure
def test_should_call_on_failure_when_unwrap_or_else_with_failure_result_and_custom_args( ): param1 = 1 param2 = "param2" global called_on_failure called_on_failure = False def on_failure(param1, param2): global called_on_failure called_on_failure = True assert isinstance(param1, int) assert isinstance(param2, str) result = Failure(Error()) _ = result.unwrap_or_else(on_failure, failure_args=(param1, param2)) assert called_on_failure
def test_should_assert_a_failure_result(): result = Result(failure=Error()) assert_failure(result)
def decorated_method(): raise Error()
def test_should_throw_an_exception_when_result_is_failure(): with pytest.raises(Error): Failure(Error()).throw()
def test_should_repr_a_failure_result(): result = Result(failure=Error()) assert "Result[status: failure | value: Error]" == result.__repr__()
def test_should_return_none_when_unwrap_a_failure_result(): result = Failure(Error()) value = result.unwrap() assert value is None
def test_should_eq_two_equal_failure_result(): result_1 = Result(failure=Error()) result_2 = Result(failure=Error()) assert result_1 == result_2
def test_should_return_default_none_when_unwrap_or_a_failure_result(): result = Failure(Error()) value = result.unwrap_or("Error") assert value == "Error"
def test_should_assert_a_failure_result_checking_instance(): result = Result(failure=Error()) assert_failure(result, value_is_instance_of=Error)
def test_should_eq_two_different_failure_result(): result_1 = Result(failure=Error()) result_2 = Result(failure=Exception()) assert result_1 != result_2
def func(value: bool) -> Result[bool, Error]: if value: return Result(success=True) else: return Result(failure=Error())
def test_should_create_a_failure_result_with_a_generic_error(): result = Result(failure=Error()) assert result.is_failure assert isinstance(result.value, Error)
def test_should_repr_as_expected_default_error(): result = Result(failure=Error()) assert "Result[status: failure | value: Error]" == result.__repr__()
result = Success("Hi!") _ = result.unwrap_and(on_success, success_args=(param1, param2)) assert called_on_success @pytest.mark.unit @pytest.mark.parametrize( "input_result,expected_result", [ (isSuccess, isSuccess), (isFailure, isFailure), (Success("Hi!"), Success("Hi!")), (Failure(Error()), Failure(Error())), ], ) def test_should_unwrap_or_return_with_contained_result(input_result, expected_result): @meiga def method(result: Result) -> Result[Any, Error]: value = result.unwrap_or_return() return Success(value) result = method(input_result) assert result == expected_result @pytest.mark.unit