def test_load_module(self) -> None: """ test the method of load_module :return: :rtype: None """ settings = Settings() with settings.unfreeze() as settings_: settings_.load_module(self.test_module) # pylint: disable=no-member self.assertIn("A", settings) self.assertEqual( settings._data["A"], # pylint: disable = protected-access Setting("project", "A", 1), ) settings = Settings() with settings.unfreeze() as settings_: settings_.load_module("tests.samples.settings") self.assertIn("A", settings) self.assertEqual( settings._data["A"], # pylint: disable = protected-access Setting("project", "A", 1), )
def setUp(self) -> None: """ :return: :rtype: None """ self.setting_a = Setting("default", "a", "a") self.setting_b = Setting("project", "b", "b") self.setting_c = Setting("env", "c", "c") self.setting_d = Setting("cmd", "d", "d")
def test_ne(self) -> None: """ :return: :rtype: None """ with self.assertRaises(CompareWithNotSettingException): self.assertTrue("a_false_setting" != self.setting_a) setting_not_same_priority = Setting("project", "a", "a") self.assertTrue(setting_not_same_priority != self.setting_a) setting_not_same_name = Setting("default", "not_same_name", "a") self.assertTrue(setting_not_same_name != self.setting_a) setting_not_same_value = Setting("default", "a", "not_same_value") self.assertTrue(setting_not_same_value != self.setting_a) setting_equal = Setting("default", "a", "a") self.assertFalse(setting_equal != self.setting_a)
def test_from_module(self) -> None: """ test the method of from_module :return: :rtype: None """ settings = Settings.from_module(self.test_module, "default") self.assertIn("A", settings) self.assertEqual( settings._data["A"], # pylint: disable = protected-access Setting("default", "A", 1), ) settings = Settings.from_module("tests.samples.settings", "default") self.assertIn("A", settings) self.assertEqual( settings._data["A"], # pylint: disable = protected-access Setting("default", "A", 1), )
def test_ge(self) -> None: """ :return: :rtype: None """ with self.assertRaises(CompareWithNotSettingException): self.assertTrue("a_false_setting" >= self.setting_a) with self.assertRaises(CompareWithNotSameNameSettingException): setting_not_same_name = Setting("default", "not_same_name", "a") self.assertTrue(setting_not_same_name >= self.setting_a) setting_b_ge = Setting("project", "b", "b") self.assertTrue(setting_b_ge >= self.setting_b) setting_b_ge = Setting("env", "b", "b") self.assertTrue(setting_b_ge >= self.setting_b) setting_b_not_ge = Setting("default", "b", "b") self.assertFalse(setting_b_not_ge >= self.setting_b)
def test_setitem(self): """ test the method of setitem :return: """ settings = BaseSettings(settings={"A": 1, "B": 2}) with settings.unfreeze() as settings_: settings_["C"] = 3 self.assertEqual(settings["C"], 3) with self.assertRaises(SettingsFrozenException): settings["C"] = 3 with self.assertRaises(SettingNameNotUpperException): with settings.unfreeze("default") as settings_: settings_["a"] = 3 with self.assertRaises(SettingsLowOrEqualPriorityException): with settings.unfreeze("default") as settings_: settings_["A"] = 3 with self.assertRaises(SettingsLowOrEqualPriorityException): with settings.unfreeze("project") as settings_: settings_["A"] = 3 try: with settings.unfreeze("default", skip_error=True) as settings_: settings_["A"] = 3 except SettingsLowOrEqualPriorityException: self.fail("__setitem__ raised SettingsLowOrEqualPriorityException") else: self.assertEqual(settings._data["A"], Setting("project", "A", 1)) try: with settings.unfreeze("project", skip_error=True) as settings_: settings_["A"] = 3 except SettingsLowOrEqualPriorityException: self.fail("__setitem__ raised SettingsLowOrEqualPriorityException") else: self.assertEqual(settings._data["A"], Setting("project", "A", 1))
def test_init(self) -> None: """ test the initialize method :return: :rtype: None """ settings = BaseSettings() self.assertDictEqual(settings._data, {}) # pylint: disable = protected-access settings = BaseSettings(settings={"A": 1, "B": 2}) self.assertDictEqual( settings._data, # pylint: disable = protected-access { "A": Setting("project", "A", 1), "B": Setting("project", "B", 2), }, ) self.assertTrue(settings.is_frozen()) self.assertEqual( settings._priority, "project" # pylint: disable = protected-access )
def test_from_yaml(self) -> None: """ :return: :rtype: None """ test_yaml = {"A": 1, "B": 2} yaml_file = NamedTemporaryFile(mode="w") yaml.dump(test_yaml, yaml_file) settings = Settings.from_yaml(yaml_file.name, "default") self.assertIn("A", settings) self.assertEqual(settings._data["A"], Setting("default", "A", 1))
def test_init(self) -> None: """ :return: :rtype: None """ settings = Settings() self.assertEqual(len(settings), 0) settings = Settings(default_settings=True) self.assertIn("LOG_LEVEL", settings) self.assertEqual( settings._data["LOG_LEVEL"], # pylint: disable = protected-access Setting("default", "LOG_LEVEL", logging.INFO), ) settings = Settings(default_settings="tests.samples.settings") self.assertIn("A", settings) self.assertEqual( settings._data["A"], # pylint: disable = protected-access Setting("default", "A", 1), ) settings = Settings({"LOG_LEVEL": logging.DEBUG}, default_settings=True) self.assertIn("LOG_LEVEL", settings) self.assertEqual( settings._data["LOG_LEVEL"], # pylint: disable = protected-access Setting("project", "LOG_LEVEL", logging.DEBUG), ) settings = Settings({"A": 0}, default_settings="tests.samples.settings") self.assertIn("A", settings) self.assertEqual( settings._data["A"], # pylint: disable = protected-access Setting("project", "A", 0), )
def test_from_json(self) -> None: """ :return: :rtype: None """ test_json = {"A": 1, "B": 2} json_file = NamedTemporaryFile() json_file.write(orjson.dumps(test_json)) json_file.seek(0) settings = Settings.from_json(json_file.name) self.assertIn("A", settings) self.assertEqual(settings._data["A"], Setting("project", "A", 1))
def test_load_yaml(self) -> None: """ :return: :rtype: None """ test_yaml = {"A": 1, "B": 2} yaml_file = NamedTemporaryFile(mode="w") yaml.dump(test_yaml, yaml_file) settings = Settings() with settings.unfreeze() as settings_: settings_.load_yaml(yaml_file.name) self.assertIn("A", settings) self.assertEqual(settings._data["A"], Setting("project", "A", 1))