test_basic.py (2957B)
1 #!/usr/bin/python 2 3 import os 4 import unittest 5 import logging 6 7 from confini import Config 8 9 logging.basicConfig(level=logging.DEBUG) 10 logg = logging.getLogger() 11 12 class TestBasic(unittest.TestCase): 13 14 wd = os.path.dirname(__file__) 15 16 def test_parse_default(self): 17 inidir = os.path.join(self.wd, 'files/default') 18 c = Config(inidir) 19 c.process() 20 r = c.get('FOO_BAR', 'plugh') 21 self.assertEqual(r, 'xyzzy') 22 r = c.get('FOO_BAZ', 'plugh') 23 self.assertEqual(r, 'plugh') 24 r = c.get('FOO_BAZ') 25 self.assertEqual(r, None) 26 with self.assertRaises(KeyError): 27 r = c.get('FOO_BAZBAZ') 28 29 30 def test_parse_two(self): 31 inidir = os.path.join(self.wd, 'files/default') 32 c = Config(inidir, override_dirs=[inidir]) 33 c.process() 34 r = c.get('FOO_BAR', 'plugh') 35 self.assertEqual(r, 'xyzzy') 36 r = c.get('FOO_BAZ', 'plugh') 37 self.assertEqual(r, 'plugh') 38 r = c.get('FOO_BAZ') 39 self.assertEqual(r, None) 40 with self.assertRaises(KeyError): 41 r = c.get('FOO_BAZBAZ') 42 43 44 def test_overwrite_guard(self): 45 inidir = os.path.join(self.wd, 'files/default') 46 c = Config(inidir) 47 c.process() 48 with self.assertRaises(AttributeError): 49 c.add('xxx', 'FOO_BAR') 50 c.add('xxx', 'FOO_BAR', exists_ok=True) 51 r = c.get('FOO_BAR') 52 self.assertEqual(r, 'xxx') 53 54 55 def test_parse_two_files(self): 56 inidir = os.path.join(self.wd, 'files') 57 c = Config(inidir) 58 c.process() 59 c.require('BERT', 'XYZZY') 60 expect = { 61 'FOO_BAR': '42', 62 'FOO_BAZ': '029a', 63 'BAR_FOO': 'oof', 64 'XYZZY_BERT': 'ernie', 65 } 66 self.assertDictEqual(expect, c.store) 67 68 69 def test_remove_require(self): 70 inidir = os.path.join(self.wd, 'files') 71 c = Config(inidir) 72 c.process() 73 c.require('BERT', 'XYZZY') 74 self.assertTrue(c.validate()) 75 c.require('ERNIE', 'XYZZY') 76 self.assertFalse(c.validate()) 77 logg.debug(c) 78 79 80 def test_remove_strict(self): 81 inidir = os.path.join(self.wd, 'files/default') 82 c = Config(inidir) 83 c.process() 84 c.remove('FOO_BAR') 85 with self.assertRaises(KeyError): 86 c.get('FOO_BAR') 87 88 89 def test_remove_wild(self): 90 inidir = os.path.join(self.wd, 'files/remove') 91 c = Config(inidir) 92 c.process() 93 c.remove('FOO', strict=False) 94 self.assertEqual(len(c.all()), 0) 95 96 c = Config(inidir) 97 c.process() 98 c.remove('FOO_BA', strict=False) 99 c.get('FOO_XYZZY') 100 self.assertEqual(len(c.all()), 1) 101 102 103 def test_all(self): 104 inidir = os.path.join(self.wd, 'files') 105 c = Config(inidir) 106 a = c.all() 107 self.assertEqual(a, list(c.store.keys())) 108 109 110 if __name__ == '__main__': 111 unittest.main()