From f9133f3a4f4549392b4e71f0c6db427a1b9a1c2d Mon Sep 17 00:00:00 2001 From: acostapazo Date: Thu, 9 Jan 2020 18:12:11 +0100 Subject: [PATCH] Add unwrap feature to Result object. Add cache for pip requirements in github action. Add pre-commit config file --- .github/workflows/ci.yml | 6 ++++++ .pre-commit-config.yaml | 10 ---------- README.md | 14 ++++++++++++++ meiga/result.py | 6 ++++++ tests/unit/test_result_unwrap.py | 21 +++++++++++++++++++++ 5 files changed, 47 insertions(+), 10 deletions(-) delete mode 100644 .pre-commit-config.yaml create mode 100644 tests/unit/test_result_unwrap.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7437dd6..d9cfd98 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,6 +24,12 @@ jobs: uses: actions/setup-python@v1 with: python-version: ${{ matrix.python-version }} + - uses: actions/cache@v1 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/ci.txt') }} + restore-keys: | + ${{ runner.os }}-pip- - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 100644 index 4bdcc6f..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1,10 +0,0 @@ -repos: -- repo: https://github.com/ambv/black - rev: stable - hooks: - - id: black - language_version: python3.6 -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v1.2.3 - hooks: - - id: flake8 diff --git a/README.md b/README.md index 3ec3dbb..046ce0e 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,20 @@ class AuthService: return NotImplementedMethodError ``` +### Unwrap Result + +If you *wrap* a Result object, its will return a valid value if it is success. Otherwise, it will return None. + +```python +result = Result(success="Hi!") +value = result.unwrap() +assert value == "Hi!" + +result = Failure(Error()) +value = result.unwrap() + +assert value is None +``` ### Handle Result diff --git a/meiga/result.py b/meiga/result.py index 2e9e4e6..0341c0b 100644 --- a/meiga/result.py +++ b/meiga/result.py @@ -61,6 +61,12 @@ def is_success(self): def is_failure(self): return not self._is_success + def unwrap(self): + if not self._is_success: + return None + else: + return self._value_success + def handle( self, success_handler: Callable = None, diff --git a/tests/unit/test_result_unwrap.py b/tests/unit/test_result_unwrap.py new file mode 100644 index 0000000..ef2188a --- /dev/null +++ b/tests/unit/test_result_unwrap.py @@ -0,0 +1,21 @@ +import pytest + +from meiga import Result, Error, Failure + + +@pytest.mark.unit +def test_should_return_the_value_when_unwrap_a_success_result(): + + result = Result(success="Hi!") + value = result.unwrap() + + assert value == "Hi!" + + +@pytest.mark.unit +def test_should_return_none_when_unwrap_a_failure_result(): + + result = Failure(Error()) + value = result.unwrap() + + assert value is None