mirror of
https://github.com/jazzband/django-configurations.git
synced 2026-03-16 22:20:27 +00:00
add tests for error handling
This commit is contained in:
parent
3b96d16510
commit
b21f207b4f
2 changed files with 44 additions and 1 deletions
|
|
@ -4,7 +4,7 @@ import sys
|
|||
import os
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .values import Value
|
||||
from .values import Value # pragma: no cover
|
||||
|
||||
|
||||
class TermStyles:
|
||||
|
|
|
|||
43
tests/test_error_handling.py
Normal file
43
tests/test_error_handling.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import io
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from configurations.errors import ValueRetrievalError, SetupError, with_error_handler
|
||||
from configurations.values import Value
|
||||
|
||||
|
||||
class ErrorHandlingTestCase(TestCase):
|
||||
def test_help_text_in_explanation_lines(self):
|
||||
value_instance = Value(help_text="THIS IS A TEST")
|
||||
exception = ValueRetrievalError(value_instance)
|
||||
self.assertIn("Help: THIS IS A TEST", exception.explanation_lines)
|
||||
|
||||
def test_help_reference_in_explanation_lines(self):
|
||||
value_instance = Value(help_reference="https://example.com")
|
||||
exception = ValueRetrievalError(value_instance)
|
||||
self.assertIn("Reference: https://example.com", exception.explanation_lines)
|
||||
|
||||
def test_example_in_explanation_lines(self):
|
||||
value_instance = Value(example_generator=lambda: "test")
|
||||
exception = ValueRetrievalError(value_instance)
|
||||
self.assertIn("Example value: 'test'", exception.explanation_lines)
|
||||
|
||||
def test_error_handler_rendering(self):
|
||||
# setup
|
||||
with patch("configurations.errors.sys.stderr", new=io.StringIO()) as mock:
|
||||
def inner():
|
||||
try:
|
||||
value_instance = Value(environ_required=True)
|
||||
value_instance.setup("TEST")
|
||||
except ValueRetrievalError as err:
|
||||
raise SetupError("This is a test exception", [err])
|
||||
|
||||
# execution
|
||||
with_error_handler(inner)()
|
||||
|
||||
# verification
|
||||
self.assertEqual(mock.getvalue().strip(),
|
||||
"This is a test exception\n"
|
||||
" * Value of TEST could not be retrieved from environment\n"
|
||||
" - TEST is taken from the environment variable DJANGO_TEST as a Value")
|
||||
Loading…
Reference in a new issue