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,29 +1,15 @@
1
- require 'assert'
2
- require 'ardb/adapter/mysql'
1
+ require "assert"
2
+ require "ardb/adapter/mysql"
3
3
 
4
4
  class Ardb::Adapter::Mysql
5
-
6
5
  class UnitTests < Assert::Context
7
6
  desc "Ardb::Adapter::Mysql"
8
7
  setup do
9
- @adapter = Ardb::Adapter::Mysql.new
8
+ @config = Factory.ardb_config
9
+ @adapter = Ardb::Adapter::Mysql.new(@config)
10
10
  end
11
11
  subject{ @adapter }
12
12
 
13
- should "know its foreign key add sql" do
14
- exp_add_sql = "ALTER TABLE :from_table"\
15
- " ADD CONSTRAINT :name"\
16
- " FOREIGN KEY (:from_column)"\
17
- " REFERENCES :to_table (:to_column)"
18
- assert_equal exp_add_sql, subject.foreign_key_add_sql
19
- end
20
-
21
- should "know its foreign key drop sql" do
22
- exp_drop_sql = "ALTER TABLE :from_table"\
23
- " DROP FOREIGN KEY :name"
24
- assert_equal exp_drop_sql, subject.foreign_key_drop_sql
25
- end
26
-
27
13
  # not currently implemented, see: https://github.com/redding/ardb/issues/13
28
14
  should "not implement the create and drop db methods" do
29
15
  assert_raises(NotImplementedError){ subject.create_db }
@@ -34,7 +20,5 @@ class Ardb::Adapter::Mysql
34
20
  should "not implement the drop tables method" do
35
21
  assert_raises(NotImplementedError){ subject.drop_tables }
36
22
  end
37
-
38
23
  end
39
-
40
24
  end
@@ -1,50 +1,42 @@
1
- require 'assert'
2
- require 'ardb/adapter/postgresql'
1
+ require "assert"
2
+ require "ardb/adapter/postgresql"
3
3
 
4
- require 'scmd'
4
+ require "scmd"
5
5
 
6
6
  class Ardb::Adapter::Postgresql
7
-
8
7
  class UnitTests < Assert::Context
9
8
  desc "Ardb::Adapter::Postgresql"
10
9
  setup do
11
- @adapter = Ardb::Adapter::Postgresql.new
10
+ @config = Factory.ardb_config
11
+ @adapter = Ardb::Adapter::Postgresql.new(@config)
12
12
  end
13
13
  subject{ @adapter }
14
14
 
15
- should have_imeths :public_schema_settings
15
+ should have_imeths :public_connect_hash
16
16
 
17
- should "know its public schema connection settings" do
18
- exp_settings = subject.config_settings.merge({
19
- 'database' => 'postgres',
20
- 'schema_search_path' => 'public'
17
+ should "know its public connect hash" do
18
+ exp = subject.connect_hash.merge({
19
+ "database" => "postgres",
20
+ "schema_search_path" => "public"
21
21
  })
22
- assert_equal exp_settings, subject.public_schema_settings
23
- end
24
-
25
- should "know its foreign key add sql" do
26
- exp_add_sql = "ALTER TABLE :from_table"\
27
- " ADD CONSTRAINT :name"\
28
- " FOREIGN KEY (:from_column)"\
29
- " REFERENCES :to_table (:to_column)"
30
- assert_equal exp_add_sql, subject.foreign_key_add_sql
22
+ assert_equal exp, subject.public_connect_hash
31
23
  end
32
24
 
33
- should "know its foreign key drop sql" do
34
- exp_drop_sql = "ALTER TABLE :from_table"\
35
- " DROP CONSTRAINT :name"
36
- assert_equal exp_drop_sql, subject.foreign_key_drop_sql
25
+ should "complain if given a database name with non-word characters" do
26
+ @config.database = "#{Factory.string}-#{Factory.string}"
27
+ assert_raises(Ardb::ConfigurationError){
28
+ Ardb::Adapter::Postgresql.new(@config)
29
+ }
37
30
  end
38
-
39
31
  end
40
32
 
41
33
  class SQLSchemaTests < UnitTests
42
34
  setup do
43
35
  @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']
36
+ "PGHOST" => @adapter.connect_hash["host"],
37
+ "PGPORT" => @adapter.connect_hash["port"],
38
+ "PGUSER" => @adapter.connect_hash["username"],
39
+ "PGPASSWORD" => @adapter.connect_hash["password"]
48
40
  }
49
41
  end
50
42
 
@@ -1,12 +1,12 @@
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
9
- @adapter = Ardb::Adapter::Sqlite.new
8
+ @config = Factory.ardb_config
9
+ @adapter = Ardb::Adapter::Sqlite.new(@config)
10
10
  end
11
11
  subject{ @adapter }
12
12
 
@@ -20,20 +20,17 @@ class Ardb::Adapter::Sqlite
20
20
  end
21
21
 
22
22
  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
25
- end
23
+ exp = File.expand_path(@config.database, @config.root_path)
24
+ assert_equal exp, subject.db_file_path
26
25
 
27
- should "not implement the foreign key sql meths" do
28
- assert_raises(NotImplementedError){ subject.foreign_key_add_sql }
29
- assert_raises(NotImplementedError){ subject.foreign_key_drop_sql }
26
+ @config.database = File.join(TMP_PATH, "abs_sqlite_db_test")
27
+ adapter = Ardb::Adapter::Sqlite.new(@config)
28
+ assert_equal @config.database, adapter.db_file_path
30
29
  end
31
30
 
32
31
  # not currently implemented, see: https://github.com/redding/ardb/issues/29
33
32
  should "not implement the drop tables method" do
34
33
  assert_raises(NotImplementedError){ subject.drop_tables }
35
34
  end
36
-
37
35
  end
38
-
39
36
  end
@@ -1,106 +1,83 @@
1
- require 'assert'
2
- require 'ardb/adapter_spy'
3
-
4
- require 'much-plugin'
5
-
6
- module Ardb::AdapterSpy
1
+ require "assert"
2
+ require "ardb/adapter_spy"
7
3
 
4
+ class Ardb::AdapterSpy
8
5
  class UnitTests < Assert::Context
9
6
  desc "Ardb::AdapterSpy"
10
7
  setup do
11
- @adapter = MyAdapter.new
8
+ @adapter_spy_class = Ardb::AdapterSpy
12
9
  end
13
- subject{ @adapter }
10
+ subject{ @adapter_spy_class }
11
+
12
+ should "be a kind of ardb adapter" do
13
+ assert subject < Ardb::Adapter::Base
14
+ end
15
+ end
16
+
17
+ class InitTests < UnitTests
18
+ desc "when init"
19
+ setup do
20
+ @config = Factory.ardb_config
21
+ @adapter_spy = @adapter_spy_class.new(@config)
22
+ end
23
+ subject{ @adapter_spy }
14
24
 
15
25
  should have_accessors :drop_tables_called_count
16
26
  should have_accessors :dump_schema_called_count, :load_schema_called_count
17
27
  should have_accessors :drop_db_called_count, :create_db_called_count
18
28
  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
29
+ should have_imeths :create_db_called?, :drop_db_called?, :drop_tables_called?
30
+ should have_imeths :connect_db_called?, :migrate_db_called?
31
+ should have_imeths :dump_schema_called?, :load_schema_called?
32
+ should have_imeths :create_db, :drop_db, :drop_tables
33
+ should have_imeths :connect_db, :migrate_db
34
+ should have_imeths :dump_schema, :load_schema
34
35
 
35
36
  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
37
  assert_equal 0, subject.create_db_called_count
38
+ assert_equal 0, subject.drop_db_called_count
39
+ assert_equal 0, subject.drop_tables_called_count
41
40
  assert_equal 0, subject.connect_db_called_count
42
41
  assert_equal 0, subject.migrate_db_called_count
42
+ assert_equal 0, subject.load_schema_called_count
43
+ assert_equal 0, subject.dump_schema_called_count
43
44
  end
44
45
 
45
46
  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?
55
-
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?
47
+ assert_equal false, subject.create_db_called?
48
+ subject.create_db
49
+ assert_equal 1, subject.create_db_called_count
50
+ assert_equal true, subject.create_db_called?
60
51
 
61
52
  assert_equal false, subject.drop_db_called?
62
53
  subject.drop_db
63
54
  assert_equal 1, subject.drop_db_called_count
64
55
  assert_equal true, subject.drop_db_called?
65
56
 
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?
57
+ assert_equal false, subject.drop_tables_called?
58
+ subject.drop_tables
59
+ assert_equal 1, subject.drop_tables_called_count
60
+ assert_equal true, subject.drop_tables_called?
75
61
 
76
62
  assert_equal false, subject.connect_db_called?
77
63
  subject.connect_db
78
64
  assert_equal 1, subject.connect_db_called_count
79
65
  assert_equal true, subject.connect_db_called?
80
- end
81
66
 
82
- end
67
+ assert_equal false, subject.migrate_db_called?
68
+ subject.migrate_db
69
+ assert_equal 1, subject.migrate_db_called_count
70
+ assert_equal true, subject.migrate_db_called?
83
71
 
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 }
72
+ assert_equal false, subject.dump_schema_called?
73
+ subject.dump_schema
74
+ assert_equal 1, subject.dump_schema_called_count
75
+ assert_equal true, subject.dump_schema_called?
93
76
 
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=
77
+ assert_equal false, subject.load_schema_called?
78
+ subject.load_schema
79
+ assert_equal 1, subject.load_schema_called_count
80
+ assert_equal true, subject.load_schema_called?
98
81
  end
99
-
100
- end
101
-
102
- class MyAdapter
103
- include Ardb::AdapterSpy
104
82
  end
105
-
106
83
  end
@@ -1,67 +1,149 @@
1
- require 'assert'
2
- require 'ardb'
1
+ require "assert"
2
+ require "ardb"
3
3
 
4
- module Ardb
4
+ require "logger"
5
+ require "ardb/adapter_spy"
6
+ require "ardb/adapter/mysql"
7
+ require "ardb/adapter/postgresql"
8
+ require "ardb/adapter/sqlite"
5
9
 
10
+ module Ardb
6
11
  class UnitTests < Assert::Context
7
12
  desc "Ardb"
8
13
  setup do
9
- @orig_ar_logger = ActiveRecord::Base.logger
10
- Adapter.reset
11
-
12
14
  @module = Ardb
13
15
  end
14
16
  teardown do
15
- Adapter.reset
16
- ActiveRecord::Base.logger = @orig_ar_logger
17
+ @module.reset_adapter
17
18
  end
18
19
  subject{ @module }
19
20
 
20
- should have_imeths :config, :configure, :adapter, :validate!, :init
21
- should have_imeths :escape_like_pattern
21
+ should have_imeths :config, :configure, :adapter, :reset_adapter
22
+ should have_imeths :init, :escape_like_pattern
22
23
 
23
- should "return its `Config` class with the `config` method" do
24
- assert_same Config, subject.config
24
+ should "default the db file env var" do
25
+ assert_equal "config/db", ENV["ARDB_DB_FILE"]
25
26
  end
26
27
 
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
28
+ should "know its config" do
29
+ assert_instance_of Config, subject.config
30
+ result = subject.config
31
+ assert_same result, subject.config
32
32
  end
33
33
 
34
- should "init the adapter on init" do
35
- assert_nil Adapter.current
36
- begin
37
- subject.init
38
- rescue LoadError
34
+ should "yield its config using `configure`" do
35
+ yielded = nil
36
+ subject.configure{ |c| yielded = c }
37
+ assert_same subject.config, yielded
38
+ end
39
+ end
40
+
41
+ class InitMethodSetupTests < UnitTests
42
+ setup do
43
+ @orig_env_pwd = ENV["PWD"]
44
+ @orig_env_ardb_db_file = ENV["ARDB_DB_FILE"]
45
+ @orig_ar_logger = ActiveRecord::Base.logger
46
+
47
+ # stub in a temporary config, this allows us to modify it and not worry
48
+ # about affecting Ardb"s global config which could cause issues on other
49
+ # tests
50
+ @ardb_config = Config.new
51
+ Assert.stub(Ardb, :config){ @ardb_config }
52
+
53
+ @ardb_adapter = nil
54
+ Assert.stub(Ardb::Adapter, :new) do |*args|
55
+ @ardb_adapter ||= Ardb::AdapterSpy.new(*args)
39
56
  end
40
57
 
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
58
+ ENV["ARDB_DB_FILE"] = "test/support/require_test_db_file"
59
+ @ardb_config.adapter = Adapter::VALID_ADAPTERS.sample
60
+ @ardb_config.database = Factory.string
61
+ end
62
+ teardown do
63
+ ActiveRecord::Base.logger = @orig_ar_logger
64
+ ENV["ARDB_DB_FILE"] = @orig_env_ardb_db_file
65
+ ENV["PWD"] = @orig_env_pwd
45
66
  end
67
+ end
46
68
 
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
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")
75
+ end
76
+
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
55
103
  end
56
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 "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
+
120
+ should "optionally establish an AR connection" do
121
+ assert_nil @ardb_adapter
122
+ subject.init
123
+ assert_equal 1, @ardb_adapter.connect_db_called_count
124
+
125
+ subject.init(true)
126
+ assert_equal 2, @ardb_adapter.connect_db_called_count
127
+
128
+ subject.init(false)
129
+ assert_equal 2, @ardb_adapter.connect_db_called_count
130
+ end
131
+
132
+ should "raise a not initialized error using its adapter before init" do
133
+ subject.reset_adapter
134
+ assert_raises(NotInitializedError){ subject.adapter }
135
+ assert_raises(NotInitializedError){ subject.escape_like_pattern(Factory.string) }
136
+
137
+ subject.init
138
+ assert_nothing_raised{ subject.adapter }
139
+ assert_nothing_raised{ subject.escape_like_pattern(Factory.string) }
140
+ end
57
141
  end
58
142
 
59
- class InitTests < UnitTests
143
+ class InitTests < InitMethodSetupTests
60
144
  desc "when init"
61
145
  setup do
62
- # don't establish connection, otherwise this errors if it can't connect to
63
- # an actual DB
64
- @module.init(false)
146
+ @module.init
65
147
  end
66
148
 
67
149
  should "demeter its adapter" do
@@ -69,7 +151,225 @@ module Ardb
69
151
  exp = subject.adapter.escape_like_pattern(pattern)
70
152
  assert_equal exp, subject.escape_like_pattern(pattern)
71
153
  end
154
+ end
155
+
156
+ class ConfigTests < UnitTests
157
+ desc "Config"
158
+ setup do
159
+ @config_class = Ardb::Config
160
+ end
161
+ subject{ @config_class }
162
+
163
+ should "know its activerecord attrs" do
164
+ exp = [
165
+ :adapter,
166
+ :database,
167
+ :encoding,
168
+ :host,
169
+ :port,
170
+ :username,
171
+ :password,
172
+ :pool,
173
+ :checkout_timeout,
174
+ :min_messages
175
+ ]
176
+ assert_equal exp, subject::ACTIVERECORD_ATTRS
177
+ end
178
+
179
+ should "know its default migrations path" do
180
+ assert_equal "db/migrations", subject::DEFAULT_MIGRATIONS_PATH
181
+ end
182
+
183
+ should "know its default schema path" do
184
+ assert_equal "db/schema", subject::DEFAULT_SCHEMA_PATH
185
+ end
186
+
187
+ should "know its schema formats" do
188
+ assert_equal :ruby, subject::RUBY_SCHEMA_FORMAT
189
+ assert_equal :sql, subject::SQL_SCHEMA_FORMAT
190
+ exp = [subject::RUBY_SCHEMA_FORMAT, subject::SQL_SCHEMA_FORMAT]
191
+ assert_equal exp, subject::VALID_SCHEMA_FORMATS
192
+ end
193
+ end
194
+
195
+ class ConfigInitTests < ConfigTests
196
+ desc "when init"
197
+ setup do
198
+ @config = @config_class.new
199
+ end
200
+ subject{ @config }
201
+
202
+ should have_accessors *Ardb::Config::ACTIVERECORD_ATTRS
203
+ should have_accessors :default_timezone, :logger, :root_path
204
+ should have_readers :schema_format
205
+ should have_writers :migrations_path, :schema_path
206
+ should have_imeths :activerecord_connect_hash, :validate!
207
+
208
+ should "default its attributs" do
209
+ assert_equal :utc, subject.default_timezone
210
+ assert_instance_of Logger, subject.logger
211
+ assert_equal ENV["PWD"], subject.root_path
212
+ exp = File.expand_path(@config_class::DEFAULT_MIGRATIONS_PATH, subject.root_path)
213
+ assert_equal exp, subject.migrations_path
214
+ exp = File.expand_path(@config_class::DEFAULT_SCHEMA_PATH, subject.root_path)
215
+ assert_equal exp, subject.schema_path
216
+ assert_equal @config_class::RUBY_SCHEMA_FORMAT, subject.schema_format
217
+ end
218
+
219
+ should "allow reading/writing its paths" do
220
+ new_root_path = Factory.path
221
+ new_migrations_path = Factory.path
222
+ new_schema_path = Factory.path
223
+
224
+ subject.root_path = new_root_path
225
+ subject.migrations_path = new_migrations_path
226
+ subject.schema_path = new_schema_path
227
+ assert_equal new_root_path, subject.root_path
228
+ exp = File.expand_path(new_migrations_path, new_root_path)
229
+ assert_equal exp, subject.migrations_path
230
+ exp = File.expand_path(new_schema_path, new_root_path)
231
+ assert_equal exp, subject.schema_path
232
+ end
233
+
234
+ should "allow setting absolute paths" do
235
+ new_migrations_path = "/#{Factory.path}"
236
+ new_schema_path = "/#{Factory.path}"
237
+
238
+ subject.root_path = [Factory.path, nil].sample
239
+ subject.migrations_path = new_migrations_path
240
+ subject.schema_path = new_schema_path
241
+ assert_equal new_migrations_path, subject.migrations_path
242
+ assert_equal new_schema_path, subject.schema_path
243
+ end
244
+
245
+ should "allow reading/writing the schema format" do
246
+ new_schema_format = Factory.string
247
+
248
+ subject.schema_format = new_schema_format
249
+ assert_equal new_schema_format.to_sym, subject.schema_format
250
+ end
251
+
252
+ should "know its activerecord connection hash" do
253
+ attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map do |attr_name|
254
+ value = [Factory.string, nil].sample
255
+ subject.send("#{attr_name}=", value)
256
+ [attr_name.to_s, value] if !value.nil?
257
+ end.compact
258
+ assert_equal Hash[attrs_and_values], subject.activerecord_connect_hash
259
+ end
260
+
261
+ should "raise errors with invalid attribute values using `validate!`" do
262
+ subject.adapter = Factory.string
263
+ subject.database = Factory.string
264
+ assert_nothing_raised{ subject.validate! }
265
+
266
+ subject.adapter = nil
267
+ assert_raises(ConfigurationError){ subject.validate! }
268
+
269
+ subject.adapter = Factory.string
270
+ subject.database = nil
271
+ assert_raises(ConfigurationError){ subject.validate! }
272
+
273
+ subject.database = Factory.string
274
+ subject.schema_format = Factory.string
275
+ assert_raises(ConfigurationError){ subject.validate! }
276
+
277
+ subject.schema_format = @config_class::VALID_SCHEMA_FORMATS.sample
278
+ assert_nothing_raised{ subject.validate! }
279
+ end
72
280
 
281
+ should "know if its equal to another config" do
282
+ attrs = @config_class::ACTIVERECORD_ATTRS + [
283
+ :default_timezone,
284
+ :logger,
285
+ :root_path,
286
+ :schema_format,
287
+ :migrations_path,
288
+ :schema_path
289
+ ]
290
+ attrs.each do |attr_name|
291
+ subject.send("#{attr_name}=", Factory.string)
292
+ end
293
+
294
+ other_config = @config_class.new
295
+ attrs.each do |attr_name|
296
+ other_config.send("#{attr_name}=", subject.send(attr_name))
297
+ end
298
+ assert_equal other_config, subject
299
+
300
+ attr_name = attrs.sample
301
+ other_config.send("#{attr_name}=", Factory.string)
302
+ assert_not_equal other_config, subject
303
+ end
73
304
  end
74
305
 
306
+ class AdapterTests < UnitTests
307
+ desc "Adapter"
308
+ setup do
309
+ @config = Factory.ardb_config
310
+
311
+ @adapter_module = Ardb::Adapter
312
+ end
313
+ subject{ @adapter_module }
314
+
315
+ should have_imeths :new
316
+ should have_imeths :sqlite, :sqlite3
317
+ should have_imeths :postgresql, :postgres
318
+ should have_imeths :mysql, :mysql2
319
+
320
+ should "know its valid adapters" do
321
+ exp = [
322
+ "sqlite",
323
+ "sqlite3",
324
+ "postgresql",
325
+ "postgres",
326
+ "mysql",
327
+ "mysql2"
328
+ ]
329
+ assert_equal exp, subject::VALID_ADAPTERS
330
+ end
331
+
332
+ should "build an adapter specific class using the passed config" do
333
+ adapter_key, exp_adapter_class = [
334
+ ["sqlite", Ardb::Adapter::Sqlite],
335
+ ["postgresql", Ardb::Adapter::Postgresql],
336
+ ["mysql", Ardb::Adapter::Mysql]
337
+ ].sample
338
+ @config.adapter = adapter_key
339
+
340
+ adapter = subject.new(@config)
341
+ assert_instance_of exp_adapter_class, adapter
342
+ assert_equal @config, adapter.config
343
+ end
344
+
345
+ should "know how to build a sqlite adapter" do
346
+ adapter = subject.sqlite(@config)
347
+ assert_instance_of Ardb::Adapter::Sqlite, adapter
348
+ assert_equal @config, adapter.config
349
+
350
+ adapter = subject.sqlite3(@config)
351
+ assert_instance_of Ardb::Adapter::Sqlite, adapter
352
+ assert_equal @config, adapter.config
353
+ end
354
+
355
+ should "know how to build a postgresql adapter" do
356
+ adapter = subject.postgresql(@config)
357
+ assert_instance_of Ardb::Adapter::Postgresql, adapter
358
+ assert_equal @config, adapter.config
359
+
360
+ adapter = subject.postgres(@config)
361
+ assert_instance_of Ardb::Adapter::Postgresql, adapter
362
+ assert_equal @config, adapter.config
363
+ end
364
+
365
+ should "know how to build a mysql adapter" do
366
+ adapter = subject.mysql(@config)
367
+ assert_instance_of Ardb::Adapter::Mysql, adapter
368
+ assert_equal @config, adapter.config
369
+
370
+ adapter = subject.mysql2(@config)
371
+ assert_instance_of Ardb::Adapter::Mysql, adapter
372
+ assert_equal @config, adapter.config
373
+ end
374
+ end
75
375
  end