ardb 0.28.3 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -7
  2. data/.l.yml +9 -0
  3. data/.rubocop.yml +3 -0
  4. data/.ruby-version +1 -0
  5. data/.t.yml +6 -0
  6. data/Gemfile +24 -8
  7. data/README.md +252 -3
  8. data/ardb.gemspec +14 -10
  9. data/bin/ardb +3 -1
  10. data/lib/ardb/adapter/base.rb +72 -47
  11. data/lib/ardb/adapter/mysql.rb +4 -17
  12. data/lib/ardb/adapter/postgresql.rb +51 -46
  13. data/lib/ardb/adapter/sqlite.rb +11 -15
  14. data/lib/ardb/adapter_spy.rb +18 -30
  15. data/lib/ardb/cli/clirb.rb +16 -18
  16. data/lib/ardb/cli/commands.rb +308 -129
  17. data/lib/ardb/cli.rb +29 -24
  18. data/lib/ardb/db_tests.rb +4 -4
  19. data/lib/ardb/default_order_by.rb +13 -21
  20. data/lib/ardb/migration.rb +15 -16
  21. data/lib/ardb/record_spy.rb +46 -61
  22. data/lib/ardb/relation_spy.rb +28 -32
  23. data/lib/ardb/require_autoloaded_active_record_files.rb +258 -57
  24. data/lib/ardb/test_helpers.rb +33 -29
  25. data/lib/ardb/use_db_default.rb +13 -21
  26. data/lib/ardb/version.rb +3 -1
  27. data/lib/ardb.rb +105 -86
  28. data/script/determine_autoloaded_active_record_files.rb +31 -24
  29. data/test/helper.rb +6 -13
  30. data/test/support/factory.rb +4 -3
  31. data/test/support/fake_schema.rb +3 -1
  32. data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
  33. data/test/support/postgresql/schema.rb +2 -1
  34. data/test/support/postgresql/setup_test_db.rb +23 -21
  35. data/test/support/relative_require_test_db_file.rb +1 -0
  36. data/test/support/require_test_db_file.rb +1 -0
  37. data/test/system/.keep +0 -0
  38. data/test/unit/adapter/base_tests.rb +80 -55
  39. data/test/unit/adapter/mysql_tests.rb +4 -19
  40. data/test/unit/adapter/postgresql_tests.rb +21 -30
  41. data/test/unit/adapter/sqlite_tests.rb +5 -11
  42. data/test/unit/adapter_spy_tests.rb +6 -17
  43. data/test/unit/ardb_tests.rb +75 -53
  44. data/test/unit/cli_tests.rb +234 -158
  45. data/test/unit/db_tests_tests.rb +7 -7
  46. data/test/unit/default_order_by_tests.rb +26 -24
  47. data/test/unit/migration_tests.rb +17 -18
  48. data/test/unit/record_spy_tests.rb +45 -41
  49. data/test/unit/relation_spy_tests.rb +40 -63
  50. data/test/unit/test_helpers_tests.rb +7 -15
  51. data/test/unit/use_db_default_tests.rb +35 -27
  52. metadata +109 -87
  53. data/lib/ardb/has_slug.rb +0 -107
  54. data/lib/ardb/migration_helpers.rb +0 -77
  55. data/lib/ardb/pg_json.rb +0 -90
  56. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
  57. data/test/system/pg_json_tests.rb +0 -85
  58. data/test/unit/has_slug_tests.rb +0 -341
  59. data/test/unit/migration_helpers_tests.rb +0 -65
  60. data/test/unit/pg_json_tests.rb +0 -39
@@ -1,10 +1,11 @@
1
- require 'assert'
2
- require 'ardb/db_tests'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'active_record'
3
+ require "assert"
4
+ require "ardb/db_tests"
5
5
 
6
- class Ardb::DbTests
6
+ require "active_record"
7
7
 
8
+ class Ardb::DbTests
8
9
  class UnitTests < Assert::Context
9
10
  desc "Ardb::DbTests"
10
11
  setup do
@@ -20,7 +21,8 @@ class Ardb::DbTests
20
21
  assert subject < Assert::Context
21
22
  end
22
23
 
23
- should "add an around callback that runs tests in a transaction that rolls back" do
24
+ should "add an around callback that runs tests in a transaction that "\
25
+ "rolls back" do
24
26
  assert_equal 1, subject.arounds.size
25
27
  callback = subject.arounds.first
26
28
 
@@ -31,7 +33,5 @@ class Ardb::DbTests
31
33
  assert_true @transaction_called
32
34
  assert_true block_yielded_to
33
35
  end
34
-
35
36
  end
36
-
37
37
  end
@@ -1,28 +1,30 @@
1
- require 'assert'
2
- require 'ardb/default_order_by'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'much-plugin'
5
- require 'ardb/record_spy'
3
+ require "assert"
4
+ require "ardb/default_order_by"
6
5
 
7
- module Ardb::DefaultOrderBy
6
+ require "much-mixin"
7
+ require "ardb/record_spy"
8
8
 
9
+ module Ardb::DefaultOrderBy
9
10
  class UnitTests < Assert::Context
10
11
  desc "Ardb::DefaultOrderBy"
11
12
  setup do
12
13
  order_by_attribute = @order_by_attribute = Factory.string.to_sym
13
- @scope_proc = proc{ self.class.where(:grouping => self.grouping) }
14
- @record_class = Ardb::RecordSpy.new do
15
- include Ardb::DefaultOrderBy
16
- attr_accessor order_by_attribute, :grouping
17
- end
14
+ @scope_proc = proc{ self.class.where(grouping: grouping) }
15
+ @record_class =
16
+ Ardb::RecordSpy.new do
17
+ include Ardb::DefaultOrderBy
18
+ attr_accessor order_by_attribute, :grouping
19
+ end
18
20
  end
19
21
  subject{ @record_class }
20
22
 
21
23
  should have_imeths :default_order_by
22
24
  should have_imeths :ardb_default_order_by_config
23
25
 
24
- should "use much-plugin" do
25
- assert_includes MuchPlugin, Ardb::DefaultOrderBy
26
+ should "use much-mixin" do
27
+ assert_includes MuchMixin, Ardb::DefaultOrderBy
26
28
  end
27
29
 
28
30
  should "know its default attribute, preprocessor and separator" do
@@ -45,8 +47,8 @@ module Ardb::DefaultOrderBy
45
47
 
46
48
  should "allow customizing the config using `default_order_by`" do
47
49
  subject.default_order_by({
48
- :attribute => @order_by_attribute,
49
- :scope => @scope_proc
50
+ attribute: @order_by_attribute,
51
+ scope: @scope_proc,
50
52
  })
51
53
 
52
54
  config = subject.ardb_default_order_by_config
@@ -60,20 +62,20 @@ module Ardb::DefaultOrderBy
60
62
  callback = subject.callbacks.find{ |v| v.type == :before_validation }
61
63
  assert_not_nil callback
62
64
  assert_equal [:ardb_default_order_by], callback.args
63
- assert_equal({ :on => :create }, callback.options)
65
+ assert_equal({ on: :create }, callback.options)
64
66
  end
65
-
66
67
  end
67
68
 
68
69
  class InitTests < UnitTests
69
70
  desc "when init"
70
71
  setup do
71
72
  @record_class.default_order_by({
72
- :attribute => @order_by_attribute,
73
- :scope => @scope_proc
73
+ attribute: @order_by_attribute,
74
+ scope: @scope_proc,
74
75
  })
75
76
  @current_max = Factory.integer
76
- @record_class.relation_spy.maximum_values[@order_by_attribute] = @current_max
77
+ @record_class.relation_spy.maximum_values[@order_by_attribute] =
78
+ @current_max
77
79
 
78
80
  @record = @record_class.new
79
81
  @record.grouping = Factory.string
@@ -86,7 +88,8 @@ module Ardb::DefaultOrderBy
86
88
  assert_equal @current_max + 1, subject.send(@order_by_attribute)
87
89
  end
88
90
 
89
- should "reset its order-by to a start value when there isn't a current max" do
91
+ should "reset its order-by to a start value when there isn't a "\
92
+ "current max" do
90
93
  @record_class.relation_spy.maximum_values.delete(@order_by_attribute)
91
94
 
92
95
  subject.instance_eval{ reset_order_by }
@@ -100,7 +103,7 @@ module Ardb::DefaultOrderBy
100
103
  assert_equal 1, @record_class.relation_spy.applied.size
101
104
  applied_expression = @record_class.relation_spy.applied.last
102
105
  assert_equal :where, applied_expression.type
103
- assert_equal [{ :grouping => subject.grouping }], applied_expression.args
106
+ assert_equal [{ grouping: subject.grouping }], applied_expression.args
104
107
  end
105
108
 
106
109
  should "reset its order-by using `ardb_default_order_by`" do
@@ -109,14 +112,13 @@ module Ardb::DefaultOrderBy
109
112
  assert_equal @current_max + 1, subject.send(@order_by_attribute)
110
113
  end
111
114
 
112
- should "not reset its order-by if its already set using `ardb_default_order_by`" do
115
+ should "not reset its order-by if its already set using "\
116
+ "`ardb_default_order_by`" do
113
117
  current_order_by = Factory.integer
114
118
  subject.send("#{@order_by_attribute}=", current_order_by)
115
119
  subject.instance_eval{ ardb_default_order_by }
116
120
 
117
121
  assert_equal current_order_by, subject.send(@order_by_attribute)
118
122
  end
119
-
120
123
  end
121
-
122
124
  end
@@ -1,19 +1,19 @@
1
- require 'assert'
2
- require 'ardb/migration'
1
+ # frozen_string_literal: true
3
2
 
4
- # This is needed to call `classify` on a string; if this isn't manually required
5
- # these tests can fail if activesupport hasn't been loaded by activerecord; the
3
+ require "assert"
4
+ require "ardb/migration"
5
+
6
+ # This is needed to call `classify` on a string; if this isn"t manually required
7
+ # these tests can fail if activesupport hasn"t been loaded by activerecord; the
6
8
  # `Migration` class will error saying `classify` is not a method on `String`
7
- require 'active_support/core_ext/string/inflections'
9
+ require "active_support/core_ext/string/inflections"
8
10
 
9
11
  class Ardb::Migration
10
-
11
12
  class UnitTests < Assert::Context
12
13
  desc "Ardb::Migration"
13
14
  setup do
14
15
  @migration_class = Ardb::Migration
15
16
  end
16
-
17
17
  end
18
18
 
19
19
  class InitTests < UnitTests
@@ -38,7 +38,7 @@ class Ardb::Migration
38
38
  end
39
39
  subject{ @migration }
40
40
 
41
- should have_readers :migrations_path, :identifier
41
+ should have_readers :migrations_path, :identifier
42
42
  should have_readers :class_name, :file_name, :file_path, :source
43
43
  should have_imeths :save!
44
44
 
@@ -55,18 +55,19 @@ class Ardb::Migration
55
55
  exp = File.join(subject.migrations_path, "#{subject.file_name}.rb")
56
56
  assert_equal exp, subject.file_path
57
57
 
58
- exp = "require 'ardb/migration_helpers'\n\n" \
59
- "class #{subject.class_name} < ActiveRecord::Migration\n" \
60
- " include Ardb::MigrationHelpers\n\n" \
61
- " def change\n" \
62
- " end\n\n" \
63
- "end\n"
58
+ exp_version = ActiveRecord::Migration.current_version
59
+ exp =
60
+ "class #{subject.class_name} "\
61
+ "< ActiveRecord::Migration[#{exp_version}]\n" \
62
+ " def change\n" \
63
+ " end\n" \
64
+ "end\n"
64
65
  assert_equal exp, subject.source
65
66
  end
66
67
 
67
68
  should "complain if no identifier is provided" do
68
69
  assert_raises(NoIdentifierError) do
69
- @migration_class.new(@ardb_config, [nil, ''].sample)
70
+ @migration_class.new(@ardb_config, [nil, ""].sample)
70
71
  end
71
72
  end
72
73
 
@@ -74,10 +75,9 @@ class Ardb::Migration
74
75
  subject.save!
75
76
 
76
77
  assert_equal [subject.migrations_path], @mkdir_called_with
77
- assert_equal [subject.file_path, 'w'], @file_open_called_with
78
+ assert_equal [subject.file_path, "w"], @file_open_called_with
78
79
  assert_equal [subject.source], @file_spy.write_called_with
79
80
  end
80
-
81
81
  end
82
82
 
83
83
  class FileSpy
@@ -91,5 +91,4 @@ class Ardb::Migration
91
91
  @write_called_with = args
92
92
  end
93
93
  end
94
-
95
94
  end
@@ -1,17 +1,19 @@
1
- require 'assert'
2
- require 'ardb/record_spy'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'much-plugin'
3
+ require "assert"
4
+ require "ardb/record_spy"
5
5
 
6
- module Ardb::RecordSpy
6
+ require "much-mixin"
7
7
 
8
+ module Ardb::RecordSpy
8
9
  class UnitTests < Assert::Context
9
10
  desc "Ardb::RecordSpy"
10
11
  setup do
11
- @record_spy_class = Class.new do
12
- include Ardb::RecordSpy
13
- attr_accessor :name
14
- end
12
+ @record_spy_class =
13
+ Class.new do
14
+ include Ardb::RecordSpy
15
+ attr_accessor :name
16
+ end
15
17
  end
16
18
  subject{ @record_spy_class }
17
19
 
@@ -41,13 +43,13 @@ module Ardb::RecordSpy
41
43
  should have_imeths :group, :having, :order, :reverse_order, :readonly
42
44
  should have_imeths :limit, :offset, :merge, :except, :only
43
45
 
44
- should "use much-plugin" do
45
- assert_includes MuchPlugin, Ardb::RecordSpy
46
+ should "use much-mixin" do
47
+ assert_includes MuchMixin, Ardb::RecordSpy
46
48
  end
47
49
 
48
50
  should "allow reading and writing the record's table name" do
49
- subject.table_name = 'my_records'
50
- assert_equal 'my_records', subject.table_name
51
+ subject.table_name = "my_records"
52
+ assert_equal "my_records", subject.table_name
51
53
  end
52
54
 
53
55
  should "default its associations" do
@@ -55,7 +57,7 @@ module Ardb::RecordSpy
55
57
  end
56
58
 
57
59
  should "add an association config with #belongs_to" do
58
- subject.belongs_to :area, :foreign_key => :area_id
60
+ subject.belongs_to :area, foreign_key: :area_id
59
61
  association = subject.associations.last
60
62
  assert_equal :belongs_to, association.type
61
63
  assert_equal :area, association.name
@@ -63,7 +65,7 @@ module Ardb::RecordSpy
63
65
  end
64
66
 
65
67
  should "add an association config with #has_many" do
66
- subject.has_many :comments, :as => :parent
68
+ subject.has_many :comments, as: :parent
67
69
  association = subject.associations.last
68
70
  assert_equal :has_many, association.type
69
71
  assert_equal :comments, association.name
@@ -71,19 +73,19 @@ module Ardb::RecordSpy
71
73
  end
72
74
 
73
75
  should "add an association config with #has_one" do
74
- subject.has_one :linking, :class_name => 'Linking'
76
+ subject.has_one :linking, class_name: "Linking"
75
77
  association = subject.associations.last
76
78
  assert_equal :has_one, association.type
77
79
  assert_equal :linking, association.name
78
- assert_equal 'Linking', association.options[:class_name]
80
+ assert_equal "Linking", association.options[:class_name]
79
81
  end
80
82
 
81
83
  should "default its validations" do
82
84
  assert_equal [], subject.validations
83
85
  end
84
86
 
85
- should "add a validation config for '*_of' validations" do
86
- subject.validates_presence_of :name, :email, :on => :create
87
+ should "add a validation config for \"*_of\" validations" do
88
+ subject.validates_presence_of :name, :email, on: :create
87
89
  validation = subject.validations.last
88
90
  assert_equal :presence, validation.type
89
91
  assert_equal :create, validation.options[:on]
@@ -110,7 +112,7 @@ module Ardb::RecordSpy
110
112
  end
111
113
 
112
114
  should "add a validation config with #validates_each" do
113
- block = proc{ }
115
+ block = proc{}
114
116
  subject.validates_each(:name, :email, &block)
115
117
  validation = subject.validations.last
116
118
  assert_equal :each, validation.type
@@ -125,7 +127,7 @@ module Ardb::RecordSpy
125
127
  assert_equal :custom, validation.type
126
128
  assert_equal :some_method, validation.method_name
127
129
 
128
- block = proc{ }
130
+ block = proc{}
129
131
  subject.validate(&block)
130
132
  validation = subject.validations.last
131
133
  assert_equal :custom, validation.type
@@ -144,15 +146,15 @@ module Ardb::RecordSpy
144
146
  end
145
147
 
146
148
  should "add a callback config with a block" do
147
- subject.before_validation(:on => :create) do
148
- self.name = 'test'
149
+ subject.before_validation(on: :create) do
150
+ self.name = "test"
149
151
  end
150
152
  callback = subject.callbacks.last
151
153
  assert_equal :before_validation, callback.type
152
154
  assert_equal :create, callback.options[:on]
153
155
  record_spy = subject.new
154
156
  record_spy.instance_eval(&callback.block)
155
- assert_equal 'test', record_spy.name
157
+ assert_equal "test", record_spy.name
156
158
  end
157
159
 
158
160
  should "default its custom callback types" do
@@ -177,7 +179,12 @@ module Ardb::RecordSpy
177
179
  assert_respond_to "around_#{name}", subject
178
180
  assert_respond_to "after_#{name}", subject
179
181
 
180
- callback_name = ["before_#{name}", "around_#{name}", "after_#{name}"].sample
182
+ callback_name =
183
+ [
184
+ "before_#{name}",
185
+ "around_#{name}",
186
+ "after_#{name}",
187
+ ].sample
181
188
  method_name = Factory.string
182
189
  subject.send(callback_name, method_name)
183
190
  callback = subject.callbacks.last
@@ -185,7 +192,7 @@ module Ardb::RecordSpy
185
192
  assert_equal [method_name], callback.args
186
193
 
187
194
  name = Factory.string
188
- subject.define_model_callbacks(name, :only => [:before])
195
+ subject.define_model_callbacks(name, only: [:before])
189
196
 
190
197
  assert_respond_to "before_#{name}", subject
191
198
  assert_not_respond_to "around_#{name}", subject
@@ -286,15 +293,15 @@ module Ardb::RecordSpy
286
293
  subject.only(*only_args)
287
294
  assert_equal only_args, only_called_with
288
295
  end
289
-
290
296
  end
291
297
 
292
298
  class GeneratorTests < UnitTests
293
299
  desc "to generate record spy classes"
294
300
  setup do
295
- @record_spy_class = Ardb::RecordSpy.new do
296
- attr_accessor :name
297
- end
301
+ @record_spy_class =
302
+ Ardb::RecordSpy.new do
303
+ attr_accessor :name
304
+ end
298
305
  @instance = @record_spy_class.new
299
306
  end
300
307
  subject{ @record_spy_class }
@@ -305,7 +312,6 @@ module Ardb::RecordSpy
305
312
  assert @instance.respond_to? :name
306
313
  assert @instance.respond_to? :name=
307
314
  end
308
-
309
315
  end
310
316
 
311
317
  class InstanceTests < UnitTests
@@ -319,24 +325,24 @@ module Ardb::RecordSpy
319
325
  should have_imeths :manually_run_callbacks, :run_callbacks
320
326
 
321
327
  should "allow spying the update_column method by just writing the value" do
322
- assert_not_equal 'updated', subject.name
328
+ assert_not_equal "updated", subject.name
323
329
 
324
- subject.update_column(:name, 'updated')
325
- assert_equal 'updated', subject.name
330
+ subject.update_column(:name, "updated")
331
+ assert_equal "updated", subject.name
326
332
  end
327
333
 
328
334
  should "have accessors for each association defined" do
329
335
  assert_nil subject.bt_thing
330
- subject.bt_thing = 'something'
331
- assert_equal 'something', subject.bt_thing
336
+ subject.bt_thing = "something"
337
+ assert_equal "something", subject.bt_thing
332
338
 
333
339
  assert_nil subject.ho_thing
334
- subject.ho_thing = 'other thing'
335
- assert_equal 'other thing', subject.ho_thing
340
+ subject.ho_thing = "other thing"
341
+ assert_equal "other thing", subject.ho_thing
336
342
 
337
343
  assert_empty subject.hm_things
338
- subject.hm_things = [1,2,3]
339
- assert_equal [1,2,3], subject.hm_things
344
+ subject.hm_things = [1, 2, 3]
345
+ assert_equal [1, 2, 3], subject.hm_things
340
346
  end
341
347
 
342
348
  should "default its manually run callbacks" do
@@ -355,7 +361,6 @@ module Ardb::RecordSpy
355
361
  assert_includes name, subject.manually_run_callbacks
356
362
  assert_true block_called
357
363
  end
358
-
359
364
  end
360
365
 
361
366
  class MyRecord
@@ -366,5 +371,4 @@ module Ardb::RecordSpy
366
371
  has_one :ho_thing
367
372
  has_many :hm_things
368
373
  end
369
-
370
374
  end