ardb 0.28.1 → 0.29.2

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 (59) hide show
  1. checksums.yaml +7 -7
  2. data/.l.yml +8 -0
  3. data/.rubocop.yml +3 -0
  4. data/.t.yml +6 -0
  5. data/Gemfile +21 -8
  6. data/README.md +252 -3
  7. data/ardb.gemspec +14 -10
  8. data/bin/ardb +3 -1
  9. data/lib/ardb.rb +110 -80
  10. data/lib/ardb/adapter/base.rb +73 -47
  11. data/lib/ardb/adapter/mysql.rb +4 -17
  12. data/lib/ardb/adapter/postgresql.rb +51 -46
  13. data/lib/ardb/adapter/sqlite.rb +11 -15
  14. data/lib/ardb/adapter_spy.rb +18 -30
  15. data/lib/ardb/cli.rb +29 -24
  16. data/lib/ardb/cli/clirb.rb +19 -17
  17. data/lib/ardb/cli/commands.rb +308 -129
  18. data/lib/ardb/db_tests.rb +4 -4
  19. data/lib/ardb/default_order_by.rb +13 -21
  20. data/lib/ardb/migration.rb +15 -16
  21. data/lib/ardb/record_spy.rb +46 -61
  22. data/lib/ardb/relation_spy.rb +27 -31
  23. data/lib/ardb/require_autoloaded_active_record_files.rb +174 -58
  24. data/lib/ardb/test_helpers.rb +13 -14
  25. data/lib/ardb/use_db_default.rb +10 -19
  26. data/lib/ardb/version.rb +3 -1
  27. data/script/determine_autoloaded_active_record_files.rb +31 -24
  28. data/test/helper.rb +6 -13
  29. data/test/support/factory.rb +4 -3
  30. data/test/support/fake_schema.rb +3 -1
  31. data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
  32. data/test/support/postgresql/schema.rb +2 -1
  33. data/test/support/postgresql/setup_test_db.rb +17 -15
  34. data/test/support/relative_require_test_db_file.rb +1 -0
  35. data/test/support/require_test_db_file.rb +1 -0
  36. data/test/system/.keep +0 -0
  37. data/test/unit/adapter/base_tests.rb +83 -55
  38. data/test/unit/adapter/mysql_tests.rb +4 -19
  39. data/test/unit/adapter/postgresql_tests.rb +21 -30
  40. data/test/unit/adapter/sqlite_tests.rb +5 -11
  41. data/test/unit/adapter_spy_tests.rb +6 -17
  42. data/test/unit/ardb_tests.rb +81 -53
  43. data/test/unit/cli_tests.rb +232 -157
  44. data/test/unit/db_tests_tests.rb +7 -7
  45. data/test/unit/default_order_by_tests.rb +21 -20
  46. data/test/unit/migration_tests.rb +17 -18
  47. data/test/unit/record_spy_tests.rb +36 -34
  48. data/test/unit/relation_spy_tests.rb +40 -63
  49. data/test/unit/test_helpers_tests.rb +7 -15
  50. data/test/unit/use_db_default_tests.rb +22 -17
  51. metadata +117 -84
  52. data/lib/ardb/has_slug.rb +0 -107
  53. data/lib/ardb/migration_helpers.rb +0 -77
  54. data/lib/ardb/pg_json.rb +0 -90
  55. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
  56. data/test/system/pg_json_tests.rb +0 -85
  57. data/test/unit/has_slug_tests.rb +0 -341
  58. data/test/unit/migration_helpers_tests.rb +0 -65
  59. data/test/unit/pg_json_tests.rb +0 -39
@@ -1,8 +1,9 @@
1
- require 'assert'
2
- require 'ardb/adapter/sqlite'
1
+ # frozen_string_literal: true
3
2
 
4
- class Ardb::Adapter::Sqlite
3
+ require "assert"
4
+ require "ardb/adapter/sqlite"
5
5
 
6
+ class Ardb::Adapter::Sqlite
6
7
  class UnitTests < Assert::Context
7
8
  desc "Ardb::Adapter::Sqlite"
8
9
  setup do
@@ -24,21 +25,14 @@ class Ardb::Adapter::Sqlite
24
25
  exp = File.expand_path(@config.database, @config.root_path)
25
26
  assert_equal exp, subject.db_file_path
26
27
 
27
- @config.database = File.join(TMP_PATH, 'abs_sqlite_db_test')
28
+ @config.database = File.join(TMP_PATH, "abs_sqlite_db_test")
28
29
  adapter = Ardb::Adapter::Sqlite.new(@config)
29
30
  assert_equal @config.database, adapter.db_file_path
30
31
  end
31
32
 
32
- should "not implement the foreign key sql meths" do
33
- assert_raises(NotImplementedError){ subject.foreign_key_add_sql }
34
- assert_raises(NotImplementedError){ subject.foreign_key_drop_sql }
35
- end
36
-
37
33
  # not currently implemented, see: https://github.com/redding/ardb/issues/29
38
34
  should "not implement the drop tables method" do
39
35
  assert_raises(NotImplementedError){ subject.drop_tables }
40
36
  end
41
-
42
37
  end
43
-
44
38
  end
@@ -1,8 +1,9 @@
1
- require 'assert'
2
- require 'ardb/adapter_spy'
1
+ # frozen_string_literal: true
3
2
 
4
- class Ardb::AdapterSpy
3
+ require "assert"
4
+ require "ardb/adapter_spy"
5
5
 
6
+ class Ardb::AdapterSpy
6
7
  class UnitTests < Assert::Context
7
8
  desc "Ardb::AdapterSpy"
8
9
  setup do
@@ -13,7 +14,6 @@ class Ardb::AdapterSpy
13
14
  should "be a kind of ardb adapter" do
14
15
  assert subject < Ardb::Adapter::Base
15
16
  end
16
-
17
17
  end
18
18
 
19
19
  class InitTests < UnitTests
@@ -28,8 +28,8 @@ class Ardb::AdapterSpy
28
28
  should have_accessors :dump_schema_called_count, :load_schema_called_count
29
29
  should have_accessors :drop_db_called_count, :create_db_called_count
30
30
  should have_accessors :connect_db_called_count, :migrate_db_called_count
31
- should have_imeths :foreign_key_add_sql, :foreign_key_drop_sql
32
- should have_imeths :create_db_called?, :drop_db_called?, :drop_tables_called?
31
+ should have_imeths :create_db_called?, :drop_db_called?
32
+ should have_imeths :drop_tables_called?
33
33
  should have_imeths :connect_db_called?, :migrate_db_called?
34
34
  should have_imeths :dump_schema_called?, :load_schema_called?
35
35
  should have_imeths :create_db, :drop_db, :drop_tables
@@ -46,15 +46,6 @@ class Ardb::AdapterSpy
46
46
  assert_equal 0, subject.dump_schema_called_count
47
47
  end
48
48
 
49
- should "know its add and drop foreign key sql" do
50
- exp = "FAKE ADD FOREIGN KEY SQL :from_table :from_column " \
51
- ":to_table :to_column :name"
52
- assert_equal exp, subject.foreign_key_add_sql
53
- exp = "FAKE DROP FOREIGN KEY SQL :from_table :from_column " \
54
- ":to_table :to_column :name"
55
- assert_equal exp, subject.foreign_key_drop_sql
56
- end
57
-
58
49
  should "know if and how many times a method is called" do
59
50
  assert_equal false, subject.create_db_called?
60
51
  subject.create_db
@@ -91,7 +82,5 @@ class Ardb::AdapterSpy
91
82
  assert_equal 1, subject.load_schema_called_count
92
83
  assert_equal true, subject.load_schema_called?
93
84
  end
94
-
95
85
  end
96
-
97
86
  end
@@ -1,26 +1,30 @@
1
- require 'assert'
2
- require 'ardb'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'logger'
5
- require 'ardb/adapter_spy'
6
- require 'ardb/adapter/mysql'
7
- require 'ardb/adapter/postgresql'
8
- require 'ardb/adapter/sqlite'
3
+ require "assert"
4
+ require "ardb"
9
5
 
10
- module Ardb
6
+ require "logger"
7
+ require "ardb/adapter_spy"
8
+ require "ardb/adapter/mysql"
9
+ require "ardb/adapter/postgresql"
10
+ require "ardb/adapter/sqlite"
11
11
 
12
+ module Ardb
12
13
  class UnitTests < Assert::Context
13
14
  desc "Ardb"
14
15
  setup do
15
16
  @module = Ardb
16
17
  end
18
+ teardown do
19
+ @module.reset_adapter
20
+ end
17
21
  subject{ @module }
18
22
 
19
- should have_imeths :config, :configure, :adapter, :init
20
- should have_imeths :escape_like_pattern
23
+ should have_imeths :config, :configure, :adapter, :reset_adapter
24
+ should have_imeths :init, :escape_like_pattern
21
25
 
22
26
  should "default the db file env var" do
23
- assert_equal 'config/db', ENV['ARDB_DB_FILE']
27
+ assert_equal "config/db", ENV["ARDB_DB_FILE"]
24
28
  end
25
29
 
26
30
  should "know its config" do
@@ -34,17 +38,16 @@ module Ardb
34
38
  subject.configure{ |c| yielded = c }
35
39
  assert_same subject.config, yielded
36
40
  end
37
-
38
41
  end
39
42
 
40
43
  class InitMethodSetupTests < UnitTests
41
44
  setup do
42
- @orig_env_pwd = ENV['PWD']
43
- @orig_env_ardb_db_file = ENV['ARDB_DB_FILE']
45
+ @orig_env_pwd = ENV["PWD"]
46
+ @orig_env_ardb_db_file = ENV["ARDB_DB_FILE"]
44
47
  @orig_ar_logger = ActiveRecord::Base.logger
45
48
 
46
49
  # stub in a temporary config, this allows us to modify it and not worry
47
- # about affecting Ardb's global config which could cause issues on other
50
+ # about affecting Ardb"s global config which could cause issues on other
48
51
  # tests
49
52
  @ardb_config = Config.new
50
53
  Assert.stub(Ardb, :config){ @ardb_config }
@@ -54,16 +57,15 @@ module Ardb
54
57
  @ardb_adapter ||= Ardb::AdapterSpy.new(*args)
55
58
  end
56
59
 
57
- ENV['ARDB_DB_FILE'] = 'test/support/require_test_db_file'
60
+ ENV["ARDB_DB_FILE"] = "test/support/require_test_db_file"
58
61
  @ardb_config.adapter = Adapter::VALID_ADAPTERS.sample
59
62
  @ardb_config.database = Factory.string
60
63
  end
61
64
  teardown do
62
65
  ActiveRecord::Base.logger = @orig_ar_logger
63
- ENV['ARDB_DB_FILE'] = @orig_env_ardb_db_file
64
- ENV['PWD'] = @orig_env_pwd
66
+ ENV["ARDB_DB_FILE"] = @orig_env_ardb_db_file
67
+ ENV["PWD"] = @orig_env_pwd
65
68
  end
66
-
67
69
  end
68
70
 
69
71
  class InitMethodTests < InitMethodSetupTests
@@ -71,25 +73,25 @@ module Ardb
71
73
 
72
74
  should "require the autoloaded active record files" do
73
75
  subject.init
74
- assert_false require('ardb/require_autoloaded_active_record_files')
76
+ assert_false require("ardb/require_autoloaded_active_record_files")
75
77
  end
76
78
 
77
79
  should "require the db file" do
78
80
  subject.init
79
- assert_false require(ENV['ARDB_DB_FILE'])
81
+ assert_false require(ENV["ARDB_DB_FILE"])
80
82
  end
81
83
 
82
84
  should "require the db file relative to the working directory if needed" do
83
- ENV['PWD'] = 'test/support'
84
- ENV['ARDB_DB_FILE'] = 'relative_require_test_db_file'
85
+ ENV["PWD"] = "test/support"
86
+ ENV["ARDB_DB_FILE"] = "relative_require_test_db_file"
85
87
  subject.init
86
- assert_false require(File.expand_path(ENV['ARDB_DB_FILE'], ENV['PWD']))
88
+ assert_false require(File.expand_path(ENV["ARDB_DB_FILE"], ENV["PWD"]))
87
89
  end
88
90
 
89
91
  should "raise an invalid db file error when it can't require it" do
90
- ENV['ARDB_DB_FILE'] = Factory.file_path
92
+ ENV["ARDB_DB_FILE"] = Factory.file_path
91
93
  error = assert_raises(InvalidDBFileError){ subject.init }
92
- exp = "can't require `#{ENV['ARDB_DB_FILE']}`, check that the " \
94
+ exp = "can't require `#{ENV["ARDB_DB_FILE"]}`, check that the " \
93
95
  "ARDB_DB_FILE env var is set to the file path of your db file"
94
96
  assert_equal exp, error.message
95
97
  end
@@ -110,6 +112,16 @@ module Ardb
110
112
  assert_same @ardb_adapter, subject.adapter
111
113
  end
112
114
 
115
+ should "set ActiveRecord::Base attributes" do
116
+ subject.init
117
+
118
+ assert_equal subject.config.logger, ActiveRecord::Base.logger
119
+ assert_equal(
120
+ subject.config.default_timezone,
121
+ ActiveRecord::Base.default_timezone,
122
+ )
123
+ end
124
+
113
125
  should "optionally establish an AR connection" do
114
126
  assert_nil @ardb_adapter
115
127
  subject.init
@@ -122,6 +134,17 @@ module Ardb
122
134
  assert_equal 2, @ardb_adapter.connect_db_called_count
123
135
  end
124
136
 
137
+ should "raise a not initialized error using its adapter before init" do
138
+ subject.reset_adapter
139
+ assert_raises(NotInitializedError){ subject.adapter }
140
+ assert_raises(NotInitializedError) do
141
+ subject.escape_like_pattern(Factory.string)
142
+ end
143
+
144
+ subject.init
145
+ assert_nothing_raised{ subject.adapter }
146
+ assert_nothing_raised{ subject.escape_like_pattern(Factory.string) }
147
+ end
125
148
  end
126
149
 
127
150
  class InitTests < InitMethodSetupTests
@@ -135,7 +158,6 @@ module Ardb
135
158
  exp = subject.adapter.escape_like_pattern(pattern)
136
159
  assert_equal exp, subject.escape_like_pattern(pattern)
137
160
  end
138
-
139
161
  end
140
162
 
141
163
  class ConfigTests < UnitTests
@@ -156,17 +178,17 @@ module Ardb
156
178
  :password,
157
179
  :pool,
158
180
  :checkout_timeout,
159
- :min_messages
181
+ :min_messages,
160
182
  ]
161
183
  assert_equal exp, subject::ACTIVERECORD_ATTRS
162
184
  end
163
185
 
164
186
  should "know its default migrations path" do
165
- assert_equal 'db/migrations', subject::DEFAULT_MIGRATIONS_PATH
187
+ assert_equal "db/migrations", subject::DEFAULT_MIGRATIONS_PATH
166
188
  end
167
189
 
168
190
  should "know its default schema path" do
169
- assert_equal 'db/schema', subject::DEFAULT_SCHEMA_PATH
191
+ assert_equal "db/schema", subject::DEFAULT_SCHEMA_PATH
170
192
  end
171
193
 
172
194
  should "know its schema formats" do
@@ -175,7 +197,6 @@ module Ardb
175
197
  exp = [subject::RUBY_SCHEMA_FORMAT, subject::SQL_SCHEMA_FORMAT]
176
198
  assert_equal exp, subject::VALID_SCHEMA_FORMATS
177
199
  end
178
-
179
200
  end
180
201
 
181
202
  class ConfigInitTests < ConfigTests
@@ -185,18 +206,27 @@ module Ardb
185
206
  end
186
207
  subject{ @config }
187
208
 
188
- should have_accessors *Ardb::Config::ACTIVERECORD_ATTRS
189
- should have_accessors :logger, :root_path
209
+ should have_accessors(*Ardb::Config::ACTIVERECORD_ATTRS)
210
+ should have_accessors :default_timezone, :logger, :root_path
190
211
  should have_readers :schema_format
191
212
  should have_writers :migrations_path, :schema_path
192
213
  should have_imeths :activerecord_connect_hash, :validate!
193
214
 
194
215
  should "default its attributs" do
216
+ assert_equal :utc, subject.default_timezone
195
217
  assert_instance_of Logger, subject.logger
196
- assert_equal ENV['PWD'], subject.root_path
197
- exp = File.expand_path(@config_class::DEFAULT_MIGRATIONS_PATH, subject.root_path)
218
+ assert_equal ENV["PWD"], subject.root_path
219
+ exp =
220
+ File.expand_path(
221
+ @config_class::DEFAULT_MIGRATIONS_PATH,
222
+ subject.root_path,
223
+ )
198
224
  assert_equal exp, subject.migrations_path
199
- exp = File.expand_path(@config_class::DEFAULT_SCHEMA_PATH, subject.root_path)
225
+ exp =
226
+ File.expand_path(
227
+ @config_class::DEFAULT_SCHEMA_PATH,
228
+ subject.root_path,
229
+ )
200
230
  assert_equal exp, subject.schema_path
201
231
  assert_equal @config_class::RUBY_SCHEMA_FORMAT, subject.schema_format
202
232
  end
@@ -235,11 +265,11 @@ module Ardb
235
265
  end
236
266
 
237
267
  should "know its activerecord connection hash" do
238
- attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map do |attr_name|
239
- value = [Factory.string, nil].sample
268
+ attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map{ |attr_name|
269
+ value = [Factory.string, nil].sample
240
270
  subject.send("#{attr_name}=", value)
241
- [attr_name.to_s, value] if !value.nil?
242
- end.compact
271
+ [attr_name.to_s, value] unless value.nil?
272
+ }.compact
243
273
  assert_equal Hash[attrs_and_values], subject.activerecord_connect_hash
244
274
  end
245
275
 
@@ -265,11 +295,12 @@ module Ardb
265
295
 
266
296
  should "know if its equal to another config" do
267
297
  attrs = @config_class::ACTIVERECORD_ATTRS + [
298
+ :default_timezone,
268
299
  :logger,
269
300
  :root_path,
270
301
  :schema_format,
271
302
  :migrations_path,
272
- :schema_path
303
+ :schema_path,
273
304
  ]
274
305
  attrs.each do |attr_name|
275
306
  subject.send("#{attr_name}=", Factory.string)
@@ -285,7 +316,6 @@ module Ardb
285
316
  other_config.send("#{attr_name}=", Factory.string)
286
317
  assert_not_equal other_config, subject
287
318
  end
288
-
289
319
  end
290
320
 
291
321
  class AdapterTests < UnitTests
@@ -304,21 +334,21 @@ module Ardb
304
334
 
305
335
  should "know its valid adapters" do
306
336
  exp = [
307
- 'sqlite',
308
- 'sqlite3',
309
- 'postgresql',
310
- 'postgres',
311
- 'mysql',
312
- 'mysql2'
337
+ "sqlite",
338
+ "sqlite3",
339
+ "postgresql",
340
+ "postgres",
341
+ "mysql",
342
+ "mysql2",
313
343
  ]
314
344
  assert_equal exp, subject::VALID_ADAPTERS
315
345
  end
316
346
 
317
347
  should "build an adapter specific class using the passed config" do
318
348
  adapter_key, exp_adapter_class = [
319
- ['sqlite', Ardb::Adapter::Sqlite],
320
- ['postgresql', Ardb::Adapter::Postgresql],
321
- ['mysql', Ardb::Adapter::Mysql]
349
+ ["sqlite", Ardb::Adapter::Sqlite],
350
+ ["postgresql", Ardb::Adapter::Postgresql],
351
+ ["mysql", Ardb::Adapter::Mysql],
322
352
  ].sample
323
353
  @config.adapter = adapter_key
324
354
 
@@ -356,7 +386,5 @@ module Ardb
356
386
  assert_instance_of Ardb::Adapter::Mysql, adapter
357
387
  assert_equal @config, adapter.config
358
388
  end
359
-
360
389
  end
361
-
362
390
  end
@@ -1,12 +1,13 @@
1
- require 'assert'
2
- require 'ardb/cli'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'ardb'
5
- require 'ardb/adapter_spy'
6
- require 'ardb/migration'
3
+ require "assert"
4
+ require "ardb/cli"
7
5
 
8
- class Ardb::CLI
6
+ require "ardb"
7
+ require "ardb/adapter_spy"
8
+ require "ardb/migration"
9
9
 
10
+ class Ardb::CLI
10
11
  class UnitTests < Assert::Context
11
12
  desc "Ardb::CLI"
12
13
  setup do
@@ -26,16 +27,22 @@ class Ardb::CLI
26
27
  end
27
28
 
28
29
  should "know its commands" do
29
- assert_equal 5, COMMANDS.size
30
+ assert_equal 9, COMMANDS.size
30
31
 
31
32
  assert_instance_of InvalidCommand, COMMANDS[Factory.string]
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']
33
+ assert_instance_of ConnectCommand, COMMANDS["connect"]
34
+ assert_instance_of CreateCommand, COMMANDS["create"]
35
+ assert_instance_of DropCommand, COMMANDS["drop"]
36
+ assert_instance_of(
37
+ GenerateMigrationCommand,
38
+ COMMANDS["generate-migration"],
39
+ )
40
+ assert_instance_of MigrateCommand, COMMANDS["migrate"]
41
+ assert_instance_of MigrateUpCommand, COMMANDS["migrate-up"]
42
+ assert_instance_of MigrateDownCommand, COMMANDS["migrate-down"]
43
+ assert_instance_of MigrateForwardCommand, COMMANDS["migrate-forward"]
44
+ assert_instance_of MigrateBackwardCommand, COMMANDS["migrate-backward"]
37
45
  end
38
-
39
46
  end
40
47
 
41
48
  class InitTests < UnitTests
@@ -50,25 +57,25 @@ class Ardb::CLI
50
57
  subject{ @cli }
51
58
 
52
59
  should have_imeths :run
53
-
54
60
  end
55
61
 
56
62
  class RunSetupTests < InitTests
57
63
  setup do
58
64
  @command_name = Factory.string
59
- @argv = [@command_name, Factory.string]
65
+ @command_class = Class.new{ include ValidCommand }
66
+ Assert.stub(@command_class, :command_name){ @command_name }
60
67
 
61
- @command_class = Class.new
62
- @command_spy = CommandSpy.new
68
+ @command_spy = CommandSpy.new(@command_name)
63
69
  Assert.stub(@command_class, :new){ @command_spy }
64
- COMMANDS.add(@command_class, @command_name)
70
+ COMMANDS.add(@command_class)
65
71
 
66
72
  @invalid_command = InvalidCommand.new(@command_name)
73
+
74
+ @argv = [@command_name, Factory.string]
67
75
  end
68
76
  teardown do
69
- COMMANDS.remove(@command_name)
77
+ COMMANDS.remove(@command_class)
70
78
  end
71
-
72
79
  end
73
80
 
74
81
  class RunTests < RunSetupTests
@@ -88,7 +95,6 @@ class Ardb::CLI
88
95
  should "have successfully exited" do
89
96
  assert_equal 0, @kernel_spy.exit_status
90
97
  end
91
-
92
98
  end
93
99
 
94
100
  class RunWithNoArgsTests < RunSetupTests
@@ -98,14 +104,13 @@ class Ardb::CLI
98
104
  end
99
105
 
100
106
  should "output the invalid command's help" do
101
- assert_equal @invalid_command.help, @stdout.read
107
+ assert_equal @invalid_command.command_help, @stdout.read
102
108
  assert_empty @stderr.read
103
109
  end
104
110
 
105
111
  should "have successfully exited" do
106
112
  assert_equal 0, @kernel_spy.exit_status
107
113
  end
108
-
109
114
  end
110
115
 
111
116
  class RunWithInvalidCommandTests < RunSetupTests
@@ -117,15 +122,14 @@ class Ardb::CLI
117
122
  end
118
123
 
119
124
  should "output that it is invalid and output the invalid command's help" do
120
- exp = "'#{@name}' is not a command.\n\n"
125
+ exp = "\"#{@name}\" is not a command.\n\n"
121
126
  assert_equal exp, @stderr.read
122
- assert_equal @invalid_command.help, @stdout.read
127
+ assert_equal @invalid_command.command_help, @stdout.read
123
128
  end
124
129
 
125
130
  should "have unsuccessfully exited" do
126
131
  assert_equal 1, @kernel_spy.exit_status
127
132
  end
128
-
129
133
  end
130
134
 
131
135
  class RunWithCommandExitErrorTests < RunSetupTests
@@ -139,30 +143,28 @@ class Ardb::CLI
139
143
  assert_equal 1, @kernel_spy.exit_status
140
144
  assert_empty @stderr.read
141
145
  end
142
-
143
146
  end
144
147
 
145
148
  class RunWithHelpTests < RunSetupTests
146
149
  desc "and run with the help switch"
147
150
  setup do
148
- @cli.run([ '--help' ])
151
+ @cli.run(["--help"])
149
152
  end
150
153
 
151
154
  should "output the invalid command's help" do
152
- assert_equal @invalid_command.help, @stdout.read
155
+ assert_equal @invalid_command.command_help, @stdout.read
153
156
  assert_empty @stderr.read
154
157
  end
155
158
 
156
159
  should "have successfully exited" do
157
160
  assert_equal 0, @kernel_spy.exit_status
158
161
  end
159
-
160
162
  end
161
163
 
162
164
  class RunWithVersionTests < RunSetupTests
163
165
  desc "and run with the version switch"
164
166
  setup do
165
- @cli.run([ '--version' ])
167
+ @cli.run(["--version"])
166
168
  end
167
169
 
168
170
  should "output its version" do
@@ -173,7 +175,6 @@ class Ardb::CLI
173
175
  should "have successfully exited" do
174
176
  assert_equal 0, @kernel_spy.exit_status
175
177
  end
176
-
177
178
  end
178
179
 
179
180
  class RunWithErrorTests < RunSetupTests
@@ -193,7 +194,6 @@ class Ardb::CLI
193
194
  should "have unsuccessfully exited" do
194
195
  assert_equal 1, @kernel_spy.exit_status
195
196
  end
196
-
197
197
  end
198
198
 
199
199
  class InvalidCommandTests < UnitTests
@@ -206,7 +206,7 @@ class Ardb::CLI
206
206
  subject{ @cmd }
207
207
 
208
208
  should have_readers :name, :clirb
209
- should have_imeths :new, :run, :help
209
+ should have_imeths :new, :run, :command_help
210
210
 
211
211
  should "know its attrs" do
212
212
  assert_equal @name, subject.name
@@ -218,12 +218,12 @@ class Ardb::CLI
218
218
  end
219
219
 
220
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' ]) }
221
+ assert_raises(CLIRB::HelpExit){ subject.new.run(["--help"]) }
222
+ assert_raises(CLIRB::VersionExit){ subject.new.run(["--version"]) }
223
223
  end
224
224
 
225
225
  should "raise a help exit if its name is empty" do
226
- cmd = @command_class.new([nil, ''].sample)
226
+ cmd = @command_class.new([nil, ""].sample)
227
227
  argv = [Factory.string, Factory.string]
228
228
  assert_raises(CLIRB::HelpExit){ cmd.new.run(argv) }
229
229
  end
@@ -237,9 +237,8 @@ class Ardb::CLI
237
237
  "Options: #{subject.clirb}\n" \
238
238
  "Commands:\n" \
239
239
  "#{COMMANDS.to_s.split("\n").map{ |l| " #{l}" }.join("\n")}\n"
240
- assert_equal exp, subject.help
240
+ assert_equal exp, subject.command_help
241
241
  end
242
-
243
242
  end
244
243
 
245
244
  class CommandSetupTests < UnitTests
@@ -253,7 +252,6 @@ class Ardb::CLI
253
252
  Assert.stub(Ardb, :adapter){ @adapter_spy }
254
253
  end
255
254
  subject{ @cmd }
256
-
257
255
  end
258
256
 
259
257
  class ValidCommandTests < CommandSetupTests
@@ -263,7 +261,10 @@ class Ardb::CLI
263
261
  @cmd = @command_class.new
264
262
  end
265
263
 
266
- should have_imeths :clirb, :run, :summary
264
+ should have_cmeths :command_name, :command_summary
265
+
266
+ should have_imeths :clirb, :run
267
+ should have_imeths :command_name, :command_summary, :command_help
267
268
 
268
269
  should "know its CLI.RB" do
269
270
  assert_instance_of CLIRB, subject.clirb
@@ -277,16 +278,32 @@ class Ardb::CLI
277
278
 
278
279
  should "take custom CLIRB build procs" do
279
280
  cmd = @command_class.new do
280
- option 'test', 'testing', :abbrev => 't'
281
+ option "test", "testing", abbrev: "t"
281
282
  end
282
- cmd.run(['-t'], @stdout, @stderr)
283
- assert_true cmd.clirb.opts['test']
283
+ cmd.run(["-t"], @stdout, @stderr)
284
+ assert_true cmd.clirb.opts["test"]
284
285
  end
285
286
 
286
- should "default its summary" do
287
- assert_equal '', subject.summary
287
+ should "not implement its command name" do
288
+ assert_raises NotImplementedError do
289
+ subject.command_name
290
+ end
288
291
  end
289
292
 
293
+ should "default its command summary" do
294
+ assert_equal "", subject.command_summary
295
+ end
296
+
297
+ should "know its command help" do
298
+ Assert.stub(subject, :command_name){ "some-command" }
299
+ Assert.stub(subject, :command_summary){ "some-summary" }
300
+
301
+ exp = "Usage: ardb #{subject.command_name} [options]\n\n" \
302
+ "Options: #{subject.clirb}\n" \
303
+ "Description:\n" \
304
+ " #{subject.command_summary}"
305
+ assert_equal exp, subject.command_help
306
+ end
290
307
  end
291
308
 
292
309
  class ConnectCommandTests < CommandSetupTests
@@ -300,17 +317,12 @@ class Ardb::CLI
300
317
  assert_kind_of ValidCommand, subject
301
318
  end
302
319
 
303
- should "know its summary" do
304
- exp = "Connect to the configured DB"
305
- assert_equal exp, subject.summary
306
- end
320
+ should "know its command name and summary" do
321
+ exp = "connect"
322
+ assert_equal exp, subject.command_name
307
323
 
308
- should "know its help" do
309
- exp = "Usage: ardb connect [options]\n\n" \
310
- "Options: #{subject.clirb}\n" \
311
- "Description:\n" \
312
- " #{subject.summary}"
313
- assert_equal exp, subject.help
324
+ exp = "Connect to the configured DB"
325
+ assert_equal exp, subject.command_summary
314
326
  end
315
327
 
316
328
  should "init ardb and connect to the db when run" do
@@ -319,7 +331,9 @@ class Ardb::CLI
319
331
  assert_equal [false], @ardb_init_called_with
320
332
  assert_true @adapter_spy.connect_db_called?
321
333
 
322
- exp = "connected to #{Ardb.config.adapter} db `#{Ardb.config.database}`\n"
334
+ exp =
335
+ "connected to #{Ardb.config.adapter} "\
336
+ "db #{Ardb.config.database.inspect}\n"
323
337
  assert_equal exp, @stdout.read
324
338
  end
325
339
 
@@ -338,7 +352,6 @@ class Ardb::CLI
338
352
  "with #{Ardb.config.activerecord_connect_hash.inspect}"
339
353
  assert_includes exp, err_output
340
354
  end
341
-
342
355
  end
343
356
 
344
357
  class CreateCommandTests < CommandSetupTests
@@ -352,17 +365,12 @@ class Ardb::CLI
352
365
  assert_kind_of ValidCommand, subject
353
366
  end
354
367
 
355
- should "know its summary" do
356
- exp = "Create the configured DB"
357
- assert_equal exp, subject.summary
358
- end
368
+ should "know its command name and summary" do
369
+ exp = "create"
370
+ assert_equal exp, subject.command_name
359
371
 
360
- should "know its help" do
361
- exp = "Usage: ardb create [options]\n\n" \
362
- "Options: #{subject.clirb}\n" \
363
- "Description:\n" \
364
- " #{subject.summary}"
365
- assert_equal exp, subject.help
372
+ exp = "Create the configured DB"
373
+ assert_equal exp, subject.command_summary
366
374
  end
367
375
 
368
376
  should "init ardb and create the db when run" do
@@ -371,7 +379,8 @@ class Ardb::CLI
371
379
  assert_equal [false], @ardb_init_called_with
372
380
  assert_true @adapter_spy.create_db_called?
373
381
 
374
- exp = "created #{Ardb.config.adapter} db `#{Ardb.config.database}`\n"
382
+ exp =
383
+ "created #{Ardb.config.adapter} db #{Ardb.config.database.inspect}\n"
375
384
  assert_equal exp, @stdout.read
376
385
  end
377
386
 
@@ -386,7 +395,6 @@ class Ardb::CLI
386
395
  exp = "error creating #{Ardb.config.database.inspect} database"
387
396
  assert_includes exp, err_output
388
397
  end
389
-
390
398
  end
391
399
 
392
400
  class DropCommandTests < CommandSetupTests
@@ -400,17 +408,12 @@ class Ardb::CLI
400
408
  assert_kind_of ValidCommand, subject
401
409
  end
402
410
 
403
- should "know its summary" do
404
- exp = "Drop the configured DB"
405
- assert_equal exp, subject.summary
406
- end
411
+ should "know its command name and summary" do
412
+ exp = "drop"
413
+ assert_equal exp, subject.command_name
407
414
 
408
- should "know its help" do
409
- exp = "Usage: ardb drop [options]\n\n" \
410
- "Options: #{subject.clirb}\n" \
411
- "Description:\n" \
412
- " #{subject.summary}"
413
- assert_equal exp, subject.help
415
+ exp = "Drop the configured DB"
416
+ assert_equal exp, subject.command_summary
414
417
  end
415
418
 
416
419
  should "init ardb and drop the db when run" do
@@ -419,7 +422,8 @@ class Ardb::CLI
419
422
  assert_equal [true], @ardb_init_called_with
420
423
  assert_true @adapter_spy.drop_db_called?
421
424
 
422
- exp = "dropped #{Ardb.config.adapter} db `#{Ardb.config.database}`\n"
425
+ exp =
426
+ "dropped #{Ardb.config.adapter} db #{Ardb.config.database.inspect}\n"
423
427
  assert_equal exp, @stdout.read
424
428
  end
425
429
 
@@ -434,35 +438,102 @@ class Ardb::CLI
434
438
  exp = "error dropping #{Ardb.config.database.inspect} database"
435
439
  assert_includes exp, err_output
436
440
  end
441
+ end
442
+
443
+ class GenerateMigrationCommandTests < CommandSetupTests
444
+ desc "GenerateMigrationCommand"
445
+ setup do
446
+ @identifier = Factory.migration_id
447
+
448
+ @migration_spy = nil
449
+ @migration_class = Ardb::Migration
450
+ Assert.stub(@migration_class, :new) do |*args|
451
+ @migration_spy = MigrationSpy.new(*args)
452
+ end
453
+
454
+ @command_class = GenerateMigrationCommand
455
+ @cmd = @command_class.new
456
+ end
457
+
458
+ should "be a valid command" do
459
+ assert_kind_of ValidCommand, subject
460
+ end
461
+
462
+ should "know its command name and summary" do
463
+ exp = "generate-migration"
464
+ assert_equal exp, subject.command_name
465
+
466
+ exp = "Generate a MIGRATION-NAME migration file"
467
+ assert_equal exp, subject.command_summary
468
+ end
469
+
470
+ should "init ardb and save a migration for the identifier when run" do
471
+ subject.run([@identifier], @stdout, @stderr)
472
+
473
+ assert_equal [false], @ardb_init_called_with
474
+ assert_equal Ardb.config, @migration_spy.ardb_config
475
+ assert_equal @identifier, @migration_spy.identifier
476
+ assert_true @migration_spy.save_called
477
+
478
+ exp = "generated #{@migration_spy.file_path}\n"
479
+ assert_equal exp, @stdout.read
480
+ end
437
481
 
482
+ should "re-raise a specific argument error on "\
483
+ "migration \"no identifer\" errors" do
484
+ Assert.stub(@migration_class, :new) do
485
+ raise Ardb::Migration::NoIdentifierError
486
+ end
487
+
488
+ ex =
489
+ assert_that{
490
+ cmd = @command_class.new
491
+ cmd.run([])
492
+ }.raises(ArgumentError)
493
+ exp = "MIGRATION-NAME must be provided"
494
+ assert_equal exp, ex.message
495
+ assert_not_empty ex.backtrace
496
+ end
497
+
498
+ should "output any errors and raise an exit error when run" do
499
+ err = StandardError.new(Factory.string)
500
+ err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
501
+ Assert.stub(@migration_class, :new){ raise err }
502
+
503
+ assert_raises(CommandExitError) do
504
+ subject.run([@identifier], @stdout, @stderr)
505
+ end
506
+ err_output = @stderr.read
507
+
508
+ assert_includes err.to_s, err_output
509
+ assert_includes err.backtrace.join("\n"), err_output
510
+
511
+ exp = "error generating migration"
512
+ assert_includes exp, err_output
513
+ end
438
514
  end
439
515
 
440
516
  class MigrateCommandTests < CommandSetupTests
441
517
  desc "MigrateCommand"
442
518
  setup do
443
- @orig_env_var_migrate_no_schema = ENV['ARDB_MIGRATE_NO_SCHEMA']
519
+ @orig_env_var_migrate_no_schema = ENV["ARDB_MIGRATE_NO_SCHEMA"]
444
520
  @command_class = MigrateCommand
445
521
  @cmd = @command_class.new
446
522
  end
447
523
  teardown do
448
- ENV['ARDB_MIGRATE_NO_SCHEMA'] = @orig_env_var_migrate_no_schema
524
+ ENV["ARDB_MIGRATE_NO_SCHEMA"] = @orig_env_var_migrate_no_schema
449
525
  end
450
526
 
451
- should "be a valid command" do
452
- assert_kind_of ValidCommand, subject
527
+ should "be a migrate command" do
528
+ assert_kind_of MigrateCommandBehaviors, subject
453
529
  end
454
530
 
455
- should "know its summary" do
456
- exp = "Migrate the configured DB"
457
- assert_equal exp, subject.summary
458
- end
531
+ should "know its command name and summary" do
532
+ exp = "migrate"
533
+ assert_equal exp, subject.command_name
459
534
 
460
- should "know its help" do
461
- exp = "Usage: ardb migrate [options]\n\n" \
462
- "Options: #{subject.clirb}\n" \
463
- "Description:\n" \
464
- " #{subject.summary}"
465
- assert_equal exp, subject.help
535
+ exp = "Migrate the configured DB"
536
+ assert_equal exp, subject.command_summary
466
537
  end
467
538
 
468
539
  should "init ardb, migrate the db and dump the schema when run" do
@@ -473,8 +544,9 @@ class Ardb::CLI
473
544
  assert_true @adapter_spy.dump_schema_called?
474
545
  end
475
546
 
476
- should "only init ardb and migrate when run with no schema dump env var set" do
477
- ENV['ARDB_MIGRATE_NO_SCHEMA'] = 'yes'
547
+ should "only init ardb and migrate when run with no schema dump "\
548
+ "env var set" do
549
+ ENV["ARDB_MIGRATE_NO_SCHEMA"] = "yes"
478
550
  subject.run([], @stdout, @stderr)
479
551
 
480
552
  assert_equal [true], @ardb_init_called_with
@@ -496,83 +568,86 @@ class Ardb::CLI
496
568
  exp = "error migrating #{Ardb.config.database.inspect} database"
497
569
  assert_includes exp, err_output
498
570
  end
499
-
500
571
  end
501
572
 
502
- class GenerateMigrationCommandTests < CommandSetupTests
503
- desc "GenerateMigrationCommand"
573
+ class MigrateUpCommandTests < CommandSetupTests
574
+ desc "MigrateUpCommand"
504
575
  setup do
505
- @identifier = Factory.migration_id
506
-
507
- @migration_spy = nil
508
- @migration_class = Ardb::Migration
509
- Assert.stub(@migration_class, :new) do |*args|
510
- @migration_spy = MigrationSpy.new(*args)
511
- end
512
-
513
- @command_class = GenerateMigrationCommand
576
+ @command_class = MigrateUpCommand
514
577
  @cmd = @command_class.new
515
578
  end
516
579
 
517
- should "be a valid command" do
518
- assert_kind_of ValidCommand, subject
580
+ should "be a migrate command" do
581
+ assert_kind_of MigrateCommandBehaviors, subject
519
582
  end
520
583
 
521
- should "know its summary" do
522
- exp = "Generate a migration file given a MIGRATION-NAME"
523
- assert_equal exp, subject.summary
584
+ should "know its command name and summary" do
585
+ exp = "migrate-up"
586
+ assert_equal exp, subject.command_name
587
+
588
+ exp = "Migrate the configured DB up"
589
+ assert_equal exp, subject.command_summary
524
590
  end
591
+ end
525
592
 
526
- should "know its help" do
527
- exp = "Usage: ardb generate-migration [options] MIGRATION-NAME\n\n" \
528
- "Options: #{subject.clirb}\n" \
529
- "Description:\n" \
530
- " #{subject.summary}"
531
- assert_equal exp, subject.help
593
+ class MigrateDownCommandTests < CommandSetupTests
594
+ desc "MigrateDownCommand"
595
+ setup do
596
+ @command_class = MigrateDownCommand
597
+ @cmd = @command_class.new
532
598
  end
533
599
 
534
- should "init ardb and save a migration for the identifier when run" do
535
- subject.run([@identifier], @stdout, @stderr)
600
+ should "be a migrate command" do
601
+ assert_kind_of MigrateCommandBehaviors, subject
602
+ end
536
603
 
537
- assert_equal [false], @ardb_init_called_with
538
- assert_equal Ardb.config, @migration_spy.ardb_config
539
- assert_equal @identifier, @migration_spy.identifier
540
- assert_true @migration_spy.save_called
604
+ should "know its command name and summary" do
605
+ exp = "migrate-down"
606
+ assert_equal exp, subject.command_name
541
607
 
542
- exp = "generated #{@migration_spy.file_path}\n"
543
- assert_equal exp, @stdout.read
608
+ exp = "Migrate the configured DB down"
609
+ assert_equal exp, subject.command_summary
544
610
  end
611
+ end
545
612
 
546
- should "re-raise a specific argument error on migration 'no identifer' errors" do
547
- Assert.stub(@migration_class, :new){ raise Ardb::Migration::NoIdentifierError }
548
- err = nil
549
- begin
550
- cmd = @command_class.new
551
- cmd.run([])
552
- rescue ArgumentError => err
553
- end
613
+ class MigrateForwardCommandTests < CommandSetupTests
614
+ desc "MigrateForwardCommand"
615
+ setup do
616
+ @command_class = MigrateForwardCommand
617
+ @cmd = @command_class.new
618
+ end
554
619
 
555
- assert_not_nil err
556
- exp = "MIGRATION-NAME must be provided"
557
- assert_equal exp, err.message
558
- assert_not_empty err.backtrace
620
+ should "be a migrate command" do
621
+ assert_kind_of MigrateCommandBehaviors, subject
559
622
  end
560
623
 
561
- should "output any errors and raise an exit error when run" do
562
- err = StandardError.new(Factory.string)
563
- err.set_backtrace(Factory.integer(3).times.map{ Factory.path })
564
- Assert.stub(@migration_class, :new){ raise err }
624
+ should "know its command name and summary" do
625
+ exp = "migrate-forward"
626
+ assert_equal exp, subject.command_name
565
627
 
566
- assert_raises(CommandExitError){ subject.run([@identifier], @stdout, @stderr) }
567
- err_output = @stderr.read
628
+ exp = "Migrate the configured DB forward"
629
+ assert_equal exp, subject.command_summary
630
+ end
631
+ end
568
632
 
569
- assert_includes err.to_s, err_output
570
- assert_includes err.backtrace.join("\n"), err_output
633
+ class MigrateBackwardCommandTests < CommandSetupTests
634
+ desc "MigrateBackwardCommand"
635
+ setup do
636
+ @command_class = MigrateBackwardCommand
637
+ @cmd = @command_class.new
638
+ end
571
639
 
572
- exp = "error generating migration"
573
- assert_includes exp, err_output
640
+ should "be a migrate command" do
641
+ assert_kind_of MigrateCommandBehaviors, subject
574
642
  end
575
643
 
644
+ should "know its command name and summary" do
645
+ exp = "migrate-backward"
646
+ assert_equal exp, subject.command_name
647
+
648
+ exp = "Migrate the configured DB backward"
649
+ assert_equal exp, subject.command_summary
650
+ end
576
651
  end
577
652
 
578
653
  class CLISpy
@@ -588,9 +663,10 @@ class Ardb::CLI
588
663
  end
589
664
 
590
665
  class CommandSpy
591
- attr_reader :argv, :stdout, :stderr, :run_called
666
+ attr_reader :command_name, :argv, :stdout, :stderr, :run_called
592
667
 
593
- def initialize
668
+ def initialize(command_name)
669
+ @command_name = command_name
594
670
  @argv = nil
595
671
  @stdout, @stderr = nil, nil
596
672
  @run_called = false
@@ -602,12 +678,12 @@ class Ardb::CLI
602
678
  @run_called = true
603
679
  end
604
680
 
605
- def summary
606
- @summary ||= Factory.string
681
+ def command_summary
682
+ @command_summary ||= Factory.string
607
683
  end
608
684
 
609
- def help
610
- @help ||= Factory.text
685
+ def command_help
686
+ @command_help ||= Factory.text
611
687
  end
612
688
  end
613
689
 
@@ -653,5 +729,4 @@ class Ardb::CLI
653
729
  self
654
730
  end
655
731
  end
656
-
657
732
  end