ardb 0.28.3 → 0.29.0

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