ardb 0.27.3 → 0.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +5 -1
- data/ardb.gemspec +3 -4
- data/lib/ardb.rb +135 -68
- data/lib/ardb/adapter/base.rb +39 -24
- data/lib/ardb/adapter/mysql.rb +1 -2
- data/lib/ardb/adapter/postgresql.rb +14 -12
- data/lib/ardb/adapter/sqlite.rb +3 -8
- data/lib/ardb/adapter_spy.rb +67 -87
- data/lib/ardb/cli.rb +11 -219
- data/lib/ardb/{clirb.rb → cli/clirb.rb} +2 -1
- data/lib/ardb/cli/commands.rb +275 -0
- data/lib/ardb/migration.rb +8 -6
- data/lib/ardb/migration_helpers.rb +1 -1
- data/lib/ardb/pg_json.rb +90 -0
- data/lib/ardb/version.rb +1 -1
- data/test/helper.rb +15 -3
- data/test/support/factory.rb +15 -0
- data/test/support/fake_schema.rb +5 -0
- data/test/support/postgresql/migrations/.gitkeep +0 -0
- data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +13 -0
- data/test/support/postgresql/schema.rb +3 -0
- data/test/support/postgresql/setup_test_db.rb +51 -0
- data/test/support/relative_require_test_db_file.rb +2 -0
- data/test/support/require_test_db_file.rb +1 -0
- data/test/system/pg_json_tests.rb +85 -0
- data/test/unit/adapter/base_tests.rb +104 -39
- data/test/unit/adapter/mysql_tests.rb +2 -1
- data/test/unit/adapter/postgresql_tests.rb +10 -9
- data/test/unit/adapter/sqlite_tests.rb +8 -3
- data/test/unit/adapter_spy_tests.rb +57 -66
- data/test/unit/ardb_tests.rb +323 -36
- data/test/unit/cli_tests.rb +193 -146
- data/test/unit/has_slug_tests.rb +9 -9
- data/test/unit/migration_helpers_tests.rb +18 -12
- data/test/unit/migration_tests.rb +18 -11
- data/test/unit/pg_json_tests.rb +39 -0
- data/test/unit/record_spy_tests.rb +1 -1
- data/test/unit/test_helpers_tests.rb +2 -6
- data/test/unit/use_db_default_tests.rb +2 -2
- metadata +29 -34
- data/lib/ardb/root_path.rb +0 -15
- data/test/unit/config_tests.rb +0 -58
data/test/unit/cli_tests.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'ardb/cli'
|
3
3
|
|
4
|
+
require 'ardb'
|
4
5
|
require 'ardb/adapter_spy'
|
5
6
|
require 'ardb/migration'
|
6
7
|
|
@@ -9,10 +10,6 @@ class Ardb::CLI
|
|
9
10
|
class UnitTests < Assert::Context
|
10
11
|
desc "Ardb::CLI"
|
11
12
|
setup do
|
12
|
-
@kernel_spy = KernelSpy.new
|
13
|
-
@stdout = IOSpy.new
|
14
|
-
@stderr = IOSpy.new
|
15
|
-
|
16
13
|
@cli_class = Ardb::CLI
|
17
14
|
end
|
18
15
|
subject{ @cli_class }
|
@@ -32,12 +29,11 @@ class Ardb::CLI
|
|
32
29
|
assert_equal 5, COMMANDS.size
|
33
30
|
|
34
31
|
assert_instance_of InvalidCommand, COMMANDS[Factory.string]
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
assert_equal GenerateMigrationCommand, COMMANDS['generate-migration']
|
32
|
+
assert_instance_of ConnectCommand, COMMANDS['connect']
|
33
|
+
assert_instance_of CreateCommand, COMMANDS['create']
|
34
|
+
assert_instance_of DropCommand, COMMANDS['drop']
|
35
|
+
assert_instance_of MigrateCommand, COMMANDS['migrate']
|
36
|
+
assert_instance_of GenerateMigrationCommand, COMMANDS['generate-migration']
|
41
37
|
end
|
42
38
|
|
43
39
|
end
|
@@ -45,6 +41,10 @@ class Ardb::CLI
|
|
45
41
|
class InitTests < UnitTests
|
46
42
|
desc "when init"
|
47
43
|
setup do
|
44
|
+
@kernel_spy = KernelSpy.new
|
45
|
+
@stdout = IOSpy.new
|
46
|
+
@stderr = IOSpy.new
|
47
|
+
|
48
48
|
@cli = @cli_class.new(@kernel_spy, @stdout, @stderr)
|
49
49
|
end
|
50
50
|
subject{ @cli }
|
@@ -59,18 +59,14 @@ class Ardb::CLI
|
|
59
59
|
@argv = [@command_name, Factory.string]
|
60
60
|
|
61
61
|
@command_class = Class.new
|
62
|
-
|
63
|
-
|
64
|
-
@
|
65
|
-
Assert.stub(@command_class, :new).with(@argv){ @command_spy }
|
66
|
-
|
67
|
-
@ardb_init_called_with = nil
|
68
|
-
Assert.stub(Ardb, :init){ |*args| @ardb_init_called_with = args }
|
62
|
+
@command_spy = CommandSpy.new
|
63
|
+
Assert.stub(@command_class, :new){ @command_spy }
|
64
|
+
COMMANDS.add(@command_class, @command_name)
|
69
65
|
|
70
66
|
@invalid_command = InvalidCommand.new(@command_name)
|
71
67
|
end
|
72
68
|
teardown do
|
73
|
-
COMMANDS.
|
69
|
+
COMMANDS.remove(@command_name)
|
74
70
|
end
|
75
71
|
|
76
72
|
end
|
@@ -85,10 +81,6 @@ class Ardb::CLI
|
|
85
81
|
assert_includes Dir.pwd, $LOAD_PATH
|
86
82
|
end
|
87
83
|
|
88
|
-
should "init Ardb without establishing a connection" do
|
89
|
-
assert_equal [false], @ardb_init_called_with
|
90
|
-
end
|
91
|
-
|
92
84
|
should "have run the command" do
|
93
85
|
assert_true @command_spy.run_called
|
94
86
|
end
|
@@ -187,7 +179,7 @@ class Ardb::CLI
|
|
187
179
|
class RunWithErrorTests < RunSetupTests
|
188
180
|
setup do
|
189
181
|
@exception = RuntimeError.new(Factory.string)
|
190
|
-
Assert.stub(@
|
182
|
+
Assert.stub(@command_spy, :run){ raise @exception }
|
191
183
|
@cli.run(@argv)
|
192
184
|
end
|
193
185
|
|
@@ -213,263 +205,305 @@ class Ardb::CLI
|
|
213
205
|
end
|
214
206
|
subject{ @cmd }
|
215
207
|
|
216
|
-
should have_readers :name, :
|
208
|
+
should have_readers :name, :clirb
|
217
209
|
should have_imeths :new, :run, :help
|
218
210
|
|
219
211
|
should "know its attrs" do
|
220
212
|
assert_equal @name, subject.name
|
221
|
-
|
222
|
-
|
223
|
-
assert_instance_of Ardb::CLIRB, subject.clirb
|
213
|
+
assert_instance_of CLIRB, subject.clirb
|
224
214
|
end
|
225
215
|
|
226
216
|
should "set its argv and return itself using `new`" do
|
227
|
-
|
228
|
-
result = subject.new(args)
|
229
|
-
assert_same subject, result
|
230
|
-
assert_equal [@name, args].flatten, subject.argv
|
217
|
+
assert_same subject, subject.new
|
231
218
|
end
|
232
219
|
|
233
|
-
should "parse its argv on run
|
234
|
-
assert_raises(
|
235
|
-
assert_raises(
|
220
|
+
should "parse its argv on run" do
|
221
|
+
assert_raises(CLIRB::HelpExit){ subject.new.run([ '--help' ]) }
|
222
|
+
assert_raises(CLIRB::VersionExit){ subject.new.run([ '--version' ]) }
|
236
223
|
end
|
237
224
|
|
238
|
-
should "raise a help exit if its
|
239
|
-
cmd = @command_class.new([nil, ''].
|
240
|
-
|
225
|
+
should "raise a help exit if its name is empty" do
|
226
|
+
cmd = @command_class.new([nil, ''].sample)
|
227
|
+
argv = [Factory.string, Factory.string]
|
228
|
+
assert_raises(CLIRB::HelpExit){ cmd.new.run(argv) }
|
241
229
|
end
|
242
230
|
|
243
231
|
should "raise an invalid command error when run" do
|
244
|
-
assert_raises(InvalidCommandError){ subject.new([Factory.string])
|
232
|
+
assert_raises(InvalidCommandError){ subject.new.run([Factory.string]) }
|
245
233
|
end
|
246
234
|
|
247
235
|
should "know its help" do
|
248
236
|
exp = "Usage: ardb [COMMAND] [options]\n\n" \
|
249
|
-
"
|
250
|
-
"
|
237
|
+
"Options: #{subject.clirb}\n" \
|
238
|
+
"Commands:\n" \
|
239
|
+
"#{COMMANDS.to_s.split("\n").map{ |l| " #{l}" }.join("\n")}\n"
|
251
240
|
assert_equal exp, subject.help
|
252
241
|
end
|
253
242
|
|
254
243
|
end
|
255
244
|
|
256
|
-
class
|
257
|
-
desc "ConnectCommand"
|
245
|
+
class CommandSetupTests < UnitTests
|
258
246
|
setup do
|
259
|
-
@
|
260
|
-
Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
|
247
|
+
@stdout, @stderr = IOSpy.new, IOSpy.new
|
261
248
|
|
262
|
-
@
|
263
|
-
Assert.stub(Ardb, :init){ @
|
249
|
+
@ardb_init_called_with = nil
|
250
|
+
Assert.stub(Ardb, :init){ |*args| @ardb_init_called_with = args }
|
264
251
|
|
265
|
-
@
|
266
|
-
|
252
|
+
@adapter_spy = Ardb::AdapterSpy.new
|
253
|
+
Assert.stub(Ardb, :adapter){ @adapter_spy }
|
267
254
|
end
|
268
255
|
subject{ @cmd }
|
269
256
|
|
270
|
-
|
257
|
+
end
|
258
|
+
|
259
|
+
class ValidCommandTests < CommandSetupTests
|
260
|
+
desc "ValidCommand"
|
261
|
+
setup do
|
262
|
+
@command_class = Class.new{ include ValidCommand }
|
263
|
+
@cmd = @command_class.new
|
264
|
+
end
|
265
|
+
|
266
|
+
should have_imeths :clirb, :run, :summary
|
271
267
|
|
272
268
|
should "know its CLI.RB" do
|
273
|
-
assert_instance_of
|
269
|
+
assert_instance_of CLIRB, subject.clirb
|
270
|
+
end
|
271
|
+
|
272
|
+
should "parse its args when run" do
|
273
|
+
argv = Factory.integer(3).times.map{ Factory.string }
|
274
|
+
subject.run(argv, @stdout, @stderr)
|
275
|
+
assert_equal argv, subject.clirb.args
|
276
|
+
end
|
277
|
+
|
278
|
+
should "take custom CLIRB build procs" do
|
279
|
+
cmd = @command_class.new do
|
280
|
+
option 'test', 'testing', :abbrev => 't'
|
281
|
+
end
|
282
|
+
cmd.run(['-t'], @stdout, @stderr)
|
283
|
+
assert_true cmd.clirb.opts['test']
|
284
|
+
end
|
285
|
+
|
286
|
+
should "default its summary" do
|
287
|
+
assert_equal '', subject.summary
|
288
|
+
end
|
289
|
+
|
290
|
+
end
|
291
|
+
|
292
|
+
class ConnectCommandTests < CommandSetupTests
|
293
|
+
desc "ConnectCommand"
|
294
|
+
setup do
|
295
|
+
@command_class = ConnectCommand
|
296
|
+
@cmd = @command_class.new
|
297
|
+
end
|
298
|
+
|
299
|
+
should "be a valid command" do
|
300
|
+
assert_kind_of ValidCommand, subject
|
301
|
+
end
|
302
|
+
|
303
|
+
should "know its summary" do
|
304
|
+
exp = "Connect to the configured DB"
|
305
|
+
assert_equal exp, subject.summary
|
274
306
|
end
|
275
307
|
|
276
308
|
should "know its help" do
|
277
309
|
exp = "Usage: ardb connect [options]\n\n" \
|
278
|
-
"Options: #{subject.clirb}"
|
310
|
+
"Options: #{subject.clirb}\n" \
|
311
|
+
"Description:\n" \
|
312
|
+
" #{subject.summary}"
|
279
313
|
assert_equal exp, subject.help
|
280
314
|
end
|
281
315
|
|
282
|
-
should "
|
283
|
-
subject.run
|
284
|
-
|
285
|
-
assert_equal [], subject.clirb.args
|
316
|
+
should "init ardb and connect to the db when run" do
|
317
|
+
subject.run([], @stdout, @stderr)
|
286
318
|
|
287
|
-
|
319
|
+
assert_equal [false], @ardb_init_called_with
|
288
320
|
assert_true @adapter_spy.connect_db_called?
|
289
321
|
|
290
|
-
exp = "connected to #{Ardb.config.
|
322
|
+
exp = "connected to #{Ardb.config.adapter} db `#{Ardb.config.database}`\n"
|
291
323
|
assert_equal exp, @stdout.read
|
292
324
|
end
|
293
325
|
|
294
|
-
should "output any errors and raise an exit error
|
326
|
+
should "output any errors and raise an exit error when run" do
|
295
327
|
err = StandardError.new(Factory.string)
|
296
328
|
err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
|
297
|
-
Assert.stub(
|
329
|
+
Assert.stub(@adapter_spy, :connect_db){ raise err }
|
298
330
|
|
299
|
-
assert_raises(CommandExitError){ subject.run }
|
331
|
+
assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
|
300
332
|
err_output = @stderr.read
|
301
333
|
|
302
334
|
assert_includes err.to_s, err_output
|
303
335
|
assert_includes err.backtrace.join("\n"), err_output
|
304
336
|
|
305
|
-
exp = "error connecting to #{Ardb.config.
|
306
|
-
"with #{Ardb.config.
|
337
|
+
exp = "error connecting to #{Ardb.config.database.inspect} database " \
|
338
|
+
"with #{Ardb.config.activerecord_connect_hash.inspect}"
|
307
339
|
assert_includes exp, err_output
|
308
340
|
end
|
309
341
|
|
310
342
|
end
|
311
343
|
|
312
|
-
class CreateCommandTests <
|
344
|
+
class CreateCommandTests < CommandSetupTests
|
313
345
|
desc "CreateCommand"
|
314
346
|
setup do
|
315
|
-
@adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
|
316
|
-
Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
|
317
|
-
|
318
347
|
@command_class = CreateCommand
|
319
|
-
@cmd = @command_class.new
|
348
|
+
@cmd = @command_class.new
|
320
349
|
end
|
321
|
-
subject{ @cmd }
|
322
350
|
|
323
|
-
should
|
351
|
+
should "be a valid command" do
|
352
|
+
assert_kind_of ValidCommand, subject
|
353
|
+
end
|
324
354
|
|
325
|
-
should "know its
|
326
|
-
|
355
|
+
should "know its summary" do
|
356
|
+
exp = "Create the configured DB"
|
357
|
+
assert_equal exp, subject.summary
|
327
358
|
end
|
328
359
|
|
329
360
|
should "know its help" do
|
330
361
|
exp = "Usage: ardb create [options]\n\n" \
|
331
|
-
"Options: #{subject.clirb}"
|
362
|
+
"Options: #{subject.clirb}\n" \
|
363
|
+
"Description:\n" \
|
364
|
+
" #{subject.summary}"
|
332
365
|
assert_equal exp, subject.help
|
333
366
|
end
|
334
367
|
|
335
|
-
should "
|
336
|
-
subject.run
|
368
|
+
should "init ardb and create the db when run" do
|
369
|
+
subject.run([], @stdout, @stderr)
|
337
370
|
|
338
|
-
assert_equal [],
|
371
|
+
assert_equal [false], @ardb_init_called_with
|
339
372
|
assert_true @adapter_spy.create_db_called?
|
340
373
|
|
341
|
-
exp = "created #{Ardb.config.
|
374
|
+
exp = "created #{Ardb.config.adapter} db `#{Ardb.config.database}`\n"
|
342
375
|
assert_equal exp, @stdout.read
|
343
376
|
end
|
344
377
|
|
345
|
-
should "output any errors and raise an exit error
|
378
|
+
should "output any errors and raise an exit error when run" do
|
346
379
|
err = StandardError.new(Factory.string)
|
347
380
|
Assert.stub(@adapter_spy, :create_db){ raise err }
|
348
381
|
|
349
|
-
assert_raises(CommandExitError){ subject.run }
|
382
|
+
assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
|
350
383
|
err_output = @stderr.read
|
351
384
|
|
352
385
|
assert_includes err.to_s, err_output
|
353
|
-
exp = "error creating #{Ardb.config.
|
386
|
+
exp = "error creating #{Ardb.config.database.inspect} database"
|
354
387
|
assert_includes exp, err_output
|
355
388
|
end
|
356
389
|
|
357
390
|
end
|
358
391
|
|
359
|
-
class DropCommandTests <
|
392
|
+
class DropCommandTests < CommandSetupTests
|
360
393
|
desc "DropCommand"
|
361
394
|
setup do
|
362
|
-
@adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
|
363
|
-
Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
|
364
|
-
|
365
395
|
@command_class = DropCommand
|
366
|
-
@cmd = @command_class.new
|
396
|
+
@cmd = @command_class.new
|
367
397
|
end
|
368
|
-
subject{ @cmd }
|
369
398
|
|
370
|
-
should
|
399
|
+
should "be a valid command" do
|
400
|
+
assert_kind_of ValidCommand, subject
|
401
|
+
end
|
371
402
|
|
372
|
-
should "know its
|
373
|
-
|
403
|
+
should "know its summary" do
|
404
|
+
exp = "Drop the configured DB"
|
405
|
+
assert_equal exp, subject.summary
|
374
406
|
end
|
375
407
|
|
376
408
|
should "know its help" do
|
377
409
|
exp = "Usage: ardb drop [options]\n\n" \
|
378
|
-
"Options: #{subject.clirb}"
|
410
|
+
"Options: #{subject.clirb}\n" \
|
411
|
+
"Description:\n" \
|
412
|
+
" #{subject.summary}"
|
379
413
|
assert_equal exp, subject.help
|
380
414
|
end
|
381
415
|
|
382
|
-
should "
|
383
|
-
subject.run
|
416
|
+
should "init ardb and drop the db when run" do
|
417
|
+
subject.run([], @stdout, @stderr)
|
384
418
|
|
385
|
-
assert_equal [],
|
419
|
+
assert_equal [true], @ardb_init_called_with
|
386
420
|
assert_true @adapter_spy.drop_db_called?
|
387
421
|
|
388
|
-
exp = "dropped #{Ardb.config.
|
422
|
+
exp = "dropped #{Ardb.config.adapter} db `#{Ardb.config.database}`\n"
|
389
423
|
assert_equal exp, @stdout.read
|
390
424
|
end
|
391
425
|
|
392
|
-
should "output any errors and raise an exit error
|
426
|
+
should "output any errors and raise an exit error when run" do
|
393
427
|
err = StandardError.new(Factory.string)
|
394
428
|
Assert.stub(@adapter_spy, :drop_db){ raise err }
|
395
429
|
|
396
|
-
assert_raises(CommandExitError){ subject.run }
|
430
|
+
assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
|
397
431
|
err_output = @stderr.read
|
398
432
|
|
399
433
|
assert_includes err.to_s, err_output
|
400
|
-
exp = "error dropping #{Ardb.config.
|
434
|
+
exp = "error dropping #{Ardb.config.database.inspect} database"
|
401
435
|
assert_includes exp, err_output
|
402
436
|
end
|
403
437
|
|
404
438
|
end
|
405
439
|
|
406
|
-
class MigrateCommandTests <
|
440
|
+
class MigrateCommandTests < CommandSetupTests
|
407
441
|
desc "MigrateCommand"
|
408
442
|
setup do
|
409
|
-
@
|
410
|
-
Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
|
411
|
-
|
412
|
-
@ardb_init_called = false
|
413
|
-
Assert.stub(Ardb, :init){ @ardb_init_called = true }
|
414
|
-
|
443
|
+
@orig_env_var_migrate_no_schema = ENV['ARDB_MIGRATE_NO_SCHEMA']
|
415
444
|
@command_class = MigrateCommand
|
416
|
-
@cmd = @command_class.new
|
445
|
+
@cmd = @command_class.new
|
446
|
+
end
|
447
|
+
teardown do
|
448
|
+
ENV['ARDB_MIGRATE_NO_SCHEMA'] = @orig_env_var_migrate_no_schema
|
417
449
|
end
|
418
|
-
subject{ @cmd }
|
419
450
|
|
420
|
-
should
|
451
|
+
should "be a valid command" do
|
452
|
+
assert_kind_of ValidCommand, subject
|
453
|
+
end
|
421
454
|
|
422
|
-
should "know its
|
423
|
-
|
455
|
+
should "know its summary" do
|
456
|
+
exp = "Migrate the configured DB"
|
457
|
+
assert_equal exp, subject.summary
|
424
458
|
end
|
425
459
|
|
426
460
|
should "know its help" do
|
427
461
|
exp = "Usage: ardb migrate [options]\n\n" \
|
428
|
-
"Options: #{subject.clirb}"
|
462
|
+
"Options: #{subject.clirb}\n" \
|
463
|
+
"Description:\n" \
|
464
|
+
" #{subject.summary}"
|
429
465
|
assert_equal exp, subject.help
|
430
466
|
end
|
431
467
|
|
432
|
-
should "
|
433
|
-
subject.run
|
434
|
-
|
435
|
-
assert_equal [], subject.clirb.args
|
468
|
+
should "init ardb, migrate the db and dump the schema when run" do
|
469
|
+
subject.run([], @stdout, @stderr)
|
436
470
|
|
437
|
-
|
471
|
+
assert_equal [true], @ardb_init_called_with
|
438
472
|
assert_true @adapter_spy.migrate_db_called?
|
439
473
|
assert_true @adapter_spy.dump_schema_called?
|
440
474
|
end
|
441
475
|
|
442
|
-
should "init ardb and
|
443
|
-
current_no_schema = ENV['ARDB_MIGRATE_NO_SCHEMA']
|
476
|
+
should "only init ardb and migrate when run with no schema dump env var set" do
|
444
477
|
ENV['ARDB_MIGRATE_NO_SCHEMA'] = 'yes'
|
445
|
-
subject.run
|
446
|
-
ENV['ARDB_MIGRATE_NO_SCHEMA'] = current_no_schema
|
478
|
+
subject.run([], @stdout, @stderr)
|
447
479
|
|
448
|
-
|
480
|
+
assert_equal [true], @ardb_init_called_with
|
449
481
|
assert_true @adapter_spy.migrate_db_called?
|
450
482
|
assert_false @adapter_spy.dump_schema_called?
|
451
483
|
end
|
452
484
|
|
453
|
-
should "output any errors and raise an exit error
|
485
|
+
should "output any errors and raise an exit error when run" do
|
454
486
|
err = StandardError.new(Factory.string)
|
455
487
|
err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
|
456
|
-
Assert.stub(
|
488
|
+
Assert.stub(@adapter_spy, :migrate_db){ raise err }
|
457
489
|
|
458
|
-
assert_raises(CommandExitError){ subject.run }
|
490
|
+
assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
|
459
491
|
err_output = @stderr.read
|
460
492
|
|
461
493
|
assert_includes err.to_s, err_output
|
462
494
|
assert_includes err.backtrace.join("\n"), err_output
|
463
495
|
|
464
|
-
exp = "error migrating #{Ardb.config.
|
496
|
+
exp = "error migrating #{Ardb.config.database.inspect} database"
|
465
497
|
assert_includes exp, err_output
|
466
498
|
end
|
467
499
|
|
468
500
|
end
|
469
501
|
|
470
|
-
class GenerateMigrationCommandTests <
|
502
|
+
class GenerateMigrationCommandTests < CommandSetupTests
|
471
503
|
desc "GenerateMigrationCommand"
|
472
504
|
setup do
|
505
|
+
@identifier = Factory.migration_id
|
506
|
+
|
473
507
|
@migration_spy = nil
|
474
508
|
@migration_class = Ardb::Migration
|
475
509
|
Assert.stub(@migration_class, :new) do |*args|
|
@@ -477,28 +511,32 @@ class Ardb::CLI
|
|
477
511
|
end
|
478
512
|
|
479
513
|
@command_class = GenerateMigrationCommand
|
480
|
-
@
|
481
|
-
@cmd = @command_class.new([@identifier], @stdout, @stderr)
|
514
|
+
@cmd = @command_class.new
|
482
515
|
end
|
483
|
-
subject{ @cmd }
|
484
516
|
|
485
|
-
should
|
517
|
+
should "be a valid command" do
|
518
|
+
assert_kind_of ValidCommand, subject
|
519
|
+
end
|
486
520
|
|
487
|
-
should "know its
|
488
|
-
|
521
|
+
should "know its summary" do
|
522
|
+
exp = "Generate a migration file given a MIGRATION-NAME"
|
523
|
+
assert_equal exp, subject.summary
|
489
524
|
end
|
490
525
|
|
491
526
|
should "know its help" do
|
492
527
|
exp = "Usage: ardb generate-migration [options] MIGRATION-NAME\n\n" \
|
493
|
-
"Options: #{subject.clirb}"
|
528
|
+
"Options: #{subject.clirb}\n" \
|
529
|
+
"Description:\n" \
|
530
|
+
" #{subject.summary}"
|
494
531
|
assert_equal exp, subject.help
|
495
532
|
end
|
496
533
|
|
497
|
-
should "
|
498
|
-
subject.run
|
534
|
+
should "init ardb and save a migration for the identifier when run" do
|
535
|
+
subject.run([@identifier], @stdout, @stderr)
|
499
536
|
|
500
|
-
assert_equal [
|
501
|
-
assert_equal
|
537
|
+
assert_equal [false], @ardb_init_called_with
|
538
|
+
assert_equal Ardb.config, @migration_spy.ardb_config
|
539
|
+
assert_equal @identifier, @migration_spy.identifier
|
502
540
|
assert_true @migration_spy.save_called
|
503
541
|
|
504
542
|
exp = "generated #{@migration_spy.file_path}\n"
|
@@ -506,11 +544,11 @@ class Ardb::CLI
|
|
506
544
|
end
|
507
545
|
|
508
546
|
should "re-raise a specific argument error on migration 'no identifer' errors" do
|
509
|
-
Assert.stub(@migration_class, :new)
|
547
|
+
Assert.stub(@migration_class, :new){ raise Ardb::Migration::NoIdentifierError }
|
510
548
|
err = nil
|
511
549
|
begin
|
512
|
-
cmd = @command_class.new
|
513
|
-
cmd.run
|
550
|
+
cmd = @command_class.new
|
551
|
+
cmd.run([])
|
514
552
|
rescue ArgumentError => err
|
515
553
|
end
|
516
554
|
|
@@ -520,12 +558,12 @@ class Ardb::CLI
|
|
520
558
|
assert_not_empty err.backtrace
|
521
559
|
end
|
522
560
|
|
523
|
-
should "output any errors and raise an exit error
|
561
|
+
should "output any errors and raise an exit error when run" do
|
524
562
|
err = StandardError.new(Factory.string)
|
525
563
|
err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
|
526
564
|
Assert.stub(@migration_class, :new){ raise err }
|
527
565
|
|
528
|
-
assert_raises(CommandExitError){ subject.run }
|
566
|
+
assert_raises(CommandExitError){ subject.run([@identifier], @stdout, @stderr) }
|
529
567
|
err_output = @stderr.read
|
530
568
|
|
531
569
|
assert_includes err.to_s, err_output
|
@@ -550,18 +588,26 @@ class Ardb::CLI
|
|
550
588
|
end
|
551
589
|
|
552
590
|
class CommandSpy
|
553
|
-
attr_reader :run_called
|
591
|
+
attr_reader :argv, :stdout, :stderr, :run_called
|
554
592
|
|
555
593
|
def initialize
|
594
|
+
@argv = nil
|
595
|
+
@stdout, @stderr = nil, nil
|
556
596
|
@run_called = false
|
557
597
|
end
|
558
598
|
|
559
|
-
def run
|
599
|
+
def run(argv, stdout = nil, stderr = nil)
|
600
|
+
@argv = argv
|
601
|
+
@stdout, @stderr = stdout, stderr
|
560
602
|
@run_called = true
|
561
603
|
end
|
562
604
|
|
605
|
+
def summary
|
606
|
+
@summary ||= Factory.string
|
607
|
+
end
|
608
|
+
|
563
609
|
def help
|
564
|
-
Factory.text
|
610
|
+
@help ||= Factory.text
|
565
611
|
end
|
566
612
|
end
|
567
613
|
|
@@ -593,10 +639,11 @@ class Ardb::CLI
|
|
593
639
|
end
|
594
640
|
|
595
641
|
class MigrationSpy
|
596
|
-
attr_reader :identifier, :file_path, :save_called
|
642
|
+
attr_reader :ardb_config, :identifier, :file_path, :save_called
|
597
643
|
|
598
|
-
def initialize(
|
599
|
-
@
|
644
|
+
def initialize(ardb_config, identifier)
|
645
|
+
@ardb_config = ardb_config
|
646
|
+
@identifier = identifier
|
600
647
|
@file_path = Factory.path
|
601
648
|
@save_called = false
|
602
649
|
end
|