-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for concise exception class
- Loading branch information
Showing
2 changed files
with
89 additions
and
3 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,74 @@ | ||
import pytest | ||
import traceback | ||
import sys | ||
from podaac.merger.harmony.service import ConciseException | ||
|
||
|
||
def test_exception_message_formatting(): | ||
""" | ||
Test that the ConciseException correctly formats the error message | ||
with file, line, function, and original error details. | ||
""" | ||
try: | ||
# Simulate an error by intentionally causing a division by zero | ||
1 / 0 | ||
except ZeroDivisionError as original_error: | ||
concise_exception = ConciseException(original_error) | ||
|
||
# Detailed assertions with informative error messages | ||
error_str = str(concise_exception) | ||
error_msg = concise_exception.message | ||
|
||
assert "Error in file" in error_msg, f"Expected file context, got: {error_msg}" | ||
assert "line" in error_msg, f"Expected line number, got: {error_msg}" | ||
assert "in function" in error_msg, f"Expected function context, got: {error_msg}" | ||
assert "division by zero" in error_msg, f"Expected original error message, got: {error_msg}" | ||
assert concise_exception.category == 'podaac/concise' | ||
|
||
def test_exception_traceback_details(): | ||
""" | ||
Verify that the exception captures the correct traceback information. | ||
""" | ||
def inner_function(): | ||
# Another function to add depth to the traceback | ||
1 / 0 | ||
|
||
try: | ||
inner_function() | ||
except ZeroDivisionError as original_error: | ||
concise_exception = ConciseException(original_error) | ||
|
||
# Extract expected details | ||
tb = traceback.extract_tb(original_error.__traceback__)[-1] | ||
expected_filename = tb.filename | ||
expected_lineno = tb.lineno | ||
expected_funcname = tb.name | ||
|
||
error_msg = concise_exception.message | ||
assert expected_filename in error_msg, f"Filename not found, got: {error_msg}" | ||
assert str(expected_lineno) in error_msg, f"Line number not found, got: {error_msg}" | ||
assert expected_funcname in error_msg, f"Function name not found, got: {error_msg}" | ||
|
||
def test_original_error_type_preservation(): | ||
""" | ||
Ensure that the original error type is preserved in the traceback. | ||
""" | ||
try: | ||
raise ValueError("Test error message") | ||
except ValueError as original_error: | ||
concise_exception = ConciseException(original_error) | ||
|
||
error_msg = concise_exception.message | ||
assert "Test error message" in error_msg, f"Original error message not found, got: {error_msg}" | ||
assert isinstance(concise_exception.original_exception, ValueError) | ||
|
||
def test_module_identifier(): | ||
""" | ||
Verify that the module identifier is set correctly. | ||
""" | ||
try: | ||
raise RuntimeError("Sample error") | ||
except RuntimeError as original_error: | ||
concise_exception = ConciseException(original_error) | ||
|
||
assert concise_exception.category == 'podaac/concise' |