|
6 | 6 | from django.conf import settings |
7 | 7 | from django.core.management import CommandError |
8 | 8 | from django.core.management import call_command |
9 | | -from django.test import TransactionTestCase |
| 9 | +from django.test import TransactionTestCase, override_settings |
10 | 10 | from django.utils import timezone |
11 | 11 | from django.utils.encoding import smart_str |
12 | 12 |
|
13 | | -from constance import config |
14 | 13 | from constance.models import Constance |
15 | 14 |
|
16 | 15 |
|
@@ -111,9 +110,29 @@ def test_set_invalid_multi_value(self): |
111 | 110 | ) |
112 | 111 |
|
113 | 112 | def test_delete_stale_records(self): |
| 113 | + self._populate_database_with_default_values() |
114 | 114 | initial_count = Constance.objects.count() |
115 | 115 |
|
116 | 116 | Constance.objects.create(key="STALE_KEY", value=None) |
117 | 117 | call_command("constance", "remove_stale_keys", stdout=self.out) |
118 | 118 |
|
119 | 119 | self.assertEqual(Constance.objects.count(), initial_count, msg=self.out) |
| 120 | + |
| 121 | + @override_settings( |
| 122 | + CONSTANCE_DATABASE_PREFIX='constance:', |
| 123 | + ) |
| 124 | + def test_delete_stale_records_respects_prefix(self): |
| 125 | + self._populate_database_with_default_values() |
| 126 | + initial_count = Constance.objects.count() |
| 127 | + |
| 128 | + call_command('constance', 'remove_stale_keys', stdout=self.out) |
| 129 | + |
| 130 | + self.assertEqual(Constance.objects.count(), initial_count, msg=self.out) |
| 131 | + |
| 132 | + def _populate_database_with_default_values(self): |
| 133 | + """ |
| 134 | + Helper function to populate the database with default values defined |
| 135 | + in settings since that's not done automatically at startup |
| 136 | + """ |
| 137 | + for key, (value, *_) in settings.CONSTANCE_CONFIG.items(): |
| 138 | + Constance.objects.create(key=f'{getattr(settings, "CONSTANCE_DATABASE_PREFIX", "")}{key}', value=value) |
0 commit comments