def test_load_config_default_no_file(self): m = mock.Mock() loader = m.a() cm = ConfigMixin() cm.add_config_item(key="test-int", saver=lambda: 123, loader=loader, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.load_config() self.assertTrue(loader.called) # not yet saved, should be called with default value self.assertTrue(loader.called_with('456'))
def test_load_config_saved(self): m = mock.Mock() loader = m.a() cm = ConfigMixin() cm.add_config_item(key="test-int", saver=lambda: 123, loader=loader, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.save_config() cm.load_config() self.assertTrue(loader.called) self.assertTrue(loader.called_with('123'))
def test_load_config_errors(self): def loader_with_errors(value: int): # noqa raise RuntimeError cm = ConfigMixin() cm.add_config_item(key="test-int", saver=lambda: 123, loader=loader_with_errors, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.save_config() try: cm.load_config() except RuntimeError: self.fail("No errors should be raised.")
def test_save_config(self): cm = ConfigMixin() cm.add_config_item(key="test-bool", saver=lambda: True, loader=lambda x: x, default=True) cm.add_config_item(key="test-int", saver=lambda: 123, loader=lambda x: x, default=456) with temporary_path() as tmp: cm.config_path = tmp.joinpath("config") cm.save_config() with cm.config_path.open("r") as fp: saved_data = json.load(fp) self.assertIn("test-bool", saved_data.keys()) self.assertIn("test-int", saved_data.keys()) self.assertTrue(saved_data["test-bool"]) self.assertEqual(123, saved_data["test-int"])