tests/test_configparser.py
author Radek Brich <radek.brich@devl.cz>
Sat, 06 Apr 2013 00:07:17 +0200
changeset 3 cc27136cdead
child 4 fdaa7dc9035e
permissions -rwxr-xr-x
Add ConfigParser and tests.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from pycolib.configparser import ConfigParser, ConfigParserError
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
import unittest
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
import os.path
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
class TestConfigParser(unittest.TestCase):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
    def setUp(self):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
        self.config = ConfigParser()
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
        self.config.add_argument('arg_str')
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        self.config.add_argument('arg_int', int, 1)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
        script_path = os.path.dirname(os.path.realpath(__file__))
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
        self.data_path = os.path.join(script_path, 'data')
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
    def test_add_argument(self):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        name = 'argname'
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
        self.config.add_argument(name, int, 1)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
        self.assertEqual(self.config.registered_args[name]['type'], int)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
        self.assertEqual(self.config.registered_args[name]['default'], 1)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
        self.assertEqual(self.config.args[name], 1)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
    def test_load_badname(self):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
        self.assertRaises(IOError, self.config.load, 'bad/file_name')
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
    def test_load_badargs(self):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
        fname = os.path.join(self.data_path, 'test_badargs.conf')
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
        self.assertRaises(ConfigParserError, self.config.load, fname)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
    def test_load_ok(self):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
        fname = os.path.join(self.data_path, 'test_ok.conf')
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
        self.config.load(fname)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
        self.assertEqual(self.config.arg_str, '1')
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
        self.assertEqual(self.config.arg_int, 2)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
if __name__ == '__main__':
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
    unittest.main()