tests/test_configparser.py
author Radek Brich <radek.brich@devl.cz>
Sat, 13 Apr 2013 14:11:26 +0200
changeset 4 fdaa7dc9035e
parent 3 cc27136cdead
permissions -rwxr-xr-x
Refactor.
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()
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    13
        self.config.add_option('arg_str')
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    14
        self.config.add_option('arg_int', int, 1)
3
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
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    18
    def test_add_option(self):
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
        name = 'argname'
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    20
        self.config.add_option(name, int, 1)
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    21
        self.assertEqual(self.config.options[name]['type'], int)
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    22
        self.assertEqual(self.config.options[name]['default'], 1)
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    23
        self.assertEqual(self.config.values[name], 1)
3
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()