Skip to content

Commit

Permalink
Merge pull request #4 from alice-biometrics/feature/add_params_to_res…
Browse files Browse the repository at this point in the history
…ult_handle

Now handle function (Result) is pararametrizable
  • Loading branch information
acostapazo authored Dec 19, 2019
2 parents 936d067 + dc2c7c8 commit f6c226c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- name: Lint with flake8
run: |
pip install flake8
black .
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Result[status: success | value: Rosalia]
Rosalia
```

In addition, you can call another function depending on the result with the optional parameters **success_handler** and **failure_handler**.
In addition, you can call another function after evaluate the result. Use optional parameters **success_handler** and **failure_handler** (Callable functions).

```python
def success_handler():
Expand All @@ -214,6 +214,25 @@ result = string_from_key(dictionary=user_info, key="first_name")
result.handle(success_handler=success_handler, failure_handler=failure_handler)
```

If you need to add some arguments as a parameters, use **success_args** and **failure_args**:

```python
def success_handler(param_1):
print(f"param_1: {param_1}")

def failure_handler(param_1, param_2):
print(f"param_1: {param_1}")
print(f"param_2: {param_2}")


result = string_from_key(dictionary=user_info, key="first_name")

result.handle(success_handler=success_handler,
failure_handler=failure_handler,
success_args=1,
failure_args=(1, 2))
```


On the other hand, if something wrong happens handle function will raise an Exception (ReturnErrorOnFailure).
Meiga has available a decorator to allow to handle the exception in case of error and unwrap the value in case of success.
Expand Down
14 changes: 11 additions & 3 deletions meiga/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,23 @@ def is_failure(self):
return not self._is_success

def handle(
self, success_handler: Callable = None, failure_handler: Callable = None
self,
success_handler: Callable = None,
failure_handler: Callable = None,
success_args=None,
failure_args=None,
) -> Any:
if not self._is_success:
if failure_handler:
failure_handler()
failure_handler(
*failure_args
) if failure_handler.__code__.co_argcount > 0 else failure_handler()
raise ReturnErrorOnFailure(self)
else:
if success_handler:
success_handler()
success_handler(
*success_args
) if success_handler.__code__.co_argcount > 0 else success_handler()
return self._value_success

value = property(get_value)
Expand Down
49 changes: 0 additions & 49 deletions tests/unit/test_result.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from meiga import Result, Error
from meiga.return_error_on_failure import ReturnErrorOnFailure


@pytest.mark.unit
Expand Down Expand Up @@ -109,51 +108,3 @@ def test_should_eq_two_different_failure_result():
result_2 = Result(failure=Exception())

assert result_1 != result_2


@pytest.mark.unit
def test_should_execute_success_handler():
global called_success_handler
called_success_handler = False

global called_failure_handler
called_failure_handler = False

def success_handler():
global called_success_handler
called_success_handler = True

def failure_handler():
global called_failure_handler
called_failure_handler = True

result = Result(success="Hi!")
result.handle(success_handler=success_handler, failure_handler=failure_handler)

assert called_success_handler is True
assert called_failure_handler is False


@pytest.mark.unit
def test_should_execute_failure_handler():
global called_success_handler
called_success_handler = False

global called_failure_handler
called_failure_handler = False

def success_handler():
global called_success_handler
called_success_handler = True

def failure_handler():
global called_failure_handler
called_failure_handler = True

result = Result(failure=Error())

with pytest.raises(ReturnErrorOnFailure):
result.handle(success_handler=success_handler, failure_handler=failure_handler)

assert called_success_handler is False
assert called_failure_handler is True
108 changes: 108 additions & 0 deletions tests/unit/test_result_handle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import pytest

from meiga import Result, Error, isFailure, isSuccess
from meiga.decorators import meiga
from meiga.return_error_on_failure import ReturnErrorOnFailure


@pytest.mark.unit
def test_should_execute_success_handler():
global called_success_handler
called_success_handler = False

global called_failure_handler
called_failure_handler = False

def success_handler():
global called_success_handler
called_success_handler = True

def failure_handler():
global called_failure_handler
called_failure_handler = True

result = Result(success="Hi!")
result.handle(success_handler=success_handler, failure_handler=failure_handler)

assert called_success_handler is True
assert called_failure_handler is False


@pytest.mark.unit
def test_should_execute_failure_handler():
global called_success_handler
called_success_handler = False

global called_failure_handler
called_failure_handler = False

def success_handler():
global called_success_handler
called_success_handler = True

def failure_handler():
global called_failure_handler
called_failure_handler = True

result = Result(failure=Error())

with pytest.raises(ReturnErrorOnFailure):
result.handle(success_handler=success_handler, failure_handler=failure_handler)

assert called_success_handler is False
assert called_failure_handler is True


@pytest.mark.unit
def test_should_execute_success_handler_with_valid_parameters():
given_first_parameter = 1
given_second_parameter = 2

global called_success_handler
called_success_handler = False

global called_failure_handler
called_failure_handler = False

def success_handler(param_1, param_2):
global called_success_handler
called_success_handler = True
assert given_first_parameter == param_1
assert given_second_parameter == param_2

def failure_handler():
global called_failure_handler
called_failure_handler = True

result = Result(success="Hi!")
result.handle(
success_handler=success_handler,
failure_handler=failure_handler,
success_args=(given_first_parameter, given_second_parameter),
)

assert called_success_handler is True
assert called_failure_handler is False


@pytest.mark.unit
@pytest.mark.parametrize("result", [isSuccess, isFailure])
def test_should_execute_handler_with_additional_and_non_required_parameters(result):
given_first_parameter = 1

def success_handler():
pass

def failure_handler():
pass

@meiga
def run():
result.handle(
success_handler=success_handler,
failure_handler=failure_handler,
success_args=given_first_parameter,
failure_args=given_first_parameter,
)

run()

0 comments on commit f6c226c

Please sign in to comment.