def test_add_section_resets_vars(self): """Reproduce BUG: calling add_section() overwrittes existing sections with an empty one. """ config = Config("fixtures/config1.ini") config.add_section("SectionA") self.assertEqual(2, config.SectionA.value_a)
def test_list_sections(self): config = Config() config.add_section("test2") config.add_section("test1") expected = [ ('test1', config.test1), ('test2', config.test2) ] self.assertEqual(config.list_sections(), expected)
def test_list_vars(self): config = Config() config.add_section("test") config.test.var2 = "test2" config.test.var1 = "test1" expected = [ ('var1', config.test.get_var('var1')), ('var2', config.test.get_var('var2')), ] self.assertEqual(config.test.list_variables(), expected)
class ConfigUnitTestCreatingConfig(unittest.TestCase): def setUp(self): self.config = Config("fixtures/config1.ini") def test_set_section_variable(self): "It allow to set new config options under a specific section" self.config.SectionA.set_var("value_b", 3) self.assertEqual(3, self.config.SectionA.value_b) def test_set_global_variable(self): "It allows to set a new config option under the global section" self.config.global_name = "global_val" self.assertEqual("global_val", self.config.global_name) def test_add_section(self): "It allows to add a new section." self.config.add_section("SectionB") self.config.SectionB.set_var("value_b", 4) self.assertEquals(4, self.config.SectionB.value_b) def test_set_variable(self): "Variables will be setted using ConfigVariable objects" self.config.SectionA.new_var = 5 self.assertTrue(isinstance(self.config.SectionA.get_var('new_var'), ConfigVariable)) self.assertEquals(5, self.config.SectionA.get_var("new_var").value) def test_set_variable_ci(self): "Variables can be set through a case insensitive name, and then properly retrieved" self.config.SectionA.NEW_VAR = 5 self.assertEquals(5, self.config.SectionA.get_var("new_var").value) self.assertEquals(5, self.config.SectionA.get_var("NEW_VAR").value) def test_del_variable(self): self.config.SectionA.NEW_VAR = 6 del self.config.SectionA.NEW_VAR self.assertRaises(KeyError, self.config.SectionA.get_var, "new_var") def test_save_config(self): "It saves its current state to a .ini file" filename = "generated_config.ini" self.config.save(filename) saved_config = Config(filename) self.assertEquals(self.config.db_name, saved_config.db_name) self.assertEquals(self.config.SectionA.value_a, saved_config.SectionA.value_a) os.remove("generated_config.ini") def test_modified_init(self): """It detects whether any changes where made since the object was constructed, should be False right after __init__""" self.assertEquals(False, self.config.modified) def test_modified_after_change(self): "modified should be True after changing the value of a variable" self.config.new_var = "new_value" self.assertEquals(True, self.config.modified) def test_modified_same_vaule(self): """Setting a variable to the same value it had should not be considered a modification""" self.config.db_name = "mysql" self.assertEquals(False, self.config.modified) def test_autosave_config(self): """It can be setted to automatically save changes to a given file.""" self.config.autosave("autosaved.ini") self.config.SectionA.set_var("value_b", 3) saved_config = Config("autosaved.ini") self.assertEquals(3, saved_config.SectionA.value_b) os.remove("autosaved.ini")