def sum_positive_values(first_value: int, second_value: int) -> Result[int, Error]:

        result_first_value = is_positive(first_value)
        if result_first_value.is_failure:
            return Result(failure=result_first_value.value)

        result_second_value = is_positive(second_value)
        if result_second_value.is_failure:
            return Result(failure=result_second_value.value)

        return Result(success=first_value + second_value)
示例#2
0
def test_should_repr_as_expected_an_error_with_message():
    given_any_message = "any message"

    class ErrorWithMessage(Error):
        def __init__(self, message: str):
            self.message = message

    result = Result(failure=ErrorWithMessage(given_any_message))

    assert (
        f"Result[status: failure | value: ErrorWithMessage: {given_any_message}]"
        == result.__repr__())
示例#3
0
def test_should_raise_a_type_error_when_result_is_constructed_with_success_and_failure_at_the_same_time(
):
    with pytest.raises(TypeError) as excinfo:
        _ = Result(success="Success", failure="Failure")
        assert (
            "Result is a monad, it cannot be success and failure at the same time."
            in str(excinfo.value))
示例#4
0
def test_should_create_a_failure_result_with_any_error():
    class AnyError(Error):
        pass

    result = Result(failure=AnyError())

    assert result.is_failure
    assert isinstance(result.value, AnyError)
    assert issubclass(result.value.__class__, Error)
示例#5
0
def test_should_be_the_same_a_result_with_true_success_with_a_success_class():

    result = Result(success=True)
    success = Success()
    success_with_true = Success(True)

    assert result == success
    assert result == success_with_true
    assert result == isSuccess
示例#6
0
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
示例#7
0
    def from_result(result: Result):
        if result.is_success:
            response = result.unwrap()
            return WebhookResponseResult(
                headers=response.headers,
                body=response.content,
                status_code=response.status_code,
                completed_in_ms=response.completed_in_ms,
            )

        error = result.value
        return WebhookResponseResult(
            headers=error.headers,
            body=error.content,
            status_code=error.status_code,
            completed_in_ms=error.completed_in_ms,
        )
示例#8
0
def test_should_assert_a_failure_result_checking_instance_and_value():
    result = Result(failure=5)
    assert_failure(result, value_is_instance_of=int, value_is_equal_to=5)
示例#9
0
def test_should_assert_a_success_result():
    result = Result(success=5)
    assert_success(result)
示例#10
0
def test_should_assert_a_failure_result():
    result = Result(failure=Error())
    assert_failure(result)
示例#11
0
def test_should_assert_a_failure_result_checking_instance():
    result = Result(failure=Error())
    assert_failure(result, value_is_instance_of=Error)
示例#12
0
def test_should_create_a_success_result_with_a_false_bool():
    result = Result(success=False)

    assert result.is_success
    assert result.value is False
示例#13
0
def test_should_raise_a_type_error_when_result_is_constructed_without_any_success_or_failure_value(
):
    with pytest.raises(TypeError) as excinfo:
        _ = Result()
        assert "Result is a monad, it must be a success or a failure." in str(
            excinfo.value)
示例#14
0
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 sum_positive_values(first_value: int, second_value: int) -> Result[int, Error]:

        is_positive(first_value).unwrap_or_return()
        is_positive(second_value).unwrap_or_return()

        return Result(success=first_value + second_value)
示例#16
0
def test_should_eq_two_different_success_result():
    result_1 = Result(success=2)
    result_2 = Result(success=3)

    assert result_1 != result_2
示例#17
0
def test_should_eq_a_result_with_another_type():
    result_1 = Result(success=2)
    result_2 = "no_result_value"

    assert result_1 != result_2
示例#18
0
def test_should_eq_two_equal_success_result():
    result_1 = Result(success=2)
    result_2 = Result(success=2)

    assert result_1 == result_2
示例#19
0
def test_should_repr_a_failure_result():
    result = Result(failure=Error())
    assert "Result[status: failure | value: Error]" == result.__repr__()
示例#20
0
def test_should_repr_a_success_result():
    result = Result(success=2)
    assert "Result[status: success | value: 2]" == result.__repr__()
示例#21
0
def test_should_create_a_success_result_with_a_none_value():
    result = Result(success=None)

    assert result.is_success
    assert result.value is None
示例#22
0
def test_should_eq_two_different_failure_result():
    result_1 = Result(failure=Error())
    result_2 = Result(failure=Exception())

    assert result_1 != result_2
 def is_positive(value: int) -> Result[bool, Error]:
     if value >= 0:
         return Result(success=True)
     else:
         return Result(failure=IsNegativeError())
示例#24
0
 def execute(self, *args, **kwargs) -> Result:
     raise Result(failure=NotImplementedError())
 def func(value: bool) -> Result[bool, Error]:
     if value:
         return Result(success=True)
     else:
         return Result(failure=Error())
示例#26
0
def test_should_assert_a_success_result_checking_instance_and_value():
    result = Result(success=5)
    assert_success(result, value_is_instance_of=int, value_is_equal_to=5)
示例#27
0
def test_property_should_works_with_any_type_on_success(given_value):

    result = Result(success=given_value)

    assert result.is_success
示例#28
0
def test_should_create_a_success_result_with_a_true_bool():
    result = Result(success=True)

    assert result.is_success
    assert result.value is True
示例#29
0
def test_should_repr_as_expected_default_error():

    result = Result(failure=Error())

    assert "Result[status: failure | value: Error]" == result.__repr__()
示例#30
0
def test_property_should_works_with_any_type_on_failure(given_value):

    result = Result(failure=given_value)

    assert result.is_failure