Package elisa :: Package core :: Package tests :: Module test_config
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.tests.test_config

  1  # Elisa - Home multimedia server 
  2  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  3  # All rights reserved. 
  4  # 
  5  # This file is available under one of two license agreements. 
  6  # 
  7  # This file is licensed under the GPL version 3. 
  8  # See "LICENSE.GPL" in the root of this distribution including a special 
  9  # exception to use Elisa with Fluendo's plugins. 
 10  # 
 11  # The GPL part of Elisa is also available under a commercial licensing 
 12  # agreement from Fluendo. 
 13  # See "LICENSE.Elisa" in the root directory of this distribution package 
 14  # for details on that license. 
 15   
 16  from twisted.trial import unittest 
 17  from elisa.core.tests.elisa_test_case import ElisaTestCase 
 18   
 19  import os 
 20  from elisa.core import config 
 21  from elisa.core import log 
 22   
23 -class TestConfig(ElisaTestCase):
24 25 test_conf = "test.conf" 26 27 test_section_dict = {'test_list':['some','list'], 28 'test_int': 0, 'test_bool_t': True, 29 'test_bool_f': False, 30 'test_str': 'foobar'} 31
32 - def tearDown(self):
33 try: 34 os.unlink(self.test_conf) 35 except OSError: 36 pass
37
38 - def load_config(self):
39 """ Load the config from test.conf file 40 """ 41 c = config.Config(self.test_conf) 42 return c
43
44 - def load_default_config(self):
45 c = config.Config() 46 return c
47
48 - def set_test_section(self, c):
49 """ Fill a 'tests' section in the config 50 """ 51 c.set_section('tests') 52 c.set_option('test_list',['some','list'], section='tests') 53 c.set_option('test_int', 0, section='tests') 54 c.set_option('test_bool_t', True, section='tests') 55 c.set_option('test_bool_f', False, section='tests') 56 c.set_option('test_str', 'foobar', section='tests')
57
58 - def test_malformed_config(self):
59 """ Create a dumb config file containing formatting errors and 60 check errors are reported. 61 """ 62 data="""\ 63 [general] 64 65 foo=['bar' 66 """ 67 f = open('malformed.conf','w') 68 f.write(data) 69 f.close() 70 71 self.assertRaises(Exception, config.Config, 'malformed.conf') 72 os.unlink('malformed.conf')
73
74 - def test_import_export(self):
75 """ Load test config, write it, load dumped config and compare 76 the two instances. 77 """ 78 c1 = self.load_config() 79 self.set_test_section(c1) 80 c1.write() 81 82 c2 = self.load_config() 83 self.assertEqual(c1.as_dict(), c2.as_dict())
84
85 - def test_list_option(self):
86 """ Test tuple and list option types 87 """ 88 c = self.load_config() 89 90 self.assertEqual(c.get_option('resolution'), None) 91 92 self.set_test_section(c) 93 self.assertEqual(c.get_option('test_list', section='tests'), 94 ['some','list']) 95 c.write()
96
97 - def test_int_option(self):
98 """ Test integer option type 99 """ 100 c = self.load_config() 101 self.set_test_section(c) 102 self.assertEqual(c.get_option('test_int', section='tests'), 0)
103
104 - def test_boolean_option(self):
105 c = self.load_config() 106 self.set_test_section(c) 107 self.assertEqual(c.get_option('test_bool_t', section='tests'), True) 108 self.assertEqual(c.get_option('test_bool_f', section='tests'), False)
109 110
111 - def test_string_option(self):
112 """ Test string option type 113 """ 114 c = self.load_config() 115 self.set_test_section(c) 116 self.assertEqual(c.get_option('test_str', section='tests'), 'foobar')
117
118 - def test_section(self):
119 """ Check the whole tests section dumped as a dictionnary 120 """ 121 c = self.load_config() 122 self.set_test_section(c) 123 self.assertEqual(c.get_section('tests'), 124 self.test_section_dict)
125
126 - def test_del_section(self):
127 """ Test section removal 128 """ 129 c = self.load_config() 130 self.set_test_section(c) 131 132 c.del_section('tests') 133 self.assert_('tests' not in c.as_dict().keys())
134
135 - def test_del_option(self):
136 """ Test option removal 137 """ 138 c = self.load_config() 139 self.set_test_section(c) 140 141 for opt_name in self.test_section_dict.keys(): 142 c.del_option(opt_name,'tests') 143 self.assertEqual(c.get_option(opt_name,'tests'), None)
144