ardb 0.27.3 → 0.29.0

Sign up to get free protection for your applications and to get access to all the features.
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,22 +1,16 @@
1
- require 'much-plugin'
1
+ require "much-plugin"
2
2
 
3
3
  module Ardb
4
-
5
4
  module UseDbDefault
6
5
  include MuchPlugin
7
6
 
8
7
  plugin_included do
9
- extend ClassMethods
10
- include InstanceMethods
11
-
12
8
  @ardb_use_db_default_attrs = []
13
9
 
14
10
  around_create :ardb_allow_db_to_default_attrs
15
-
16
11
  end
17
12
 
18
- module ClassMethods
19
-
13
+ plugin_class_methods do
20
14
  def ardb_use_db_default_attrs
21
15
  @ardb_use_db_default_attrs
22
16
  end
@@ -25,11 +19,9 @@ module Ardb
25
19
  @ardb_use_db_default_attrs += attrs.map(&:to_s)
26
20
  @ardb_use_db_default_attrs.uniq!
27
21
  end
28
-
29
22
  end
30
23
 
31
- module InstanceMethods
32
-
24
+ plugin_instance_methods do
33
25
  private
34
26
 
35
27
  def ardb_allow_db_to_default_attrs
@@ -42,15 +34,12 @@ module Ardb
42
34
  unchanged_names.each{ |name| @attributes.delete(name) }
43
35
  yield
44
36
  # we have to go and fetch the attr value from the DB, otherwise
45
- # activerecord doesn't know the value that the DB used
37
+ # activerecord doesn"t know the value that the DB used
46
38
  scope = self.class.where(:id => self.id)
47
39
  unchanged_names.each do |name|
48
40
  @attributes[name] = scope.pluck(name).first
49
41
  end
50
42
  end
51
-
52
43
  end
53
-
54
44
  end
55
-
56
45
  end
@@ -1,3 +1,3 @@
1
1
  module Ardb
2
- VERSION = "0.27.3"
2
+ VERSION = "0.29.0"
3
3
  end
@@ -1,8 +1,8 @@
1
- require 'active_record'
1
+ require "active_record"
2
2
 
3
- # this can be slow, this is one of the reasons this shouldn't be done during
3
+ # this can be slow, this is one of the reasons this shouldn"t be done during
4
4
  # the startup of our apps
5
- gemspec = Gem.loaded_specs['activerecord']
5
+ gemspec = Gem.loaded_specs["activerecord"]
6
6
 
7
7
  puts "Looking at files in: "
8
8
  puts " #{gemspec.lib_dirs_glob.inspect}"
@@ -11,15 +11,18 @@ paths = Dir["#{gemspec.lib_dirs_glob}/**/*.rb"]
11
11
 
12
12
  # these are regexs for files we want to ignore requiring. for example,
13
13
  # generators fail when we try to require them. the others are pieces of active
14
- # record we don't use in a production environment
14
+ # record we don"t use in a production environment
15
15
  ignored_regexes = [
16
16
  /rails\/generators/,
17
17
  /active_record\/railtie/,
18
18
  /active_record\/migration/,
19
19
  /active_record\/fixtures/,
20
+ /active_record\/fixture_set/,
20
21
  /active_record\/schema/,
21
22
  /active_record\/connection_adapters/,
22
23
  /active_record\/test_case/,
24
+ /active_record\/test_databases/,
25
+ /active_record\/test_fixtures/,
23
26
  /active_record\/coders\/yaml_column/
24
27
  ]
25
28
 
@@ -31,12 +34,12 @@ ignored = []
31
34
  errored = []
32
35
 
33
36
  paths.sort.each do |full_path|
34
- relative_path_with_rb = full_path.gsub("#{gemspec.lib_dirs_glob}/", '')
35
- relative_path = relative_path_with_rb.gsub(/\.rb\z/, '')
37
+ relative_path_with_rb = full_path.gsub("#{gemspec.lib_dirs_glob}/", "")
38
+ relative_path = relative_path_with_rb.gsub(/\.rb\z/, "")
36
39
 
37
40
  result = Result.new(relative_path)
38
41
 
39
- # see if it's ignored
42
+ # see if it"s ignored
40
43
  ignored_regexes.each do |regex|
41
44
  if relative_path =~ regex
42
45
  result.state = :ignored
@@ -87,5 +90,5 @@ puts "\n"
87
90
 
88
91
  puts "Needs To Be Required:\n"
89
92
  needs_to_be_required.each do |result|
90
- puts "require '#{result.file}'"
93
+ puts "require \"#{result.file}\""
91
94
  end
@@ -4,10 +4,13 @@
4
4
  # add the root dir to the load path
5
5
  $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
6
6
 
7
- # require pry for debugging (`binding.pry`)
8
- require 'pry'
9
- require 'test/support/factory'
7
+ TEST_SUPPORT_PATH = File.expand_path("../support", __FILE__)
8
+ TMP_PATH = File.expand_path("../../tmp", __FILE__)
9
+
10
+ require "logger"
11
+ log_path = File.expand_path("../../log/test.log", __FILE__)
12
+ TEST_LOGGER = Logger.new(File.open(log_path, "w"))
10
13
 
11
- ENV['ARDB_DB_FILE'] = 'tmp/testdb/config/db'
12
- require 'ardb'
13
- Ardb.init(false)
14
+ # require pry for debugging (`binding.pry`)
15
+ require "pry"
16
+ require "test/support/factory"
@@ -1,11 +1,26 @@
1
- require 'assert/factory'
1
+ require "assert/factory"
2
2
 
3
3
  module Factory
4
4
  extend Assert::Factory
5
5
 
6
6
  def self.migration_id
7
- # identifiers need to be plural b/c af activesupport's pluralize
7
+ # identifiers need to be plural b/c af activesupport"s pluralize
8
8
  "#{Factory.string}_things"
9
9
  end
10
10
 
11
+ def self.ardb_config
12
+ Ardb::Config.new.tap do |c|
13
+ c.adapter = Factory.string
14
+ c.database = Factory.string
15
+ c.encoding = Factory.string
16
+ c.host = Factory.string
17
+ c.port = Factory.integer
18
+ c.username = Factory.string
19
+ c.password = Factory.string
20
+ c.pool = Factory.integer
21
+ c.checkout_timeout = Factory.integer
22
+ c.min_messages = Factory.string
23
+ end
24
+ end
25
+
11
26
  end
@@ -0,0 +1,5 @@
1
+ if !defined?(FAKE_SCHEMA)
2
+ fake_schema_class = Struct.new(:load_count)
3
+ FAKE_SCHEMA = fake_schema_class.new(0)
4
+ end
5
+ FAKE_SCHEMA.load_count += 1
@@ -0,0 +1,2 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ end
@@ -0,0 +1,51 @@
1
+ require "assert"
2
+ require "ardb"
3
+
4
+ class PostgresqlDbTests < Assert::Context
5
+ setup do
6
+ @orig_env_ardb_db_file = ENV["ARDB_DB_FILE"]
7
+ ActiveRecord::Base.logger = @orig_ar_loggerF
8
+
9
+ # no-op, we"re manually configuring ardb so we don"t need this to do anything
10
+ ENV["ARDB_DB_FILE"] = File.join(TEST_SUPPORT_PATH, "require_test_db_file")
11
+
12
+ @ardb_config = Ardb::Config.new.tap do |c|
13
+ c.adapter = "postgresql"
14
+ c.database = "redding_ardb_test"
15
+ c.encoding = "unicode"
16
+ c.min_messages = "WARNING"
17
+
18
+ c.logger = TEST_LOGGER
19
+ c.root_path = File.join(TEST_SUPPORT_PATH, "postgresql")
20
+ c.migrations_path = "migrations"
21
+ c.schema_path = "schema"
22
+ c.schema_format = :ruby
23
+ end
24
+ Assert.stub(Ardb, :config){ @ardb_config }
25
+
26
+ Ardb.init(false)
27
+
28
+ Ardb.adapter.drop_db
29
+ Ardb.adapter.create_db
30
+ Ardb.adapter.load_schema
31
+ Ardb.adapter.connect_db
32
+ end
33
+ teardown do
34
+ Ardb.reset_adapter
35
+ ActiveRecord::Base.logger = @orig_ar_logger
36
+ ENV["ARDB_DB_FILE"] = @orig_env_ardb_db_file
37
+ end
38
+
39
+ private
40
+
41
+ # useful when testing creating/dropping/migrating DBs
42
+ def silence_stdout
43
+ current_stdout = $stdout.dup
44
+ $stdout = File.new("/dev/null", "w")
45
+ begin
46
+ yield
47
+ ensure
48
+ $stdout = current_stdout
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,2 @@
1
+ # this file is used to test that Ardb will relatively require a db file when
2
+ # init
@@ -0,0 +1 @@
1
+ # this file is used to test that Ardb will require a db file when init
File without changes
@@ -1,34 +1,44 @@
1
- require 'assert'
2
- require 'ardb/adapter/base'
1
+ require "assert"
2
+ require "ardb/adapter/base"
3
3
 
4
- class Ardb::Adapter::Base
4
+ require "ardb"
5
+ # This is needed by the schema dumper but it doesn"t handle requiring it so we
6
+ # have to manually, otherwise this errors when you try to run the adapter base
7
+ # tests by themselves
8
+ require "active_support/core_ext/class/attribute_accessors"
5
9
 
10
+ class Ardb::Adapter::Base
6
11
  class UnitTests < Assert::Context
7
12
  desc "Ardb::Adapter::Base"
8
13
  setup do
9
- @adapter = Ardb::Adapter::Base.new
14
+ @config = Factory.ardb_config
15
+ @adapter = Ardb::Adapter::Base.new(@config)
10
16
  end
11
17
  subject{ @adapter }
12
18
 
13
- should have_readers :config_settings, :database
14
- should have_readers :ruby_schema_path, :sql_schema_path
19
+ should have_readers :config
20
+ should have_imeths :connect_hash, :database, :migrations_path
21
+ should have_imeths :schema_format, :ruby_schema_path, :sql_schema_path
15
22
  should have_imeths :escape_like_pattern
16
- should have_imeths :foreign_key_add_sql, :foreign_key_drop_sql
17
- should have_imeths :create_db, :drop_db, :connect_db, :migrate_db
23
+ should have_imeths :create_db, :drop_db, :drop_tables
24
+ should have_imeths :connect_db, :migrate_db
18
25
  should have_imeths :load_schema, :load_ruby_schema, :load_sql_schema
19
26
  should have_imeths :dump_schema, :dump_ruby_schema, :dump_sql_schema
20
27
 
21
- should "know its config settings " do
22
- assert_equal Ardb.config.db_settings, subject.config_settings
28
+ should "know its config" do
29
+ assert_equal @config, subject.config
23
30
  end
24
31
 
25
- should "know its database" do
26
- assert_equal Ardb.config.db.database, subject.database
32
+ should "demeter its config" do
33
+ assert_equal @config.activerecord_connect_hash, subject.connect_hash
34
+ assert_equal @config.database, subject.database
35
+ assert_equal @config.migrations_path, subject.migrations_path
36
+ assert_equal @config.schema_format, subject.schema_format
27
37
  end
28
38
 
29
- should "know its schema paths" do
30
- assert_equal "#{Ardb.config.schema_path}.rb", subject.ruby_schema_path
31
- assert_equal "#{Ardb.config.schema_path}.sql", subject.sql_schema_path
39
+ should "know its ruby and sql schema paths" do
40
+ assert_equal "#{@config.schema_path}.rb", subject.ruby_schema_path
41
+ assert_equal "#{@config.schema_path}.sql", subject.sql_schema_path
32
42
  end
33
43
 
34
44
  should "know how to escape like patterns" do
@@ -37,12 +47,15 @@ class Ardb::Adapter::Base
37
47
  "#{Factory.string}\\" \
38
48
  "#{Factory.string} " \
39
49
  "#{Factory.string}"
40
- exp = pattern.gsub("\\"){ "\\\\" }.gsub('%', "\\%").gsub('_', "\\_")
50
+ exp = pattern.gsub("\\"){ "\\\\" }.gsub("%", "\\%").gsub("_", "\\_")
41
51
  assert_equal exp, subject.escape_like_pattern(pattern)
52
+
53
+ pattern = Factory.string
54
+ assert_equal pattern, subject.escape_like_pattern(pattern)
42
55
  end
43
56
 
44
57
  should "allow using a custom escape char when escaping like patterns" do
45
- escape_char = '#'
58
+ escape_char = "#"
46
59
  pattern = "#{Factory.string}%" \
47
60
  "#{Factory.string}_" \
48
61
  "#{Factory.string}\\" \
@@ -50,15 +63,10 @@ class Ardb::Adapter::Base
50
63
  "#{Factory.string} " \
51
64
  "#{Factory.string}"
52
65
  exp = pattern.gsub(escape_char, "#{escape_char}#{escape_char}")
53
- exp = exp.gsub('%', "#{escape_char}%").gsub('_', "#{escape_char}_")
66
+ exp = exp.gsub("%", "#{escape_char}%").gsub("_", "#{escape_char}_")
54
67
  assert_equal exp, subject.escape_like_pattern(pattern, escape_char)
55
68
  end
56
69
 
57
- should "not implement the foreign key sql meths" do
58
- assert_raises(NotImplementedError){ subject.foreign_key_add_sql }
59
- assert_raises(NotImplementedError){ subject.foreign_key_drop_sql }
60
- end
61
-
62
70
  should "not implement the create and drop db methods" do
63
71
  assert_raises(NotImplementedError){ subject.create_db }
64
72
  assert_raises(NotImplementedError){ subject.drop_db }
@@ -73,60 +81,89 @@ class Ardb::Adapter::Base
73
81
  assert_raises(NotImplementedError){ subject.dump_sql_schema }
74
82
  end
75
83
 
84
+ should "know if its equal to another adapter" do
85
+ matching_adapter = Ardb::Adapter::Base.new(@config)
86
+ assert_equal matching_adapter, subject
87
+
88
+ non_matching_adapter = Ardb::Adapter::Base.new(Factory.ardb_config)
89
+ assert_not_equal non_matching_adapter, subject
90
+ end
76
91
  end
77
92
 
78
93
  class ConnectDbTests < UnitTests
79
94
  desc "`connect_db`"
80
95
  setup do
81
- @ar_base_conn_called = false
82
- Assert.stub(ActiveRecord::Base, :connection) do |*args|
83
- @ar_base_conn_called = true
96
+ @ar_establish_connection_called_with = nil
97
+ Assert.stub(ActiveRecord::Base, :establish_connection) do |options|
98
+ @ar_establish_connection_called_with = options
84
99
  end
85
100
 
86
- @adapter.connect_db
87
- end
101
+ @ar_connection_pool = FakeConnectionPool.new
102
+ Assert.stub(ActiveRecord::Base, :connection_pool){ @ar_connection_pool }
103
+
104
+ @ar_with_connection_called = false
105
+ Assert.stub(@ar_connection_pool, :with_connection) do
106
+ @ar_with_connection_called = true
107
+ end
88
108
 
89
- should "call activerecord base's connection method" do
90
- assert_true @ar_base_conn_called
109
+ subject.connect_db
91
110
  end
92
111
 
112
+ should "use activerecord to establish and then checkout a connection" do
113
+ assert_equal subject.connect_hash, @ar_establish_connection_called_with
114
+ assert_true @ar_with_connection_called
115
+ end
93
116
  end
94
117
 
95
- class MigrateDbTests < UnitTests
96
- desc "`migrate_db`"
118
+ class MigrateTests < UnitTests
97
119
  setup do
98
- ENV["MIGRATE_VERSION"] = Factory.integer.to_s if Factory.boolean
99
- ENV["MIGRATE_QUIET"] = Factory.boolean.to_s if Factory.boolean
100
-
101
- @migrator_called_with = []
102
- Assert.stub(ActiveRecord::Migrator, :migrate) do |*args|
103
- @migrator_called_with = args
120
+ Assert.stub(ActiveRecord::MigrationContext, :new) do |*args, &block|
121
+ @fake_migration_context ||= FakeMigrationContext.new(*args)
104
122
  end
105
-
106
- @adapter.migrate_db
107
123
  end
108
124
 
109
- should "add the ardb migration helper recorder to activerecord's command recorder" do
110
- exp = Ardb::MigrationHelpers::RecorderMixin
111
- assert_includes exp, ActiveRecord::Migration::CommandRecorder
125
+ should "migrate the db with `migrate_db`" do
126
+ subject.migrate_db
127
+
128
+ assert_equal [subject.migrations_path], @fake_migration_context.init_with
129
+ assert_equal [nil], @fake_migration_context.up_called_with
112
130
  end
113
131
 
114
- should "set the activerecord migrator's migrations path" do
115
- exp = Ardb.config.migrations_path
116
- assert_equal exp, ActiveRecord::Migrator.migrations_path
132
+ should "migrate the db with `migrate_db_up`" do
133
+ subject.migrate_db_up
134
+ assert_equal [nil], @fake_migration_context.up_called_with
135
+
136
+ target = Factory.string
137
+ subject.migrate_db_up(target)
138
+ assert_equal [target], @fake_migration_context.up_called_with
117
139
  end
118
140
 
119
- should "set the activerecord migration's verbose attr" do
120
- exp = ENV["MIGRATE_QUIET"].nil?
121
- assert_equal exp, ActiveRecord::Migration.verbose
141
+ should "migrate the db with `migrate_db_down`" do
142
+ subject.migrate_db_down
143
+ assert_equal [nil], @fake_migration_context.down_called_with
144
+
145
+ target = Factory.string
146
+ subject.migrate_db_down(target)
147
+ assert_equal [target], @fake_migration_context.down_called_with
122
148
  end
123
149
 
124
- should "call the activerecord migrator's migrate method" do
125
- version = ENV.key?("MIGRATE_VERSION") ? ENV["MIGRATE_VERSION"].to_i : nil
126
- exp = [Ardb.config.migrations_path, version]
127
- assert_equal exp, @migrator_called_with
150
+ should "migrate the db with `migrate_db_forward`" do
151
+ subject.migrate_db_forward
152
+ assert_equal [1], @fake_migration_context.forward_called_with
153
+
154
+ steps = Factory.integer(3)
155
+ subject.migrate_db_forward(steps)
156
+ assert_equal [steps], @fake_migration_context.forward_called_with
128
157
  end
129
158
 
159
+ should "migrate the db with `migrate_db_backward`" do
160
+ subject.migrate_db_backward
161
+ assert_equal [1], @fake_migration_context.rollback_called_with
162
+
163
+ steps = Factory.integer(3)
164
+ subject.migrate_db_backward(steps)
165
+ assert_equal [steps], @fake_migration_context.rollback_called_with
166
+ end
130
167
  end
131
168
 
132
169
  class LoadAndDumpSchemaTests < UnitTests
@@ -135,18 +172,15 @@ class Ardb::Adapter::Base
135
172
  @captured_stdout = ""
136
173
  $stdout = StringIO.new(@captured_stdout)
137
174
 
138
- @orig_schema_format = Ardb.config.schema_format
139
-
140
- @adapter = SchemaSpyAdapter.new
175
+ @adapter = SchemaSpyAdapter.new(@config)
141
176
  end
142
177
  teardown do
143
- Ardb.config.schema_format = @orig_schema_format
144
178
  $stdout = @orig_stdout
145
179
  end
146
180
 
147
181
  should "load a ruby schema using `load_schema` when the format is ruby" do
148
- Ardb.config.schema_format = :ruby
149
- adapter = SchemaSpyAdapter.new
182
+ @config.schema_format = Ardb::Config::RUBY_SCHEMA_FORMAT
183
+ adapter = SchemaSpyAdapter.new(@config)
150
184
 
151
185
  assert_false adapter.load_ruby_schema_called
152
186
  assert_false adapter.load_sql_schema_called
@@ -156,8 +190,8 @@ class Ardb::Adapter::Base
156
190
  end
157
191
 
158
192
  should "load a SQL schema using `load_schema` when the format is sql" do
159
- Ardb.config.schema_format = :sql
160
- adapter = SchemaSpyAdapter.new
193
+ @config.schema_format = Ardb::Config::SQL_SCHEMA_FORMAT
194
+ adapter = SchemaSpyAdapter.new(@config)
161
195
 
162
196
  assert_false adapter.load_ruby_schema_called
163
197
  assert_false adapter.load_sql_schema_called
@@ -172,8 +206,8 @@ class Ardb::Adapter::Base
172
206
  end
173
207
 
174
208
  should "always dump a ruby schema using `dump_schema`" do
175
- Ardb.config.schema_format = :ruby
176
- adapter = SchemaSpyAdapter.new
209
+ @config.schema_format = Ardb::Config::RUBY_SCHEMA_FORMAT
210
+ adapter = SchemaSpyAdapter.new(@config)
177
211
 
178
212
  assert_false adapter.dump_ruby_schema_called
179
213
  assert_false adapter.dump_sql_schema_called
@@ -181,8 +215,8 @@ class Ardb::Adapter::Base
181
215
  assert_true adapter.dump_ruby_schema_called
182
216
  assert_false adapter.dump_sql_schema_called
183
217
 
184
- Ardb.config.schema_format = :sql
185
- adapter = SchemaSpyAdapter.new
218
+ @config.schema_format = Ardb::Config::SQL_SCHEMA_FORMAT
219
+ adapter = SchemaSpyAdapter.new(@config)
186
220
 
187
221
  assert_false adapter.dump_ruby_schema_called
188
222
  adapter.dump_schema
@@ -190,8 +224,8 @@ class Ardb::Adapter::Base
190
224
  end
191
225
 
192
226
  should "dump a SQL schema using `dump_schema` when the format is sql" do
193
- Ardb.config.schema_format = :sql
194
- adapter = SchemaSpyAdapter.new
227
+ @config.schema_format = Ardb::Config::SQL_SCHEMA_FORMAT
228
+ adapter = SchemaSpyAdapter.new(@config)
195
229
 
196
230
  assert_false adapter.dump_ruby_schema_called
197
231
  adapter.dump_schema
@@ -202,18 +236,12 @@ class Ardb::Adapter::Base
202
236
  subject.load_schema
203
237
  assert_empty @captured_stdout
204
238
  end
205
-
206
239
  end
207
240
 
208
241
  class LoadRubySchemaTests < UnitTests
209
242
  setup do
210
- @orig_schema_path = Ardb.config.schema_path
211
- Ardb.config.schema_path = 'fake_schema'
212
-
213
- @adapter = Ardb::Adapter::Base.new
214
- end
215
- teardown do
216
- Ardb.config.schema_path = @orig_schema_path
243
+ @config.schema_path = File.join(TEST_SUPPORT_PATH, "fake_schema")
244
+ @adapter = Ardb::Adapter::Base.new(@config)
217
245
  end
218
246
 
219
247
  should "load a ruby schema file using `load_ruby_schema`" do
@@ -224,9 +252,40 @@ class Ardb::Adapter::Base
224
252
  subject.load_ruby_schema
225
253
  assert_equal 2, FAKE_SCHEMA.load_count
226
254
  end
255
+ end
256
+
257
+ class DumpRubySchemaTests < UnitTests
258
+ setup do
259
+ @config.schema_path = File.join(TMP_PATH, "testdb", "test_dump_ruby_schema")
260
+ FileUtils.rm_rf(File.dirname(@config.schema_path))
261
+
262
+ @schema_dumper_connection, @schema_dumper_file = [nil, nil]
263
+ Assert.stub(ActiveRecord::SchemaDumper, :dump) do |connection, file|
264
+ @schema_dumper_connection = connection
265
+ @schema_dumper_file = file
266
+ end
267
+
268
+ @fake_connection = FakeConnection.new
269
+ Assert.stub(ActiveRecord::Base, :connection){ @fake_connection }
270
+
271
+ @adapter = Ardb::Adapter::Base.new(@config)
272
+ end
273
+ teardown do
274
+ FileUtils.rm_rf(File.dirname(@config.schema_path))
275
+ end
227
276
 
277
+ should "dump a ruby schema file using `dump_ruby_schema`" do
278
+ assert_false File.exists?(subject.ruby_schema_path)
279
+ subject.dump_ruby_schema
280
+ assert_true File.exists?(subject.ruby_schema_path)
281
+ assert_equal @fake_connection, @schema_dumper_connection
282
+ assert_instance_of File, @schema_dumper_file
283
+ assert_equal subject.ruby_schema_path, @schema_dumper_file.path
284
+ end
228
285
  end
229
286
 
287
+ class FakeConnection; end
288
+
230
289
  class SchemaSpyAdapter < Ardb::Adapter::Base
231
290
  attr_reader :load_ruby_schema_called, :dump_ruby_schema_called
232
291
  attr_reader :load_sql_schema_called, :dump_sql_schema_called
@@ -260,4 +319,33 @@ class Ardb::Adapter::Base
260
319
  end
261
320
  end
262
321
 
322
+ class FakeConnectionPool
323
+ def with_connection(&block); end
324
+ end
325
+
326
+ class FakeMigrationContext
327
+ attr_reader :init_with
328
+ attr_reader :up_called_with, :down_called_with
329
+ attr_reader :forward_called_with, :rollback_called_with
330
+
331
+ def initialize(*args)
332
+ @init_with = args
333
+ end
334
+
335
+ def up(*args)
336
+ @up_called_with = args
337
+ end
338
+
339
+ def down(*args)
340
+ @down_called_with = args
341
+ end
342
+
343
+ def forward(*args)
344
+ @forward_called_with = args
345
+ end
346
+
347
+ def rollback(*args)
348
+ @rollback_called_with = args
349
+ end
350
+ end
263
351
  end