def test_inifile_nonexistent(self): logging.getLogger().setLevel(logging.CRITICAL) cfg = LayeredConfig(INIFile("nonexistent.ini")) self.assertEqual([], list(cfg)) # make sure a nonexistent inifile doesn't interfere with the # rest of the LayeredConfig object defobj = Defaults({'datadir': 'something'}) iniobj = INIFile("nonexistent.ini") cfg = LayeredConfig(defobj, iniobj) self.assertEqual("something", cfg.datadir) # and make sure it's settable (should set up the INIFile # object and affect it, and leave the defaults dict untouched # as it's the lowest priority) cfg.datadir = "else" self.assertEqual("else", cfg.datadir) self.assertEqual("else", iniobj.get("datadir")) self.assertEqual("something", defobj.get("datadir")) # same as above, but with a "empty" INIFile object iniobj = INIFile() cfg = LayeredConfig(defobj, iniobj) self.assertEqual("something", cfg.datadir) cfg.datadir = "else" self.assertEqual("else", cfg.datadir)
class TestINIFile(TestINIFileHelper, unittest.TestCase, TestConfigSourceHelper): supported_types = (str, ) supports_nesting = False def setUp(self): super(TestINIFile, self).setUp() self.simple = INIFile("simple.ini") self.complex = INIFile("complex.ini") # Overrides of TestHelper.test_get, .test_typed and # .test_subsection_nested due to limitations of INIFile # INIFile carries no typing information def test_get(self): self.assertEqual(self.simple.get("home"), "mydata") self.assertEqual(self.simple.get("processes"), "4") self.assertEqual(self.simple.get("force"), "True") self.assertEqual(self.simple.get("extra"), "foo, bar") self.assertEqual(self.simple.get("expires"), "2014-10-15") self.assertEqual(self.simple.get("lastrun"), "2014-10-15 14:32:07") def test_typed(self): for key in self.simple.keys(): self.assertFalse(self.simple.typed(key)) # Override: INIFile doesn't support nested subsections def test_subsection_nested(self): subsec = self.complex.subsection('mymodule') self.assertEqual(set(subsec.subsections()), set(())) def test_inifile_default_as_root(self): # using a rootsection named DEFAULT triggers different # cascading-like behaviour in configparser. # load a modified version of complex.ini with open("complex.ini") as fp: ini = fp.read() with open("complex-otherroot.ini", "w") as fp: fp.write(ini.replace("[__root__]", "[DEFAULT]")) cfg = LayeredConfig( INIFile("complex-otherroot.ini", rootsection="DEFAULT")) # this is a modified/simplified version of ._test_subsections self.assertEqual(cfg.home, 'mydata') self.assertEqual(cfg.processes, '4') self.assertEqual(cfg.force, 'True') self.assertEqual(cfg.mymodule.force, 'False') self.assertEqual(cfg.extra, "foo, bar") self.assertEqual(cfg.mymodule.extra, "foo, baz") with self.assertRaises(AttributeError): cfg.expires self.assertEqual(cfg.mymodule.expires, "2014-10-15") # this is really unwanted cascading behaviour self.assertEqual(cfg.mymodule.home, 'mydata') self.assertEqual(cfg.mymodule.processes, '4') os.unlink("complex-otherroot.ini") def test_inifile_nonexistent(self): logging.getLogger().setLevel(logging.CRITICAL) cfg = LayeredConfig(INIFile("nonexistent.ini")) self.assertEqual([], list(cfg)) # make sure a nonexistent inifile doesn't interfere with the # rest of the LayeredConfig object defobj = Defaults({'datadir': 'something'}) iniobj = INIFile("nonexistent.ini") cfg = LayeredConfig(defobj, iniobj) self.assertEqual("something", cfg.datadir) # and make sure it's settable (should set up the INIFile # object and affect it, and leave the defaults dict untouched # as it's the lowest priority) cfg.datadir = "else" self.assertEqual("else", cfg.datadir) self.assertEqual("else", iniobj.get("datadir")) self.assertEqual("something", defobj.get("datadir")) # same as above, but with a "empty" INIFile object iniobj = INIFile() cfg = LayeredConfig(defobj, iniobj) self.assertEqual("something", cfg.datadir) cfg.datadir = "else" self.assertEqual("else", cfg.datadir) def test_write(self): cfg = LayeredConfig(INIFile("complex.ini")) cfg.mymodule.expires = date(2014, 10, 24) # calling write for any submodule will force a write of the # entire config file LayeredConfig.write(cfg.mymodule) want = """[__root__] home = mydata processes = 4 force = True extra = foo, bar [mymodule] force = False extra = foo, baz expires = 2014-10-24 [extramodule] unique = True """ with open("complex.ini") as fp: got = fp.read().replace("\r\n", "\n") self.assertEqual(want, got)
class TestINIFile(TestINIFileHelper, unittest.TestCase, TestConfigSourceHelper): supported_types = (str,) supports_nesting = False def setUp(self): super(TestINIFile, self).setUp() self.simple = INIFile("simple.ini") self.complex = INIFile("complex.ini") # Overrides of TestHelper.test_get, .test_typed and # .test_subsection_nested due to limitations of INIFile # INIFile carries no typing information def test_get(self): self.assertEqual(self.simple.get("home"), "mydata") self.assertEqual(self.simple.get("processes"), "4") self.assertEqual(self.simple.get("force"), "True") self.assertEqual(self.simple.get("extra"), "foo, bar") self.assertEqual(self.simple.get("expires"), "2014-10-15") self.assertEqual(self.simple.get("lastrun"), "2014-10-15 14:32:07") def test_typed(self): for key in self.simple.keys(): self.assertFalse(self.simple.typed(key)) # Override: INIFile doesn't support nested subsections def test_subsection_nested(self): subsec = self.complex.subsection('mymodule') self.assertEqual(set(subsec.subsections()), set(())) def test_inifile_default_as_root(self): # using a rootsection named DEFAULT triggers different # cascading-like behaviour in configparser. # load a modified version of complex.ini with open("complex.ini") as fp: ini = fp.read() with open("complex-otherroot.ini", "w") as fp: fp.write(ini.replace("[__root__]", "[DEFAULT]")) cfg = LayeredConfig(INIFile("complex-otherroot.ini", rootsection="DEFAULT")) # this is a modified/simplified version of ._test_subsections self.assertEqual(cfg.home, 'mydata') self.assertEqual(cfg.processes, '4') self.assertEqual(cfg.force, 'True') self.assertEqual(cfg.mymodule.force, 'False') self.assertEqual(cfg.extra, "foo, bar") self.assertEqual(cfg.mymodule.extra, "foo, baz") with self.assertRaises(AttributeError): cfg.expires self.assertEqual(cfg.mymodule.expires, "2014-10-15") # this is really unwanted cascading behaviour self.assertEqual(cfg.mymodule.home, 'mydata') self.assertEqual(cfg.mymodule.processes, '4') os.unlink("complex-otherroot.ini") def test_inifile_nonexistent(self): logging.getLogger().setLevel(logging.CRITICAL) cfg = LayeredConfig(INIFile("nonexistent.ini")) self.assertEqual([], list(cfg)) # make sure a nonexistent inifile doesn't interfere with the # rest of the LayeredConfig object defobj = Defaults({'datadir': 'something'}) iniobj = INIFile("nonexistent.ini") cfg = LayeredConfig(defobj, iniobj) self.assertEqual("something", cfg.datadir) # and make sure it's settable (should set up the INIFile # object and affect it, and leave the defaults dict untouched # as it's the lowest priority) cfg.datadir = "else" self.assertEqual("else", cfg.datadir) self.assertEqual("else", iniobj.get("datadir")) self.assertEqual("something", defobj.get("datadir")) # same as above, but with a "empty" INIFile object iniobj = INIFile() cfg = LayeredConfig(defobj, iniobj) self.assertEqual("something", cfg.datadir) cfg.datadir = "else" self.assertEqual("else", cfg.datadir) def test_write(self): cfg = LayeredConfig(INIFile("complex.ini")) cfg.mymodule.expires = date(2014, 10, 24) # calling write for any submodule will force a write of the # entire config file LayeredConfig.write(cfg.mymodule) want = """[__root__] home = mydata processes = 4 force = True extra = foo, bar [mymodule] force = False extra = foo, baz expires = 2014-10-24 [extramodule] unique = True """ with open("complex.ini") as fp: got = fp.read().replace("\r\n", "\n") self.assertEqual(want, got)