2024-07-05 14:38:26 +00:00
|
|
|
import contextlib
|
2017-01-11 08:52:19 +00:00
|
|
|
from datetime import datetime
|
2024-07-03 14:21:33 +00:00
|
|
|
from io import StringIO
|
2016-11-27 05:55:21 +00:00
|
|
|
from textwrap import dedent
|
|
|
|
|
|
2019-08-12 16:00:07 +00:00
|
|
|
from django.conf import settings
|
2024-07-03 14:21:33 +00:00
|
|
|
from django.core.management import CommandError
|
|
|
|
|
from django.core.management import call_command
|
2016-11-27 05:55:21 +00:00
|
|
|
from django.test import TransactionTestCase
|
2019-08-12 16:00:07 +00:00
|
|
|
from django.utils import timezone
|
2016-11-27 05:55:21 +00:00
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
|
|
|
|
|
|
from constance import config
|
2023-03-10 17:38:31 +00:00
|
|
|
from constance.models import Constance
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CliTestCase(TransactionTestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.out = StringIO()
|
|
|
|
|
|
|
|
|
|
def test_help(self):
|
2024-07-05 14:38:26 +00:00
|
|
|
with contextlib.suppress(SystemExit):
|
2016-11-27 05:55:21 +00:00
|
|
|
call_command('constance', '--help')
|
|
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
|
call_command('constance', 'list', stdout=self.out)
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertEqual(
|
|
|
|
|
set(self.out.getvalue().splitlines()),
|
|
|
|
|
set(
|
|
|
|
|
dedent(
|
|
|
|
|
smart_str(
|
2024-09-04 09:41:53 +00:00
|
|
|
""" BOOL_VALUE\tTrue
|
|
|
|
|
EMAIL_VALUE\ttest@example.com
|
|
|
|
|
INT_VALUE\t1
|
|
|
|
|
LINEBREAK_VALUE\tSpam spam
|
|
|
|
|
DATE_VALUE\t2010-12-24
|
|
|
|
|
TIME_VALUE\t23:59:59
|
|
|
|
|
TIMEDELTA_VALUE\t1 day, 2:03:00
|
|
|
|
|
STRING_VALUE\tHello world
|
|
|
|
|
CHOICE_VALUE\tyes
|
|
|
|
|
DECIMAL_VALUE\t0.1
|
|
|
|
|
DATETIME_VALUE\t2010-08-23 11:29:24
|
|
|
|
|
FLOAT_VALUE\t3.1415926536
|
|
|
|
|
JSON_VALUE\t{'key': 'value', 'key2': 2, 'key3': [1, 2, 3], 'key4': {'key': 'value'}, 'key5': datetime.date(2019, 1, 1), 'key6': None}
|
|
|
|
|
LIST_VALUE\t[1, '1', datetime.date(2019, 1, 1)]
|
|
|
|
|
""" # noqa: E501
|
2024-07-03 14:21:33 +00:00
|
|
|
)
|
|
|
|
|
).splitlines()
|
|
|
|
|
),
|
|
|
|
|
)
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
|
call_command('constance', *('get EMAIL_VALUE'.split()), stdout=self.out)
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertEqual(self.out.getvalue().strip(), 'test@example.com')
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
def test_set(self):
|
|
|
|
|
call_command('constance', *('set EMAIL_VALUE blah@example.com'.split()), stdout=self.out)
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertEqual(config.EMAIL_VALUE, 'blah@example.com')
|
2016-11-27 05:55:21 +00:00
|
|
|
|
2017-01-11 08:52:19 +00:00
|
|
|
call_command('constance', *('set', 'DATETIME_VALUE', '2011-09-24', '12:30:25'), stdout=self.out)
|
|
|
|
|
|
2019-08-12 16:00:07 +00:00
|
|
|
expected = datetime(2011, 9, 24, 12, 30, 25)
|
|
|
|
|
if settings.USE_TZ:
|
|
|
|
|
expected = timezone.make_aware(expected)
|
|
|
|
|
self.assertEqual(config.DATETIME_VALUE, expected)
|
2017-01-11 08:52:19 +00:00
|
|
|
|
2016-11-27 05:55:21 +00:00
|
|
|
def test_get_invalid_name(self):
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
CommandError,
|
|
|
|
|
'NOT_A_REAL_CONFIG is not defined in settings.CONSTANCE_CONFIG',
|
|
|
|
|
call_command,
|
|
|
|
|
'constance',
|
|
|
|
|
'get',
|
|
|
|
|
'NOT_A_REAL_CONFIG',
|
|
|
|
|
)
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
def test_set_invalid_name(self):
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
CommandError,
|
|
|
|
|
'NOT_A_REAL_CONFIG is not defined in settings.CONSTANCE_CONFIG',
|
|
|
|
|
call_command,
|
|
|
|
|
'constance',
|
|
|
|
|
'set',
|
|
|
|
|
'NOT_A_REAL_CONFIG',
|
|
|
|
|
'foo',
|
|
|
|
|
)
|
2016-11-27 05:55:21 +00:00
|
|
|
|
|
|
|
|
def test_set_invalid_value(self):
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
CommandError,
|
|
|
|
|
'Enter a valid email address.',
|
|
|
|
|
call_command,
|
|
|
|
|
'constance',
|
|
|
|
|
'set',
|
|
|
|
|
'EMAIL_VALUE',
|
|
|
|
|
'not a valid email',
|
|
|
|
|
)
|
2017-01-11 08:52:19 +00:00
|
|
|
|
|
|
|
|
def test_set_invalid_multi_value(self):
|
2024-07-03 14:21:33 +00:00
|
|
|
self.assertRaisesMessage(
|
|
|
|
|
CommandError,
|
|
|
|
|
'Enter a list of values.',
|
|
|
|
|
call_command,
|
|
|
|
|
'constance',
|
|
|
|
|
'set',
|
|
|
|
|
'DATETIME_VALUE',
|
|
|
|
|
'2011-09-24 12:30:25',
|
|
|
|
|
)
|
2020-01-27 19:24:32 +00:00
|
|
|
|
|
|
|
|
def test_delete_stale_records(self):
|
|
|
|
|
initial_count = Constance.objects.count()
|
|
|
|
|
|
|
|
|
|
Constance.objects.create(key='STALE_KEY', value=None)
|
|
|
|
|
call_command('constance', 'remove_stale_keys', stdout=self.out)
|
|
|
|
|
|
2023-03-10 17:38:31 +00:00
|
|
|
self.assertEqual(Constance.objects.count(), initial_count, msg=self.out)
|