2023-03-10 17:38:31 +00:00
|
|
|
from unittest import mock
|
2019-04-06 14:13:56 +00:00
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
2024-07-03 14:21:33 +00:00
|
|
|
from constance import settings
|
|
|
|
|
from constance.checks import check_fieldsets
|
|
|
|
|
from constance.checks import get_inconsistent_fieldnames
|
|
|
|
|
|
2019-04-06 14:13:56 +00:00
|
|
|
|
|
|
|
|
class ChecksTestCase(TestCase):
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.CONFIG_FIELDSETS", {"Set1": settings.CONFIG.keys()})
|
2019-04-06 14:13:56 +00:00
|
|
|
def test_get_inconsistent_fieldnames_none(self):
|
|
|
|
|
"""
|
2023-03-13 22:45:31 +00:00
|
|
|
Test that get_inconsistent_fieldnames returns an empty data and no checks fail
|
2019-04-06 14:13:56 +00:00
|
|
|
if CONFIG_FIELDSETS accounts for every key in settings.CONFIG.
|
|
|
|
|
"""
|
2023-03-13 22:45:31 +00:00
|
|
|
missing_keys, extra_keys = get_inconsistent_fieldnames()
|
|
|
|
|
self.assertFalse(missing_keys)
|
|
|
|
|
self.assertFalse(extra_keys)
|
2019-04-06 14:13:56 +00:00
|
|
|
|
|
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
|
|
|
|
{"Set1": list(settings.CONFIG.keys())[:-1]},
|
2019-04-06 14:13:56 +00:00
|
|
|
)
|
2023-03-13 22:45:31 +00:00
|
|
|
def test_get_inconsistent_fieldnames_for_missing_keys(self):
|
2019-04-06 14:13:56 +00:00
|
|
|
"""
|
2023-03-13 22:45:31 +00:00
|
|
|
Test that get_inconsistent_fieldnames returns data and the check fails
|
2019-04-06 14:13:56 +00:00
|
|
|
if CONFIG_FIELDSETS does not account for every key in settings.CONFIG.
|
|
|
|
|
"""
|
2023-03-13 22:45:31 +00:00
|
|
|
missing_keys, extra_keys = get_inconsistent_fieldnames()
|
|
|
|
|
self.assertTrue(missing_keys)
|
|
|
|
|
self.assertFalse(extra_keys)
|
|
|
|
|
self.assertEqual(1, len(check_fieldsets()))
|
|
|
|
|
|
|
|
|
|
@mock.patch(
|
2025-10-07 09:25:07 +00:00
|
|
|
"constance.settings.CONFIG_FIELDSETS",
|
|
|
|
|
{"Set1": [*settings.CONFIG.keys(), "FORGOTTEN_KEY"]},
|
2023-03-13 22:45:31 +00:00
|
|
|
)
|
|
|
|
|
def test_get_inconsistent_fieldnames_for_extra_keys(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that get_inconsistent_fieldnames returns data and the check fails
|
|
|
|
|
if CONFIG_FIELDSETS contains extra key that is absent in settings.CONFIG.
|
|
|
|
|
"""
|
|
|
|
|
missing_keys, extra_keys = get_inconsistent_fieldnames()
|
|
|
|
|
self.assertFalse(missing_keys)
|
|
|
|
|
self.assertTrue(extra_keys)
|
2019-04-06 14:13:56 +00:00
|
|
|
self.assertEqual(1, len(check_fieldsets()))
|
|
|
|
|
|
2025-10-07 09:25:07 +00:00
|
|
|
@mock.patch("constance.settings.CONFIG_FIELDSETS", {})
|
2019-04-06 14:13:56 +00:00
|
|
|
def test_check_fieldsets(self):
|
2024-07-05 14:38:26 +00:00
|
|
|
"""check_fieldsets should not output warning if CONFIG_FIELDSETS is not defined."""
|
2019-04-06 14:13:56 +00:00
|
|
|
del settings.CONFIG_FIELDSETS
|
|
|
|
|
self.assertEqual(0, len(check_fieldsets()))
|