ardb 0.28.1 → 0.29.2
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.
- checksums.yaml +7 -7
- data/.l.yml +8 -0
- data/.rubocop.yml +3 -0
- data/.t.yml +6 -0
- data/Gemfile +21 -8
- data/README.md +252 -3
- data/ardb.gemspec +14 -10
- data/bin/ardb +3 -1
- data/lib/ardb.rb +110 -80
- data/lib/ardb/adapter/base.rb +73 -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.rb +29 -24
- data/lib/ardb/cli/clirb.rb +19 -17
- data/lib/ardb/cli/commands.rb +308 -129
- 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 +27 -31
- data/lib/ardb/require_autoloaded_active_record_files.rb +174 -58
- data/lib/ardb/test_helpers.rb +13 -14
- data/lib/ardb/use_db_default.rb +10 -19
- data/lib/ardb/version.rb +3 -1
- 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 +17 -15
- 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 +83 -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 +81 -53
- data/test/unit/cli_tests.rb +232 -157
- data/test/unit/db_tests_tests.rb +7 -7
- data/test/unit/default_order_by_tests.rb +21 -20
- data/test/unit/migration_tests.rb +17 -18
- data/test/unit/record_spy_tests.rb +36 -34
- 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 +22 -17
- metadata +117 -84
- 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/db_tests_tests.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
|
2
|
-
require 'ardb/db_tests'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
3
|
+
require "assert"
|
4
|
+
require "ardb/db_tests"
|
5
5
|
|
6
|
-
|
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
|
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,16 +1,17 @@
|
|
1
|
-
|
2
|
-
require 'ardb/default_order_by'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "assert"
|
4
|
+
require "ardb/default_order_by"
|
6
5
|
|
7
|
-
|
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(:
|
14
|
+
@scope_proc = proc{ self.class.where(grouping: grouping) }
|
14
15
|
@record_class = Ardb::RecordSpy.new do
|
15
16
|
include Ardb::DefaultOrderBy
|
16
17
|
attr_accessor order_by_attribute, :grouping
|
@@ -21,8 +22,8 @@ module Ardb::DefaultOrderBy
|
|
21
22
|
should have_imeths :default_order_by
|
22
23
|
should have_imeths :ardb_default_order_by_config
|
23
24
|
|
24
|
-
should "use much-
|
25
|
-
assert_includes
|
25
|
+
should "use much-mixin" do
|
26
|
+
assert_includes MuchMixin, Ardb::DefaultOrderBy
|
26
27
|
end
|
27
28
|
|
28
29
|
should "know its default attribute, preprocessor and separator" do
|
@@ -45,8 +46,8 @@ module Ardb::DefaultOrderBy
|
|
45
46
|
|
46
47
|
should "allow customizing the config using `default_order_by`" do
|
47
48
|
subject.default_order_by({
|
48
|
-
:
|
49
|
-
:
|
49
|
+
attribute: @order_by_attribute,
|
50
|
+
scope: @scope_proc,
|
50
51
|
})
|
51
52
|
|
52
53
|
config = subject.ardb_default_order_by_config
|
@@ -60,20 +61,20 @@ module Ardb::DefaultOrderBy
|
|
60
61
|
callback = subject.callbacks.find{ |v| v.type == :before_validation }
|
61
62
|
assert_not_nil callback
|
62
63
|
assert_equal [:ardb_default_order_by], callback.args
|
63
|
-
assert_equal({ :
|
64
|
+
assert_equal({ on: :create }, callback.options)
|
64
65
|
end
|
65
|
-
|
66
66
|
end
|
67
67
|
|
68
68
|
class InitTests < UnitTests
|
69
69
|
desc "when init"
|
70
70
|
setup do
|
71
71
|
@record_class.default_order_by({
|
72
|
-
:
|
73
|
-
:
|
72
|
+
attribute: @order_by_attribute,
|
73
|
+
scope: @scope_proc,
|
74
74
|
})
|
75
75
|
@current_max = Factory.integer
|
76
|
-
@record_class.relation_spy.maximum_values[@order_by_attribute] =
|
76
|
+
@record_class.relation_spy.maximum_values[@order_by_attribute] =
|
77
|
+
@current_max
|
77
78
|
|
78
79
|
@record = @record_class.new
|
79
80
|
@record.grouping = Factory.string
|
@@ -86,7 +87,8 @@ module Ardb::DefaultOrderBy
|
|
86
87
|
assert_equal @current_max + 1, subject.send(@order_by_attribute)
|
87
88
|
end
|
88
89
|
|
89
|
-
should "reset its order-by to a start value when there isn't a
|
90
|
+
should "reset its order-by to a start value when there isn't a "\
|
91
|
+
"current max" do
|
90
92
|
@record_class.relation_spy.maximum_values.delete(@order_by_attribute)
|
91
93
|
|
92
94
|
subject.instance_eval{ reset_order_by }
|
@@ -100,7 +102,7 @@ module Ardb::DefaultOrderBy
|
|
100
102
|
assert_equal 1, @record_class.relation_spy.applied.size
|
101
103
|
applied_expression = @record_class.relation_spy.applied.last
|
102
104
|
assert_equal :where, applied_expression.type
|
103
|
-
assert_equal [{ :
|
105
|
+
assert_equal [{ grouping: subject.grouping }], applied_expression.args
|
104
106
|
end
|
105
107
|
|
106
108
|
should "reset its order-by using `ardb_default_order_by`" do
|
@@ -109,14 +111,13 @@ module Ardb::DefaultOrderBy
|
|
109
111
|
assert_equal @current_max + 1, subject.send(@order_by_attribute)
|
110
112
|
end
|
111
113
|
|
112
|
-
should "not reset its order-by if its already set using
|
114
|
+
should "not reset its order-by if its already set using "\
|
115
|
+
"`ardb_default_order_by`" do
|
113
116
|
current_order_by = Factory.integer
|
114
117
|
subject.send("#{@order_by_attribute}=", current_order_by)
|
115
118
|
subject.instance_eval{ ardb_default_order_by }
|
116
119
|
|
117
120
|
assert_equal current_order_by, subject.send(@order_by_attribute)
|
118
121
|
end
|
119
|
-
|
120
122
|
end
|
121
|
-
|
122
123
|
end
|
@@ -1,19 +1,19 @@
|
|
1
|
-
|
2
|
-
require 'ardb/migration'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
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
|
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
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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,
|
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,
|
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,10 +1,11 @@
|
|
1
|
-
|
2
|
-
require 'ardb/record_spy'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
3
|
+
require "assert"
|
4
|
+
require "ardb/record_spy"
|
5
5
|
|
6
|
-
|
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
|
@@ -41,13 +42,13 @@ module Ardb::RecordSpy
|
|
41
42
|
should have_imeths :group, :having, :order, :reverse_order, :readonly
|
42
43
|
should have_imeths :limit, :offset, :merge, :except, :only
|
43
44
|
|
44
|
-
should "use much-
|
45
|
-
assert_includes
|
45
|
+
should "use much-mixin" do
|
46
|
+
assert_includes MuchMixin, Ardb::RecordSpy
|
46
47
|
end
|
47
48
|
|
48
49
|
should "allow reading and writing the record's table name" do
|
49
|
-
subject.table_name =
|
50
|
-
assert_equal
|
50
|
+
subject.table_name = "my_records"
|
51
|
+
assert_equal "my_records", subject.table_name
|
51
52
|
end
|
52
53
|
|
53
54
|
should "default its associations" do
|
@@ -55,7 +56,7 @@ module Ardb::RecordSpy
|
|
55
56
|
end
|
56
57
|
|
57
58
|
should "add an association config with #belongs_to" do
|
58
|
-
subject.belongs_to :area, :
|
59
|
+
subject.belongs_to :area, foreign_key: :area_id
|
59
60
|
association = subject.associations.last
|
60
61
|
assert_equal :belongs_to, association.type
|
61
62
|
assert_equal :area, association.name
|
@@ -63,7 +64,7 @@ module Ardb::RecordSpy
|
|
63
64
|
end
|
64
65
|
|
65
66
|
should "add an association config with #has_many" do
|
66
|
-
subject.has_many :comments, :
|
67
|
+
subject.has_many :comments, as: :parent
|
67
68
|
association = subject.associations.last
|
68
69
|
assert_equal :has_many, association.type
|
69
70
|
assert_equal :comments, association.name
|
@@ -71,19 +72,19 @@ module Ardb::RecordSpy
|
|
71
72
|
end
|
72
73
|
|
73
74
|
should "add an association config with #has_one" do
|
74
|
-
subject.has_one :linking, :
|
75
|
+
subject.has_one :linking, class_name: "Linking"
|
75
76
|
association = subject.associations.last
|
76
77
|
assert_equal :has_one, association.type
|
77
78
|
assert_equal :linking, association.name
|
78
|
-
assert_equal
|
79
|
+
assert_equal "Linking", association.options[:class_name]
|
79
80
|
end
|
80
81
|
|
81
82
|
should "default its validations" do
|
82
83
|
assert_equal [], subject.validations
|
83
84
|
end
|
84
85
|
|
85
|
-
should "add a validation config for
|
86
|
-
subject.validates_presence_of :name, :email, :
|
86
|
+
should "add a validation config for \"*_of\" validations" do
|
87
|
+
subject.validates_presence_of :name, :email, on: :create
|
87
88
|
validation = subject.validations.last
|
88
89
|
assert_equal :presence, validation.type
|
89
90
|
assert_equal :create, validation.options[:on]
|
@@ -110,7 +111,7 @@ module Ardb::RecordSpy
|
|
110
111
|
end
|
111
112
|
|
112
113
|
should "add a validation config with #validates_each" do
|
113
|
-
block = proc{
|
114
|
+
block = proc{}
|
114
115
|
subject.validates_each(:name, :email, &block)
|
115
116
|
validation = subject.validations.last
|
116
117
|
assert_equal :each, validation.type
|
@@ -125,7 +126,7 @@ module Ardb::RecordSpy
|
|
125
126
|
assert_equal :custom, validation.type
|
126
127
|
assert_equal :some_method, validation.method_name
|
127
128
|
|
128
|
-
block = proc{
|
129
|
+
block = proc{}
|
129
130
|
subject.validate(&block)
|
130
131
|
validation = subject.validations.last
|
131
132
|
assert_equal :custom, validation.type
|
@@ -144,15 +145,15 @@ module Ardb::RecordSpy
|
|
144
145
|
end
|
145
146
|
|
146
147
|
should "add a callback config with a block" do
|
147
|
-
subject.before_validation(:
|
148
|
-
self.name =
|
148
|
+
subject.before_validation(on: :create) do
|
149
|
+
self.name = "test"
|
149
150
|
end
|
150
151
|
callback = subject.callbacks.last
|
151
152
|
assert_equal :before_validation, callback.type
|
152
153
|
assert_equal :create, callback.options[:on]
|
153
154
|
record_spy = subject.new
|
154
155
|
record_spy.instance_eval(&callback.block)
|
155
|
-
assert_equal
|
156
|
+
assert_equal "test", record_spy.name
|
156
157
|
end
|
157
158
|
|
158
159
|
should "default its custom callback types" do
|
@@ -177,7 +178,12 @@ module Ardb::RecordSpy
|
|
177
178
|
assert_respond_to "around_#{name}", subject
|
178
179
|
assert_respond_to "after_#{name}", subject
|
179
180
|
|
180
|
-
callback_name =
|
181
|
+
callback_name =
|
182
|
+
[
|
183
|
+
"before_#{name}",
|
184
|
+
"around_#{name}",
|
185
|
+
"after_#{name}",
|
186
|
+
].sample
|
181
187
|
method_name = Factory.string
|
182
188
|
subject.send(callback_name, method_name)
|
183
189
|
callback = subject.callbacks.last
|
@@ -185,7 +191,7 @@ module Ardb::RecordSpy
|
|
185
191
|
assert_equal [method_name], callback.args
|
186
192
|
|
187
193
|
name = Factory.string
|
188
|
-
subject.define_model_callbacks(name, :
|
194
|
+
subject.define_model_callbacks(name, only: [:before])
|
189
195
|
|
190
196
|
assert_respond_to "before_#{name}", subject
|
191
197
|
assert_not_respond_to "around_#{name}", subject
|
@@ -286,7 +292,6 @@ module Ardb::RecordSpy
|
|
286
292
|
subject.only(*only_args)
|
287
293
|
assert_equal only_args, only_called_with
|
288
294
|
end
|
289
|
-
|
290
295
|
end
|
291
296
|
|
292
297
|
class GeneratorTests < UnitTests
|
@@ -305,7 +310,6 @@ module Ardb::RecordSpy
|
|
305
310
|
assert @instance.respond_to? :name
|
306
311
|
assert @instance.respond_to? :name=
|
307
312
|
end
|
308
|
-
|
309
313
|
end
|
310
314
|
|
311
315
|
class InstanceTests < UnitTests
|
@@ -319,24 +323,24 @@ module Ardb::RecordSpy
|
|
319
323
|
should have_imeths :manually_run_callbacks, :run_callbacks
|
320
324
|
|
321
325
|
should "allow spying the update_column method by just writing the value" do
|
322
|
-
assert_not_equal
|
326
|
+
assert_not_equal "updated", subject.name
|
323
327
|
|
324
|
-
subject.update_column(:name,
|
325
|
-
assert_equal
|
328
|
+
subject.update_column(:name, "updated")
|
329
|
+
assert_equal "updated", subject.name
|
326
330
|
end
|
327
331
|
|
328
332
|
should "have accessors for each association defined" do
|
329
333
|
assert_nil subject.bt_thing
|
330
|
-
subject.bt_thing =
|
331
|
-
assert_equal
|
334
|
+
subject.bt_thing = "something"
|
335
|
+
assert_equal "something", subject.bt_thing
|
332
336
|
|
333
337
|
assert_nil subject.ho_thing
|
334
|
-
subject.ho_thing =
|
335
|
-
assert_equal
|
338
|
+
subject.ho_thing = "other thing"
|
339
|
+
assert_equal "other thing", subject.ho_thing
|
336
340
|
|
337
341
|
assert_empty subject.hm_things
|
338
|
-
subject.hm_things = [1,2,3]
|
339
|
-
assert_equal [1,2,3], subject.hm_things
|
342
|
+
subject.hm_things = [1, 2, 3]
|
343
|
+
assert_equal [1, 2, 3], subject.hm_things
|
340
344
|
end
|
341
345
|
|
342
346
|
should "default its manually run callbacks" do
|
@@ -355,7 +359,6 @@ module Ardb::RecordSpy
|
|
355
359
|
assert_includes name, subject.manually_run_callbacks
|
356
360
|
assert_true block_called
|
357
361
|
end
|
358
|
-
|
359
362
|
end
|
360
363
|
|
361
364
|
class MyRecord
|
@@ -366,5 +369,4 @@ module Ardb::RecordSpy
|
|
366
369
|
has_one :ho_thing
|
367
370
|
has_many :hm_things
|
368
371
|
end
|
369
|
-
|
370
372
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
|
2
|
-
require 'ardb/relation_spy'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
3
|
+
require "assert"
|
4
|
+
require "ardb/relation_spy"
|
5
5
|
|
6
|
+
class Ardb::RelationSpy
|
6
7
|
class UnitTests < Assert::Context
|
7
8
|
desc "Ardb::RelationSpy"
|
8
9
|
setup do
|
@@ -50,12 +51,13 @@ class Ardb::RelationSpy
|
|
50
51
|
assert_equal other_relation, subject
|
51
52
|
end
|
52
53
|
|
53
|
-
should "build a fake sql string for its applied expressions using
|
54
|
-
|
55
|
-
subject.
|
56
|
-
subject.
|
54
|
+
should "build a fake sql string for its applied expressions using "\
|
55
|
+
"`to_sql`" do
|
56
|
+
subject.select "column"
|
57
|
+
subject.from "table"
|
58
|
+
subject.joins "my_table.my_column ON my_table.my_column = table.column"
|
57
59
|
|
58
|
-
expected = subject.applied.map(&:to_sql).join(
|
60
|
+
expected = subject.applied.map(&:to_sql).join(", ")
|
59
61
|
assert_equal expected, subject.to_sql
|
60
62
|
end
|
61
63
|
|
@@ -70,7 +72,6 @@ class Ardb::RelationSpy
|
|
70
72
|
assert_nil subject.limit_value
|
71
73
|
assert_nil subject.offset_value
|
72
74
|
end
|
73
|
-
|
74
75
|
end
|
75
76
|
|
76
77
|
class SelectTests < UnitTests
|
@@ -83,9 +84,8 @@ class Ardb::RelationSpy
|
|
83
84
|
should "add a select applied expression with the passed args" do
|
84
85
|
assert_instance_of AppliedExpression, @applied
|
85
86
|
assert_equal :select, @applied.type
|
86
|
-
assert_equal [
|
87
|
+
assert_equal [:column_a, :column_b], @applied.args
|
87
88
|
end
|
88
|
-
|
89
89
|
end
|
90
90
|
|
91
91
|
class FromTests < UnitTests
|
@@ -98,9 +98,8 @@ class Ardb::RelationSpy
|
|
98
98
|
should "add a from applied expression with the passed args" do
|
99
99
|
assert_instance_of AppliedExpression, @applied
|
100
100
|
assert_equal :from, @applied.type
|
101
|
-
assert_equal [
|
101
|
+
assert_equal ["some SQL"], @applied.args
|
102
102
|
end
|
103
|
-
|
104
103
|
end
|
105
104
|
|
106
105
|
class IncludesTests < UnitTests
|
@@ -113,9 +112,8 @@ class Ardb::RelationSpy
|
|
113
112
|
should "add an includes applied expression with the passed args" do
|
114
113
|
assert_instance_of AppliedExpression, @applied
|
115
114
|
assert_equal :includes, @applied.type
|
116
|
-
assert_equal [
|
115
|
+
assert_equal [:table_a, :table_b], @applied.args
|
117
116
|
end
|
118
|
-
|
119
117
|
end
|
120
118
|
|
121
119
|
class JoinsTests < UnitTests
|
@@ -128,24 +126,22 @@ class Ardb::RelationSpy
|
|
128
126
|
should "add a joins applied expression with the passed args" do
|
129
127
|
assert_instance_of AppliedExpression, @applied
|
130
128
|
assert_equal :joins, @applied.type
|
131
|
-
assert_equal [
|
129
|
+
assert_equal [:table_a, :table_b], @applied.args
|
132
130
|
end
|
133
|
-
|
134
131
|
end
|
135
132
|
|
136
133
|
class WhereTests < UnitTests
|
137
134
|
desc "where"
|
138
135
|
setup do
|
139
|
-
@relation_spy.where :
|
136
|
+
@relation_spy.where column_a: "some value"
|
140
137
|
@applied = subject.applied.first
|
141
138
|
end
|
142
139
|
|
143
140
|
should "add a where applied expression with the passed args" do
|
144
141
|
assert_instance_of AppliedExpression, @applied
|
145
142
|
assert_equal :where, @applied.type
|
146
|
-
assert_equal [
|
143
|
+
assert_equal [{ column_a: "some value" }], @applied.args
|
147
144
|
end
|
148
|
-
|
149
145
|
end
|
150
146
|
|
151
147
|
class OrderTests < UnitTests
|
@@ -158,9 +154,8 @@ class Ardb::RelationSpy
|
|
158
154
|
should "add an order applied expression with the passed args" do
|
159
155
|
assert_instance_of AppliedExpression, @applied
|
160
156
|
assert_equal :order, @applied.type
|
161
|
-
assert_equal [
|
157
|
+
assert_equal [:column_a, :column_b], @applied.args
|
162
158
|
end
|
163
|
-
|
164
159
|
end
|
165
160
|
|
166
161
|
class ReverseOrderTests < UnitTests
|
@@ -174,7 +169,6 @@ class Ardb::RelationSpy
|
|
174
169
|
assert_instance_of AppliedExpression, @applied
|
175
170
|
assert_equal :reverse_order, @applied.type
|
176
171
|
end
|
177
|
-
|
178
172
|
end
|
179
173
|
|
180
174
|
class GroupTests < UnitTests
|
@@ -187,24 +181,22 @@ class Ardb::RelationSpy
|
|
187
181
|
should "add a group applied expression with the passed args" do
|
188
182
|
assert_instance_of AppliedExpression, @applied
|
189
183
|
assert_equal :group, @applied.type
|
190
|
-
assert_equal [
|
184
|
+
assert_equal [:column_a, :column_b], @applied.args
|
191
185
|
end
|
192
|
-
|
193
186
|
end
|
194
187
|
|
195
188
|
class HavingTests < UnitTests
|
196
189
|
desc "having"
|
197
190
|
setup do
|
198
|
-
@relation_spy.having
|
191
|
+
@relation_spy.having "COUNT(column_a) > 0"
|
199
192
|
@applied = subject.applied.first
|
200
193
|
end
|
201
194
|
|
202
195
|
should "add a having applied expression with the passed args" do
|
203
196
|
assert_instance_of AppliedExpression, @applied
|
204
197
|
assert_equal :having, @applied.type
|
205
|
-
assert_equal [
|
198
|
+
assert_equal ["COUNT(column_a) > 0"], @applied.args
|
206
199
|
end
|
207
|
-
|
208
200
|
end
|
209
201
|
|
210
202
|
class ReadonlyTests < UnitTests
|
@@ -217,9 +209,8 @@ class Ardb::RelationSpy
|
|
217
209
|
should "add a readonly applied expression with the passed args" do
|
218
210
|
assert_instance_of AppliedExpression, @applied
|
219
211
|
assert_equal :readonly, @applied.type
|
220
|
-
assert_equal [
|
212
|
+
assert_equal [true], @applied.args
|
221
213
|
end
|
222
|
-
|
223
214
|
end
|
224
215
|
|
225
216
|
class LimitTests < UnitTests
|
@@ -232,13 +223,12 @@ class Ardb::RelationSpy
|
|
232
223
|
should "add a limit applied expression with the passed args" do
|
233
224
|
assert_instance_of AppliedExpression, @applied
|
234
225
|
assert_equal :limit, @applied.type
|
235
|
-
assert_equal [
|
226
|
+
assert_equal [100], @applied.args
|
236
227
|
end
|
237
228
|
|
238
229
|
should "set it's limit value" do
|
239
230
|
assert_equal 100, subject.limit_value
|
240
231
|
end
|
241
|
-
|
242
232
|
end
|
243
233
|
|
244
234
|
class OffsetTests < UnitTests
|
@@ -251,19 +241,19 @@ class Ardb::RelationSpy
|
|
251
241
|
should "add an offset applied expression with the passed args" do
|
252
242
|
assert_instance_of AppliedExpression, @applied
|
253
243
|
assert_equal :offset, @applied.type
|
254
|
-
assert_equal [
|
244
|
+
assert_equal [100], @applied.args
|
255
245
|
end
|
256
246
|
|
257
247
|
should "set it's offset value" do
|
258
248
|
assert_equal 100, subject.offset_value
|
259
249
|
end
|
260
|
-
|
261
250
|
end
|
262
251
|
|
263
252
|
class MergeWithARelationSpyTests < UnitTests
|
264
253
|
desc "merge with a relation spy"
|
265
254
|
setup do
|
266
|
-
@other_relation_spy =
|
255
|
+
@other_relation_spy =
|
256
|
+
Ardb::RelationSpy.new.select("column").joins("table")
|
267
257
|
@relation_spy.merge @other_relation_spy
|
268
258
|
end
|
269
259
|
|
@@ -272,13 +262,12 @@ class Ardb::RelationSpy
|
|
272
262
|
assert_includes applied, @relation_spy.applied
|
273
263
|
end
|
274
264
|
end
|
275
|
-
|
276
265
|
end
|
277
266
|
|
278
267
|
class MergeWithNonRelationSpyTests < UnitTests
|
279
268
|
desc "merge without a relation spy"
|
280
269
|
setup do
|
281
|
-
@fake_relation =
|
270
|
+
@fake_relation = "relation"
|
282
271
|
@relation_spy.merge @fake_relation
|
283
272
|
@applied = subject.applied.first
|
284
273
|
end
|
@@ -286,29 +275,27 @@ class Ardb::RelationSpy
|
|
286
275
|
should "add a merge applied expression with the passed args" do
|
287
276
|
assert_instance_of AppliedExpression, @applied
|
288
277
|
assert_equal :merge, @applied.type
|
289
|
-
assert_equal [
|
278
|
+
assert_equal [@fake_relation], @applied.args
|
290
279
|
end
|
291
|
-
|
292
280
|
end
|
293
281
|
|
294
282
|
class MergeWithSelfTests < UnitTests
|
295
283
|
desc "merge with itself"
|
296
284
|
setup do
|
297
|
-
@fake_relation =
|
285
|
+
@fake_relation = "relation"
|
298
286
|
@relation_spy.merge @relation_spy
|
299
287
|
end
|
300
288
|
|
301
289
|
should "not alter the applied expressions" do
|
302
290
|
assert_empty subject.applied
|
303
291
|
end
|
304
|
-
|
305
292
|
end
|
306
293
|
|
307
294
|
class WithExpressionsTests < UnitTests
|
308
295
|
setup do
|
309
|
-
@relation_spy.select(
|
310
|
-
@relation_spy.where(:
|
311
|
-
@relation_spy.group(
|
296
|
+
@relation_spy.select("column").includes("table").joins("table")
|
297
|
+
@relation_spy.where(column: "value").order("column")
|
298
|
+
@relation_spy.group("column").having("count(*) > 1")
|
312
299
|
@relation_spy.limit(1).offset(1)
|
313
300
|
end
|
314
301
|
end
|
@@ -324,10 +311,10 @@ class Ardb::RelationSpy
|
|
324
311
|
should "remove any applied expressions in the passed types" do
|
325
312
|
relation_spy = subject.except(:includes, :where, :group, :offset)
|
326
313
|
applied_types = relation_spy.applied.map(&:type)
|
327
|
-
[
|
314
|
+
[:select, :joins, :order, :having, :limit].each do |type|
|
328
315
|
assert_includes type, applied_types
|
329
316
|
end
|
330
|
-
[
|
317
|
+
[:includes, :where, :group, :offset].each do |type|
|
331
318
|
assert_not_includes type, applied_types
|
332
319
|
end
|
333
320
|
end
|
@@ -345,7 +332,6 @@ class Ardb::RelationSpy
|
|
345
332
|
relation_spy = subject.except(:offset)
|
346
333
|
assert_nil relation_spy.offset_value
|
347
334
|
end
|
348
|
-
|
349
335
|
end
|
350
336
|
|
351
337
|
class OnlyTests < WithExpressionsTests
|
@@ -359,28 +345,29 @@ class Ardb::RelationSpy
|
|
359
345
|
should "remove any applied expressions not in the passed types" do
|
360
346
|
relation_spy = subject.only(:includes, :where, :group, :offset)
|
361
347
|
applied_types = relation_spy.applied.map(&:type)
|
362
|
-
[
|
348
|
+
[:includes, :where, :group, :offset].each do |type|
|
363
349
|
assert_includes type, applied_types
|
364
350
|
end
|
365
|
-
[
|
351
|
+
[:select, :joins, :order, :having, :limit].each do |type|
|
366
352
|
assert_not_includes type, applied_types
|
367
353
|
end
|
368
354
|
end
|
369
355
|
|
370
|
-
should "unset the limit value if limit is not included in the passed
|
356
|
+
should "unset the limit value if limit is not included in the passed "\
|
357
|
+
"types" do
|
371
358
|
relation_spy = subject.only(:limit)
|
372
359
|
assert_not_nil relation_spy.limit_value
|
373
360
|
relation_spy = subject.only(:select)
|
374
361
|
assert_nil relation_spy.limit_value
|
375
362
|
end
|
376
363
|
|
377
|
-
should "unset the offset value if offset is not included in the passed
|
364
|
+
should "unset the offset value if offset is not included in the passed "\
|
365
|
+
"types" do
|
378
366
|
relation_spy = subject.only(:offset)
|
379
367
|
assert_not_nil relation_spy.offset_value
|
380
368
|
relation_spy = subject.only(:select)
|
381
369
|
assert_nil relation_spy.offset_value
|
382
370
|
end
|
383
|
-
|
384
371
|
end
|
385
372
|
|
386
373
|
class WithResultsTests < UnitTests
|
@@ -401,11 +388,9 @@ class Ardb::RelationSpy
|
|
401
388
|
should "raise a not found error if a result can't be found" do
|
402
389
|
assert_raises(NotFoundError){ subject.find(1000) }
|
403
390
|
end
|
404
|
-
|
405
391
|
end
|
406
392
|
|
407
393
|
class FirstTests < WithResultsTests
|
408
|
-
|
409
394
|
should "return the first item from `all` using `first`" do
|
410
395
|
assert_equal subject.all.first, subject.first
|
411
396
|
subject.offset 2
|
@@ -418,11 +403,9 @@ class Ardb::RelationSpy
|
|
418
403
|
subject.limit 0
|
419
404
|
assert_raises(NotFoundError){ subject.first! }
|
420
405
|
end
|
421
|
-
|
422
406
|
end
|
423
407
|
|
424
408
|
class LastTests < WithResultsTests
|
425
|
-
|
426
409
|
should "return the last item from `all` using `last`" do
|
427
410
|
assert_equal subject.all.last, subject.last
|
428
411
|
subject.limit 2
|
@@ -435,7 +418,6 @@ class Ardb::RelationSpy
|
|
435
418
|
subject.limit 0
|
436
419
|
assert_raises(NotFoundError){ subject.last! }
|
437
420
|
end
|
438
|
-
|
439
421
|
end
|
440
422
|
|
441
423
|
class AllTests < WithResultsTests
|
@@ -458,7 +440,6 @@ class Ardb::RelationSpy
|
|
458
440
|
subject.offset 2
|
459
441
|
assert_equal @results[2, 2], subject.all
|
460
442
|
end
|
461
|
-
|
462
443
|
end
|
463
444
|
|
464
445
|
class CountTests < WithResultsTests
|
@@ -469,7 +450,6 @@ class Ardb::RelationSpy
|
|
469
450
|
subject.limit 2
|
470
451
|
assert_equal subject.all.size, subject.count
|
471
452
|
end
|
472
|
-
|
473
453
|
end
|
474
454
|
|
475
455
|
class PluckTests < WithResultsTests
|
@@ -484,13 +464,12 @@ class Ardb::RelationSpy
|
|
484
464
|
exp = [@column_value] * @results.size
|
485
465
|
assert_equal exp, @relation_spy.pluck(@column_name)
|
486
466
|
end
|
487
|
-
|
488
467
|
end
|
489
468
|
|
490
469
|
class AppliedExpressionTests < UnitTests
|
491
470
|
desc "AppliedExpression"
|
492
471
|
setup do
|
493
|
-
@applied_expression = AppliedExpression.new(:select,
|
472
|
+
@applied_expression = AppliedExpression.new(:select, "column")
|
494
473
|
end
|
495
474
|
subject{ @applied_expression }
|
496
475
|
|
@@ -501,9 +480,7 @@ class Ardb::RelationSpy
|
|
501
480
|
expected = "#{subject.type}: #{subject.args.inspect}"
|
502
481
|
assert_equal expected, subject.to_sql
|
503
482
|
end
|
504
|
-
|
505
483
|
end
|
506
484
|
|
507
485
|
Result = Struct.new(:id)
|
508
|
-
|
509
486
|
end
|