/
config_test.py
133 lines (109 loc) · 5.24 KB
/
config_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from test import *
from config import *
import codecs
import unittest
class ConfigTest(unittest.TestCase):
def test_it_should_parse_migrations_dir_with_one_relative_dir(self):
config = Config()
dirs = config._parse_migrations_dir('.')
assert len(dirs) == 1
assert dirs[0] == os.path.abspath('.')
def test_it_should_parse_migrations_dir_with_two_relative_dirs(self):
config = Config()
dirs = config._parse_migrations_dir('test:migrations:./a/relative/path:another/path')
assert len(dirs) == 4
assert dirs[0] == os.path.abspath('test')
assert dirs[1] == os.path.abspath('migrations')
assert dirs[2] == os.path.abspath('./a/relative/path')
assert dirs[3] == os.path.abspath('another/path')
def test_it_should_parse_migrations_dir_with_one_absolute_dir(self):
config = Config()
dirs = config._parse_migrations_dir(os.path.abspath('.'))
assert len(dirs) == 1
assert dirs[0] == os.path.abspath('.')
def test_it_should_parse_migrations_dir_with_two_absolute_dirs(self):
config = Config()
dirs = config._parse_migrations_dir('%s:%s:%s:%s' % (
os.path.abspath('test'), os.path.abspath('migrations'),
os.path.abspath('./a/relative/path'), os.path.abspath('another/path'))
)
assert len(dirs) == 4
assert dirs[0] == os.path.abspath('test')
assert dirs[1] == os.path.abspath('migrations')
assert dirs[2] == os.path.abspath('./a/relative/path')
assert dirs[3] == os.path.abspath('another/path')
class FileConfigTest(unittest.TestCase):
def setUp(self):
config_file = '''
HOST = os.getenv('DB_HOST') or 'localhost'
USERNAME = os.getenv('DB_USERNAME') or 'root'
PASSWORD = os.getenv('DB_PASSWORD') or ''
DATABASE = os.getenv('DB_DATABASE') or 'migration_example'
MIGRATIONS_DIR = os.getenv('MIGRATIONS_DIR') or 'example'
'''
f = open('sample.conf', 'w')
f.write(config_file)
f.close()
def tearDown(self):
os.remove('sample.conf')
def test_it_should_read_config_file(self):
config_path = os.path.abspath('sample.conf')
config = FileConfig(config_path)
self.assertEquals(config.get('db_host'), 'localhost')
self.assertEquals(config.get('db_user'), 'root')
self.assertEquals(config.get('db_password'), '')
self.assertEquals(config.get('db_name'), 'migration_example')
self.assertEquals(config.get('db_version_table'), Config.DB_VERSION_TABLE)
self.assertEquals(config.get('migrations_dir'), [os.path.abspath('example')])
def test_it_should_stop_execution_when_an_invalid_key_is_requested(self):
config_path = os.path.abspath('sample.conf')
config = FileConfig(config_path)
try:
config.get('invalid_config')
self.fail('it should not pass here')
except:
pass
def test_it_should_create_new_configs(self):
config_path = os.path.abspath('sample.conf')
config = FileConfig(config_path)
# ensure that the config does not exist
self.assertRaises(Exception, config.get, 'sample_config', 'TEST')
# create the config
config.put('sample_config', 'TEST')
# read the config
self.assertEquals(config.get('sample_config'), 'TEST')
def test_it_should_not_override_existing_configs(self):
config_path = os.path.abspath('sample.conf')
config = FileConfig(config_path)
config.put('sample_config', 'TEST')
self.assertRaises(Exception, config.put, 'sample_config', 'TEST')
class InPlaceConfigTest(unittest.TestCase):
def test_it_should_configure_default_parameters(self):
config = InPlaceConfig('localhost', 'user', 'passwd', 'db', 'dir')
self.assertEquals(config.get('db_host'), 'localhost')
self.assertEquals(config.get('db_user'), 'user')
self.assertEquals(config.get('db_password'), 'passwd')
self.assertEquals(config.get('db_name'), 'db')
self.assertEquals(config.get('db_version_table'), Config.DB_VERSION_TABLE)
self.assertEquals(config.get('migrations_dir'), [os.path.abspath('dir')])
def test_it_should_stop_execution_when_an_invalid_key_is_requested(self):
config = InPlaceConfig('localhost', 'user', 'passwd', 'db', 'dir')
try:
config.get('invalid_config')
self.fail('it should not pass here')
except:
pass
def test_it_should_create_new_configs(self):
config = InPlaceConfig('localhost', 'user', 'passwd', 'db', 'dir')
# ensure that the config does not exist
self.assertRaises(Exception, config.get, 'sample_config', 'TEST')
# create the config
config.put('sample_config', 'TEST')
# read the config
self.assertEquals(config.get('sample_config'), 'TEST')
def test_it_should_not_override_existing_configs(self):
config = InPlaceConfig('localhost', 'user', 'passwd', 'db', 'dir')
config.put('sample_config', 'TEST')
self.assertRaises(Exception, config.put, 'sample_config', 'TEST')
if __name__ == '__main__':
unittest.main()