Add test for get/set float methods
[wifi-radar.git] / test / unit / config.py
blobb62418be035e151d46d10e95abe4002aa4821762
1 # test.config - tests for configuration management
3 # Part of WiFi Radar: A utility for managing WiFi profiles on GNU/Linux.
5 # Copyright (C) 2014 Sean Robinson <seankrobinson@gmail.com>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License in LICENSE.GPL for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 import unittest
24 from wifiradar.config import ConfigFile
27 class TestConfigFile(unittest.TestCase):
28 def setUp(self):
29 self.config_file = ConfigFile('./test_config_file', {})
31 def test_set_get_opt(self):
32 """ Test set_opt and get_opt methods. """
33 self.assertEqual(self.config_file.sections(), [])
34 self.config_file.set_opt('TEST_SECTION', 'str_opt', 'str_value')
35 self.assertEqual('str_value',
36 self.config_file.get_opt('TEST_SECTION', 'str_opt'))
38 def test_set_get_bool_opt(self):
39 """ Test set_bool_opt and get_opt_as_bool methods. """
40 self.assertEqual(self.config_file.sections(), [])
41 # Alternate True/False tests in case set_bool_opt does not change
42 # the value in 'bool_opt'
44 # Check boolean conversion.
45 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', True)
46 self.assertEqual(True,
47 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
48 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', False)
49 self.assertEqual(False,
50 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
52 # Check successful integer conversion.
53 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', 1)
54 self.assertEqual(True,
55 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
56 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', 0)
57 self.assertEqual(False,
58 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
60 # Check string conversion.
61 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', 'True')
62 self.assertEqual(True,
63 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
64 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', 'False')
65 self.assertEqual(False,
66 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
68 # extra possible values
69 self.config_file.set_bool_opt('TEST_SECTION', 'bool_opt', 2)
70 self.assertEqual(True,
71 self.config_file.get_opt_as_bool('TEST_SECTION', 'bool_opt'))
73 # Check for exceptions.
74 self.assertRaises(ValueError, self.config_file.set_bool_opt,
75 'TEST_SECTION', 'bool_opt', -1)
76 self.assertRaises(ValueError, self.config_file.set_bool_opt,
77 'TEST_SECTION', 'bool_opt', 'Not False')
78 self.assertRaises(ValueError, self.config_file.set_bool_opt,
79 'TEST_SECTION', 'bool_opt', 1.0)
81 def test_set_get_int_opt(self):
82 """ Test set_int_opt and get_opt_as_int methods. """
83 self.assertEqual(self.config_file.sections(), [])
84 self.config_file.set_int_opt('TEST_SECTION', 'int_opt', 42)
85 self.assertEqual(42,
86 self.config_file.get_opt_as_int('TEST_SECTION', 'int_opt'))
88 def test_set_get_float_opt(self):
89 """ Test set_float_opt and get_opt_as_float methods. """
90 self.assertEqual(self.config_file.sections(), [])
91 self.config_file.set_float_opt('TEST_SECTION', 'float_opt', 3.0)
92 self.assertEqual(3.0,
93 self.config_file.get_opt_as_float('TEST_SECTION', 'float_opt'))