ardb 0.27.3 → 0.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -7
  2. data/Gemfile +4 -5
  3. data/README.md +252 -3
  4. data/ardb.gemspec +8 -8
  5. data/bin/ardb +1 -1
  6. data/lib/ardb.rb +155 -72
  7. data/lib/ardb/adapter/base.rb +67 -47
  8. data/lib/ardb/adapter/mysql.rb +3 -19
  9. data/lib/ardb/adapter/postgresql.rb +33 -37
  10. data/lib/ardb/adapter/sqlite.rb +7 -16
  11. data/lib/ardb/adapter_spy.rb +58 -92
  12. data/lib/ardb/cli.rb +18 -226
  13. data/lib/ardb/{clirb.rb → cli/clirb.rb} +16 -18
  14. data/lib/ardb/cli/commands.rb +365 -0
  15. data/lib/ardb/db_tests.rb +2 -4
  16. data/lib/ardb/default_order_by.rb +3 -13
  17. data/lib/ardb/migration.rb +18 -20
  18. data/lib/ardb/record_spy.rb +7 -26
  19. data/lib/ardb/relation_spy.rb +0 -6
  20. data/lib/ardb/require_autoloaded_active_record_files.rb +103 -58
  21. data/lib/ardb/test_helpers.rb +2 -5
  22. data/lib/ardb/use_db_default.rb +4 -15
  23. data/lib/ardb/version.rb +1 -1
  24. data/script/determine_autoloaded_active_record_files.rb +11 -8
  25. data/test/helper.rb +9 -6
  26. data/test/support/factory.rb +17 -2
  27. data/test/support/fake_schema.rb +5 -0
  28. data/test/support/postgresql/migrations/.keep +0 -0
  29. data/test/support/postgresql/schema.rb +2 -0
  30. data/test/support/postgresql/setup_test_db.rb +51 -0
  31. data/test/support/relative_require_test_db_file.rb +2 -0
  32. data/test/support/require_test_db_file.rb +1 -0
  33. data/test/system/.keep +0 -0
  34. data/test/unit/adapter/base_tests.rb +163 -75
  35. data/test/unit/adapter/mysql_tests.rb +4 -20
  36. data/test/unit/adapter/postgresql_tests.rb +20 -28
  37. data/test/unit/adapter/sqlite_tests.rb +9 -12
  38. data/test/unit/adapter_spy_tests.rb +48 -71
  39. data/test/unit/ardb_tests.rb +338 -38
  40. data/test/unit/cli_tests.rb +334 -225
  41. data/test/unit/db_tests_tests.rb +3 -6
  42. data/test/unit/default_order_by_tests.rb +4 -8
  43. data/test/unit/migration_tests.rb +20 -17
  44. data/test/unit/record_spy_tests.rb +18 -23
  45. data/test/unit/relation_spy_tests.rb +17 -46
  46. data/test/unit/test_helpers_tests.rb +5 -20
  47. data/test/unit/use_db_default_tests.rb +9 -13
  48. metadata +111 -100
  49. data/lib/ardb/has_slug.rb +0 -107
  50. data/lib/ardb/migration_helpers.rb +0 -77
  51. data/lib/ardb/root_path.rb +0 -15
  52. data/test/unit/config_tests.rb +0 -58
  53. data/test/unit/has_slug_tests.rb +0 -341
  54. data/test/unit/migration_helpers_tests.rb +0 -59
@@ -1,18 +1,14 @@
1
- require 'assert'
2
- require 'ardb/cli'
1
+ require "assert"
2
+ require "ardb/cli"
3
3
 
4
- require 'ardb/adapter_spy'
5
- require 'ardb/migration'
4
+ require "ardb"
5
+ require "ardb/adapter_spy"
6
+ require "ardb/migration"
6
7
 
7
8
  class Ardb::CLI
8
-
9
9
  class UnitTests < Assert::Context
10
10
  desc "Ardb::CLI"
11
11
  setup do
12
- @kernel_spy = KernelSpy.new
13
- @stdout = IOSpy.new
14
- @stderr = IOSpy.new
15
-
16
12
  @cli_class = Ardb::CLI
17
13
  end
18
14
  subject{ @cli_class }
@@ -29,50 +25,52 @@ class Ardb::CLI
29
25
  end
30
26
 
31
27
  should "know its commands" do
32
- assert_equal 5, COMMANDS.size
33
-
34
- assert_instance_of InvalidCommand, COMMANDS[Factory.string]
35
-
36
- assert_equal ConnectCommand, COMMANDS['connect']
37
- assert_equal CreateCommand, COMMANDS['create']
38
- assert_equal DropCommand, COMMANDS['drop']
39
- assert_equal MigrateCommand, COMMANDS['migrate']
40
- assert_equal GenerateMigrationCommand, COMMANDS['generate-migration']
28
+ assert_equal 9, COMMANDS.size
29
+
30
+ assert_instance_of InvalidCommand, COMMANDS[Factory.string]
31
+ assert_instance_of ConnectCommand, COMMANDS["connect"]
32
+ assert_instance_of CreateCommand, COMMANDS["create"]
33
+ assert_instance_of DropCommand, COMMANDS["drop"]
34
+ assert_instance_of GenerateMigrationCommand, COMMANDS["generate-migration"]
35
+ assert_instance_of MigrateCommand, COMMANDS["migrate"]
36
+ assert_instance_of MigrateUpCommand, COMMANDS["migrate-up"]
37
+ assert_instance_of MigrateDownCommand, COMMANDS["migrate-down"]
38
+ assert_instance_of MigrateForwardCommand, COMMANDS["migrate-forward"]
39
+ assert_instance_of MigrateBackwardCommand, COMMANDS["migrate-backward"]
41
40
  end
42
-
43
41
  end
44
42
 
45
43
  class InitTests < UnitTests
46
44
  desc "when init"
47
45
  setup do
46
+ @kernel_spy = KernelSpy.new
47
+ @stdout = IOSpy.new
48
+ @stderr = IOSpy.new
49
+
48
50
  @cli = @cli_class.new(@kernel_spy, @stdout, @stderr)
49
51
  end
50
52
  subject{ @cli }
51
53
 
52
54
  should have_imeths :run
53
-
54
55
  end
55
56
 
56
57
  class RunSetupTests < InitTests
57
58
  setup do
58
59
  @command_name = Factory.string
59
- @argv = [@command_name, Factory.string]
60
-
61
- @command_class = Class.new
62
- COMMANDS[@command_name] = @command_class
60
+ @command_class = Class.new{ include ValidCommand }
61
+ Assert.stub(@command_class, :command_name) { @command_name }
63
62
 
64
- @command_spy = CommandSpy.new
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 }
63
+ @command_spy = CommandSpy.new(@command_name)
64
+ Assert.stub(@command_class, :new){ @command_spy }
65
+ COMMANDS.add(@command_class)
69
66
 
70
67
  @invalid_command = InvalidCommand.new(@command_name)
68
+
69
+ @argv = [@command_name, Factory.string]
71
70
  end
72
71
  teardown do
73
- COMMANDS.delete(@command_name)
72
+ COMMANDS.remove(@command_class)
74
73
  end
75
-
76
74
  end
77
75
 
78
76
  class RunTests < RunSetupTests
@@ -85,10 +83,6 @@ class Ardb::CLI
85
83
  assert_includes Dir.pwd, $LOAD_PATH
86
84
  end
87
85
 
88
- should "init Ardb without establishing a connection" do
89
- assert_equal [false], @ardb_init_called_with
90
- end
91
-
92
86
  should "have run the command" do
93
87
  assert_true @command_spy.run_called
94
88
  end
@@ -96,7 +90,6 @@ class Ardb::CLI
96
90
  should "have successfully exited" do
97
91
  assert_equal 0, @kernel_spy.exit_status
98
92
  end
99
-
100
93
  end
101
94
 
102
95
  class RunWithNoArgsTests < RunSetupTests
@@ -106,14 +99,13 @@ class Ardb::CLI
106
99
  end
107
100
 
108
101
  should "output the invalid command's help" do
109
- assert_equal @invalid_command.help, @stdout.read
102
+ assert_equal @invalid_command.command_help, @stdout.read
110
103
  assert_empty @stderr.read
111
104
  end
112
105
 
113
106
  should "have successfully exited" do
114
107
  assert_equal 0, @kernel_spy.exit_status
115
108
  end
116
-
117
109
  end
118
110
 
119
111
  class RunWithInvalidCommandTests < RunSetupTests
@@ -125,15 +117,14 @@ class Ardb::CLI
125
117
  end
126
118
 
127
119
  should "output that it is invalid and output the invalid command's help" do
128
- exp = "'#{@name}' is not a command.\n\n"
120
+ exp = "\"#{@name}\" is not a command.\n\n"
129
121
  assert_equal exp, @stderr.read
130
- assert_equal @invalid_command.help, @stdout.read
122
+ assert_equal @invalid_command.command_help, @stdout.read
131
123
  end
132
124
 
133
125
  should "have unsuccessfully exited" do
134
126
  assert_equal 1, @kernel_spy.exit_status
135
127
  end
136
-
137
128
  end
138
129
 
139
130
  class RunWithCommandExitErrorTests < RunSetupTests
@@ -147,30 +138,28 @@ class Ardb::CLI
147
138
  assert_equal 1, @kernel_spy.exit_status
148
139
  assert_empty @stderr.read
149
140
  end
150
-
151
141
  end
152
142
 
153
143
  class RunWithHelpTests < RunSetupTests
154
144
  desc "and run with the help switch"
155
145
  setup do
156
- @cli.run([ '--help' ])
146
+ @cli.run([ "--help" ])
157
147
  end
158
148
 
159
149
  should "output the invalid command's help" do
160
- assert_equal @invalid_command.help, @stdout.read
150
+ assert_equal @invalid_command.command_help, @stdout.read
161
151
  assert_empty @stderr.read
162
152
  end
163
153
 
164
154
  should "have successfully exited" do
165
155
  assert_equal 0, @kernel_spy.exit_status
166
156
  end
167
-
168
157
  end
169
158
 
170
159
  class RunWithVersionTests < RunSetupTests
171
160
  desc "and run with the version switch"
172
161
  setup do
173
- @cli.run([ '--version' ])
162
+ @cli.run([ "--version" ])
174
163
  end
175
164
 
176
165
  should "output its version" do
@@ -181,13 +170,12 @@ class Ardb::CLI
181
170
  should "have successfully exited" do
182
171
  assert_equal 0, @kernel_spy.exit_status
183
172
  end
184
-
185
173
  end
186
174
 
187
175
  class RunWithErrorTests < RunSetupTests
188
176
  setup do
189
177
  @exception = RuntimeError.new(Factory.string)
190
- Assert.stub(@command_class, :new).with(@argv){ raise @exception }
178
+ Assert.stub(@command_spy, :run){ raise @exception }
191
179
  @cli.run(@argv)
192
180
  end
193
181
 
@@ -201,7 +189,6 @@ class Ardb::CLI
201
189
  should "have unsuccessfully exited" do
202
190
  assert_equal 1, @kernel_spy.exit_status
203
191
  end
204
-
205
192
  end
206
193
 
207
194
  class InvalidCommandTests < UnitTests
@@ -213,328 +200,441 @@ class Ardb::CLI
213
200
  end
214
201
  subject{ @cmd }
215
202
 
216
- should have_readers :name, :argv, :clirb
217
- should have_imeths :new, :run, :help
203
+ should have_readers :name, :clirb
204
+ should have_imeths :new, :run, :command_help
218
205
 
219
206
  should "know its attrs" do
220
207
  assert_equal @name, subject.name
221
- assert_equal [], subject.argv
222
-
223
- assert_instance_of Ardb::CLIRB, subject.clirb
208
+ assert_instance_of CLIRB, subject.clirb
224
209
  end
225
210
 
226
211
  should "set its argv and return itself using `new`" do
227
- args = [Factory.string, Factory.string]
228
- result = subject.new(args)
229
- assert_same subject, result
230
- assert_equal [@name, args].flatten, subject.argv
212
+ assert_same subject, subject.new
231
213
  end
232
214
 
233
- should "parse its argv on run`" do
234
- assert_raises(Ardb::CLIRB::HelpExit){ subject.new(['--help']).run }
235
- assert_raises(Ardb::CLIRB::VersionExit){ subject.new(['--version']).run }
215
+ should "parse its argv on run" do
216
+ assert_raises(CLIRB::HelpExit){ subject.new.run([ "--help" ]) }
217
+ assert_raises(CLIRB::VersionExit){ subject.new.run([ "--version" ]) }
236
218
  end
237
219
 
238
- should "raise a help exit if its argv is empty" do
239
- cmd = @command_class.new([nil, ''].choice)
240
- assert_raises(Ardb::CLIRB::HelpExit){ cmd.new([]).run }
220
+ should "raise a help exit if its name is empty" do
221
+ cmd = @command_class.new([nil, ""].sample)
222
+ argv = [Factory.string, Factory.string]
223
+ assert_raises(CLIRB::HelpExit){ cmd.new.run(argv) }
241
224
  end
242
225
 
243
226
  should "raise an invalid command error when run" do
244
- assert_raises(InvalidCommandError){ subject.new([Factory.string]).run }
227
+ assert_raises(InvalidCommandError){ subject.new.run([Factory.string]) }
245
228
  end
246
229
 
247
230
  should "know its help" do
248
231
  exp = "Usage: ardb [COMMAND] [options]\n\n" \
249
- "Commands: #{COMMANDS.keys.sort.join(', ')}\n" \
250
- "Options: #{subject.clirb}"
251
- assert_equal exp, subject.help
232
+ "Options: #{subject.clirb}\n" \
233
+ "Commands:\n" \
234
+ "#{COMMANDS.to_s.split("\n").map{ |l| " #{l}" }.join("\n")}\n"
235
+ assert_equal exp, subject.command_help
252
236
  end
253
-
254
237
  end
255
238
 
256
- class ConnectCommandTests < UnitTests
257
- desc "ConnectCommand"
239
+ class CommandSetupTests < UnitTests
258
240
  setup do
259
- @adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
260
- Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
241
+ @stdout, @stderr = IOSpy.new, IOSpy.new
261
242
 
262
- @ardb_init_called = false
263
- Assert.stub(Ardb, :init){ @ardb_init_called = true }
243
+ @ardb_init_called_with = nil
244
+ Assert.stub(Ardb, :init){ |*args| @ardb_init_called_with = args }
264
245
 
265
- @command_class = ConnectCommand
266
- @cmd = @command_class.new([], @stdout, @stderr)
246
+ @adapter_spy = Ardb::AdapterSpy.new
247
+ Assert.stub(Ardb, :adapter){ @adapter_spy }
267
248
  end
268
249
  subject{ @cmd }
250
+ end
269
251
 
270
- should have_readers :clirb
252
+ class ValidCommandTests < CommandSetupTests
253
+ desc "ValidCommand"
254
+ setup do
255
+ @command_class = Class.new{ include ValidCommand }
256
+ @cmd = @command_class.new
257
+ end
258
+
259
+ should have_cmeths :command_name, :command_summary
260
+
261
+ should have_imeths :clirb, :run
262
+ should have_imeths :command_name, :command_summary, :command_help
271
263
 
272
264
  should "know its CLI.RB" do
273
- assert_instance_of Ardb::CLIRB, subject.clirb
265
+ assert_instance_of CLIRB, subject.clirb
274
266
  end
275
267
 
276
- should "know its help" do
277
- exp = "Usage: ardb connect [options]\n\n" \
278
- "Options: #{subject.clirb}"
279
- assert_equal exp, subject.help
268
+ should "parse its args when run" do
269
+ argv = Factory.integer(3).times.map{ Factory.string }
270
+ subject.run(argv, @stdout, @stderr)
271
+ assert_equal argv, subject.clirb.args
272
+ end
273
+
274
+ should "take custom CLIRB build procs" do
275
+ cmd = @command_class.new do
276
+ option "test", "testing", :abbrev => "t"
277
+ end
278
+ cmd.run(["-t"], @stdout, @stderr)
279
+ assert_true cmd.clirb.opts["test"]
280
+ end
281
+
282
+ should "not implement its command name" do
283
+ assert_raises NotImplementedError do
284
+ subject.command_name
285
+ end
280
286
  end
281
287
 
282
- should "parse its args, init ardb and connect to the db on run" do
283
- subject.run
288
+ should "default its command summary" do
289
+ assert_equal "", subject.command_summary
290
+ end
291
+
292
+ should "know its command help" do
293
+ Assert.stub(subject, :command_name) { "some-command" }
294
+ Assert.stub(subject, :command_summary) { "some-summary" }
295
+
296
+ exp = "Usage: ardb #{subject.command_name} [options]\n\n" \
297
+ "Options: #{subject.clirb}\n" \
298
+ "Description:\n" \
299
+ " #{subject.command_summary}"
300
+ assert_equal exp, subject.command_help
301
+ end
302
+ end
303
+
304
+ class ConnectCommandTests < CommandSetupTests
305
+ desc "ConnectCommand"
306
+ setup do
307
+ @command_class = ConnectCommand
308
+ @cmd = @command_class.new
309
+ end
310
+
311
+ should "be a valid command" do
312
+ assert_kind_of ValidCommand, subject
313
+ end
284
314
 
285
- assert_equal [], subject.clirb.args
315
+ should "know its command name and summary" do
316
+ exp = "connect"
317
+ assert_equal exp, subject.command_name
286
318
 
287
- assert_true @ardb_init_called
319
+ exp = "Connect to the configured DB"
320
+ assert_equal exp, subject.command_summary
321
+ end
322
+
323
+ should "init ardb and connect to the db when run" do
324
+ subject.run([], @stdout, @stderr)
325
+
326
+ assert_equal [false], @ardb_init_called_with
288
327
  assert_true @adapter_spy.connect_db_called?
289
328
 
290
- exp = "connected to #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`\n"
329
+ exp = "connected to #{Ardb.config.adapter} db #{Ardb.config.database.inspect}\n"
291
330
  assert_equal exp, @stdout.read
292
331
  end
293
332
 
294
- should "output any errors and raise an exit error on run" do
333
+ should "output any errors and raise an exit error when run" do
295
334
  err = StandardError.new(Factory.string)
296
335
  err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
297
- Assert.stub(Ardb, :init){ raise err }
336
+ Assert.stub(@adapter_spy, :connect_db){ raise err }
298
337
 
299
- assert_raises(CommandExitError){ subject.run }
338
+ assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
300
339
  err_output = @stderr.read
301
340
 
302
341
  assert_includes err.to_s, err_output
303
342
  assert_includes err.backtrace.join("\n"), err_output
304
343
 
305
- exp = "error connecting to #{Ardb.config.db.database.inspect} database " \
306
- "with #{Ardb.config.db_settings.inspect}"
344
+ exp = "error connecting to #{Ardb.config.database.inspect} database " \
345
+ "with #{Ardb.config.activerecord_connect_hash.inspect}"
307
346
  assert_includes exp, err_output
308
347
  end
309
-
310
348
  end
311
349
 
312
- class CreateCommandTests < UnitTests
350
+ class CreateCommandTests < CommandSetupTests
313
351
  desc "CreateCommand"
314
352
  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
353
  @command_class = CreateCommand
319
- @cmd = @command_class.new([], @stdout, @stderr)
354
+ @cmd = @command_class.new
320
355
  end
321
- subject{ @cmd }
322
-
323
- should have_readers :clirb
324
356
 
325
- should "know its CLI.RB" do
326
- assert_instance_of Ardb::CLIRB, subject.clirb
357
+ should "be a valid command" do
358
+ assert_kind_of ValidCommand, subject
327
359
  end
328
360
 
329
- should "know its help" do
330
- exp = "Usage: ardb create [options]\n\n" \
331
- "Options: #{subject.clirb}"
332
- assert_equal exp, subject.help
361
+ should "know its command name and summary" do
362
+ exp = "create"
363
+ assert_equal exp, subject.command_name
364
+
365
+ exp = "Create the configured DB"
366
+ assert_equal exp, subject.command_summary
333
367
  end
334
368
 
335
- should "parse its args and create the db on run" do
336
- subject.run
369
+ should "init ardb and create the db when run" do
370
+ subject.run([], @stdout, @stderr)
337
371
 
338
- assert_equal [], subject.clirb.args
372
+ assert_equal [false], @ardb_init_called_with
339
373
  assert_true @adapter_spy.create_db_called?
340
374
 
341
- exp = "created #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`\n"
375
+ exp = "created #{Ardb.config.adapter} db #{Ardb.config.database.inspect}\n"
342
376
  assert_equal exp, @stdout.read
343
377
  end
344
378
 
345
- should "output any errors and raise an exit error on run" do
379
+ should "output any errors and raise an exit error when run" do
346
380
  err = StandardError.new(Factory.string)
347
381
  Assert.stub(@adapter_spy, :create_db){ raise err }
348
382
 
349
- assert_raises(CommandExitError){ subject.run }
383
+ assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
350
384
  err_output = @stderr.read
351
385
 
352
386
  assert_includes err.to_s, err_output
353
- exp = "error creating #{Ardb.config.db.database.inspect} database"
387
+ exp = "error creating #{Ardb.config.database.inspect} database"
354
388
  assert_includes exp, err_output
355
389
  end
356
-
357
390
  end
358
391
 
359
- class DropCommandTests < UnitTests
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([], @stdout, @stderr)
396
+ @cmd = @command_class.new
367
397
  end
368
- subject{ @cmd }
369
-
370
- should have_readers :clirb
371
398
 
372
- should "know its CLI.RB" do
373
- assert_instance_of Ardb::CLIRB, subject.clirb
399
+ should "be a valid command" do
400
+ assert_kind_of ValidCommand, subject
374
401
  end
375
402
 
376
- should "know its help" do
377
- exp = "Usage: ardb drop [options]\n\n" \
378
- "Options: #{subject.clirb}"
379
- assert_equal exp, subject.help
403
+ should "know its command name and summary" do
404
+ exp = "drop"
405
+ assert_equal exp, subject.command_name
406
+
407
+ exp = "Drop the configured DB"
408
+ assert_equal exp, subject.command_summary
380
409
  end
381
410
 
382
- should "parse its args and drop the db on run" do
383
- subject.run
411
+ should "init ardb and drop the db when run" do
412
+ subject.run([], @stdout, @stderr)
384
413
 
385
- assert_equal [], subject.clirb.args
414
+ assert_equal [true], @ardb_init_called_with
386
415
  assert_true @adapter_spy.drop_db_called?
387
416
 
388
- exp = "dropped #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`\n"
417
+ exp = "dropped #{Ardb.config.adapter} db #{Ardb.config.database.inspect}\n"
389
418
  assert_equal exp, @stdout.read
390
419
  end
391
420
 
392
- should "output any errors and raise an exit error on run" do
421
+ should "output any errors and raise an exit error when run" do
393
422
  err = StandardError.new(Factory.string)
394
423
  Assert.stub(@adapter_spy, :drop_db){ raise err }
395
424
 
396
- assert_raises(CommandExitError){ subject.run }
425
+ assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
397
426
  err_output = @stderr.read
398
427
 
399
428
  assert_includes err.to_s, err_output
400
- exp = "error dropping #{Ardb.config.db.database.inspect} database"
429
+ exp = "error dropping #{Ardb.config.database.inspect} database"
401
430
  assert_includes exp, err_output
402
431
  end
403
-
404
432
  end
405
433
 
406
- class MigrateCommandTests < UnitTests
407
- desc "MigrateCommand"
434
+ class GenerateMigrationCommandTests < CommandSetupTests
435
+ desc "GenerateMigrationCommand"
408
436
  setup do
409
- @adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
410
- Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
437
+ @identifier = Factory.migration_id
411
438
 
412
- @ardb_init_called = false
413
- Assert.stub(Ardb, :init){ @ardb_init_called = true }
439
+ @migration_spy = nil
440
+ @migration_class = Ardb::Migration
441
+ Assert.stub(@migration_class, :new) do |*args|
442
+ @migration_spy = MigrationSpy.new(*args)
443
+ end
414
444
 
415
- @command_class = MigrateCommand
416
- @cmd = @command_class.new([], @stdout, @stderr)
445
+ @command_class = GenerateMigrationCommand
446
+ @cmd = @command_class.new
417
447
  end
418
- subject{ @cmd }
419
448
 
420
- should have_readers :clirb
449
+ should "be a valid command" do
450
+ assert_kind_of ValidCommand, subject
451
+ end
421
452
 
422
- should "know its CLI.RB" do
423
- assert_instance_of Ardb::CLIRB, subject.clirb
453
+ should "know its command name and summary" do
454
+ exp = "generate-migration"
455
+ assert_equal exp, subject.command_name
456
+
457
+ exp = "Generate a MIGRATION-NAME migration file"
458
+ assert_equal exp, subject.command_summary
424
459
  end
425
460
 
426
- should "know its help" do
427
- exp = "Usage: ardb migrate [options]\n\n" \
428
- "Options: #{subject.clirb}"
429
- assert_equal exp, subject.help
461
+ should "init ardb and save a migration for the identifier when run" do
462
+ subject.run([@identifier], @stdout, @stderr)
463
+
464
+ assert_equal [false], @ardb_init_called_with
465
+ assert_equal Ardb.config, @migration_spy.ardb_config
466
+ assert_equal @identifier, @migration_spy.identifier
467
+ assert_true @migration_spy.save_called
468
+
469
+ exp = "generated #{@migration_spy.file_path}\n"
470
+ assert_equal exp, @stdout.read
430
471
  end
431
472
 
432
- should "parse its args, init ardb and migrate the db, dump schema on run" do
433
- subject.run
473
+ should "re-raise a specific argument error on migration \"no identifer\" errors" do
474
+ Assert.stub(@migration_class, :new){ raise Ardb::Migration::NoIdentifierError }
475
+ err = nil
476
+ begin
477
+ cmd = @command_class.new
478
+ cmd.run([])
479
+ rescue ArgumentError => err
480
+ end
434
481
 
435
- assert_equal [], subject.clirb.args
482
+ assert_not_nil err
483
+ exp = "MIGRATION-NAME must be provided"
484
+ assert_equal exp, err.message
485
+ assert_not_empty err.backtrace
486
+ end
436
487
 
437
- assert_true @ardb_init_called
488
+ should "output any errors and raise an exit error when run" do
489
+ err = StandardError.new(Factory.string)
490
+ err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
491
+ Assert.stub(@migration_class, :new){ raise err }
492
+
493
+ assert_raises(CommandExitError){ subject.run([@identifier], @stdout, @stderr) }
494
+ err_output = @stderr.read
495
+
496
+ assert_includes err.to_s, err_output
497
+ assert_includes err.backtrace.join("\n"), err_output
498
+
499
+ exp = "error generating migration"
500
+ assert_includes exp, err_output
501
+ end
502
+ end
503
+
504
+ class MigrateCommandTests < CommandSetupTests
505
+ desc "MigrateCommand"
506
+ setup do
507
+ @orig_env_var_migrate_no_schema = ENV["ARDB_MIGRATE_NO_SCHEMA"]
508
+ @command_class = MigrateCommand
509
+ @cmd = @command_class.new
510
+ end
511
+ teardown do
512
+ ENV["ARDB_MIGRATE_NO_SCHEMA"] = @orig_env_var_migrate_no_schema
513
+ end
514
+
515
+ should "be a migrate command" do
516
+ assert_kind_of MigrateCommandBehaviors, subject
517
+ end
518
+
519
+ should "know its command name and summary" do
520
+ exp = "migrate"
521
+ assert_equal exp, subject.command_name
522
+
523
+ exp = "Migrate the configured DB"
524
+ assert_equal exp, subject.command_summary
525
+ end
526
+
527
+ should "init ardb, migrate the db and dump the schema when run" do
528
+ subject.run([], @stdout, @stderr)
529
+
530
+ assert_equal [true], @ardb_init_called_with
438
531
  assert_true @adapter_spy.migrate_db_called?
439
532
  assert_true @adapter_spy.dump_schema_called?
440
533
  end
441
534
 
442
- should "init ardb and only migrate on run with no schema dump env var set" do
443
- current_no_schema = ENV['ARDB_MIGRATE_NO_SCHEMA']
444
- ENV['ARDB_MIGRATE_NO_SCHEMA'] = 'yes'
445
- subject.run
446
- ENV['ARDB_MIGRATE_NO_SCHEMA'] = current_no_schema
535
+ should "only init ardb and migrate when run with no schema dump env var set" do
536
+ ENV["ARDB_MIGRATE_NO_SCHEMA"] = "yes"
537
+ subject.run([], @stdout, @stderr)
447
538
 
448
- assert_true @ardb_init_called
539
+ assert_equal [true], @ardb_init_called_with
449
540
  assert_true @adapter_spy.migrate_db_called?
450
541
  assert_false @adapter_spy.dump_schema_called?
451
542
  end
452
543
 
453
- should "output any errors and raise an exit error on run" do
544
+ should "output any errors and raise an exit error when run" do
454
545
  err = StandardError.new(Factory.string)
455
546
  err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
456
- Assert.stub(Ardb, :init){ raise err }
547
+ Assert.stub(@adapter_spy, :migrate_db){ raise err }
457
548
 
458
- assert_raises(CommandExitError){ subject.run }
549
+ assert_raises(CommandExitError){ subject.run([], @stdout, @stderr) }
459
550
  err_output = @stderr.read
460
551
 
461
552
  assert_includes err.to_s, err_output
462
553
  assert_includes err.backtrace.join("\n"), err_output
463
554
 
464
- exp = "error migrating #{Ardb.config.db.database.inspect} database"
555
+ exp = "error migrating #{Ardb.config.database.inspect} database"
465
556
  assert_includes exp, err_output
466
557
  end
467
-
468
558
  end
469
559
 
470
- class GenerateMigrationCommandTests < UnitTests
471
- desc "GenerateMigrationCommand"
560
+ class MigrateUpCommandTests < CommandSetupTests
561
+ desc "MigrateUpCommand"
472
562
  setup do
473
- @migration_spy = nil
474
- @migration_class = Ardb::Migration
475
- Assert.stub(@migration_class, :new) do |*args|
476
- @migration_spy = MigrationSpy.new(*args)
477
- end
563
+ @command_class = MigrateUpCommand
564
+ @cmd = @command_class.new
565
+ end
478
566
 
479
- @command_class = GenerateMigrationCommand
480
- @identifier = Factory.migration_id
481
- @cmd = @command_class.new([@identifier], @stdout, @stderr)
567
+ should "be a migrate command" do
568
+ assert_kind_of MigrateCommandBehaviors, subject
482
569
  end
483
- subject{ @cmd }
484
570
 
485
- should have_readers :clirb
571
+ should "know its command name and summary" do
572
+ exp = "migrate-up"
573
+ assert_equal exp, subject.command_name
486
574
 
487
- should "know its CLI.RB" do
488
- assert_instance_of Ardb::CLIRB, subject.clirb
575
+ exp = "Migrate the configured DB up"
576
+ assert_equal exp, subject.command_summary
489
577
  end
578
+ end
490
579
 
491
- should "know its help" do
492
- exp = "Usage: ardb generate-migration [options] MIGRATION-NAME\n\n" \
493
- "Options: #{subject.clirb}"
494
- assert_equal exp, subject.help
580
+ class MigrateDownCommandTests < CommandSetupTests
581
+ desc "MigrateDownCommand"
582
+ setup do
583
+ @command_class = MigrateDownCommand
584
+ @cmd = @command_class.new
495
585
  end
496
586
 
497
- should "parse its args and save a migration for the identifier on run" do
498
- subject.run
587
+ should "be a migrate command" do
588
+ assert_kind_of MigrateCommandBehaviors, subject
589
+ end
499
590
 
500
- assert_equal [@identifier], subject.clirb.args
501
- assert_equal @identifier, @migration_spy.identifier
502
- assert_true @migration_spy.save_called
591
+ should "know its command name and summary" do
592
+ exp = "migrate-down"
593
+ assert_equal exp, subject.command_name
503
594
 
504
- exp = "generated #{@migration_spy.file_path}\n"
505
- assert_equal exp, @stdout.read
595
+ exp = "Migrate the configured DB down"
596
+ assert_equal exp, subject.command_summary
506
597
  end
598
+ end
507
599
 
508
- should "re-raise a specific argument error on migration 'no identifer' errors" do
509
- Assert.stub(@migration_class, :new) { raise Ardb::Migration::NoIdentifierError }
510
- err = nil
511
- begin
512
- cmd = @command_class.new([])
513
- cmd.run
514
- rescue ArgumentError => err
515
- end
600
+ class MigrateForwardCommandTests < CommandSetupTests
601
+ desc "MigrateForwardCommand"
602
+ setup do
603
+ @command_class = MigrateForwardCommand
604
+ @cmd = @command_class.new
605
+ end
516
606
 
517
- assert_not_nil err
518
- exp = "MIGRATION-NAME must be provided"
519
- assert_equal exp, err.message
520
- assert_not_empty err.backtrace
607
+ should "be a migrate command" do
608
+ assert_kind_of MigrateCommandBehaviors, subject
521
609
  end
522
610
 
523
- should "output any errors and raise an exit error on run" do
524
- err = StandardError.new(Factory.string)
525
- err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
526
- Assert.stub(@migration_class, :new){ raise err }
611
+ should "know its command name and summary" do
612
+ exp = "migrate-forward"
613
+ assert_equal exp, subject.command_name
527
614
 
528
- assert_raises(CommandExitError){ subject.run }
529
- err_output = @stderr.read
615
+ exp = "Migrate the configured DB forward"
616
+ assert_equal exp, subject.command_summary
617
+ end
618
+ end
530
619
 
531
- assert_includes err.to_s, err_output
532
- assert_includes err.backtrace.join("\n"), err_output
620
+ class MigrateBackwardCommandTests < CommandSetupTests
621
+ desc "MigrateBackwardCommand"
622
+ setup do
623
+ @command_class = MigrateBackwardCommand
624
+ @cmd = @command_class.new
625
+ end
533
626
 
534
- exp = "error generating migration"
535
- assert_includes exp, err_output
627
+ should "be a migrate command" do
628
+ assert_kind_of MigrateCommandBehaviors, subject
536
629
  end
537
630
 
631
+ should "know its command name and summary" do
632
+ exp = "migrate-backward"
633
+ assert_equal exp, subject.command_name
634
+
635
+ exp = "Migrate the configured DB backward"
636
+ assert_equal exp, subject.command_summary
637
+ end
538
638
  end
539
639
 
540
640
  class CLISpy
@@ -550,18 +650,27 @@ class Ardb::CLI
550
650
  end
551
651
 
552
652
  class CommandSpy
553
- attr_reader :run_called
653
+ attr_reader :command_name, :argv, :stdout, :stderr, :run_called
554
654
 
555
- def initialize
655
+ def initialize(command_name)
656
+ @command_name = command_name
657
+ @argv = nil
658
+ @stdout, @stderr = nil, nil
556
659
  @run_called = false
557
660
  end
558
661
 
559
- def run
662
+ def run(argv, stdout = nil, stderr = nil)
663
+ @argv = argv
664
+ @stdout, @stderr = stdout, stderr
560
665
  @run_called = true
561
666
  end
562
667
 
563
- def help
564
- Factory.text
668
+ def command_summary
669
+ @command_summary ||= Factory.string
670
+ end
671
+
672
+ def command_help
673
+ @command_help ||= Factory.text
565
674
  end
566
675
  end
567
676
 
@@ -593,10 +702,11 @@ class Ardb::CLI
593
702
  end
594
703
 
595
704
  class MigrationSpy
596
- attr_reader :identifier, :file_path, :save_called
705
+ attr_reader :ardb_config, :identifier, :file_path, :save_called
597
706
 
598
- def initialize(*args)
599
- @identifier = args.first
707
+ def initialize(ardb_config, identifier)
708
+ @ardb_config = ardb_config
709
+ @identifier = identifier
600
710
  @file_path = Factory.path
601
711
  @save_called = false
602
712
  end
@@ -606,5 +716,4 @@ class Ardb::CLI
606
716
  self
607
717
  end
608
718
  end
609
-
610
719
  end