ardb 0.28.3 → 0.30.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.
- 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
@@ -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
|
@@ -1,10 +1,11 @@
|
|
1
|
-
|
2
|
-
require 'ardb/test_helpers'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
3
|
+
require "assert"
|
4
|
+
require "ardb/test_helpers"
|
5
5
|
|
6
|
-
|
6
|
+
require "ardb/adapter_spy"
|
7
7
|
|
8
|
+
module Ardb::TestHelpers
|
8
9
|
class UnitTests < Assert::Context
|
9
10
|
desc "Ardb::TestHelpers"
|
10
11
|
subject{ Ardb::TestHelpers }
|
@@ -13,7 +14,6 @@ module Ardb::TestHelpers
|
|
13
14
|
should have_imeths :create_db!, :create_db, :drop_db!, :drop_db
|
14
15
|
should have_imeths :connect_db!, :connect_db, :migrate_db!, :migrate_db
|
15
16
|
should have_imeths :reset_db, :reset_db!
|
16
|
-
|
17
17
|
end
|
18
18
|
|
19
19
|
class UsageTests < UnitTests
|
@@ -21,7 +21,6 @@ module Ardb::TestHelpers
|
|
21
21
|
@adapter_spy = Ardb::AdapterSpy.new
|
22
22
|
Assert.stub(Ardb, :adapter){ @adapter_spy }
|
23
23
|
end
|
24
|
-
|
25
24
|
end
|
26
25
|
|
27
26
|
class DropTablesTests < UsageTests
|
@@ -32,7 +31,6 @@ module Ardb::TestHelpers
|
|
32
31
|
subject.drop_tables
|
33
32
|
assert_equal 1, @adapter_spy.drop_tables_called_count
|
34
33
|
end
|
35
|
-
|
36
34
|
end
|
37
35
|
|
38
36
|
class LoadSchemaTests < UsageTests
|
@@ -43,7 +41,6 @@ module Ardb::TestHelpers
|
|
43
41
|
subject.load_schema
|
44
42
|
assert_equal 1, @adapter_spy.load_schema_called_count
|
45
43
|
end
|
46
|
-
|
47
44
|
end
|
48
45
|
|
49
46
|
class CreateDbTests < UsageTests
|
@@ -64,7 +61,6 @@ module Ardb::TestHelpers
|
|
64
61
|
subject.create_db!
|
65
62
|
assert_equal 2, @adapter_spy.create_db_called_count
|
66
63
|
end
|
67
|
-
|
68
64
|
end
|
69
65
|
|
70
66
|
class DropDbTests < UsageTests
|
@@ -85,7 +81,6 @@ module Ardb::TestHelpers
|
|
85
81
|
subject.drop_db!
|
86
82
|
assert_equal 2, @adapter_spy.drop_db_called_count
|
87
83
|
end
|
88
|
-
|
89
84
|
end
|
90
85
|
|
91
86
|
class ConnectDbTests < UsageTests
|
@@ -106,7 +101,6 @@ module Ardb::TestHelpers
|
|
106
101
|
subject.connect_db!
|
107
102
|
assert_equal 2, @adapter_spy.connect_db_called_count
|
108
103
|
end
|
109
|
-
|
110
104
|
end
|
111
105
|
|
112
106
|
class MigrateDbTests < UsageTests
|
@@ -127,13 +121,13 @@ module Ardb::TestHelpers
|
|
127
121
|
subject.migrate_db!
|
128
122
|
assert_equal 2, @adapter_spy.migrate_db_called_count
|
129
123
|
end
|
130
|
-
|
131
124
|
end
|
132
125
|
|
133
126
|
class ResetDbTests < UsageTests
|
134
127
|
desc "reset db methods"
|
135
128
|
|
136
|
-
should "tell the adapter to drop/create the db and load the schema
|
129
|
+
should "tell the adapter to drop/create the db and load the schema "\
|
130
|
+
"only once" do
|
137
131
|
assert_equal 0, @adapter_spy.drop_db_called_count
|
138
132
|
assert_equal 0, @adapter_spy.create_db_called_count
|
139
133
|
assert_equal 0, @adapter_spy.load_schema_called_count
|
@@ -168,7 +162,5 @@ module Ardb::TestHelpers
|
|
168
162
|
assert_equal 2, @adapter_spy.create_db_called_count
|
169
163
|
assert_equal 2, @adapter_spy.load_schema_called_count
|
170
164
|
end
|
171
|
-
|
172
165
|
end
|
173
|
-
|
174
166
|
end
|
@@ -1,25 +1,27 @@
|
|
1
|
-
|
2
|
-
require 'ardb/use_db_default'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "assert"
|
4
|
+
require "ardb/use_db_default"
|
6
5
|
|
7
|
-
|
6
|
+
require "much-mixin"
|
7
|
+
require "ardb/record_spy"
|
8
8
|
|
9
|
+
module Ardb::UseDbDefault
|
9
10
|
class UnitTests < Assert::Context
|
10
11
|
desc "Ardb::UseDbDefault"
|
11
12
|
setup do
|
12
|
-
@record_class =
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
@record_class =
|
14
|
+
Class.new do
|
15
|
+
include UseDbDefaultRecordSpy
|
16
|
+
include Ardb::UseDbDefault
|
17
|
+
end
|
16
18
|
end
|
17
19
|
subject{ @record_class }
|
18
20
|
|
19
21
|
should have_imeths :use_db_default, :ardb_use_db_default_attrs
|
20
22
|
|
21
|
-
should "use much-
|
22
|
-
assert_includes
|
23
|
+
should "use much-mixin" do
|
24
|
+
assert_includes MuchMixin, Ardb::UseDbDefault
|
23
25
|
end
|
24
26
|
|
25
27
|
should "know its use db default attrs" do
|
@@ -27,9 +29,9 @@ module Ardb::UseDbDefault
|
|
27
29
|
end
|
28
30
|
|
29
31
|
should "add use db default attributes using `use_db_default`" do
|
30
|
-
|
31
|
-
subject.use_db_default(
|
32
|
-
assert_includes
|
32
|
+
an_attr_name = Factory.string
|
33
|
+
subject.use_db_default(an_attr_name)
|
34
|
+
assert_includes an_attr_name, subject.ardb_use_db_default_attrs
|
33
35
|
|
34
36
|
attr_names = [Factory.string, Factory.string.to_sym]
|
35
37
|
subject.use_db_default(*attr_names)
|
@@ -58,7 +60,6 @@ module Ardb::UseDbDefault
|
|
58
60
|
exp = [:ardb_allow_db_to_default_attrs]
|
59
61
|
assert_equal exp, callback.args
|
60
62
|
end
|
61
|
-
|
62
63
|
end
|
63
64
|
|
64
65
|
class InitTests < UnitTests
|
@@ -70,9 +71,10 @@ module Ardb::UseDbDefault
|
|
70
71
|
@record = @record_class.new
|
71
72
|
|
72
73
|
# simulate activerecords `@attributes` hash
|
73
|
-
@original_attrs =
|
74
|
-
|
75
|
-
|
74
|
+
@original_attrs =
|
75
|
+
@attr_names.reduce({}) do |h, n|
|
76
|
+
h.merge!(n => [nil, Factory.string, Factory.integer].sample)
|
77
|
+
end
|
76
78
|
@original_attrs.merge!(Factory.string => Factory.string)
|
77
79
|
@record.attributes = @original_attrs.dup
|
78
80
|
|
@@ -83,9 +85,10 @@ module Ardb::UseDbDefault
|
|
83
85
|
# we should always get the record we just inserted back
|
84
86
|
@record_class.relation_spy.results = [@record]
|
85
87
|
# add pluck values into the relation spy
|
86
|
-
@record_class.relation_spy.pluck_values =
|
87
|
-
|
88
|
-
|
88
|
+
@record_class.relation_spy.pluck_values =
|
89
|
+
@attr_names.reduce({}) do |h, n|
|
90
|
+
h.merge!(n => Factory.string)
|
91
|
+
end
|
89
92
|
end
|
90
93
|
subject{ @record }
|
91
94
|
|
@@ -95,7 +98,9 @@ module Ardb::UseDbDefault
|
|
95
98
|
# when it was created (yielded)
|
96
99
|
attrs_before_yield = nil
|
97
100
|
subject.instance_eval do
|
98
|
-
ardb_allow_db_to_default_attrs
|
101
|
+
ardb_allow_db_to_default_attrs do
|
102
|
+
attrs_before_yield = attributes.dup
|
103
|
+
end
|
99
104
|
end
|
100
105
|
|
101
106
|
assert_instance_of Hash, attrs_before_yield
|
@@ -119,7 +124,7 @@ module Ardb::UseDbDefault
|
|
119
124
|
|
120
125
|
applied_expr = @record_class.relation_spy.applied.first
|
121
126
|
assert_equal :where, applied_expr.type
|
122
|
-
assert_equal [{ :
|
127
|
+
assert_equal [{ id: subject.id }], applied_expr.args
|
123
128
|
|
124
129
|
@unchanged_attr_names.each do |name|
|
125
130
|
exp = @record_class.relation_spy.pluck_values[name]
|
@@ -132,7 +137,6 @@ module Ardb::UseDbDefault
|
|
132
137
|
assert_equal @original_attrs[name], subject.attributes[name]
|
133
138
|
end
|
134
139
|
end
|
135
|
-
|
136
140
|
end
|
137
141
|
|
138
142
|
module UseDbDefaultRecordSpy
|
@@ -144,17 +148,21 @@ module Ardb::UseDbDefault
|
|
144
148
|
attr_accessor :attributes, :changed_use_db_default_attrs
|
145
149
|
|
146
150
|
def use_db_default_attr_changed?(attr_name)
|
147
|
-
|
151
|
+
changed_use_db_default_attrs.include?(attr_name)
|
148
152
|
end
|
149
153
|
|
150
154
|
def method_missing(method, *args, &block)
|
151
155
|
match_data = method.to_s.match(/(\w+)_changed\?/)
|
152
156
|
if match_data && match_data[1]
|
153
|
-
|
157
|
+
use_db_default_attr_changed?(match_data[1])
|
154
158
|
else
|
155
159
|
super
|
156
160
|
end
|
157
161
|
end
|
158
|
-
end
|
159
162
|
|
163
|
+
def respond_to_missing?(method)
|
164
|
+
match_data = method.to_s.match(/(\w+)_changed\?/)
|
165
|
+
match_data && match_data[1]
|
166
|
+
end
|
167
|
+
end
|
160
168
|
end
|