arel_extensions 1.1.8 → 1.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/Gemfile +1 -1
- data/gemfiles/rails3.gemfile +12 -12
- data/gemfiles/rails4.gemfile +17 -17
- data/gemfiles/rails5_0.gemfile +17 -17
- data/gemfiles/rails5_1_4.gemfile +17 -17
- data/gemfiles/rails5_2.gemfile +17 -17
- data/lib/arel_extensions/boolean_functions.rb +30 -11
- data/lib/arel_extensions/math.rb +111 -112
- data/lib/arel_extensions/math_functions.rb +50 -31
- data/lib/arel_extensions/nodes/function.rb +25 -26
- data/lib/arel_extensions/nodes/json.rb +81 -0
- data/lib/arel_extensions/predications.rb +95 -97
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/mssql.rb +121 -108
- data/lib/arel_extensions/visitors/mysql.rb +230 -148
- data/lib/arel_extensions/visitors/oracle.rb +242 -232
- data/lib/arel_extensions/visitors/oracle12.rb +39 -1
- data/lib/arel_extensions/visitors/postgresql.rb +216 -134
- data/lib/arel_extensions/visitors/sqlite.rb +200 -189
- data/lib/arel_extensions/visitors/to_sql.rb +239 -171
- data/lib/arel_extensions/visitors.rb +63 -33
- data/lib/arel_extensions.rb +1 -0
- data/test/visitors/test_oracle.rb +1 -1
- data/test/visitors/test_to_sql.rb +206 -195
- data/test/with_ar/all_agnostic_test.rb +278 -235
- metadata +3 -2
@@ -24,7 +24,7 @@ module ArelExtensions
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def setup_db
|
27
|
-
@cnx.drop_table(:user_tests) rescue nil
|
27
|
+
@cnx.drop_table(:user_tests) rescue nil
|
28
28
|
@cnx.create_table :user_tests do |t|
|
29
29
|
t.column :age, :integer
|
30
30
|
t.column :name, :string
|
@@ -67,7 +67,7 @@ module ArelExtensions
|
|
67
67
|
@test = User.where(:id => u.id)
|
68
68
|
u = User.create :age => -42, :name => "Negatif", :comments => '1,22,3,42,2', :created_at => d, :updated_at => d.to_time, :score => 0.17
|
69
69
|
@neg = User.where(:id => u.id)
|
70
|
-
|
70
|
+
|
71
71
|
|
72
72
|
@age = User.arel_table[:age]
|
73
73
|
@name = User.arel_table[:name]
|
@@ -78,7 +78,7 @@ module ArelExtensions
|
|
78
78
|
@duration = User.arel_table[:duration]
|
79
79
|
@price = Product.arel_table[:price]
|
80
80
|
@not_in_table = User.arel_table[:not_in_table]
|
81
|
-
|
81
|
+
|
82
82
|
@ut = User.arel_table
|
83
83
|
@pt = Product.arel_table
|
84
84
|
end
|
@@ -92,6 +92,15 @@ module ArelExtensions
|
|
92
92
|
scope.select(node.as('res')).to_a.first.res
|
93
93
|
end
|
94
94
|
|
95
|
+
#manage the difference between adapters that handle or not json type
|
96
|
+
def parse_json(h)
|
97
|
+
if @env_db != 'postgresql'
|
98
|
+
JSON.parse("{\"res\":#{h}}")['res']
|
99
|
+
else
|
100
|
+
h
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
95
104
|
# Math Functions
|
96
105
|
def test_classical_arel
|
97
106
|
assert_in_epsilon 42.16, t(@laure, @score + 22), 0.01
|
@@ -154,6 +163,7 @@ module ArelExtensions
|
|
154
163
|
assert_equal 'Laure 2', t(@laure, @name + ' ' + 2)
|
155
164
|
assert_equal 'Test Laure', t(@laure, Arel::Nodes.build_quoted('Test ') + @name)
|
156
165
|
|
166
|
+
skip "No group_concat in SqlServer before 2017" if @env_db == 'mssql'
|
157
167
|
assert_equal "Lucas Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat(' '))
|
158
168
|
assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat(','))
|
159
169
|
assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat)
|
@@ -202,7 +212,7 @@ module ArelExtensions
|
|
202
212
|
end
|
203
213
|
assert_equal 'Lu', t(@lucas, @name[0,2])
|
204
214
|
assert_equal 'Lu', t(@lucas, @name[0..1])
|
205
|
-
|
215
|
+
|
206
216
|
#substring should accept string function
|
207
217
|
assert_equal 'Ce', t(@camille, @name.substring(1, 1).concat('e'))
|
208
218
|
assert_equal 'Ce', t(@camille, @name.substring(1, 1)+'e')
|
@@ -221,8 +231,8 @@ module ArelExtensions
|
|
221
231
|
if @env_db == 'postgresql' # may return real boolean
|
222
232
|
assert t(@neg, @name >= 'Mest') == true || t(@neg, @name >= 'Mest') == 't' # depends of ar version
|
223
233
|
assert t(@neg, @name <= (@name + 'Z')) == true || t(@neg, @name <= (@name + 'Z')) == 't'
|
224
|
-
elsif @env_db == 'oracle'
|
225
|
-
|
234
|
+
elsif @env_db == 'oracle'
|
235
|
+
assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name >= 'Mest').then(1).else(0))
|
226
236
|
assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name <= (@name + 'Z')).then(1).else(0))
|
227
237
|
assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name > 'Mest').then(1).else(0))
|
228
238
|
assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name < (@name + 'Z')).then(1).else(0))
|
@@ -233,23 +243,23 @@ module ArelExtensions
|
|
233
243
|
assert_equal 1, t(@neg, @name < (@name + 'Z'))
|
234
244
|
end
|
235
245
|
end
|
236
|
-
|
237
|
-
def test_compare_on_date_time_types
|
238
|
-
|
239
|
-
|
240
|
-
|
246
|
+
|
247
|
+
def test_compare_on_date_time_types
|
248
|
+
skip "Sqlite can't compare time" if $sqlite
|
249
|
+
skip "Oracle can't compare time" if @env_db == 'oracle'
|
250
|
+
#@created_at == 2016-05-23
|
241
251
|
assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2014-01-01').then(1).else(0))
|
242
252
|
assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2018-01-01').then(1).else(0))
|
243
253
|
#@updated_at == 2014-03-03 12:42:00
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
254
|
+
assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 10:10:10').then(1).else(0))
|
255
|
+
assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 13:10:10').then(1).else(0))
|
256
|
+
#@duration == 12:42:21
|
257
|
+
#puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0)).to_sql
|
258
|
+
#puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0)).to_sql
|
259
|
+
assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0))
|
260
|
+
assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0))
|
251
261
|
end
|
252
|
-
|
262
|
+
|
253
263
|
|
254
264
|
def test_regexp_not_regexp
|
255
265
|
skip "Sqlite version can't load extension for regexp" if $sqlite && $load_extension_disabled
|
@@ -261,7 +271,7 @@ module ArelExtensions
|
|
261
271
|
end
|
262
272
|
|
263
273
|
def test_imatches
|
264
|
-
|
274
|
+
#puts User.where(@name.imatches('m%')).to_sql
|
265
275
|
assert_equal 1, User.where(@name.imatches('m%')).count
|
266
276
|
assert_equal 4, User.where(@name.imatches_any(['L%', '%e'])).count
|
267
277
|
assert_equal 6, User.where(@name.idoes_not_match('L%')).count
|
@@ -274,7 +284,7 @@ module ArelExtensions
|
|
274
284
|
|
275
285
|
def test_replace_once
|
276
286
|
skip "TODO"
|
277
|
-
#
|
287
|
+
#skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
|
278
288
|
assert_equal "LuCas", t(@lucas, @name.substring(1, @name.locate('c') - 1) + 'C' + @name.substring(@name.locate('c') + 1, @name.length))
|
279
289
|
end
|
280
290
|
|
@@ -317,7 +327,7 @@ module ArelExtensions
|
|
317
327
|
assert_equal 0, @sophie.where(@comments.not_blank).count
|
318
328
|
assert_equal 1, @camille.where(@comments.blank).count
|
319
329
|
assert_equal 0, @camille.where(@comments.not_blank).count
|
320
|
-
|
330
|
+
|
321
331
|
assert_equal 0, @neg.where(@comments.blank).count
|
322
332
|
assert_equal 1, @neg.where(@comments.not_blank).count
|
323
333
|
assert_equal 'false', t(@myung, @name.blank.then('true', 'false'))
|
@@ -344,16 +354,12 @@ module ArelExtensions
|
|
344
354
|
else
|
345
355
|
assert_equal('', t(@laure, @comments.coalesce("")))
|
346
356
|
end
|
347
|
-
|
348
|
-
|
349
357
|
assert_equal 100, t(@test, @age.coalesce(100))
|
350
358
|
assert_equal "Camille", t(@camille, @name.coalesce(nil, "default"))
|
351
359
|
assert_equal 20, t(@test, @age.coalesce(nil, 20))
|
352
360
|
|
353
361
|
assert_equal 20, t(@test, @age.coalesce(10)+10)
|
354
362
|
assert_equal 'Laure10', t(@laure, @comments.coalesce("Laure") + 10)
|
355
|
-
|
356
|
-
|
357
363
|
end
|
358
364
|
|
359
365
|
# Comparators
|
@@ -373,7 +379,7 @@ module ArelExtensions
|
|
373
379
|
|
374
380
|
def test_date_duration
|
375
381
|
#Year
|
376
|
-
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
382
|
+
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
377
383
|
assert_equal 0, User.where(@created_at.year.eq("2012")).count
|
378
384
|
#Month
|
379
385
|
assert_equal 5, t(@camille, @created_at.month).to_i
|
@@ -401,7 +407,7 @@ module ArelExtensions
|
|
401
407
|
assert_equal 0, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 42)).to_i
|
402
408
|
if @env_db == 'oracle' && Arel::VERSION.to_i > 6 # in rails 5, result is multiplied by 24*60*60 = 86400...
|
403
409
|
assert_equal 42 * 86400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
|
404
|
-
assert_equal(-3600 * 86400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
|
410
|
+
assert_equal(-3600 * 86400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
|
405
411
|
else
|
406
412
|
assert_equal 42, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
|
407
413
|
assert_equal(-3600, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
|
@@ -411,30 +417,30 @@ module ArelExtensions
|
|
411
417
|
assert_includes [nil, 0, 'f', false], t(@lucas, (@updated_at - Time.utc(2014, 3, 3, 12, 41, 18)) < -1)
|
412
418
|
end
|
413
419
|
if @env_db == 'mysql'
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
420
|
+
date1 = Date.new(2016, 5, 23)
|
421
|
+
durPos = 10.years
|
422
|
+
durNeg = -10.years
|
423
|
+
date2 = date1 + durPos
|
424
|
+
date3 = date1 - durPos
|
425
|
+
# Pull Request #5 tests
|
426
|
+
assert_includes [date2,"2026-05-23"], t(@test,(@created_at + durPos))
|
427
|
+
assert_includes [date3,"2006-05-23"], t(@test,(@created_at + durNeg))
|
428
|
+
# we test with the ruby object or the string because some adapters don't return an object Date
|
423
429
|
end
|
424
430
|
end
|
425
431
|
end
|
426
432
|
|
427
433
|
# TODO; cast types
|
428
434
|
def test_cast_types
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
435
|
+
assert_equal "5", t(@lucas, @age.cast(:string))
|
436
|
+
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle'
|
437
|
+
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).cast(:string).eq("12:42:21")).then(1).else(0))
|
438
|
+
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0))
|
439
|
+
end
|
434
440
|
end
|
435
441
|
|
436
|
-
def test_is_null
|
437
|
-
|
442
|
+
def test_is_null
|
443
|
+
#puts User.where(@age.is_null).select(@name).to_sql
|
438
444
|
assert_equal "Test", User.where(@age.is_null).select(@name).first.name
|
439
445
|
end
|
440
446
|
|
@@ -445,18 +451,18 @@ module ArelExtensions
|
|
445
451
|
assert_equal "Sophie2", t(@sophie, @name + 2)
|
446
452
|
assert_equal "Sophie1997-06-15", t(@sophie, @name + d)
|
447
453
|
assert_equal "Sophie15", t(@sophie, @name + @age)
|
448
|
-
assert_equal "SophieSophie", t(@sophie, @name + @name)
|
454
|
+
assert_equal "SophieSophie", t(@sophie, @name + @name)
|
449
455
|
assert_equal "SophieSophieSophie", t(@sophie, @name + @name + @name)
|
450
456
|
assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name.concat(@name)))
|
451
457
|
assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name).concat(@name))
|
452
458
|
#FIXME: should work as expected in Oracle
|
453
|
-
assert_equal "Sophie2016-05-23", t(@sophie, @name + @created_at) unless @env_db == 'oracle'
|
459
|
+
assert_equal "Sophie2016-05-23", t(@sophie, @name + @created_at) unless @env_db == 'oracle'
|
454
460
|
#concat Integer
|
455
461
|
assert_equal 1, User.where((@age + 10).eq(33)).count
|
456
462
|
assert_equal 1, User.where((@age + "1").eq(6)).count
|
457
463
|
assert_equal 1, User.where((@age + @age).eq(10)).count
|
458
464
|
#concat Date
|
459
|
-
|
465
|
+
#puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
460
466
|
assert_equal "2016-05-24", t(@myung, @created_at + 1).to_date.to_s
|
461
467
|
assert_equal "2016-05-25", t(@myung, @created_at + 2.day).to_date.to_s
|
462
468
|
end
|
@@ -475,28 +481,27 @@ module ArelExtensions
|
|
475
481
|
end
|
476
482
|
|
477
483
|
def test_wday
|
478
|
-
d = Date.new(2016, 6, 26)
|
484
|
+
#d = Date.new(2016, 6, 26)
|
479
485
|
assert_equal(@env_db == 'oracle' || @env_db == 'mssql' ? 2 : 1, t(@myung, @created_at.wday).to_i) # monday
|
480
|
-
assert_equal 0, User.select(d.wday).as("res").first.to_i
|
481
486
|
end
|
482
487
|
|
483
488
|
# Boolean functions
|
484
489
|
def test_boolean_functions
|
485
|
-
|
490
|
+
assert @laure.where(
|
486
491
|
(@score.round > 19).⋀(@score.round < 21).⋁(@score.round(1) >= 20.1)
|
487
|
-
)
|
492
|
+
)
|
488
493
|
end
|
489
|
-
|
494
|
+
|
490
495
|
# Union operator
|
491
|
-
def test_union_operator
|
496
|
+
def test_union_operator
|
492
497
|
assert_equal 3, User.find_by_sql((@ut.project(@age).where(@age.gt(22)) + @ut.project(@age).where(@age.lt(0))).to_sql).length
|
493
|
-
|
498
|
+
assert_equal 2, User.find_by_sql((@ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(21))).to_sql).length
|
494
499
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.gt(22)) + @ut.project(@age).where(@age.lt(0))).as('my_union')).length
|
495
500
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(23)) + @ut.project(@age).where(@age.eq(21))).as('my_union')).length
|
496
501
|
assert_equal 2, User.select('*').from((@ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(21))).as('my_union')).length
|
497
|
-
|
502
|
+
|
498
503
|
assert_equal 3, User.find_by_sql((@ut.project(@age).where(@age.gt(22)).union_all(@ut.project(@age).where(@age.lt(0)))).to_sql).length
|
499
|
-
|
504
|
+
assert_equal 3, User.find_by_sql((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(20))).union_all(@ut.project(@age).where(@age.eq(21)))).to_sql).length
|
500
505
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.gt(22)).union_all(@ut.project(@age).where(@age.lt(0)))).as('my_union')).length
|
501
506
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(23))).union_all(@ut.project(@age).where(@age.eq(21)))).as('my_union')).length
|
502
507
|
assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(20))).union_all(@ut.project(@age).where(@age.eq(21)))).as('my_union')).length
|
@@ -504,185 +509,223 @@ module ArelExtensions
|
|
504
509
|
assert (@ut.project(@age) + @ut.project(@age)).as('toto').table_name # as on union should answer to table_name (TableAlias)
|
505
510
|
end
|
506
511
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
512
|
+
# Case clause
|
513
|
+
def test_case
|
514
|
+
assert_equal 4, User.find_by_sql(@ut.project(@score.when(20.16).then(1).else(0).as('score_bin')).to_sql).sum(&:score_bin)
|
515
|
+
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
516
|
+
assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
|
517
|
+
assert_equal "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
|
518
|
+
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
|
519
|
+
assert_equal "65.621", t(@arthur, @score.when(65.62).then(@score.cast(:string)).else(@score.cast(:string))+1).tr('0','') #tr is here because of precision on cast for some DBMS
|
520
|
+
end
|
521
|
+
|
522
|
+
def test_format_numbers
|
523
|
+
#score of Arthur = 65.62
|
524
|
+
skip " Works with SQLite if the version used knows printf" if @env_db = $sqlite
|
525
|
+
|
526
|
+
assert_equal "Wrong Format" , t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
|
527
|
+
assert_equal "AZERTY65,62" , t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
|
528
|
+
assert_equal "65,62AZERTY" , t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
|
529
|
+
assert_equal "$ 65.62 €" , t(@arthur, @score.format_number("$ %.2f €","en_US"))
|
530
|
+
assert_equal "$ 66 €" , t(@arthur, @score.format_number("$ %.0f €","en_US"))
|
531
|
+
assert_equal "$ 0065,62 €" , t(@arthur, @score.format_number("$ %07.2f €","fr_FR"))
|
532
|
+
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-07.2f €","fr_FR"))
|
533
|
+
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
|
534
|
+
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
|
535
|
+
assert_equal "$ 65,6 €" , t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
|
536
|
+
assert_equal "$ +65,62 €" , t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
|
537
|
+
assert_equal "$ +065,62 €" , t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
|
538
|
+
assert_includes ["$ 6,56e1 €","$ 6,56e+01 €"], t(@arthur, @score.format_number("$ %.2e €","fr_FR"))
|
539
|
+
assert_includes ["$ 6,56E1 €","$ 6,56E+01 €"], t(@arthur, @score.format_number("$ %.2E €","fr_FR"))
|
540
|
+
assert_includes ["$ 6,562E1 €","$ 6,562E+01 €"], t(@arthur, @score.format_number("$ %.3E €","fr_FR"))
|
541
|
+
assert_equal "123 456 765,6" , t(@arthur, (@score+123456700).format_number("%.1f","sv_SE")).gsub("\u00A0"," ") #some DBMS put no-break space here (it makes sense thus)
|
542
|
+
assert_equal "123456765,6" , t(@arthur, (@score+123456700).format_number("%.1f","fr_FR")).gsub("\u00A0","") #because SqlServer does it like no one else
|
543
|
+
assert_equal "123,456,765.6" , t(@arthur, (@score+123456700).format_number("%.1f","en_US"))
|
544
|
+
assert_equal " 123,456,765.6" , t(@arthur, (@score+123456700).format_number("%16.1f","en_US"))
|
545
|
+
assert_equal "$ 0,00 €" , t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
|
546
|
+
assert_equal "$ 0,00 €" , t(@arthur, (@score-65.62).format_number("$ %.2f €","fr_FR"))
|
547
|
+
end
|
548
|
+
|
549
|
+
def test_accent_insensitive
|
550
|
+
skip "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
|
551
|
+
skip "Not finished" if @env_db == 'mysql'
|
552
|
+
# actual comments value: "arrêté"
|
553
|
+
#AI & CI
|
554
|
+
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
555
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrêté")).then("1").else("0"))
|
556
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
|
557
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("àrrétè")).then("1").else("0"))
|
558
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
|
559
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrete")).then("1").else("0"))
|
560
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrêté")).then("1").else("0"))
|
561
|
+
#AI & CS
|
562
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrêté")).then("1").else("0"))
|
563
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
|
564
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
|
565
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arretez")).then("1").else("0"))
|
566
|
+
if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
|
567
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrete")).then("1").else("0"))
|
568
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrêté")).then("1").else("0"))
|
569
|
+
end
|
570
|
+
end
|
571
|
+
#AS & CI
|
572
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrêté")).then("1").else("0"))
|
573
|
+
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
574
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
|
575
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("àrrétè")).then("1").else("0"))
|
576
|
+
end
|
577
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arretez")).then("1").else("0"))
|
578
|
+
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
579
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrete")).then("1").else("0"))
|
580
|
+
end
|
581
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrêté")).then("1").else("0"))
|
582
|
+
#AS & CS
|
583
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrêté")).then("1").else("0"))
|
584
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
|
585
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
|
586
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arretez")).then("1").else("0"))
|
587
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrete")).then("1").else("0"))
|
588
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrêté")).then("1").else("0"))
|
589
|
+
end
|
590
|
+
|
591
|
+
def test_subquery_with_order
|
592
|
+
assert_equal 8, User.where(:name => User.select(:name).order(:name)).count
|
593
|
+
assert_equal 8, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
|
594
|
+
if !['mysql'].include?(@env_db) # MySql can't have limit in IN subquery
|
595
|
+
assert_equal 2, User.where(:name => User.select(:name).order(:name).limit(2)).count
|
596
|
+
#assert_equal 6, User.where(:name => User.select(:name).order(:name).offset(2)).count
|
597
|
+
end
|
598
|
+
end
|
599
|
+
|
600
|
+
def test_in_with_nil
|
601
|
+
assert_equal true , @myung.where(@age.in(1)).blank?
|
602
|
+
assert_equal false , @myung.where(@age.in(23)).blank?
|
603
|
+
assert_equal true , @myung.where(@age.in([1])).blank?
|
604
|
+
assert_equal true , @myung.where(@age.in([1,2])).blank?
|
605
|
+
assert_equal false , @myung.where(@age.in([1,23])).blank?
|
606
|
+
assert_equal true , @myung.where(@age.in(nil)).blank?
|
607
|
+
assert_equal true , @myung.where(@age.in([nil])).blank?
|
608
|
+
assert_equal true , @myung.where(@age.in([nil,1])).blank?
|
609
|
+
assert_equal false , @myung.where(@age.in([nil,23])).blank?
|
610
|
+
assert_equal true , @myung.where(@age.in([nil,1,2])).blank?
|
611
|
+
assert_equal false , @myung.where(@age.in([nil,1,23])).blank?
|
612
|
+
assert_equal true , @test.where(@age.in(1)).blank?
|
613
|
+
assert_equal true , @test.where(@age.in([1])).blank?
|
614
|
+
assert_equal true , @test.where(@age.in([1,2])).blank?
|
615
|
+
assert_equal false , @test.where(@age.in(nil)).blank?
|
616
|
+
assert_equal false , @test.where(@age.in([nil])).blank?
|
617
|
+
assert_equal false , @test.where(@age.in([nil,1])).blank?
|
618
|
+
assert_equal false , @test.where(@age.in([nil,1,2])).blank?
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_scope_with_in_plus_new
|
622
|
+
begin
|
623
|
+
@test.where(@age.in([1,2])).new
|
624
|
+
@test.where(@age.not_in([1,2])).new
|
625
|
+
assert true
|
626
|
+
rescue
|
627
|
+
assert false
|
628
|
+
end
|
629
|
+
end
|
630
|
+
|
631
|
+
def test_is_not_null
|
632
|
+
assert_equal false , @myung.where(@age.is_not_null).blank?
|
633
|
+
assert_equal true , @test.where(@age.is_not_null).blank?
|
634
|
+
end
|
635
|
+
|
636
|
+
def test_not_in_with_nil
|
637
|
+
assert_equal false , @myung.where(@age.not_in(1)).blank?
|
638
|
+
assert_equal true , @myung.where(@age.not_in(23)).blank?
|
639
|
+
assert_equal false , @myung.where(@age.not_in([1])).blank?
|
640
|
+
assert_equal false , @myung.where(@age.not_in([1,2])).blank?
|
641
|
+
assert_equal true , @myung.where(@age.not_in([1,23])).blank?
|
642
|
+
assert_equal false , @myung.where(@age.not_in(nil)).blank?
|
643
|
+
assert_equal false , @myung.where(@age.not_in([nil])).blank?
|
644
|
+
assert_equal false , @myung.where(@age.not_in([nil,1])).blank?
|
645
|
+
assert_equal true , @myung.where(@age.not_in([nil,23])).blank?
|
646
|
+
assert_equal false , @myung.where(@age.not_in([nil,1,2])).blank?
|
647
|
+
assert_equal true , @myung.where(@age.not_in([nil,1,23])).blank?
|
648
|
+
|
649
|
+
assert_equal false , @myung.where(@age.not_in(1..2)).blank?
|
650
|
+
|
651
|
+
#if the column is null, the entry will never be selected with not in (like every DBMS does)
|
652
|
+
#assert_equal false , @test.where(@age.not_in(1)).blank?
|
653
|
+
#assert_equal false , @test.where(@age.not_in([1])).blank?
|
654
|
+
#assert_equal false , @test.where(@age.not_in([1,2])).blank?
|
655
|
+
#assert_equal true , @test.where(@age.not_in(nil)).blank?
|
656
|
+
#assert_equal true , @test.where(@age.not_in([nil])).blank?
|
657
|
+
#assert_equal true , @test.where(@age.not_in([nil,1])).blank?
|
658
|
+
#assert_equal true , @test.where(@age.not_in([nil,1,2])).blank?
|
659
|
+
end
|
660
|
+
|
661
|
+
def test_alias_shortened
|
662
|
+
if ['postgresql','oracle'].include?(@env_db)
|
663
|
+
new_alias = Arel.shorten('azerty' * 15)
|
664
|
+
at = User.arel_table.alias('azerty' * 15)
|
665
|
+
assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
|
666
|
+
assert_equal '"user_tests" "u"'.downcase, User.arel_table.alias('u').to_sql.downcase
|
667
|
+
assert_equal %Q[SELECT "#{new_alias}"."id" FROM "user_tests" "#{new_alias}"].downcase,
|
668
|
+
User.select(at[:id]).from(at).to_sql.downcase
|
669
|
+
end
|
670
|
+
end
|
671
|
+
|
672
|
+
def test_stat_functions
|
673
|
+
skip "SQLite doesn't work for most on this functions" if $sqlite
|
674
|
+
#puts t(User.where(nil), @score.average)
|
675
|
+
#puts t(User.where(nil), @score.variance(true))
|
676
|
+
#puts t(User.where(nil), @score.variance(false))
|
677
|
+
#puts t(User.where(nil), @score.std(true))
|
678
|
+
#puts t(User.where(nil), @score.std(false))
|
679
|
+
|
680
|
+
assert ( 15.98625 - t(User.where(nil), @score.average)).abs < 0.01
|
681
|
+
assert (613.75488 - t(User.where(nil), @score.variance)).abs < 0.01
|
682
|
+
assert ( 537.0355 - t(User.where(nil), @score.variance(false))).abs < 0.01
|
683
|
+
assert ( 24.77408 - t(User.where(nil), @score.std)).abs < 0.01
|
684
|
+
assert ( 23.17403 - t(User.where(nil), @score.std(false))).abs < 0.01
|
685
|
+
end
|
686
|
+
|
687
|
+
def test_levenshtein_distance
|
688
|
+
skip "Not Yet Implemented" if $sqlite
|
689
|
+
assert_equal 0, t(@arthur,@name.levenshtein_distance("Arthur"))
|
690
|
+
assert_equal 2, t(@arthur,@name.levenshtein_distance("Artoor"))
|
691
|
+
assert_equal 1, t(@arthur,@name.levenshtein_distance("Artehur"))
|
692
|
+
end
|
693
|
+
|
694
|
+
def test_json
|
695
|
+
skip "Can't be tested on travis"
|
696
|
+
#creation
|
697
|
+
assert_equal 'Arthur', t(@arthur,ArelExtensions::Nodes::Json.new(@name))
|
698
|
+
assert_equal ["Arthur","Arthur"], parse_json(t(@arthur,ArelExtensions::Nodes::Json.new(@name,@name)))
|
699
|
+
assert_equal ({"Arthur" => "Arthur", "Arthur2" => "ArthurArthur"}), parse_json(t(@arthur,ArelExtensions::Nodes::Json.new({@name => @name,@name+"2" => @name+@name})))
|
700
|
+
assert_equal ({"Arthur" => "Arthur","Arthur2" => 1}), parse_json(t(@arthur,ArelExtensions::Nodes::Json.new({@name => @name,@name+"2" => 1})))
|
701
|
+
assert_equal ([{"age" => 21},{"name" => "Arthur","score" => 65.62}]), parse_json(t(@arthur,ArelExtensions::Nodes::Json.new([{age: @age},{name: @name,score: @score}])))
|
702
|
+
|
703
|
+
skip "Not Yet Implemented" if $sqlite || ['oracle','mssql'].include?(@env_db)
|
704
|
+
|
705
|
+
#get
|
706
|
+
h1 = ArelExtensions::Nodes::Json.new({@name => @name+@name,@name+"2" => 1})
|
707
|
+
assert_equal "ArthurArthur", parse_json(t(@arthur,h1.get(@name)))
|
708
|
+
h2 = ArelExtensions::Nodes::Json.new([{age: @age},{name: @name,score: @score}])
|
709
|
+
assert_equal ({"age" => 21}), parse_json(t(@arthur,h2.get(0)))
|
710
|
+
assert_equal 21, parse_json(t(@arthur,h2.get(0).get('age')))
|
711
|
+
assert_nil t(@arthur,h2.get('age'))
|
712
|
+
#set
|
713
|
+
assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1}), parse_json(t(@arthur,h1.set(@name, ['toto','tata'])))
|
714
|
+
assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.set(@name+"3",2)))
|
715
|
+
assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => nil}), parse_json(t(@arthur,h1.set(@name+"3",nil)))
|
716
|
+
assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => {"a" => 2}}), parse_json(t(@arthur,h1.set(@name+"3",{a: 2})))
|
717
|
+
#merge
|
718
|
+
assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata']},{@name+"3" => 2})))
|
719
|
+
assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata'], @name+"3" => 2})))
|
720
|
+
assert_equal ({"Arthur" => "ArthurArthur","Arthur2" => 1}), parse_json(t(@arthur,h1.merge({})))
|
721
|
+
end
|
722
|
+
|
723
|
+
def test_as_on_everything
|
724
|
+
name = @arthur.select(@name.as('NaMe')).first.attributes
|
725
|
+
assert_equal 'Arthur', name["NaMe"] || name["name"] #because of Oracle
|
726
|
+
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes["Na Me"]
|
727
|
+
assert_equal 'Arthur', @arthur.select(@name.as('Na-Me')).first.attributes["Na-Me"]
|
728
|
+
end
|
686
729
|
end
|
687
730
|
end
|
688
731
|
end
|