ardb 0.28.3 → 0.30.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -7
- data/.l.yml +9 -0
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -0
- data/.t.yml +6 -0
- data/Gemfile +24 -8
- data/README.md +252 -3
- data/ardb.gemspec +14 -10
- data/bin/ardb +3 -1
- data/lib/ardb/adapter/base.rb +72 -47
- data/lib/ardb/adapter/mysql.rb +4 -17
- data/lib/ardb/adapter/postgresql.rb +51 -46
- data/lib/ardb/adapter/sqlite.rb +11 -15
- data/lib/ardb/adapter_spy.rb +18 -30
- data/lib/ardb/cli/clirb.rb +16 -18
- data/lib/ardb/cli/commands.rb +308 -129
- data/lib/ardb/cli.rb +29 -24
- data/lib/ardb/db_tests.rb +4 -4
- data/lib/ardb/default_order_by.rb +13 -21
- data/lib/ardb/migration.rb +15 -16
- data/lib/ardb/record_spy.rb +46 -61
- data/lib/ardb/relation_spy.rb +28 -32
- data/lib/ardb/require_autoloaded_active_record_files.rb +258 -57
- data/lib/ardb/test_helpers.rb +33 -29
- data/lib/ardb/use_db_default.rb +13 -21
- data/lib/ardb/version.rb +3 -1
- data/lib/ardb.rb +105 -86
- data/script/determine_autoloaded_active_record_files.rb +31 -24
- data/test/helper.rb +6 -13
- data/test/support/factory.rb +4 -3
- data/test/support/fake_schema.rb +3 -1
- data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
- data/test/support/postgresql/schema.rb +2 -1
- data/test/support/postgresql/setup_test_db.rb +23 -21
- data/test/support/relative_require_test_db_file.rb +1 -0
- data/test/support/require_test_db_file.rb +1 -0
- data/test/system/.keep +0 -0
- data/test/unit/adapter/base_tests.rb +80 -55
- data/test/unit/adapter/mysql_tests.rb +4 -19
- data/test/unit/adapter/postgresql_tests.rb +21 -30
- data/test/unit/adapter/sqlite_tests.rb +5 -11
- data/test/unit/adapter_spy_tests.rb +6 -17
- data/test/unit/ardb_tests.rb +75 -53
- data/test/unit/cli_tests.rb +234 -158
- data/test/unit/db_tests_tests.rb +7 -7
- data/test/unit/default_order_by_tests.rb +26 -24
- data/test/unit/migration_tests.rb +17 -18
- data/test/unit/record_spy_tests.rb +45 -41
- data/test/unit/relation_spy_tests.rb +40 -63
- data/test/unit/test_helpers_tests.rb +7 -15
- data/test/unit/use_db_default_tests.rb +35 -27
- metadata +109 -87
- data/lib/ardb/has_slug.rb +0 -107
- data/lib/ardb/migration_helpers.rb +0 -77
- data/lib/ardb/pg_json.rb +0 -90
- data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
- data/test/system/pg_json_tests.rb +0 -85
- data/test/unit/has_slug_tests.rb +0 -341
- data/test/unit/migration_helpers_tests.rb +0 -65
- data/test/unit/pg_json_tests.rb +0 -39
data/test/unit/ardb_tests.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
|
2
|
-
require 'ardb'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require 'ardb/adapter/mysql'
|
7
|
-
require 'ardb/adapter/postgresql'
|
8
|
-
require 'ardb/adapter/sqlite'
|
3
|
+
require "assert"
|
4
|
+
require "ardb"
|
9
5
|
|
10
|
-
|
6
|
+
require "logger"
|
7
|
+
require "ardb/adapter_spy"
|
8
|
+
require "ardb/adapter/mysql"
|
9
|
+
require "ardb/adapter/postgresql"
|
10
|
+
require "ardb/adapter/sqlite"
|
11
11
|
|
12
|
+
module Ardb
|
12
13
|
class UnitTests < Assert::Context
|
13
14
|
desc "Ardb"
|
14
15
|
setup do
|
@@ -23,7 +24,7 @@ module Ardb
|
|
23
24
|
should have_imeths :init, :escape_like_pattern
|
24
25
|
|
25
26
|
should "default the db file env var" do
|
26
|
-
assert_equal
|
27
|
+
assert_equal "config/db", ENV["ARDB_DB_FILE"]
|
27
28
|
end
|
28
29
|
|
29
30
|
should "know its config" do
|
@@ -37,17 +38,16 @@ module Ardb
|
|
37
38
|
subject.configure{ |c| yielded = c }
|
38
39
|
assert_same subject.config, yielded
|
39
40
|
end
|
40
|
-
|
41
41
|
end
|
42
42
|
|
43
43
|
class InitMethodSetupTests < UnitTests
|
44
44
|
setup do
|
45
|
-
@orig_env_pwd = ENV[
|
46
|
-
@orig_env_ardb_db_file = ENV[
|
45
|
+
@orig_env_pwd = ENV["PWD"]
|
46
|
+
@orig_env_ardb_db_file = ENV["ARDB_DB_FILE"]
|
47
47
|
@orig_ar_logger = ActiveRecord::Base.logger
|
48
48
|
|
49
49
|
# stub in a temporary config, this allows us to modify it and not worry
|
50
|
-
# about affecting Ardb
|
50
|
+
# about affecting Ardb"s global config which could cause issues on other
|
51
51
|
# tests
|
52
52
|
@ardb_config = Config.new
|
53
53
|
Assert.stub(Ardb, :config){ @ardb_config }
|
@@ -57,16 +57,15 @@ module Ardb
|
|
57
57
|
@ardb_adapter ||= Ardb::AdapterSpy.new(*args)
|
58
58
|
end
|
59
59
|
|
60
|
-
ENV[
|
60
|
+
ENV["ARDB_DB_FILE"] = "test/support/require_test_db_file"
|
61
61
|
@ardb_config.adapter = Adapter::VALID_ADAPTERS.sample
|
62
62
|
@ardb_config.database = Factory.string
|
63
63
|
end
|
64
64
|
teardown do
|
65
65
|
ActiveRecord::Base.logger = @orig_ar_logger
|
66
|
-
ENV[
|
67
|
-
ENV[
|
66
|
+
ENV["ARDB_DB_FILE"] = @orig_env_ardb_db_file
|
67
|
+
ENV["PWD"] = @orig_env_pwd
|
68
68
|
end
|
69
|
-
|
70
69
|
end
|
71
70
|
|
72
71
|
class InitMethodTests < InitMethodSetupTests
|
@@ -74,25 +73,25 @@ module Ardb
|
|
74
73
|
|
75
74
|
should "require the autoloaded active record files" do
|
76
75
|
subject.init
|
77
|
-
assert_false require(
|
76
|
+
assert_false require("ardb/require_autoloaded_active_record_files")
|
78
77
|
end
|
79
78
|
|
80
79
|
should "require the db file" do
|
81
80
|
subject.init
|
82
|
-
assert_false require(ENV[
|
81
|
+
assert_false require(ENV["ARDB_DB_FILE"])
|
83
82
|
end
|
84
83
|
|
85
84
|
should "require the db file relative to the working directory if needed" do
|
86
|
-
ENV[
|
87
|
-
ENV[
|
85
|
+
ENV["PWD"] = "test/support"
|
86
|
+
ENV["ARDB_DB_FILE"] = "relative_require_test_db_file"
|
88
87
|
subject.init
|
89
|
-
assert_false require(File.expand_path(ENV[
|
88
|
+
assert_false require(File.expand_path(ENV["ARDB_DB_FILE"], ENV["PWD"]))
|
90
89
|
end
|
91
90
|
|
92
91
|
should "raise an invalid db file error when it can't require it" do
|
93
|
-
ENV[
|
92
|
+
ENV["ARDB_DB_FILE"] = Factory.file_path
|
94
93
|
error = assert_raises(InvalidDBFileError){ subject.init }
|
95
|
-
exp = "can't require `#{ENV[
|
94
|
+
exp = "can't require `#{ENV["ARDB_DB_FILE"]}`, check that the " \
|
96
95
|
"ARDB_DB_FILE env var is set to the file path of your db file"
|
97
96
|
assert_equal exp, error.message
|
98
97
|
end
|
@@ -113,6 +112,23 @@ module Ardb
|
|
113
112
|
assert_same @ardb_adapter, subject.adapter
|
114
113
|
end
|
115
114
|
|
115
|
+
should "set ActiveRecord::Base attributes" do
|
116
|
+
subject.init
|
117
|
+
|
118
|
+
assert_equal subject.config.logger, ActiveRecord::Base.logger
|
119
|
+
if ActiveRecord.respond_to?(:default_timezone=)
|
120
|
+
assert_equal(
|
121
|
+
subject.config.default_timezone,
|
122
|
+
ActiveRecord.default_timezone,
|
123
|
+
)
|
124
|
+
else
|
125
|
+
assert_equal(
|
126
|
+
subject.config.default_timezone,
|
127
|
+
ActiveRecord::Base.default_timezone,
|
128
|
+
)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
116
132
|
should "optionally establish an AR connection" do
|
117
133
|
assert_nil @ardb_adapter
|
118
134
|
subject.init
|
@@ -128,13 +144,14 @@ module Ardb
|
|
128
144
|
should "raise a not initialized error using its adapter before init" do
|
129
145
|
subject.reset_adapter
|
130
146
|
assert_raises(NotInitializedError){ subject.adapter }
|
131
|
-
assert_raises(NotInitializedError)
|
147
|
+
assert_raises(NotInitializedError) do
|
148
|
+
subject.escape_like_pattern(Factory.string)
|
149
|
+
end
|
132
150
|
|
133
151
|
subject.init
|
134
152
|
assert_nothing_raised{ subject.adapter }
|
135
153
|
assert_nothing_raised{ subject.escape_like_pattern(Factory.string) }
|
136
154
|
end
|
137
|
-
|
138
155
|
end
|
139
156
|
|
140
157
|
class InitTests < InitMethodSetupTests
|
@@ -148,7 +165,6 @@ module Ardb
|
|
148
165
|
exp = subject.adapter.escape_like_pattern(pattern)
|
149
166
|
assert_equal exp, subject.escape_like_pattern(pattern)
|
150
167
|
end
|
151
|
-
|
152
168
|
end
|
153
169
|
|
154
170
|
class ConfigTests < UnitTests
|
@@ -169,17 +185,17 @@ module Ardb
|
|
169
185
|
:password,
|
170
186
|
:pool,
|
171
187
|
:checkout_timeout,
|
172
|
-
:min_messages
|
188
|
+
:min_messages,
|
173
189
|
]
|
174
190
|
assert_equal exp, subject::ACTIVERECORD_ATTRS
|
175
191
|
end
|
176
192
|
|
177
193
|
should "know its default migrations path" do
|
178
|
-
assert_equal
|
194
|
+
assert_equal "db/migrations", subject::DEFAULT_MIGRATIONS_PATH
|
179
195
|
end
|
180
196
|
|
181
197
|
should "know its default schema path" do
|
182
|
-
assert_equal
|
198
|
+
assert_equal "db/schema", subject::DEFAULT_SCHEMA_PATH
|
183
199
|
end
|
184
200
|
|
185
201
|
should "know its schema formats" do
|
@@ -188,7 +204,6 @@ module Ardb
|
|
188
204
|
exp = [subject::RUBY_SCHEMA_FORMAT, subject::SQL_SCHEMA_FORMAT]
|
189
205
|
assert_equal exp, subject::VALID_SCHEMA_FORMATS
|
190
206
|
end
|
191
|
-
|
192
207
|
end
|
193
208
|
|
194
209
|
class ConfigInitTests < ConfigTests
|
@@ -198,18 +213,27 @@ module Ardb
|
|
198
213
|
end
|
199
214
|
subject{ @config }
|
200
215
|
|
201
|
-
should have_accessors
|
202
|
-
should have_accessors :logger, :root_path
|
216
|
+
should have_accessors(*Ardb::Config::ACTIVERECORD_ATTRS)
|
217
|
+
should have_accessors :default_timezone, :logger, :root_path
|
203
218
|
should have_readers :schema_format
|
204
219
|
should have_writers :migrations_path, :schema_path
|
205
220
|
should have_imeths :activerecord_connect_hash, :validate!
|
206
221
|
|
207
222
|
should "default its attributs" do
|
223
|
+
assert_equal :utc, subject.default_timezone
|
208
224
|
assert_instance_of Logger, subject.logger
|
209
|
-
assert_equal ENV[
|
210
|
-
exp =
|
225
|
+
assert_equal ENV["PWD"], subject.root_path
|
226
|
+
exp =
|
227
|
+
File.expand_path(
|
228
|
+
@config_class::DEFAULT_MIGRATIONS_PATH,
|
229
|
+
subject.root_path,
|
230
|
+
)
|
211
231
|
assert_equal exp, subject.migrations_path
|
212
|
-
exp =
|
232
|
+
exp =
|
233
|
+
File.expand_path(
|
234
|
+
@config_class::DEFAULT_SCHEMA_PATH,
|
235
|
+
subject.root_path,
|
236
|
+
)
|
213
237
|
assert_equal exp, subject.schema_path
|
214
238
|
assert_equal @config_class::RUBY_SCHEMA_FORMAT, subject.schema_format
|
215
239
|
end
|
@@ -248,11 +272,11 @@ module Ardb
|
|
248
272
|
end
|
249
273
|
|
250
274
|
should "know its activerecord connection hash" do
|
251
|
-
attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map
|
252
|
-
value = [Factory.string,
|
275
|
+
attrs_and_values = @config_class::ACTIVERECORD_ATTRS.map{ |attr_name|
|
276
|
+
value = [Factory.string, nil].sample
|
253
277
|
subject.send("#{attr_name}=", value)
|
254
|
-
[attr_name.to_s, value]
|
255
|
-
|
278
|
+
[attr_name.to_s, value] unless value.nil?
|
279
|
+
}.compact
|
256
280
|
assert_equal Hash[attrs_and_values], subject.activerecord_connect_hash
|
257
281
|
end
|
258
282
|
|
@@ -278,11 +302,12 @@ module Ardb
|
|
278
302
|
|
279
303
|
should "know if its equal to another config" do
|
280
304
|
attrs = @config_class::ACTIVERECORD_ATTRS + [
|
305
|
+
:default_timezone,
|
281
306
|
:logger,
|
282
307
|
:root_path,
|
283
308
|
:schema_format,
|
284
309
|
:migrations_path,
|
285
|
-
:schema_path
|
310
|
+
:schema_path,
|
286
311
|
]
|
287
312
|
attrs.each do |attr_name|
|
288
313
|
subject.send("#{attr_name}=", Factory.string)
|
@@ -298,7 +323,6 @@ module Ardb
|
|
298
323
|
other_config.send("#{attr_name}=", Factory.string)
|
299
324
|
assert_not_equal other_config, subject
|
300
325
|
end
|
301
|
-
|
302
326
|
end
|
303
327
|
|
304
328
|
class AdapterTests < UnitTests
|
@@ -317,21 +341,21 @@ module Ardb
|
|
317
341
|
|
318
342
|
should "know its valid adapters" do
|
319
343
|
exp = [
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
344
|
+
"sqlite",
|
345
|
+
"sqlite3",
|
346
|
+
"postgresql",
|
347
|
+
"postgres",
|
348
|
+
"mysql",
|
349
|
+
"mysql2",
|
326
350
|
]
|
327
351
|
assert_equal exp, subject::VALID_ADAPTERS
|
328
352
|
end
|
329
353
|
|
330
354
|
should "build an adapter specific class using the passed config" do
|
331
355
|
adapter_key, exp_adapter_class = [
|
332
|
-
[
|
333
|
-
[
|
334
|
-
[
|
356
|
+
["sqlite", Ardb::Adapter::Sqlite],
|
357
|
+
["postgresql", Ardb::Adapter::Postgresql],
|
358
|
+
["mysql", Ardb::Adapter::Mysql],
|
335
359
|
].sample
|
336
360
|
@config.adapter = adapter_key
|
337
361
|
|
@@ -369,7 +393,5 @@ module Ardb
|
|
369
393
|
assert_instance_of Ardb::Adapter::Mysql, adapter
|
370
394
|
assert_equal @config, adapter.config
|
371
395
|
end
|
372
|
-
|
373
396
|
end
|
374
|
-
|
375
397
|
end
|