Skip to content

Instantly share code, notes, and snippets.

@gjbagrowski
Created August 20, 2015 11:21
Show Gist options
  • Save gjbagrowski/9de6f259c6897509c650 to your computer and use it in GitHub Desktop.
Save gjbagrowski/9de6f259c6897509c650 to your computer and use it in GitHub Desktop.
SettingsTestCaseMixin
class XMPPSettingsTestCase(SettingsTestCaseMixin, TestCase):
nested_setting = 'XMPP_SETTINGS'
SETTINGS = (
('host', basestring),
('conference_host', basestring),
('endpoint', basestring),
('archive_timezone', (basestring, datetime.tzinfo)),
)
class SettingsTestCaseMixin(object):
nested_setting = False
"""Support settings keys that are directories."""
SETTINGS = ()
"""An iterable of (<setting_name>, <setting_type_or_types_tuple>)."""
def _get_test_setting(self, setting):
if self.nested_setting:
return getattr(settings, self.nested_setting)[setting]
else:
return getattr(settings, setting)
def _has_test_setting(self, setting):
if self.nested_setting:
return setting in getattr(settings, self.nested_setting)
else:
return hasattr(settings, setting)
def test_setting_type(self):
for setting, setting_type in self.SETTINGS:
self.assertTrue(
isinstance(self._get_test_setting(setting), setting_type),
"Setting {} is not of type {}, but {}".format(
setting, setting_type, type(setting),
))
def test_settings_keys(self):
for setting, setting_type in self.SETTINGS:
self.assertTrue(
self._has_test_setting(setting),
"Setting {} not found in settings".format(setting))
def test_mixin_configuration(self):
if self.nested_setting:
self.assertTrue(isinstance(self.nested_setting, basestring))
self.assertTrue(hasattr(settings, self.nested_setting))
self.assertTrue(
isinstance(getattr(settings, self.nested_setting), dict))
for setting in self.SETTINGS:
self.assertEqual(len(setting), 2)
self.assertTrue(isinstance(setting[0], basestring))
self.assertTrue(isinstance(setting[1], (type, tuple)))
if isinstance(setting[1], tuple):
for stype in setting[1]:
self.assertTrue(isinstance(stype, type))
class AccountsSettingsTestCase(SettingsTestCaseMixin, TestCase):
SETTINGS = (
('USER_STATUS_THROTTLE', datetime.timedelta),
('AGENT_WORK_DAY_DEFALT_DURATION', datetime.timedelta),
('AGENT_WORK_DAY_DEFALT_BREAK_DURATION', datetime.timedelta),
('AGENT_WORK_DAY_DEFALT_HOURLY_BREAK_DURATION', datetime.timedelta),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment