This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ba3ddd
commit 98f9101
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |