Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions constance/management/commands/constance.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def handle(self, command, key=None, value=None, *args, **options):
for k, v in get_values().items():
self.stdout.write(f"{k}\t{v}", ending="\n")
elif command == self.REMOVE_STALE_KEYS:
prefix = getattr(settings, 'CONSTANCE_DATABASE_PREFIX', '')
actual_keys = [f'{prefix}{key}' for key in settings.CONSTANCE_CONFIG]
prefix = getattr(settings, "CONSTANCE_DATABASE_PREFIX", "")
actual_keys = [f"{prefix}{key}" for key in settings.CONSTANCE_CONFIG]
stale_records = Constance.objects.exclude(key__in=actual_keys)
if stale_records:
self.stdout.write("The following record will be deleted:", ending="\n")
Expand Down
15 changes: 8 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from django.conf import settings
from django.core.management import CommandError
from django.core.management import call_command
from django.test import TransactionTestCase, override_settings
from django.test import TransactionTestCase
from django.test import override_settings
from django.utils import timezone
from django.utils.encoding import smart_str

from constance.models import Constance
from constance import config
from constance.models import Constance


class CliTestCase(TransactionTestCase):
Expand Down Expand Up @@ -51,12 +52,12 @@ def test_list(self):
)

def test_get(self):
call_command("constance", *("get EMAIL_VALUE".split()), stdout=self.out)
call_command("constance", *(["get", "EMAIL_VALUE"]), stdout=self.out)

self.assertEqual(self.out.getvalue().strip(), "test@example.com")

def test_set(self):
call_command("constance", *("set EMAIL_VALUE blah@example.com".split()), stdout=self.out)
call_command("constance", *(["set", "EMAIL_VALUE", "blah@example.com"]), stdout=self.out)

self.assertEqual(config.EMAIL_VALUE, "blah@example.com")

Expand Down Expand Up @@ -120,13 +121,13 @@ def test_delete_stale_records(self):
self.assertEqual(Constance.objects.count(), initial_count, msg=self.out)

@override_settings(
CONSTANCE_DATABASE_PREFIX='constance:',
CONSTANCE_DATABASE_PREFIX="constance:",
)
def test_delete_stale_records_respects_prefix(self):
self._populate_database_with_default_values()
initial_count = Constance.objects.count()

call_command('constance', 'remove_stale_keys', stdout=self.out)
call_command("constance", "remove_stale_keys", stdout=self.out)

self.assertEqual(Constance.objects.count(), initial_count, msg=self.out)

Expand All @@ -136,4 +137,4 @@ def _populate_database_with_default_values(self):
in settings since that's not done automatically at startup
"""
for key, (value, *_) in settings.CONSTANCE_CONFIG.items():
Constance.objects.create(key=f'{getattr(settings, "CONSTANCE_DATABASE_PREFIX", "")}{key}', value=value)
Constance.objects.create(key=f"{getattr(settings, 'CONSTANCE_DATABASE_PREFIX', '')}{key}", value=value)
Loading