2016-11-27 05:55:21 +00:00
|
|
|
import datetime
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
from constance.management.commands.constance import _set_constance_value
|
2025-01-14 16:40:57 +00:00
|
|
|
from constance.utils import get_values
|
|
|
|
|
from constance.utils import get_values_for_keys
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
class UtilsTestCase(TestCase):
|
2016-11-27 05:55:21 +00:00
|
|
|
def test_set_value_validation(self):
|
2025-10-07 09:25:07 +00:00
|
|
|
self.assertRaisesMessage(ValidationError, "Enter a whole number.", _set_constance_value, "INT_VALUE", "foo")
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertRaisesMessage(
|
2025-10-07 09:15:14 +00:00
|
|
|
ValidationError,
|
2025-10-07 09:25:07 +00:00
|
|
|
"Enter a valid email address.",
|
2025-10-07 09:15:14 +00:00
|
|
|
_set_constance_value,
|
2025-10-07 09:25:07 +00:00
|
|
|
"EMAIL_VALUE",
|
|
|
|
|
"not a valid email",
|
2024-07-03 14:21:33 +00:00
|
|
|
)
|
|
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
ValidationError,
|
2025-10-07 09:25:07 +00:00
|
|
|
"Enter a valid date.",
|
2024-07-03 14:21:33 +00:00
|
|
|
_set_constance_value,
|
2025-10-07 09:25:07 +00:00
|
|
|
"DATETIME_VALUE",
|
2024-07-03 14:21:33 +00:00
|
|
|
(
|
2025-10-07 09:25:07 +00:00
|
|
|
"2000-00-00",
|
|
|
|
|
"99:99:99",
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
ValidationError,
|
2025-10-07 09:25:07 +00:00
|
|
|
"Enter a valid time.",
|
2024-07-03 14:21:33 +00:00
|
|
|
_set_constance_value,
|
2025-10-07 09:25:07 +00:00
|
|
|
"DATETIME_VALUE",
|
2024-07-03 14:21:33 +00:00
|
|
|
(
|
2025-10-07 09:25:07 +00:00
|
|
|
"2016-01-01",
|
|
|
|
|
"99:99:99",
|
2024-07-03 14:21:33 +00:00
|
|
|
),
|
|
|
|
|
)
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
def test_get_values(self):
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertEqual(
|
|
|
|
|
get_values(),
|
|
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"FLOAT_VALUE": 3.1415926536,
|
|
|
|
|
"BOOL_VALUE": True,
|
|
|
|
|
"EMAIL_VALUE": "test@example.com",
|
|
|
|
|
"INT_VALUE": 1,
|
|
|
|
|
"CHOICE_VALUE": "yes",
|
|
|
|
|
"TIME_VALUE": datetime.time(23, 59, 59),
|
|
|
|
|
"DATE_VALUE": datetime.date(2010, 12, 24),
|
|
|
|
|
"TIMEDELTA_VALUE": datetime.timedelta(days=1, hours=2, minutes=3),
|
|
|
|
|
"LINEBREAK_VALUE": "Spam spam",
|
|
|
|
|
"DECIMAL_VALUE": Decimal("0.1"),
|
|
|
|
|
"STRING_VALUE": "Hello world",
|
|
|
|
|
"DATETIME_VALUE": datetime.datetime(2010, 8, 23, 11, 29, 24),
|
|
|
|
|
"LIST_VALUE": [1, "1", datetime.date(2019, 1, 1)],
|
|
|
|
|
"JSON_VALUE": {
|
|
|
|
|
"key": "value",
|
|
|
|
|
"key2": 2,
|
|
|
|
|
"key3": [1, 2, 3],
|
|
|
|
|
"key4": {"key": "value"},
|
|
|
|
|
"key5": datetime.date(2019, 1, 1),
|
|
|
|
|
"key6": None,
|
2024-09-04 09:41:53 +00:00
|
|
|
},
|
2024-07-03 14:21:33 +00:00
|
|
|
},
|
|
|
|
|
)
|
2025-01-14 16:31:32 +00:00
|
|
|
|
|
|
|
|
def test_get_values_for_keys(self):
|
|
|
|
|
self.assertEqual(
|
2025-10-07 09:25:07 +00:00
|
|
|
get_values_for_keys(["BOOL_VALUE", "CHOICE_VALUE", "LINEBREAK_VALUE"]),
|
2025-01-14 16:31:32 +00:00
|
|
|
{
|
2025-10-07 09:25:07 +00:00
|
|
|
"BOOL_VALUE": True,
|
|
|
|
|
"CHOICE_VALUE": "yes",
|
|
|
|
|
"LINEBREAK_VALUE": "Spam spam",
|
2025-01-14 16:31:32 +00:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_get_values_for_keys_empty_keys(self):
|
|
|
|
|
result = get_values_for_keys([])
|
|
|
|
|
self.assertEqual(result, {})
|
|
|
|
|
|
|
|
|
|
def test_get_values_for_keys_throw_error_if_no_key(self):
|
|
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
AttributeError,
|
|
|
|
|
'"OLD_VALUE, BOLD_VALUE" keys not found in configuration.',
|
|
|
|
|
get_values_for_keys,
|
2025-10-07 09:25:07 +00:00
|
|
|
["BOOL_VALUE", "OLD_VALUE", "BOLD_VALUE"],
|
2025-01-14 16:31:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_get_values_for_keys_invalid_input_type(self):
|
|
|
|
|
with self.assertRaises(TypeError):
|
2025-10-07 09:25:07 +00:00
|
|
|
get_values_for_keys("key1")
|