Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

33 add assert success and assert failure to result class #34

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,19 @@

## Installation 💻

~~~
```console
pip install meiga
~~~

## Overview

<iframe src="https://www.linkedin.com/embed/feed/update/urn:li:ugcPost:6973279084642480129?compact=1" height="399" width="710" frameborder="0" allowfullscreen="" title="Embedded post"></iframe>
```

## Documentation

Please, check out our [documentation]() TODO

This package provides a new type class, the `Result[Type, Type]`
This Result type allows to simplify a wide range of problems, like handling potential undefined values, or reduce complexity handling exceptions. Additionally, code can be simplified following a semantic pipeline reducing the visual noise of checking data types, controlling runtime flow and side-effects.

This package is based in another solutions from another modern languages as this swift-based [Result](https://github.com/antitypical/Result) implementation.
Please, check out our [documentation](https://alice-biometrics.github.io/meiga/)

## Contribute

We'd love you to contribute to *meiga* 📜⚗️ 🙋🏾‍♀️🤷🏾‍♂️!

For more information, check our [documentation]()
For more information, check our [documentation](https://alice-biometrics.github.io/meiga/contributing/)

## Contact 📬

Expand Down
11 changes: 6 additions & 5 deletions meiga/assertions/assert_failure.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from typing import Any
from typing import TYPE_CHECKING, Any, Type

from meiga.result import Result
if TYPE_CHECKING:
from meiga.result import Result


def assert_failure(
result: Result, value_is_instance_of: Any = None, value_is_equal_to: Any = None
):
result: "Result", value_is_instance_of: Type = None, value_is_equal_to: Any = None
) -> None:
assert (
result.is_failure
), f"result is not failure as expected. Given failure value is {result.value}"
if value_is_instance_of:
assert isinstance(result.value, value_is_instance_of), (
f"Value is not instance of {value_is_instance_of}. "
f"Given value is {result.value}"
f"Given value is {result.value} of {type({result.value})}"
)
if value_is_equal_to:
assert result.value == value_is_equal_to, (
Expand Down
11 changes: 6 additions & 5 deletions meiga/assertions/assert_success.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from typing import Any
from typing import TYPE_CHECKING, Any, Type

from meiga.result import Result
if TYPE_CHECKING:
from meiga.result import Result


def assert_success(
result: Result, value_is_instance_of: Any = None, value_is_equal_to: Any = None
):
result: "Result", value_is_instance_of: Type = None, value_is_equal_to: Any = None
) -> None:
assert (
result.is_success
), f"result is not success as expected. Given failure value is {result.value}"
if value_is_instance_of:
assert isinstance(result.value, value_is_instance_of), (
f"Value is not instance of {value_is_instance_of}. "
f"Given value is {result.value}"
f"Given value is {result.value} of {type({result.value})}"
)
if value_is_equal_to:
assert result.value == value_is_equal_to, (
Expand Down
19 changes: 19 additions & 0 deletions meiga/result.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Callable, Generic, Optional, Type, TypeVar, Union, cast

from meiga.assertions import assert_failure, assert_success
from meiga.deprecation import (
get_on_failure_handler_from_deprecated_args,
get_on_success_handler_from_deprecated_args,
Expand Down Expand Up @@ -166,4 +167,22 @@ def map(self, transform: Callable) -> None:
new_value = transform(self.value)
self.set_value(new_value)

def assert_success(
self, value_is_instance_of: Type = None, value_is_equal_to: Any = None
) -> None:
assert_success(
result=self,
value_is_instance_of=value_is_instance_of,
value_is_equal_to=value_is_equal_to,
)

def assert_failure(
self, value_is_instance_of: Type = None, value_is_equal_to: Any = None
) -> None:
assert_failure(
result=self,
value_is_instance_of=value_is_instance_of,
value_is_equal_to=value_is_equal_to,
)

value = property(get_value)
58 changes: 24 additions & 34 deletions tests/unit/test_result_assertions.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
import pytest

from meiga import Error, Result
from meiga.assertions import assert_failure, assert_success


@pytest.mark.unit
def test_should_assert_a_success_result():
result = Result(success=5)
assert_success(result)


@pytest.mark.unit
def test_should_assert_a_success_result_checking_instance():
result = Result(success=5)
assert_success(result, value_is_instance_of=int)


@pytest.mark.unit
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)


@pytest.mark.unit
def test_should_assert_a_failure_result():
result = Result(failure=Error())
assert_failure(result)


@pytest.mark.unit
def test_should_assert_a_failure_result_checking_instance():
result = Result(failure=Error())
assert_failure(result, value_is_instance_of=Error)


@pytest.mark.unit
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)
class TestResultAssertion:
def should_assert_a_success(self):
result = Result(success=5)
result.assert_success()

def should_assert_a_success_checking_instance(self):
result = Result(success=5)
result.assert_success(value_is_instance_of=int)

def should_assert_a_success_checking_instance_and_value(self):
result = Result(success=5)
result.assert_success(value_is_instance_of=int, value_is_equal_to=5)

def should_assert_a_failure(self):
result = Result(failure=Error())
result.assert_failure()

def should_assert_a_failure_checking_instance(self):
result = Result(failure=Error())
result.assert_failure(value_is_instance_of=Error)

def test_should_assert_a_failure_result_checking_instance_and_value(self):
result = Result(failure=5)
result.assert_failure(value_is_instance_of=int, value_is_equal_to=5)