diff --git a/dav_base/tests/test_config.py b/dav_base/tests/test_config.py index d2ed44c..a90402e 100644 --- a/dav_base/tests/test_config.py +++ b/dav_base/tests/test_config.py @@ -68,6 +68,11 @@ class AppSettingsTestCase(SimpleTestCase): self.app_name = 'dav_base' self.test_setting_value = TEST_SETTING_VALUE + def test_no_settings_file(self): + """Test that nothing bad happens if no settings file can be read""" + app_settings = AppSettings('', ()) + self.assertIsInstance(app_settings, AppSettings) + def test_defaults(self): """Test the default value for optional and unset settings""" default_settings = ( @@ -78,15 +83,39 @@ class AppSettingsTestCase(SimpleTestCase): self.assertEqual(app_settings.no_test_setting, 1) self.assertEqual(app_settings.no_test_setting_2, 2) - def test_improperlyconfigured(self): + def test_improperlyconfigured_by_class(self): """Test if mandatory but unset setting raise correct exception""" default_settings = ( DefaultSetting('no_test_setting', ImproperlyConfigured), ) - try: + with self.assertRaisesRegex(ImproperlyConfigured, 'NO_TEST_SETTING must be defined in main.settings-dav_base'): + app_settings = AppSettings(self.app_name, default_settings) + + def test_improperlyconfigured_by_instance(self): + """Test if mandatory but unset setting raise correct exception""" + default_settings = ( + DefaultSetting('no_test_setting', ImproperlyConfigured('Some Error Message')), + ) + with self.assertRaisesRegex(ImproperlyConfigured, 'Some Error Message'): + app_settings = AppSettings(self.app_name, default_settings) + + def test_improperlyconfigured_by_func(self): + """Test if invalid setting raise correct exception""" + def validator(value): + return False + default_settings = ( + DefaultSetting('test_setting', 1, validator=validator), + ) + with self.assertRaises(ImproperlyConfigured): + app_settings = AppSettings(self.app_name, default_settings) + + def test_improperlyconfigured_by_regex(self): + """Test if invalid setting raise correct exception""" + default_settings = ( + DefaultSetting('test_setting', 1, validator=r'^[01]$'), + ) + with self.assertRaises(ImproperlyConfigured): app_settings = AppSettings(self.app_name, default_settings) - except ImproperlyConfigured as e: - self.assertEqual(str(e), 'NO_TEST_SETTING must be defined in main.settings-dav_base') def test_settings_from_file(self): """Test if value from settings file eliminate exception"""