ardb 0.27.3 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -1
  3. data/ardb.gemspec +3 -4
  4. data/lib/ardb.rb +135 -68
  5. data/lib/ardb/adapter/base.rb +39 -24
  6. data/lib/ardb/adapter/mysql.rb +1 -2
  7. data/lib/ardb/adapter/postgresql.rb +14 -12
  8. data/lib/ardb/adapter/sqlite.rb +3 -8
  9. data/lib/ardb/adapter_spy.rb +67 -87
  10. data/lib/ardb/cli.rb +11 -219
  11. data/lib/ardb/{clirb.rb → cli/clirb.rb} +2 -1
  12. data/lib/ardb/cli/commands.rb +275 -0
  13. data/lib/ardb/migration.rb +8 -6
  14. data/lib/ardb/migration_helpers.rb +1 -1
  15. data/lib/ardb/pg_json.rb +90 -0
  16. data/lib/ardb/version.rb +1 -1
  17. data/test/helper.rb +15 -3
  18. data/test/support/factory.rb +15 -0
  19. data/test/support/fake_schema.rb +5 -0
  20. data/test/support/postgresql/migrations/.gitkeep +0 -0
  21. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +13 -0
  22. data/test/support/postgresql/schema.rb +3 -0
  23. data/test/support/postgresql/setup_test_db.rb +51 -0
  24. data/test/support/relative_require_test_db_file.rb +2 -0
  25. data/test/support/require_test_db_file.rb +1 -0
  26. data/test/system/pg_json_tests.rb +85 -0
  27. data/test/unit/adapter/base_tests.rb +104 -39
  28. data/test/unit/adapter/mysql_tests.rb +2 -1
  29. data/test/unit/adapter/postgresql_tests.rb +10 -9
  30. data/test/unit/adapter/sqlite_tests.rb +8 -3
  31. data/test/unit/adapter_spy_tests.rb +57 -66
  32. data/test/unit/ardb_tests.rb +323 -36
  33. data/test/unit/cli_tests.rb +193 -146
  34. data/test/unit/has_slug_tests.rb +9 -9
  35. data/test/unit/migration_helpers_tests.rb +18 -12
  36. data/test/unit/migration_tests.rb +18 -11
  37. data/test/unit/pg_json_tests.rb +39 -0
  38. data/test/unit/record_spy_tests.rb +1 -1
  39. data/test/unit/test_helpers_tests.rb +2 -6
  40. data/test/unit/use_db_default_tests.rb +2 -2
  41. metadata +29 -34
  42. data/lib/ardb/root_path.rb +0 -15
  43. data/test/unit/config_tests.rb +0 -58
@@ -8,18 +8,19 @@ class Ardb::Adapter::Postgresql
8
8
  class UnitTests < Assert::Context
9
9
  desc "Ardb::Adapter::Postgresql"
10
10
  setup do
11
- @adapter = Ardb::Adapter::Postgresql.new
11
+ @config = Factory.ardb_config
12
+ @adapter = Ardb::Adapter::Postgresql.new(@config)
12
13
  end
13
14
  subject{ @adapter }
14
15
 
15
- should have_imeths :public_schema_settings
16
+ should have_imeths :public_connect_hash
16
17
 
17
- should "know its public schema connection settings" do
18
- exp_settings = subject.config_settings.merge({
18
+ should "know its public connect hash" do
19
+ exp = subject.connect_hash.merge({
19
20
  'database' => 'postgres',
20
21
  'schema_search_path' => 'public'
21
22
  })
22
- assert_equal exp_settings, subject.public_schema_settings
23
+ assert_equal exp, subject.public_connect_hash
23
24
  end
24
25
 
25
26
  should "know its foreign key add sql" do
@@ -41,10 +42,10 @@ class Ardb::Adapter::Postgresql
41
42
  class SQLSchemaTests < UnitTests
42
43
  setup do
43
44
  @env = {
44
- 'PGHOST' => @adapter.config_settings['host'],
45
- 'PGPORT' => @adapter.config_settings['port'],
46
- 'PGUSER' => @adapter.config_settings['username'],
47
- 'PGPASSWORD' => @adapter.config_settings['password']
45
+ 'PGHOST' => @adapter.connect_hash['host'],
46
+ 'PGPORT' => @adapter.connect_hash['port'],
47
+ 'PGUSER' => @adapter.connect_hash['username'],
48
+ 'PGPASSWORD' => @adapter.connect_hash['password']
48
49
  }
49
50
  end
50
51
 
@@ -6,7 +6,8 @@ class Ardb::Adapter::Sqlite
6
6
  class UnitTests < Assert::Context
7
7
  desc "Ardb::Adapter::Sqlite"
8
8
  setup do
9
- @adapter = Ardb::Adapter::Sqlite.new
9
+ @config = Factory.ardb_config
10
+ @adapter = Ardb::Adapter::Sqlite.new(@config)
10
11
  end
11
12
  subject{ @adapter }
12
13
 
@@ -20,8 +21,12 @@ class Ardb::Adapter::Sqlite
20
21
  end
21
22
 
22
23
  should "know its db file path" do
23
- exp_path = Ardb.config.root_path.join(Ardb.config.db.database).to_s
24
- assert_equal exp_path, subject.db_file_path
24
+ exp = File.expand_path(@config.database, @config.root_path)
25
+ assert_equal exp, subject.db_file_path
26
+
27
+ @config.database = File.join(TMP_PATH, 'abs_sqlite_db_test')
28
+ adapter = Ardb::Adapter::Sqlite.new(@config)
29
+ assert_equal @config.database, adapter.db_file_path
25
30
  end
26
31
 
27
32
  should "not implement the foreign key sql meths" do
@@ -1,106 +1,97 @@
1
1
  require 'assert'
2
2
  require 'ardb/adapter_spy'
3
3
 
4
- require 'much-plugin'
5
-
6
- module Ardb::AdapterSpy
4
+ class Ardb::AdapterSpy
7
5
 
8
6
  class UnitTests < Assert::Context
9
7
  desc "Ardb::AdapterSpy"
10
8
  setup do
11
- @adapter = MyAdapter.new
9
+ @adapter_spy_class = Ardb::AdapterSpy
10
+ end
11
+ subject{ @adapter_spy_class }
12
+
13
+ should "be a kind of ardb adapter" do
14
+ assert subject < Ardb::Adapter::Base
12
15
  end
13
- subject{ @adapter }
16
+
17
+ end
18
+
19
+ class InitTests < UnitTests
20
+ desc "when init"
21
+ setup do
22
+ @config = Factory.ardb_config
23
+ @adapter_spy = @adapter_spy_class.new(@config)
24
+ end
25
+ subject{ @adapter_spy }
14
26
 
15
27
  should have_accessors :drop_tables_called_count
16
28
  should have_accessors :dump_schema_called_count, :load_schema_called_count
17
29
  should have_accessors :drop_db_called_count, :create_db_called_count
18
30
  should have_accessors :connect_db_called_count, :migrate_db_called_count
19
- should have_imeths :drop_tables_called?, :drop_tables
20
- should have_imeths :dump_schema_called?, :dump_schema
21
- should have_imeths :load_schema_called?, :load_schema
22
- should have_imeths :drop_db_called?, :drop_db
23
- should have_imeths :create_db_called?, :create_db
24
- should have_imeths :connect_db_called?, :connect_db
25
- should have_imeths :migrate_db_called?, :migrate_db
26
-
27
- should "use much-plugin" do
28
- assert_includes MuchPlugin, Ardb::AdapterSpy
29
- end
30
-
31
- should "included the adapter spy instance methods" do
32
- assert_includes Ardb::AdapterSpy::InstanceMethods, subject.class
33
- end
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?
33
+ should have_imeths :connect_db_called?, :migrate_db_called?
34
+ should have_imeths :dump_schema_called?, :load_schema_called?
35
+ should have_imeths :create_db, :drop_db, :drop_tables
36
+ should have_imeths :connect_db, :migrate_db
37
+ should have_imeths :dump_schema, :load_schema
34
38
 
35
39
  should "default all call counts to zero" do
36
- assert_equal 0, subject.drop_tables_called_count
37
- assert_equal 0, subject.dump_schema_called_count
38
- assert_equal 0, subject.load_schema_called_count
39
- assert_equal 0, subject.drop_db_called_count
40
40
  assert_equal 0, subject.create_db_called_count
41
+ assert_equal 0, subject.drop_db_called_count
42
+ assert_equal 0, subject.drop_tables_called_count
41
43
  assert_equal 0, subject.connect_db_called_count
42
44
  assert_equal 0, subject.migrate_db_called_count
45
+ assert_equal 0, subject.load_schema_called_count
46
+ assert_equal 0, subject.dump_schema_called_count
43
47
  end
44
48
 
45
- should "know if and how many times a method is called" do
46
- assert_equal false, subject.drop_tables_called?
47
- subject.drop_tables
48
- assert_equal 1, subject.drop_tables_called_count
49
- assert_equal true, subject.drop_tables_called?
50
-
51
- assert_equal false, subject.dump_schema_called?
52
- subject.dump_schema
53
- assert_equal 1, subject.dump_schema_called_count
54
- assert_equal true, subject.dump_schema_called?
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
55
57
 
56
- assert_equal false, subject.load_schema_called?
57
- subject.load_schema
58
- assert_equal 1, subject.load_schema_called_count
59
- assert_equal true, subject.load_schema_called?
58
+ should "know if and how many times a method is called" do
59
+ assert_equal false, subject.create_db_called?
60
+ subject.create_db
61
+ assert_equal 1, subject.create_db_called_count
62
+ assert_equal true, subject.create_db_called?
60
63
 
61
64
  assert_equal false, subject.drop_db_called?
62
65
  subject.drop_db
63
66
  assert_equal 1, subject.drop_db_called_count
64
67
  assert_equal true, subject.drop_db_called?
65
68
 
66
- assert_equal false, subject.create_db_called?
67
- subject.create_db
68
- assert_equal 1, subject.create_db_called_count
69
- assert_equal true, subject.create_db_called?
70
-
71
- assert_equal false, subject.migrate_db_called?
72
- subject.migrate_db
73
- assert_equal 1, subject.migrate_db_called_count
74
- assert_equal true, subject.migrate_db_called?
69
+ assert_equal false, subject.drop_tables_called?
70
+ subject.drop_tables
71
+ assert_equal 1, subject.drop_tables_called_count
72
+ assert_equal true, subject.drop_tables_called?
75
73
 
76
74
  assert_equal false, subject.connect_db_called?
77
75
  subject.connect_db
78
76
  assert_equal 1, subject.connect_db_called_count
79
77
  assert_equal true, subject.connect_db_called?
80
- end
81
78
 
82
- end
79
+ assert_equal false, subject.migrate_db_called?
80
+ subject.migrate_db
81
+ assert_equal 1, subject.migrate_db_called_count
82
+ assert_equal true, subject.migrate_db_called?
83
83
 
84
- class NewMethTests < UnitTests
85
- desc "`new` method"
86
- setup do
87
- @adapter_spy_class = Ardb::AdapterSpy.new do
88
- attr_accessor :name
89
- end
90
- @adapter = @adapter_spy_class.new
91
- end
92
- subject{ @adapter }
84
+ assert_equal false, subject.dump_schema_called?
85
+ subject.dump_schema
86
+ assert_equal 1, subject.dump_schema_called_count
87
+ assert_equal true, subject.dump_schema_called?
93
88
 
94
- should "build a new spy class and use any custom definition" do
95
- assert_includes Ardb::AdapterSpy, subject.class
96
- assert subject.respond_to? :name
97
- assert subject.respond_to? :name=
89
+ assert_equal false, subject.load_schema_called?
90
+ subject.load_schema
91
+ assert_equal 1, subject.load_schema_called_count
92
+ assert_equal true, subject.load_schema_called?
98
93
  end
99
94
 
100
95
  end
101
96
 
102
- class MyAdapter
103
- include Ardb::AdapterSpy
104
- end
105
-
106
97
  end
@@ -1,67 +1,133 @@
1
1
  require 'assert'
2
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'
9
+
4
10
  module Ardb
5
11
 
6
12
  class UnitTests < Assert::Context
7
13
  desc "Ardb"
8
14
  setup do
9
- @orig_ar_logger = ActiveRecord::Base.logger
10
- Adapter.reset
11
-
12
15
  @module = Ardb
13
16
  end
14
- teardown do
15
- Adapter.reset
16
- ActiveRecord::Base.logger = @orig_ar_logger
17
- end
18
17
  subject{ @module }
19
18
 
20
- should have_imeths :config, :configure, :adapter, :validate!, :init
19
+ should have_imeths :config, :configure, :adapter, :init
21
20
  should have_imeths :escape_like_pattern
22
21
 
23
- should "return its `Config` class with the `config` method" do
24
- assert_same Config, subject.config
22
+ should "default the db file env var" do
23
+ assert_equal 'config/db', ENV['ARDB_DB_FILE']
24
+ end
25
+
26
+ should "know its config" do
27
+ assert_instance_of Config, subject.config
28
+ result = subject.config
29
+ assert_same result, subject.config
25
30
  end
26
31
 
27
- should "complain if init'ing and not all configs are set" do
28
- orig_adapter = Ardb.config.db.adapter
29
- Ardb.config.db.adapter = nil
30
- assert_raises(NotConfiguredError){ subject.init }
31
- Ardb.config.db.adapter = orig_adapter
32
+ should "yield its config using `configure`" do
33
+ yielded = nil
34
+ subject.configure{ |c| yielded = c }
35
+ assert_same subject.config, yielded
32
36
  end
33
37
 
34
- should "init the adapter on init" do
35
- assert_nil Adapter.current
36
- begin
37
- subject.init
38
- rescue LoadError
38
+ end
39
+
40
+ class InitMethodSetupTests < UnitTests
41
+ setup do
42
+ @orig_env_pwd = ENV['PWD']
43
+ @orig_env_ardb_db_file = ENV['ARDB_DB_FILE']
44
+ @orig_ar_logger = ActiveRecord::Base.logger
45
+
46
+ # 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
48
+ # tests
49
+ @ardb_config = Config.new
50
+ Assert.stub(Ardb, :config){ @ardb_config }
51
+
52
+ @ardb_adapter = nil
53
+ Assert.stub(Ardb::Adapter, :new) do |*args|
54
+ @ardb_adapter ||= Ardb::AdapterSpy.new(*args)
39
55
  end
40
56
 
41
- assert_not_nil Adapter.current
42
- exp_adapter = Adapter.send(subject.config.db.adapter)
43
- assert_equal exp_adapter, Adapter.current
44
- assert_same Adapter.current, subject.adapter
57
+ ENV['ARDB_DB_FILE'] = 'test/support/require_test_db_file'
58
+ @ardb_config.adapter = Adapter::VALID_ADAPTERS.sample
59
+ @ardb_config.database = Factory.string
60
+ end
61
+ teardown do
62
+ ActiveRecord::Base.logger = @orig_ar_logger
63
+ ENV['ARDB_DB_FILE'] = @orig_env_ardb_db_file
64
+ ENV['PWD'] = @orig_env_pwd
65
+ end
66
+
67
+ end
68
+
69
+ class InitMethodTests < InitMethodSetupTests
70
+ desc "`init` method"
71
+
72
+ should "require the autoloaded active record files" do
73
+ subject.init
74
+ assert_false require('ardb/require_autoloaded_active_record_files')
45
75
  end
46
76
 
47
- should "establish an AR connection on init" do
48
- assert_raises(LoadError) do
49
- # not going to test this b/c I don't want to bring in all the crap it
50
- # takes to actually establish a connection with AR (adapters, etc)
51
- # plus, most of this should be handled by AR, ns-options, and the above
52
- # tests anyway
53
- subject.init
54
- end
77
+ should "require the db file" do
78
+ subject.init
79
+ assert_false require(ENV['ARDB_DB_FILE'])
80
+ end
81
+
82
+ 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
+ subject.init
86
+ assert_false require(File.expand_path(ENV['ARDB_DB_FILE'], ENV['PWD']))
87
+ end
88
+
89
+ should "raise an invalid db file error when it can't require it" do
90
+ ENV['ARDB_DB_FILE'] = Factory.file_path
91
+ error = assert_raises(InvalidDBFileError){ subject.init }
92
+ exp = "can't require `#{ENV['ARDB_DB_FILE']}`, check that the " \
93
+ "ARDB_DB_FILE env var is set to the file path of your db file"
94
+ assert_equal exp, error.message
95
+ end
96
+
97
+ should "validate its config" do
98
+ validate_called = false
99
+ Assert.stub(@ardb_config, :validate!){ validate_called = true }
100
+
101
+ subject.init
102
+ assert_true validate_called
103
+ end
104
+
105
+ should "build an adapter using its config" do
106
+ subject.init
107
+
108
+ assert_not_nil @ardb_adapter
109
+ assert_equal subject.config, @ardb_adapter.config
110
+ assert_same @ardb_adapter, subject.adapter
111
+ end
112
+
113
+ should "optionally establish an AR connection" do
114
+ assert_nil @ardb_adapter
115
+ subject.init
116
+ assert_equal 1, @ardb_adapter.connect_db_called_count
117
+
118
+ subject.init(true)
119
+ assert_equal 2, @ardb_adapter.connect_db_called_count
120
+
121
+ subject.init(false)
122
+ assert_equal 2, @ardb_adapter.connect_db_called_count
55
123
  end
56
124
 
57
125
  end
58
126
 
59
- class InitTests < UnitTests
127
+ class InitTests < InitMethodSetupTests
60
128
  desc "when init"
61
129
  setup do
62
- # don't establish connection, otherwise this errors if it can't connect to
63
- # an actual DB
64
- @module.init(false)
130
+ @module.init
65
131
  end
66
132
 
67
133
  should "demeter its adapter" do
@@ -72,4 +138,225 @@ module Ardb
72
138
 
73
139
  end
74
140
 
141
+ class ConfigTests < UnitTests
142
+ desc "Config"
143
+ setup do
144
+ @config_class = Ardb::Config
145
+ end
146
+ subject{ @config_class }
147
+
148
+ should "know its activerecord attrs" do
149
+ exp = [
150
+ :adapter,
151
+ :database,
152
+ :encoding,
153
+ :host,
154
+ :port,
155
+ :username,
156
+ :password,
157
+ :pool,
158
+ :checkout_timeout,
159
+ :min_messages
160
+ ]
161
+ assert_equal exp, subject::ACTIVERECORD_ATTRS
162
+ end
163
+
164
+ should "know its default migrations path" do
165
+ assert_equal 'db/migrations', subject::DEFAULT_MIGRATIONS_PATH
166
+ end
167
+
168
+ should "know its default schema path" do
169
+ assert_equal 'db/schema', subject::DEFAULT_SCHEMA_PATH
170
+ end
171
+
172
+ should "know its schema formats" do
173
+ assert_equal :ruby, subject::RUBY_SCHEMA_FORMAT
174
+ assert_equal :sql, subject::SQL_SCHEMA_FORMAT
175
+ exp = [subject::RUBY_SCHEMA_FORMAT, subject::SQL_SCHEMA_FORMAT]
176
+ assert_equal exp, subject::VALID_SCHEMA_FORMATS
177
+ end
178
+
179
+ end
180
+
181
+ class ConfigInitTests < ConfigTests
182
+ desc "when init"
183
+ setup do
184
+ @config = @config_class.new
185
+ end
186
+ subject{ @config }
187
+
188
+ should have_accessors *Ardb::Config::ACTIVERECORD_ATTRS
189
+ should have_accessors :logger, :root_path
190
+ should have_readers :schema_format
191
+ should have_writers :migrations_path, :schema_path
192
+ should have_imeths :activerecord_connect_hash, :validate!
193
+
194
+ should "default its attributs" do
195
+ 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)
198
+ assert_equal exp, subject.migrations_path
199
+ exp = File.expand_path(@config_class::DEFAULT_SCHEMA_PATH, subject.root_path)
200
+ assert_equal exp, subject.schema_path
201
+ assert_equal @config_class::RUBY_SCHEMA_FORMAT, subject.schema_format
202
+ end
203
+
204
+ should "allow reading/writing its paths" do
205
+ new_root_path = Factory.path
206
+ new_migrations_path = Factory.path
207
+ new_schema_path = Factory.path
208
+
209
+ subject.root_path = new_root_path
210
+ subject.migrations_path = new_migrations_path
211
+ subject.schema_path = new_schema_path
212
+ assert_equal new_root_path, subject.root_path
213
+ exp = File.expand_path(new_migrations_path, new_root_path)
214
+ assert_equal exp, subject.migrations_path
215
+ exp = File.expand_path(new_schema_path, new_root_path)
216
+ assert_equal exp, subject.schema_path
217
+ end
218
+
219
+ should "allow setting absolute paths" do
220
+ new_migrations_path = "/#{Factory.path}"
221
+ new_schema_path = "/#{Factory.path}"
222
+
223
+ subject.root_path = [Factory.path, nil].sample
224
+ subject.migrations_path = new_migrations_path
225
+ subject.schema_path = new_schema_path
226
+ assert_equal new_migrations_path, subject.migrations_path
227
+ assert_equal new_schema_path, subject.schema_path
228
+ end
229
+
230
+ should "allow reading/writing the schema format" do
231
+ new_schema_format = Factory.string
232
+
233
+ subject.schema_format = new_schema_format
234
+ assert_equal new_schema_format.to_sym, subject.schema_format
235
+ end
236
+
237
+ 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
240
+ subject.send("#{attr_name}=", value)
241
+ [attr_name.to_s, value] if !value.nil?
242
+ end.compact
243
+ assert_equal Hash[attrs_and_values], subject.activerecord_connect_hash
244
+ end
245
+
246
+ should "raise errors with invalid attribute values using `validate!`" do
247
+ subject.adapter = Factory.string
248
+ subject.database = Factory.string
249
+ assert_nothing_raised{ subject.validate! }
250
+
251
+ subject.adapter = nil
252
+ assert_raises(ConfigurationError){ subject.validate! }
253
+
254
+ subject.adapter = Factory.string
255
+ subject.database = nil
256
+ assert_raises(ConfigurationError){ subject.validate! }
257
+
258
+ subject.database = Factory.string
259
+ subject.schema_format = Factory.string
260
+ assert_raises(ConfigurationError){ subject.validate! }
261
+
262
+ subject.schema_format = @config_class::VALID_SCHEMA_FORMATS.sample
263
+ assert_nothing_raised{ subject.validate! }
264
+ end
265
+
266
+ should "know if its equal to another config" do
267
+ attrs = @config_class::ACTIVERECORD_ATTRS + [
268
+ :logger,
269
+ :root_path,
270
+ :schema_format,
271
+ :migrations_path,
272
+ :schema_path
273
+ ]
274
+ attrs.each do |attr_name|
275
+ subject.send("#{attr_name}=", Factory.string)
276
+ end
277
+
278
+ other_config = @config_class.new
279
+ attrs.each do |attr_name|
280
+ other_config.send("#{attr_name}=", subject.send(attr_name))
281
+ end
282
+ assert_equal other_config, subject
283
+
284
+ attr_name = attrs.sample
285
+ other_config.send("#{attr_name}=", Factory.string)
286
+ assert_not_equal other_config, subject
287
+ end
288
+
289
+ end
290
+
291
+ class AdapterTests < UnitTests
292
+ desc "Adapter"
293
+ setup do
294
+ @config = Factory.ardb_config
295
+
296
+ @adapter_module = Ardb::Adapter
297
+ end
298
+ subject{ @adapter_module }
299
+
300
+ should have_imeths :new
301
+ should have_imeths :sqlite, :sqlite3
302
+ should have_imeths :postgresql, :postgres
303
+ should have_imeths :mysql, :mysql2
304
+
305
+ should "know its valid adapters" do
306
+ exp = [
307
+ 'sqlite',
308
+ 'sqlite3',
309
+ 'postgresql',
310
+ 'postgres',
311
+ 'mysql',
312
+ 'mysql2'
313
+ ]
314
+ assert_equal exp, subject::VALID_ADAPTERS
315
+ end
316
+
317
+ should "build an adapter specific class using the passed config" do
318
+ adapter_key, exp_adapter_class = [
319
+ ['sqlite', Ardb::Adapter::Sqlite],
320
+ ['postgresql', Ardb::Adapter::Postgresql],
321
+ ['mysql', Ardb::Adapter::Mysql]
322
+ ].sample
323
+ @config.adapter = adapter_key
324
+
325
+ adapter = subject.new(@config)
326
+ assert_instance_of exp_adapter_class, adapter
327
+ assert_equal @config, adapter.config
328
+ end
329
+
330
+ should "know how to build a sqlite adapter" do
331
+ adapter = subject.sqlite(@config)
332
+ assert_instance_of Ardb::Adapter::Sqlite, adapter
333
+ assert_equal @config, adapter.config
334
+
335
+ adapter = subject.sqlite3(@config)
336
+ assert_instance_of Ardb::Adapter::Sqlite, adapter
337
+ assert_equal @config, adapter.config
338
+ end
339
+
340
+ should "know how to build a postgresql adapter" do
341
+ adapter = subject.postgresql(@config)
342
+ assert_instance_of Ardb::Adapter::Postgresql, adapter
343
+ assert_equal @config, adapter.config
344
+
345
+ adapter = subject.postgres(@config)
346
+ assert_instance_of Ardb::Adapter::Postgresql, adapter
347
+ assert_equal @config, adapter.config
348
+ end
349
+
350
+ should "know how to build a mysql adapter" do
351
+ adapter = subject.mysql(@config)
352
+ assert_instance_of Ardb::Adapter::Mysql, adapter
353
+ assert_equal @config, adapter.config
354
+
355
+ adapter = subject.mysql2(@config)
356
+ assert_instance_of Ardb::Adapter::Mysql, adapter
357
+ assert_equal @config, adapter.config
358
+ end
359
+
360
+ end
361
+
75
362
  end