confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Info | Log | Files | Refs | README | LICENSE

test_env.py (2870B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import logging
      5 
      6 # local imports
      7 from confini.env import ConfigEnvParser
      8 from confini import Config
      9 
     10 logging.basicConfig(level=logging.DEBUG)
     11 logg = logging.getLogger()
     12 
     13 class TestEnv(unittest.TestCase):
     14 
     15     wd = os.path.dirname(__file__)
     16 
     17     def setUp(self):
     18         pass
     19 
     20     def tearDown(self):
     21         pass
     22 
     23     def test_dict_override(self):
     24         inidir = os.path.join(self.wd, 'files')
     25         c = Config(inidir)
     26         c.process()
     27 
     28         override_dict = {
     29                 'FOO_BAR': '666',
     30                 'XYZZY_BERT': 'oscar',
     31                 'BAR_FOO': None,
     32                 }
     33         c.dict_override(override_dict, 'arbitrary dict')
     34 
     35         expect = {
     36             'FOO_BAR': '666',
     37             'FOO_BAZ': '029a',
     38             'BAR_FOO': 'oof',
     39             'XYZZY_BERT': 'oscar',
     40                 }
     41         self.assertDictEqual(expect, c.store)
     42 
     43         override_dict = {
     44                 'BAR_FOO': 'barbarbar',
     45                 }
     46         c.dict_override(override_dict, 'arbitrary dict')
     47 
     48         expect = {
     49             'FOO_BAR': '666',
     50             'FOO_BAZ': '029a',
     51             'BAR_FOO': 'barbarbar',
     52             'XYZZY_BERT': 'oscar',
     53                 }
     54         self.assertDictEqual(expect, c.store)
     55 
     56 
     57 
     58     def test_env_a_override(self):
     59         os.environ['FOO_BAR'] = '43'
     60         inidir = os.path.join(self.wd, 'files')
     61         c = Config(inidir)
     62         c.process()
     63 
     64         os.environ['ZZZ_FOO_BAR'] = '44'
     65         inidir = os.path.join(self.wd, 'files')
     66         c = Config(inidir, env_prefix='ZZZ')
     67         c.process()
     68         expect = {
     69             'FOO_BAR': '44',
     70             'FOO_BAZ': '029a',
     71             'BAR_FOO': 'oof',
     72             'XYZZY_BERT': 'ernie',
     73                 }
     74         self.assertDictEqual(expect, c.store)
     75 
     76         os.environ['ZZZ_FOO_BAR'] = ''
     77         inidir = os.path.join(self.wd, 'files')
     78         c = Config(inidir, env_prefix='ZZZ')
     79         c.process()
     80         expect = {
     81             'FOO_BAR': '',
     82             'FOO_BAZ': '029a',
     83             'BAR_FOO': 'oof',
     84             'XYZZY_BERT': 'ernie',
     85                 }
     86         self.assertDictEqual(expect, c.store)
     87 
     88         del os.environ['ZZZ_FOO_BAR']
     89         inidir = os.path.join(self.wd, 'files')
     90         c = Config(inidir, env_prefix='ZZZ')
     91         c.process()
     92         expect = {
     93             'FOO_BAR': '42',
     94             'FOO_BAZ': '029a',
     95             'BAR_FOO': 'oof',
     96             'XYZZY_BERT': 'ernie',
     97                 }
     98         self.assertDictEqual(expect, c.store)
     99 
    100     def test_env_parser(self):
    101         envpath = os.path.join(self.wd, 'files', 'env', 'env.txt')
    102         c = ConfigEnvParser()
    103         p = c.from_file(envpath)
    104         self.assertTrue(p.has_section('foo'))
    105         self.assertTrue(p.has_section('bar'))
    106         self.assertTrue(p.has_option('foo', 'bar'))
    107 
    108 
    109 if __name__ == '__main__':
    110     unittest.main()