Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
feat: add def_result decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
HamidMolareza committed Mar 31, 2023
1 parent 1ba3ddd commit 98f9101
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ disable=
W0718, # Catching too general exception Exception (broad-exception-caught)
R0903, # Too few public methods (too-few-public-methods)
R0913, # Too many arguments (too-many-arguments)
W0703, # Catching too general exception Exception (broad-except)
24 changes: 24 additions & 0 deletions def_result/decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from def_result.Result import Result
from def_result.ResultDetails.Errors.ExceptionError import ExceptionError


def def_result(func):
"""
A decorator function that wraps the return value of a function with a `Result`
object containing additional information such as success status and error messages.
Args:
func: The function to be wrapped.
Returns:
A new function that wraps the return value of `func` in a `Result` object.
"""
def wrapper(*args, **kwargs):
try:
result_value = func(*args, **kwargs)
result = Result.ok(value=result_value)
except Exception as e:
result = Result.fail(detail=ExceptionError(message=str(e), exception=e))
return result

return wrapper
36 changes: 36 additions & 0 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest

from def_result.decorator import def_result
from def_result.Result import Result
from tests.helpers import assert_result, assert_result_detail


def test_divide_numbers(a: int, b: int):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b


class TestDefResultDecorator(unittest.TestCase):
def test_def_result_decorator_ok(self):
# Test that the decorator returns a Result object with ok status when the function runs successfully
decorated_function = def_result(test_divide_numbers)
result = decorated_function(10, 2)

assert_result(test_class=self, result=result, success=True, value=5)

def test_def_result_decorator_error(self):
# Test that the decorator returns a Result object with error status and ExceptionError detail when
# the function raises an exception
self.assertRaises(ValueError, test_divide_numbers, 10, 0)

decorated_function = def_result(test_divide_numbers)
result = decorated_function(10, 0)

assert_result(test_class=self, result=result, success=False, detail=result.detail)
assert_result_detail(test_class=self, result_detail=result.detail, title="An exception occurred",
message="Cannot divide by zero", code=500)


if __name__ == '__main__':
unittest.main()

0 comments on commit 98f9101

Please sign in to comment.