/
test_cli.py
351 lines (257 loc) · 9.97 KB
/
test_cli.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
###############################################################################
#
# Copyright 2014 by Shoobx, Inc.
#
###############################################################################
import os
import io
import unittest
import tempfile
import shutil
import textwrap
import logging
import pytest
from configparser import SafeConfigParser
import multiprocessing
from migrant import cli, backend, exceptions
HERE = os.path.dirname(__file__)
SAMPLE_CONFIG = """
[db1]
backend = mongo
repository = repo/
backend_uri = localhost:27017/acme
[db2]
backend = test
repository = /repo
"""
INTEGRATION_CONFIG = """
[test]
backend = test
repository = %s
""" % os.path.join(
HERE, "scripts"
)
INTEGRATION_CONFIG += """
[virgin]
backend = test
repository = %s
""" % os.path.join(
HERE, "noscripts"
)
class MockedDb:
def __init__(self, name, manager):
self.name = name
self.migrations = manager.list()
self.data = manager.dict()
class MockedBackend(backend.MigrantBackend):
def __init__(self, dbs, manager=None):
self.dbs = dbs
self.new_scripts = manager.list() if manager else []
self.inits = 0
def list_migrations(self, db):
return list(db.migrations)
def push_migration(self, db, migration):
db.migrations.append(migration)
def pop_migration(self, db, migration):
db.migrations.remove(migration)
def generate_connections(self):
"""Generate connections to process
"""
yield from self.dbs
def generate_test_connections(self):
return self.generate_connections()
def on_new_script(self, rev_name):
"""Called when new script is created
"""
self.new_scripts.append(rev_name)
def on_repo_init(self):
"""Called when new script repository is initialized
"""
self.inits += 1
class ConfigTest(unittest.TestCase):
def test_get_db_config(self):
cp = SafeConfigParser()
cp.readfp(io.StringIO(SAMPLE_CONFIG), "SAMPLE_CONFIG")
config = cli.get_db_config(cp, "db1")
self.assertEqual(
config,
{
"backend": "mongo",
"backend_uri": "localhost:27017/acme",
"repository": "repo/",
},
)
class UpgradeTest(unittest.TestCase):
@pytest.fixture(autouse=True)
def backend_fixture(self, tmpdir, migrant_backend):
m = multiprocessing.Manager()
self.db0 = MockedDb("db0", m)
self.backend = MockedBackend([self.db0], m)
migrant_backend.set(self.backend)
migrant_ini = tmpdir.join("migrant.ini")
migrant_ini.write(INTEGRATION_CONFIG)
self.migrant_ini = str(migrant_ini)
self.cfg = cli.load_config(self.migrant_ini)
self.logstream = io.StringIO()
hdl = logging.StreamHandler(self.logstream)
hdl.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
logging.root.addHandler(hdl)
logging.root.setLevel(logging.INFO)
def test_status_dirty(self):
self.db0.migrations.extend(["aaaa_first"])
args = cli.parser.parse_args(["test", "status"])
cli.dispatch(args, self.cfg)
log = self.logstream.getvalue()
self.assertIn("Pending actions: 2", log)
def test_status_clean(self):
self.db0.migrations.extend(["aaaa_first", "bbbb_second", "cccc_third"])
args = cli.parser.parse_args(["test", "status"])
cli.dispatch(args, self.cfg)
log = self.logstream.getvalue()
self.assertIn("Up-to-date", log)
def test_no_scripts(self):
args = cli.parser.parse_args(["virgin", "upgrade"])
cli.dispatch(args, self.cfg)
self.assertEqual(list(self.db0.migrations), ["INITIAL"])
def test_initial_upgrade(self):
args = cli.parser.parse_args(["test", "upgrade"])
cli.dispatch(args, self.cfg)
self.assertEqual(
list(self.db0.migrations), ["INITIAL", "aaaa_first", "bbbb_second", "cccc_third"]
)
def test_subsequent_emtpy_upgrade(self):
args = cli.parser.parse_args(["test", "upgrade"])
cli.dispatch(args, self.cfg)
# this should be a noop
cli.dispatch(args, self.cfg)
self.assertEqual(
list(self.db0.migrations), ["INITIAL", "aaaa_first", "bbbb_second", "cccc_third"]
)
def test_upgrade_latest(self):
self.db0.migrations.extend(["aaaa_first"])
args = cli.parser.parse_args(["test", "upgrade"])
cli.dispatch(args, self.cfg)
self.assertEqual(
list(self.db0.migrations), ["aaaa_first", "bbbb_second", "cccc_third"]
)
self.assertEqual(dict(self.db0.data), {"hello": "world", "value": "c"})
def test_upgrade_particular(self):
self.db0.migrations.extend(["aaaa_first"])
args = cli.parser.parse_args(["test", "upgrade", "--revision", "bbbb_second"])
cli.dispatch(args, self.cfg)
self.assertEqual(list(self.db0.migrations), ["aaaa_first", "bbbb_second"])
self.assertEqual(dict(self.db0.data), {"value": "b"})
def test_downgrade(self):
self.db0.migrations.extend(["INITIAL", "aaaa_first", "bbbb_second", "cccc_third"])
self.db0.data.update({"hello": "world", "value": "c"})
args = cli.parser.parse_args(["test", "upgrade", "--revision", "aaaa_first"])
cli.dispatch(args, self.cfg)
self.assertEqual(list(self.db0.migrations), ["INITIAL", "aaaa_first"])
self.assertEqual(dict(self.db0.data), {"value": "a"})
def test_downgrade_to_initial(self):
self.db0.migrations.extend(["INITIAL", "aaaa_first", "bbbb_second", "cccc_third"])
self.db0.data.update({"hello": "world", "value": "c"})
args = cli.parser.parse_args(["test", "upgrade", "--revision", "INITIAL"])
cli.dispatch(args, self.cfg)
self.assertEqual(list(self.db0.migrations), ["INITIAL"])
self.assertEqual(dict(self.db0.data), {})
def test_dry_run_upgrade(self):
self.db0.migrations.extend(["aaaa_first"])
self.db0.data.update({"value": "a"})
args = cli.parser.parse_args(["test", "upgrade", "--dry-run"])
cli.dispatch(args, self.cfg)
self.assertEqual(list(self.db0.migrations), ["aaaa_first"])
self.assertEqual(dict(self.db0.data), {"value": "a"})
def test_test(self):
self.db0.migrations = ["INITIAL", "aaaa_first", "bbbb_second", "cccc_third"]
self.db0.data = {"hello": "world", "value": "c"}
cli.main(["-c", self.migrant_ini, "test", "test"])
log = self.logstream.getvalue()
assert "Testing upgrade" in log
assert "Testing downgrade" in log
assert "Testing completed" in log
class InitTest(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp("migrant")
def tearDown(self):
shutil.rmtree(self.dir)
@pytest.fixture
def sample_config(tmpdir):
SAMPLE_CONFIG = textwrap.dedent(
"""
[newdb]
backend = noop
repository = %s/repo
"""
% tmpdir
)
cfg = SafeConfigParser()
cfg.readfp(io.StringIO(SAMPLE_CONFIG), "SAMPLE_CONFIG")
return cfg
def initialize(cfg):
# Initialize repository
args = cli.parser.parse_args(["newdb", "init"])
cli.dispatch(args, cfg)
def get_scripts_filename(cfg):
repodir = cfg.get("newdb", "repository")
return os.path.join(repodir, "scripts.lst")
def test_missing_cmd():
with pytest.raises(SystemExit):
cli.parser.parse_args(["newdb"])
def test_init(sample_config):
args = cli.parser.parse_args(["newdb", "init"])
cli.dispatch(args, sample_config)
repodir = sample_config.get("newdb", "repository")
assert os.path.exists(repodir)
def test_init_subsequent(sample_config):
os.mkdir(sample_config.get("newdb", "repository"))
slistfname = get_scripts_filename(sample_config)
with open(slistfname, "w") as f:
f.write("aaaa_test.py")
args = cli.parser.parse_args(["newdb", "init"])
cli.dispatch(args, sample_config)
with open(slistfname) as slist:
assert slist.read() == "aaaa_test.py"
def test_new_uninitialized(sample_config):
args = cli.parser.parse_args(["newdb", "new", "First script"])
with pytest.raises(exceptions.RepositoryNotFound):
cli.dispatch(args, sample_config)
def test_new_initialized(sample_config):
initialize(sample_config)
args = cli.parser.parse_args(["newdb", "new", "First script"])
cli.dispatch(args, sample_config)
with open(get_scripts_filename(sample_config)) as slist:
lines = slist.readlines()
assert len(lines) == 3
assert lines[-1].strip().endswith("_first_script.py")
def test_new_subsequent(sample_config):
initialize(sample_config)
args = cli.parser.parse_args(["newdb", "new", "First script"])
cli.dispatch(args, sample_config)
args = cli.parser.parse_args(["newdb", "new", "Second script"])
cli.dispatch(args, sample_config)
with open(get_scripts_filename(sample_config)) as slist:
lines = slist.readlines()
assert len(lines) == 4
assert lines[-2].strip().endswith("_first_script.py")
assert lines[-1].strip().endswith("_second_script.py")
def test_new_duplicate(sample_config):
initialize(sample_config)
args = cli.parser.parse_args(["newdb", "new", "First script"])
cli.dispatch(args, sample_config)
with pytest.raises(exceptions.ScriptAlreadyExists):
cli.dispatch(args, sample_config)
def test_new_on_repo_init(sample_config, migrant_backend):
backend = MockedBackend([])
migrant_backend.set(backend)
args = cli.parser.parse_args(["newdb", "init"])
cli.dispatch(args, sample_config)
assert backend.inits == 1
def test_new_on_new_script(sample_config, migrant_backend):
backend = MockedBackend([])
migrant_backend.set(backend)
initialize(sample_config)
args = cli.parser.parse_args(["newdb", "new", "First script"])
cli.dispatch(args, sample_config)
assert len(backend.new_scripts) == 1
assert backend.new_scripts[0].endswith("_first_script")