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

Commit

Permalink
fix: minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
HamidMolareza committed Mar 31, 2023
1 parent c3e1db9 commit fb9db93
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 4 additions & 8 deletions def_result/ResultDetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, title: str,
Raises:
ValueError: If title is empty or None.
"""
if not title:
if title is None or title == '':
raise ValueError("title is required")
self.title = title
self.message = message
Expand All @@ -57,11 +57,9 @@ def add_more_data(self, data: Any) -> None:
data (Any): Additional data to add to the result detail.
"""


if not data:
if data is None:
return
if not self.more_data:
self.more_data = []
self.more_data = []
self.more_data.append(data)

def __str__(self):
Expand All @@ -70,10 +68,8 @@ def __str__(self):
Returns:
str: A string representation of the result detail.
"""
result: str = ""
result: str = f"Title: {self.title}\n"

if self.title:
result += f"Title: {self.title}\n"
if self.message:
result += f"Message: {self.message}\n"
if self.code:
Expand Down
2 changes: 1 addition & 1 deletion def_result/ResultDetails/ErrorDetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def add_or_update_error(self, key: str, value: str):
def __str__(self):
error_details = super().__str__()

if self.errors:
if self.errors is not None:
error_details += "Errors: " + str(self.errors) + "\n"

if self.exception:
Expand Down
11 changes: 9 additions & 2 deletions tests/ResultDetails/test_ErrorDetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ def test_add_or_update_error(self):
def test_str(self):
error_detail = ErrorDetail(title="title", message="message", code=100, errors={"message": "error"},
exception=Exception("Fake exception"), more_data=["more_data"])

print(str(error_detail))
self.assertTrue(str(error_detail).startswith(
"Title: title\nMessage: message\nCode: 100\nErrors: {'message': 'error'}\nException: Fake exception"))

error_detail = ErrorDetail(title="title", message="message", code=100,
exception=Exception("Fake exception"), more_data=["more_data"])
self.assertTrue(str(error_detail).startswith(
"Title: title\nMessage: message\nCode: 100\nException: Fake exception"))

error_detail = ErrorDetail(title="title", message="message", code=100, more_data=["more_data"])
self.assertTrue(str(error_detail).startswith(
"Title: title\nMessage: message\nCode: 100"))


if __name__ == '__main__':
unittest.main()
10 changes: 10 additions & 0 deletions tests/test_ResultDetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def test_init_with_optional_args(self):
assert_result_detail(test_class=self, result_detail=result_detail, title='title', message='message', code=100,
more_data=["more data"])

def test_init_without_required_args(self):
self.assertRaises(ValueError, ResultDetail, title=None)
self.assertRaises(ValueError, ResultDetail, title='')

def test_is_instance_of(self):
test_detail = FakeDetail("title")

Expand All @@ -29,6 +33,12 @@ def test_add_more_data(self):
result_detail.add_more_data("more data")
self.assertEqual(["more data"], result_detail.more_data)

def test_add_more_data_with_none(self):
result_detail = ResultDetail(title='title')

result_detail.add_more_data(None)
self.assertEqual([], result_detail.more_data)

def test_str_without_args(self):
result_detail = ResultDetail(title='title')
expected = "Title: title\n"
Expand Down

0 comments on commit fb9db93

Please sign in to comment.