arel_extensions 1.2.25 → 2.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -7
- data/.travis.yml +91 -61
- data/Gemfile +20 -15
- data/README.md +12 -17
- data/Rakefile +29 -40
- data/appveyor.yml +1 -1
- data/arel_extensions.gemspec +3 -3
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +9 -9
- data/gemfiles/rails4.gemfile +13 -13
- data/gemfiles/rails5_0.gemfile +13 -13
- data/gemfiles/rails5_1_4.gemfile +13 -13
- data/gemfiles/rails5_2.gemfile +13 -13
- data/init/mssql.sql +4 -4
- data/init/mysql.sql +38 -38
- data/init/postgresql.sql +21 -21
- data/lib/arel_extensions/attributes.rb +1 -0
- data/lib/arel_extensions/boolean_functions.rb +14 -55
- data/lib/arel_extensions/common_sql_functions.rb +8 -7
- data/lib/arel_extensions/comparators.rb +15 -14
- data/lib/arel_extensions/date_duration.rb +5 -4
- data/lib/arel_extensions/insert_manager.rb +16 -17
- data/lib/arel_extensions/math.rb +12 -11
- data/lib/arel_extensions/math_functions.rb +22 -29
- data/lib/arel_extensions/nodes/abs.rb +1 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -0
- data/lib/arel_extensions/nodes/case.rb +8 -11
- data/lib/arel_extensions/nodes/cast.rb +2 -4
- data/lib/arel_extensions/nodes/ceil.rb +1 -1
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +3 -2
- data/lib/arel_extensions/nodes/collate.rb +2 -1
- data/lib/arel_extensions/nodes/concat.rb +16 -7
- data/lib/arel_extensions/nodes/date_diff.rb +13 -10
- data/lib/arel_extensions/nodes/duration.rb +3 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
- data/lib/arel_extensions/nodes/floor.rb +1 -1
- data/lib/arel_extensions/nodes/format.rb +8 -34
- data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
- data/lib/arel_extensions/nodes/function.rb +16 -25
- data/lib/arel_extensions/nodes/json.rb +36 -43
- data/lib/arel_extensions/nodes/length.rb +0 -5
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
- data/lib/arel_extensions/nodes/locate.rb +1 -0
- data/lib/arel_extensions/nodes/log10.rb +2 -1
- data/lib/arel_extensions/nodes/matches.rb +6 -4
- data/lib/arel_extensions/nodes/md5.rb +1 -0
- data/lib/arel_extensions/nodes/power.rb +5 -5
- data/lib/arel_extensions/nodes/rand.rb +1 -0
- data/lib/arel_extensions/nodes/repeat.rb +4 -2
- data/lib/arel_extensions/nodes/replace.rb +6 -22
- data/lib/arel_extensions/nodes/round.rb +6 -5
- data/lib/arel_extensions/nodes/soundex.rb +15 -15
- data/lib/arel_extensions/nodes/std.rb +21 -18
- data/lib/arel_extensions/nodes/substring.rb +16 -8
- data/lib/arel_extensions/nodes/then.rb +0 -0
- data/lib/arel_extensions/nodes/trim.rb +5 -3
- data/lib/arel_extensions/nodes/union.rb +5 -2
- data/lib/arel_extensions/nodes/union_all.rb +3 -0
- data/lib/arel_extensions/nodes/wday.rb +4 -0
- data/lib/arel_extensions/nodes.rb +1 -1
- data/lib/arel_extensions/null_functions.rb +7 -5
- data/lib/arel_extensions/predications.rb +34 -35
- data/lib/arel_extensions/railtie.rb +5 -5
- data/lib/arel_extensions/set_functions.rb +4 -2
- data/lib/arel_extensions/string_functions.rb +23 -52
- data/lib/arel_extensions/tasks.rb +5 -5
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
- data/lib/arel_extensions/visitors/mssql.rb +58 -64
- data/lib/arel_extensions/visitors/mysql.rb +98 -149
- data/lib/arel_extensions/visitors/oracle.rb +70 -73
- data/lib/arel_extensions/visitors/oracle12.rb +15 -2
- data/lib/arel_extensions/visitors/postgresql.rb +63 -116
- data/lib/arel_extensions/visitors/sqlite.rb +70 -83
- data/lib/arel_extensions/visitors/to_sql.rb +110 -142
- data/lib/arel_extensions/visitors.rb +60 -68
- data/lib/arel_extensions.rb +19 -81
- data/test/database.yml +0 -2
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +43 -28
- data/test/support/fake_record.rb +2 -2
- data/test/test_comparators.rb +12 -9
- data/test/visitors/test_bulk_insert_oracle.rb +8 -8
- data/test/visitors/test_bulk_insert_sqlite.rb +10 -9
- data/test/visitors/test_bulk_insert_to_sql.rb +10 -8
- data/test/visitors/test_oracle.rb +42 -42
- data/test/visitors/test_to_sql.rb +196 -361
- data/test/with_ar/all_agnostic_test.rb +160 -195
- data/test/with_ar/insert_agnostic_test.rb +4 -3
- data/test/with_ar/test_bulk_sqlite.rb +9 -6
- data/test/with_ar/test_math_sqlite.rb +12 -8
- data/test/with_ar/test_string_mysql.rb +11 -5
- data/test/with_ar/test_string_sqlite.rb +12 -4
- metadata +11 -22
- data/.github/workflows/ruby.yml +0 -102
- data/gemfiles/rails6.gemfile +0 -30
- data/gemfiles/rails6_1.gemfile +0 -30
- data/gemspecs/arel_extensions-v1.gemspec +0 -28
- data/gemspecs/arel_extensions-v2.gemspec +0 -28
- data/generate_gems.sh +0 -15
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
- data/lib/arel_extensions/nodes/sum.rb +0 -7
- data/lib/arel_extensions/visitors/convert_format.rb +0 -37
- data/test/arelx_test_helper.rb +0 -26
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
@@ -1,8 +1,9 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
require 'date'
|
3
3
|
|
4
4
|
module ArelExtensions
|
5
5
|
module WthAr
|
6
|
+
|
6
7
|
class ListTest < Minitest::Test
|
7
8
|
require 'minitest/pride'
|
8
9
|
def connect_db
|
@@ -67,8 +68,7 @@ module ArelExtensions
|
|
67
68
|
@test = User.where(:id => u.id)
|
68
69
|
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
70
|
@neg = User.where(:id => u.id)
|
70
|
-
|
71
|
-
@justin = User.where(:id => u.id)
|
71
|
+
|
72
72
|
|
73
73
|
@age = User.arel_table[:age]
|
74
74
|
@name = User.arel_table[:name]
|
@@ -94,7 +94,7 @@ module ArelExtensions
|
|
94
94
|
scope.select(node.as('res')).to_a.first.res
|
95
95
|
end
|
96
96
|
|
97
|
-
#
|
97
|
+
#manage the difference between adapters that handle or not json type
|
98
98
|
def parse_json(h)
|
99
99
|
if @env_db != 'postgresql'
|
100
100
|
JSON.parse("{\"res\":#{h}}")['res']
|
@@ -116,28 +116,25 @@ module ArelExtensions
|
|
116
116
|
end
|
117
117
|
|
118
118
|
def test_ceil
|
119
|
-
|
119
|
+
# skip "Sqlite version can't load extension for ceil" if $sqlite && $load_extension_disabled
|
120
120
|
assert_equal 2, t(@test, @score.ceil) # 1.62
|
121
121
|
assert_equal(-20, t(@camille, @score.ceil)) # -20.16
|
122
122
|
assert_equal(-20, t(@camille, (@score - 0.5).ceil)) # -20.16
|
123
123
|
assert_equal 63, t(@arthur, @age.ceil + 42)
|
124
|
-
assert_equal 11, t(@justin, @score.ceil) # 11.0
|
125
124
|
end
|
126
125
|
|
127
126
|
def test_floor
|
128
|
-
|
127
|
+
# skip "Sqlite version can't load extension for floor" if $sqlite && $load_extension_disabled
|
129
128
|
assert_equal 0, t(@neg, @score.floor)
|
130
129
|
assert_equal 1, t(@test, @score.floor) # 1.62
|
131
130
|
assert_equal(-9, t(@test, (@score - 10).floor)) # 1.62
|
132
131
|
assert_equal 42, t(@arthur, @score.floor - 23)
|
133
|
-
assert_equal 11, t(@justin, @score.floor) # 11.0
|
134
|
-
assert_equal(-21, t(@camille, @score.floor)) # # -20.16
|
135
132
|
end
|
136
133
|
|
137
134
|
def test_rand
|
138
135
|
assert 42 != User.select(Arel.rand.as('res')).first.res
|
139
136
|
assert 0 <= User.select(Arel.rand.abs.as('res')).first.res
|
140
|
-
assert_equal
|
137
|
+
assert_equal 8, User.order(Arel.rand).limit(50).count
|
141
138
|
end
|
142
139
|
|
143
140
|
def test_round
|
@@ -150,24 +147,18 @@ module ArelExtensions
|
|
150
147
|
def test_sum
|
151
148
|
if @env_db == 'mssql'
|
152
149
|
skip "SQL Server forces order?" # TODO
|
153
|
-
assert_equal
|
154
|
-
assert_equal
|
155
|
-
assert_equal
|
156
|
-
assert_equal
|
150
|
+
assert_equal 68, User.select((@age.sum + 1).as("res"), User.arel_table[:id].sum).take(50).reorder(@age).first.res
|
151
|
+
assert_equal 134, User.reorder(nil).select((@age.sum + @age.sum).as("res"), User.arel_table[:id].sum).take(50).first.res
|
152
|
+
assert_equal 201, User.reorder(nil).select(((@age * 3).sum).as("res"), User.arel_table[:id].sum).take(50).first.res
|
153
|
+
assert_equal 4009, User.reorder(nil).select(((@age * @age).sum).as("res"), User.arel_table[:id].sum).take(50).first.res
|
157
154
|
else
|
158
|
-
assert_equal
|
159
|
-
assert_equal
|
160
|
-
assert_equal
|
161
|
-
assert_equal
|
155
|
+
assert_equal 68, User.select((@age.sum + 1).as("res")).take(50).first.res
|
156
|
+
assert_equal 134, User.select((@age.sum + @age.sum).as("res")).take(50).first.res
|
157
|
+
assert_equal 201, User.select(((@age * 3).sum).as("res")).take(50).first.res
|
158
|
+
assert_equal 4009, User.select(((@age * @age).sum).as("res")).take(50).first.res
|
162
159
|
end
|
163
160
|
end
|
164
161
|
|
165
|
-
def test_aggregation_with_ar_calculation
|
166
|
-
# Since Arel10 (Rails6.1), some unwanted behaviors on aggregated calculation were present.
|
167
|
-
# This should works no matter which version of rails is used
|
168
|
-
assert User.group(:score).average(:id).values.all?{|e| !e.nil?}
|
169
|
-
end
|
170
|
-
|
171
162
|
# String Functions
|
172
163
|
def test_concat
|
173
164
|
assert_equal 'Camille Camille', t(@camille, @name + ' ' + @name)
|
@@ -184,7 +175,6 @@ module ArelExtensions
|
|
184
175
|
assert_equal "Sophie,Lucas,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@name.desc))
|
185
176
|
assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',[@score.asc,@name.asc]))
|
186
177
|
assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@score.asc,@name.asc))
|
187
|
-
assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',order: [@score.asc,@name.asc]))
|
188
178
|
end
|
189
179
|
|
190
180
|
def test_length
|
@@ -225,7 +215,7 @@ module ArelExtensions
|
|
225
215
|
assert_equal 'Lu', t(@lucas, @name[0,2])
|
226
216
|
assert_equal 'Lu', t(@lucas, @name[0..1])
|
227
217
|
|
228
|
-
#
|
218
|
+
#substring should accept string function
|
229
219
|
assert_equal 'Ce', t(@camille, @name.substring(1, 1).concat('e'))
|
230
220
|
assert_equal 'Ce', t(@camille, @name.substring(1, 1)+'e')
|
231
221
|
end
|
@@ -238,7 +228,7 @@ module ArelExtensions
|
|
238
228
|
end
|
239
229
|
|
240
230
|
def test_string_comparators
|
241
|
-
#
|
231
|
+
#skip "Oracle can't use math operators to compare strings" if @env_db == 'oracle' # use GREATEST ?
|
242
232
|
skip "SQL Server can't use math operators to compare strings" if @env_db == 'mssql' # use GREATEST ?
|
243
233
|
if @env_db == 'postgresql' # may return real boolean
|
244
234
|
assert t(@neg, @name >= 'Mest') == true || t(@neg, @name >= 'Mest') == 't' # depends of ar version
|
@@ -259,50 +249,44 @@ module ArelExtensions
|
|
259
249
|
def test_compare_on_date_time_types
|
260
250
|
skip "Sqlite can't compare time" if $sqlite
|
261
251
|
skip "Oracle can't compare time" if @env_db == 'oracle'
|
262
|
-
|
252
|
+
#@created_at == 2016-05-23
|
263
253
|
assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2014-01-01').then(1).else(0))
|
264
254
|
assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2018-01-01').then(1).else(0))
|
265
|
-
|
255
|
+
#@updated_at == 2014-03-03 12:42:00
|
266
256
|
assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 10:10:10').then(1).else(0))
|
267
257
|
assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 13:10:10').then(1).else(0))
|
268
|
-
|
269
|
-
#
|
270
|
-
#
|
258
|
+
#@duration == 12:42:21
|
259
|
+
#puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0)).to_sql
|
260
|
+
#puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0)).to_sql
|
271
261
|
assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0))
|
272
262
|
assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0))
|
273
263
|
end
|
274
264
|
|
265
|
+
|
275
266
|
def test_regexp_not_regexp
|
276
267
|
skip "Sqlite version can't load extension for regexp" if $sqlite && $load_extension_disabled
|
277
268
|
skip "SQL Server does not know about REGEXP without extensions" if @env_db == 'mssql'
|
278
269
|
assert_equal 1, User.where(@name =~ '^M').count
|
279
|
-
assert_equal
|
270
|
+
assert_equal 6, User.where(@name !~ '^L').count
|
280
271
|
assert_equal 1, User.where(@name =~ /^M/).count
|
281
|
-
assert_equal
|
272
|
+
assert_equal 6, User.where(@name !~ /^L/).count
|
282
273
|
end
|
283
274
|
|
284
275
|
def test_imatches
|
285
|
-
#
|
276
|
+
#puts User.where(@name.imatches('m%')).to_sql
|
286
277
|
assert_equal 1, User.where(@name.imatches('m%')).count
|
287
278
|
assert_equal 4, User.where(@name.imatches_any(['L%', '%e'])).count
|
288
|
-
assert_equal
|
279
|
+
assert_equal 6, User.where(@name.idoes_not_match('L%')).count
|
289
280
|
end
|
290
281
|
|
291
282
|
def test_replace
|
292
283
|
assert_equal "LucaX", t(@lucas, @name.replace("s", "X"))
|
293
284
|
assert_equal "replace", t(@lucas, @name.replace(@name, "replace"))
|
294
|
-
|
295
|
-
skip "Sqlite does not seem to support regexp_replace" if $sqlite
|
296
|
-
skip "SQL Server does not know about REGEXP without extensions" if @env_db == 'mssql'
|
297
|
-
skip "Travis mysql version does not support REGEXP_REPLACE" if @env_db == 'mysql'
|
298
|
-
assert_equal "LXcXs", t(@lucas, @name.replace(/[ua]/, "X"))
|
299
|
-
assert_equal "LXcXs", t(@lucas, @name.regexp_replace(/[ua]/, "X"))
|
300
|
-
assert_equal "LXcXs", t(@lucas, @name.regexp_replace('[ua]', "X"))
|
301
285
|
end
|
302
286
|
|
303
287
|
def test_replace_once
|
304
288
|
skip "TODO"
|
305
|
-
#
|
289
|
+
#skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
|
306
290
|
assert_equal "LuCas", t(@lucas, @name.substring(1, @name.locate('c') - 1) + 'C' + @name.substring(@name.locate('c') + 1, @name.length))
|
307
291
|
end
|
308
292
|
|
@@ -310,8 +294,8 @@ module ArelExtensions
|
|
310
294
|
skip "Sqlite version can't load extension for soundex" if $sqlite && $load_extension_disabled
|
311
295
|
skip "PostgreSql version can't load extension for soundex" if @env_db == 'postgresql'
|
312
296
|
assert_equal "C540", t(@camille, @name.soundex)
|
313
|
-
assert_equal
|
314
|
-
assert_equal
|
297
|
+
assert_equal 8, User.where(@name.soundex.eq(@name.soundex)).count
|
298
|
+
assert_equal 8, User.where(@name.soundex == @name.soundex).count
|
315
299
|
end
|
316
300
|
|
317
301
|
def test_change_case
|
@@ -360,7 +344,6 @@ module ArelExtensions
|
|
360
344
|
assert_equal '2016-05-23', t(@lucas, @created_at.format('%Y-%m-%d'))
|
361
345
|
skip "SQL Server does not accept any format" if @env_db == 'mssql'
|
362
346
|
assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S'))
|
363
|
-
assert_equal '12:42%', t(@lucas, @updated_at.format('%R%%'))
|
364
347
|
end
|
365
348
|
|
366
349
|
def test_coalesce
|
@@ -391,37 +374,37 @@ module ArelExtensions
|
|
391
374
|
assert_equal 2, User.where(@age <= 10).count
|
392
375
|
assert_equal 3, User.where(@age > 20).count
|
393
376
|
assert_equal 4, User.where(@age >= 20).count
|
394
|
-
assert_equal
|
377
|
+
assert_equal 1, User.where(@age > 5).where(@age < 20).count
|
395
378
|
end
|
396
379
|
|
397
380
|
def test_date_comparator
|
398
381
|
d = Date.new(2016, 5, 23)
|
399
382
|
assert_equal 0, User.where(@created_at < d).count
|
400
|
-
assert_equal
|
383
|
+
assert_equal 8, User.where(@created_at >= d).count
|
401
384
|
end
|
402
385
|
|
403
386
|
def test_date_duration
|
404
|
-
#
|
387
|
+
#Year
|
405
388
|
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
406
389
|
assert_equal 0, User.where(@created_at.year.eq("2012")).count
|
407
|
-
#
|
390
|
+
#Month
|
408
391
|
assert_equal 5, t(@camille, @created_at.month).to_i
|
409
|
-
assert_equal
|
410
|
-
#
|
392
|
+
assert_equal 8, User.where(@created_at.month.eq("05")).count
|
393
|
+
#Week
|
411
394
|
assert_equal(@env_db == 'mssql' ? 22 : 21, t(@arthur, @created_at.week).to_i)
|
412
|
-
assert_equal
|
413
|
-
#
|
395
|
+
assert_equal 8, User.where(@created_at.month.eq("05")).count
|
396
|
+
#Day
|
414
397
|
assert_equal 23, t(@laure, @created_at.day).to_i
|
415
398
|
assert_equal 0, User.where(@created_at.day.eq("05")).count
|
416
399
|
|
417
|
-
#
|
418
|
-
#
|
400
|
+
#skip "manage DATE" if @env_db == 'oracle'
|
401
|
+
#Hour
|
419
402
|
assert_equal 0, t(@laure, @created_at.hour).to_i
|
420
403
|
assert_equal 12, t(@lucas, @updated_at.hour).to_i
|
421
|
-
#
|
404
|
+
#Minute
|
422
405
|
assert_equal 0, t(@laure, @created_at.minute).to_i
|
423
406
|
assert_equal 42, t(@lucas, @updated_at.minute).to_i
|
424
|
-
#
|
407
|
+
#Second
|
425
408
|
assert_equal 0, t(@laure, @created_at.second).to_i
|
426
409
|
assert_equal 0, t(@lucas, @updated_at.second).to_i
|
427
410
|
end
|
@@ -441,7 +424,6 @@ module ArelExtensions
|
|
441
424
|
end
|
442
425
|
end
|
443
426
|
|
444
|
-
|
445
427
|
skip "not yet implemented" if $sqlite
|
446
428
|
|
447
429
|
date1 = Date.new(2016, 5, 23)
|
@@ -454,13 +436,13 @@ module ArelExtensions
|
|
454
436
|
|
455
437
|
datetime1 = Time.utc(2014, 3, 3, 12, 42, 0)
|
456
438
|
# Pull Request #5 tests
|
457
|
-
#
|
439
|
+
#puts (@created_at + durPos).cast(:date).to_sql
|
458
440
|
assert_includes [date2,"2026-05-23"], t(@test,(@created_at + durPos).cast(:date))
|
459
441
|
assert_includes [date3,"2006-05-23"], t(@test,(@created_at + durNeg).cast(:date))
|
460
442
|
|
461
|
-
#
|
443
|
+
#puts (@created_at + @created_at.day).cast(:date).to_sql
|
462
444
|
assert_includes [date4,"2016-06-15"], t(@test,(@created_at + @created_at.day).cast(:date))
|
463
|
-
#
|
445
|
+
#puts (@created_at - @created_at.day).cast(:date).to_sql
|
464
446
|
assert_includes [date5,"2016-04-30"], t(@test,(@created_at - @created_at.day).cast(:date))
|
465
447
|
|
466
448
|
assert_includes [datetime1 + 42.seconds,"2014-03-03 12:42:42 UTC"], t(@lucas,(@updated_at + @updated_at.minute))
|
@@ -468,59 +450,57 @@ module ArelExtensions
|
|
468
450
|
|
469
451
|
# (@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))).to_sql
|
470
452
|
assert_includes [datetime1 + (12*60+42).seconds,"2014-03-03 12:54:42 UTC"],
|
471
|
-
|
453
|
+
t(@lucas,(@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))))
|
472
454
|
|
473
455
|
assert_includes [datetime1 + (12*60+42).minutes,"2014-03-04 01:24:00 UTC"],
|
474
|
-
|
456
|
+
t(@lucas,(@updated_at + Arel.duration('mn',(@updated_at.hour*60 + @updated_at.minute))))
|
475
457
|
|
476
458
|
assert_includes ["2024-03-03"], t(@lucas,(@updated_at + durPos).format('%Y-%m-%d'))
|
477
|
-
#
|
459
|
+
#puts (@updated_at - durPos).to_sql
|
478
460
|
assert_includes ["2004-03-03"], t(@lucas,(@updated_at - durPos).format('%Y-%m-%d'))
|
479
461
|
|
480
462
|
|
481
463
|
# we test with the ruby object or the string because some adapters don't return an object Date
|
482
|
-
|
464
|
+
# end
|
483
465
|
end
|
484
466
|
|
485
467
|
# TODO; cast types
|
486
468
|
def test_cast_types
|
487
469
|
assert_equal "5", t(@lucas, @age.cast(:string))
|
488
|
-
skip "jdbc adapters does not work properly here (v52 works fine)" if RUBY_PLATFORM =~ /java/i
|
489
470
|
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
|
490
|
-
assert_equal 1, t(@laure,
|
491
|
-
assert_equal 1, t(@laure,
|
471
|
+
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).cast(:string).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle' || @env_db == 'mssql'
|
472
|
+
assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle'
|
492
473
|
assert_equal "20.16", t(@laure,@score.cast(:string)).gsub(/[0]*\z/,'')
|
493
474
|
assert_equal "20.161", t(@laure,@score.cast(:string)+1).gsub(/[0]*1\z/,'1')
|
494
475
|
assert_equal 21.16, t(@laure,@score.cast(:string).cast(:decimal)+1)
|
495
476
|
assert_equal 21, t(@laure,@score.cast(:string).cast(:int)+1)
|
496
477
|
|
497
478
|
assert_equal String, t(@lucas,@updated_at.cast(:string)).class
|
498
|
-
|
499
479
|
assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
|
500
480
|
assert_equal Time, t(@lucas,@updated_at.cast(:string).cast(:datetime)).class
|
501
481
|
assert_equal Time, t(@lucas,@updated_at.cast(:time)).class
|
502
482
|
|
503
|
-
assert_equal "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)) unless @env_db == 'mssql' #
|
483
|
+
assert_equal "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)) unless @env_db == 'mssql' #locale dependent
|
504
484
|
assert_equal Date.parse("2014-03-03"), t(@lucas,Arel::Nodes.build_quoted('2014-03-03').cast(:date))
|
505
485
|
assert_equal Date.parse("5014-03-03"), t(@lucas,(@age.cast(:string) + '014-03-03').cast(:date))
|
506
486
|
assert_equal Time.parse("2014-03-03 12:42:00 UTC"), t(@lucas,@updated_at.cast(:string).cast(:datetime))
|
507
487
|
assert_equal Date.parse("2014-03-03"), t(@lucas,@updated_at.cast(:date))
|
508
|
-
assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' #
|
488
|
+
assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' #DateTime
|
509
489
|
end
|
510
490
|
end
|
511
491
|
|
512
492
|
def test_is_null
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
516
|
-
#
|
517
|
-
#
|
493
|
+
#puts User.where(@age.is_null).select(@name).to_sql
|
494
|
+
#puts @age.is_null
|
495
|
+
#puts @age.is_null.inspect
|
496
|
+
#puts @age.is_null.to_sql
|
497
|
+
#puts @age=='34'
|
518
498
|
assert_equal "Test", User.select(@name).where(@age.is_null.to_sql).first.name
|
519
499
|
end
|
520
500
|
|
521
501
|
def test_math_plus
|
522
502
|
d = Date.new(1997, 6, 15)
|
523
|
-
#
|
503
|
+
#Concat String
|
524
504
|
assert_equal "SophiePhan", t(@sophie, @name + "Phan")
|
525
505
|
assert_equal "Sophie2", t(@sophie, @name + 2)
|
526
506
|
assert_equal "Sophie1997-06-15", t(@sophie, @name + d)
|
@@ -529,24 +509,25 @@ module ArelExtensions
|
|
529
509
|
assert_equal "SophieSophieSophie", t(@sophie, @name + @name + @name)
|
530
510
|
assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name.concat(@name)))
|
531
511
|
assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name).concat(@name))
|
532
|
-
#
|
512
|
+
#FIXME: should work as expected in Oracle
|
533
513
|
assert_equal "Sophie2016-05-23", t(@sophie, @name + @created_at) unless @env_db == 'oracle'
|
534
|
-
#
|
514
|
+
#concat Integer
|
535
515
|
assert_equal 1, User.where((@age + 10).eq(33)).count
|
536
516
|
assert_equal 1, User.where((@age + "1").eq(6)).count
|
537
517
|
assert_equal 1, User.where((@age + @age).eq(10)).count
|
538
|
-
#
|
539
|
-
#
|
518
|
+
#concat Date
|
519
|
+
#puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
540
520
|
assert_equal "2016-05-24", t(@myung, @created_at + 1).to_date.to_s
|
541
521
|
assert_equal "2016-05-25", t(@myung, @created_at + 2.day).to_date.to_s
|
542
522
|
end
|
543
523
|
|
524
|
+
|
544
525
|
def test_math_minus
|
545
526
|
d = Date.new(2016, 5, 20)
|
546
|
-
#
|
547
|
-
assert_equal
|
527
|
+
#Datediff
|
528
|
+
assert_equal 8, User.where((@created_at - @created_at).eq(0)).count
|
548
529
|
assert_equal 3, @laure.select((@created_at - d).as("res")).first.res.abs.to_i
|
549
|
-
#
|
530
|
+
#Substraction
|
550
531
|
assert_equal 0, User.where((@age - 10).eq(50)).count
|
551
532
|
assert_equal 0, User.where((@age - "10").eq(50)).count
|
552
533
|
# assert_equal 0, User.where((@age - 9.5).eq(50.5)).count # should work: TODO
|
@@ -554,7 +535,7 @@ module ArelExtensions
|
|
554
535
|
end
|
555
536
|
|
556
537
|
def test_wday
|
557
|
-
#
|
538
|
+
#d = Date.new(2016, 6, 26)
|
558
539
|
assert_equal(@env_db == 'oracle' || @env_db == 'mssql' ? 2 : 1, t(@myung, @created_at.wday).to_i) # monday
|
559
540
|
end
|
560
541
|
|
@@ -585,46 +566,45 @@ module ArelExtensions
|
|
585
566
|
# Case clause
|
586
567
|
def test_case
|
587
568
|
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)
|
588
|
-
assert_equal 4, User.where(@score.when(20.16).then(1).else(0).eq(1)).count
|
589
569
|
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
590
570
|
assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
|
591
571
|
assert_equal "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
|
592
572
|
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
|
593
|
-
assert_equal "65.621", t(@arthur, @score.when(65.62).then(@score.cast(:string)).else(@score.cast(:string))+1).tr('0','') #
|
573
|
+
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
|
594
574
|
end
|
595
575
|
|
596
576
|
def test_format_numbers
|
597
|
-
#
|
577
|
+
#score of Arthur = 65.62
|
598
578
|
skip " Works with SQLite if the version used knows printf" if $sqlite
|
599
579
|
|
600
|
-
assert_equal "Wrong Format", t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
|
601
|
-
assert_equal "AZERTY65,62", t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
|
602
|
-
assert_equal "65,62AZERTY", t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
|
603
|
-
assert_equal "$ 65.62 €", t(@arthur, @score.format_number("$ %.2f €","en_US"))
|
604
|
-
assert_equal "$ 66 €", t(@arthur, @score.format_number("$ %.0f €","en_US"))
|
605
|
-
assert_equal "$ 0065,62 €", t(@arthur, @score.format_number("$ %07.2f €","fr_FR"))
|
606
|
-
assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ %-07.2f €","fr_FR"))
|
607
|
-
assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
|
608
|
-
assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
|
609
|
-
assert_equal "$ 65,6 €", t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
|
610
|
-
assert_equal "$ +65,62 €", t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
|
611
|
-
assert_equal "$ +065,62 €", t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
|
580
|
+
assert_equal "Wrong Format" , t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
|
581
|
+
assert_equal "AZERTY65,62" , t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
|
582
|
+
assert_equal "65,62AZERTY" , t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
|
583
|
+
assert_equal "$ 65.62 €" , t(@arthur, @score.format_number("$ %.2f €","en_US"))
|
584
|
+
assert_equal "$ 66 €" , t(@arthur, @score.format_number("$ %.0f €","en_US"))
|
585
|
+
assert_equal "$ 0065,62 €" , t(@arthur, @score.format_number("$ %07.2f €","fr_FR"))
|
586
|
+
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-07.2f €","fr_FR"))
|
587
|
+
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
|
588
|
+
assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
|
589
|
+
assert_equal "$ 65,6 €" , t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
|
590
|
+
assert_equal "$ +65,62 €" , t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
|
591
|
+
assert_equal "$ +065,62 €" , t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
|
612
592
|
assert_includes ["$ 6,56e1 €","$ 6,56e+01 €"], t(@arthur, @score.format_number("$ %.2e €","fr_FR"))
|
613
593
|
assert_includes ["$ 6,56E1 €","$ 6,56E+01 €"], t(@arthur, @score.format_number("$ %.2E €","fr_FR"))
|
614
594
|
assert_includes ["$ 6,562E1 €","$ 6,562E+01 €"], t(@arthur, @score.format_number("$ %.3E €","fr_FR"))
|
615
|
-
assert_equal "123 456 765,6", t(@arthur, (@score+123456700).format_number("%.1f","sv_SE")).gsub("\u00A0"," ") #
|
616
|
-
assert_equal "123456765,6", t(@arthur, (@score+123456700).format_number("%.1f","fr_FR")).gsub("\u00A0","") #
|
617
|
-
assert_equal "123,456,765.6", t(@arthur, (@score+123456700).format_number("%.1f","en_US"))
|
618
|
-
assert_equal " 123,456,765.6", t(@arthur, (@score+123456700).format_number("%16.1f","en_US"))
|
619
|
-
assert_equal "$ 0,00 €", t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
|
620
|
-
assert_equal "$ 0,00 €", t(@arthur, (@score-65.62).format_number("$ %.2f €","fr_FR"))
|
595
|
+
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)
|
596
|
+
assert_equal "123456765,6" , t(@arthur, (@score+123456700).format_number("%.1f","fr_FR")).gsub("\u00A0","") #because SqlServer does it like no one else
|
597
|
+
assert_equal "123,456,765.6" , t(@arthur, (@score+123456700).format_number("%.1f","en_US"))
|
598
|
+
assert_equal " 123,456,765.6" , t(@arthur, (@score+123456700).format_number("%16.1f","en_US"))
|
599
|
+
assert_equal "$ 0,00 €" , t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
|
600
|
+
assert_equal "$ 0,00 €" , t(@arthur, (@score-65.62).format_number("$ %.2f €","fr_FR"))
|
621
601
|
end
|
622
602
|
|
623
603
|
def test_accent_insensitive
|
624
604
|
skip "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
|
625
605
|
skip "Not finished" if @env_db == 'mysql'
|
626
606
|
# actual comments value: "arrêté"
|
627
|
-
#
|
607
|
+
#AI & CI
|
628
608
|
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
629
609
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrêté")).then("1").else("0"))
|
630
610
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
|
@@ -632,7 +612,7 @@ module ArelExtensions
|
|
632
612
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
|
633
613
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrete")).then("1").else("0"))
|
634
614
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrêté")).then("1").else("0"))
|
635
|
-
#
|
615
|
+
#AI & CS
|
636
616
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrêté")).then("1").else("0"))
|
637
617
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
|
638
618
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
|
@@ -642,7 +622,7 @@ module ArelExtensions
|
|
642
622
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrêté")).then("1").else("0"))
|
643
623
|
end
|
644
624
|
end
|
645
|
-
#
|
625
|
+
#AS & CI
|
646
626
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrêté")).then("1").else("0"))
|
647
627
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
648
628
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
|
@@ -653,7 +633,7 @@ module ArelExtensions
|
|
653
633
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrete")).then("1").else("0"))
|
654
634
|
end
|
655
635
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrêté")).then("1").else("0"))
|
656
|
-
#
|
636
|
+
#AS & CS
|
657
637
|
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrêté")).then("1").else("0"))
|
658
638
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
|
659
639
|
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
|
@@ -663,33 +643,33 @@ module ArelExtensions
|
|
663
643
|
end
|
664
644
|
|
665
645
|
def test_subquery_with_order
|
666
|
-
assert_equal
|
667
|
-
assert_equal
|
646
|
+
assert_equal 8, User.where(:name => User.select(:name).order(:name)).count
|
647
|
+
assert_equal 8, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
|
668
648
|
if !['mysql'].include?(@env_db) # MySql can't have limit in IN subquery
|
669
649
|
assert_equal 2, User.where(:name => User.select(:name).order(:name).limit(2)).count
|
670
|
-
#
|
650
|
+
#assert_equal 6, User.where(:name => User.select(:name).order(:name).offset(2)).count
|
671
651
|
end
|
672
652
|
end
|
673
653
|
|
674
654
|
def test_in_with_nil
|
675
|
-
assert_equal true, @myung.where(@age.in(1)).blank?
|
676
|
-
assert_equal false, @myung.where(@age.in(23)).blank?
|
677
|
-
assert_equal true, @myung.where(@age.in([1])).blank?
|
678
|
-
assert_equal true, @myung.where(@age.in([1,2])).blank?
|
679
|
-
assert_equal false, @myung.where(@age.in([1,23])).blank?
|
680
|
-
assert_equal true, @myung.where(@age.in(nil)).blank?
|
681
|
-
assert_equal true, @myung.where(@age.in([nil])).blank?
|
682
|
-
assert_equal true, @myung.where(@age.in([nil,1])).blank?
|
683
|
-
assert_equal false, @myung.where(@age.in([nil,23])).blank?
|
684
|
-
assert_equal true, @myung.where(@age.in([nil,1,2])).blank?
|
685
|
-
assert_equal false, @myung.where(@age.in([nil,1,23])).blank?
|
686
|
-
assert_equal true, @test.where(@age.in(1)).blank?
|
687
|
-
assert_equal true, @test.where(@age.in([1])).blank?
|
688
|
-
assert_equal true, @test.where(@age.in([1,2])).blank?
|
689
|
-
assert_equal false, @test.where(@age.in(nil)).blank?
|
690
|
-
assert_equal false, @test.where(@age.in([nil])).blank?
|
691
|
-
assert_equal false, @test.where(@age.in([nil,1])).blank?
|
692
|
-
assert_equal false, @test.where(@age.in([nil,1,2])).blank?
|
655
|
+
assert_equal true , @myung.where(@age.in(1)).blank?
|
656
|
+
assert_equal false , @myung.where(@age.in(23)).blank?
|
657
|
+
assert_equal true , @myung.where(@age.in([1])).blank?
|
658
|
+
assert_equal true , @myung.where(@age.in([1,2])).blank?
|
659
|
+
assert_equal false , @myung.where(@age.in([1,23])).blank?
|
660
|
+
assert_equal true , @myung.where(@age.in(nil)).blank?
|
661
|
+
assert_equal true , @myung.where(@age.in([nil])).blank?
|
662
|
+
assert_equal true , @myung.where(@age.in([nil,1])).blank?
|
663
|
+
assert_equal false , @myung.where(@age.in([nil,23])).blank?
|
664
|
+
assert_equal true , @myung.where(@age.in([nil,1,2])).blank?
|
665
|
+
assert_equal false , @myung.where(@age.in([nil,1,23])).blank?
|
666
|
+
assert_equal true , @test.where(@age.in(1)).blank?
|
667
|
+
assert_equal true , @test.where(@age.in([1])).blank?
|
668
|
+
assert_equal true , @test.where(@age.in([1,2])).blank?
|
669
|
+
assert_equal false , @test.where(@age.in(nil)).blank?
|
670
|
+
assert_equal false , @test.where(@age.in([nil])).blank?
|
671
|
+
assert_equal false , @test.where(@age.in([nil,1])).blank?
|
672
|
+
assert_equal false , @test.where(@age.in([nil,1,2])).blank?
|
693
673
|
end
|
694
674
|
|
695
675
|
def test_scope_with_in_plus_new
|
@@ -703,40 +683,33 @@ module ArelExtensions
|
|
703
683
|
end
|
704
684
|
|
705
685
|
def test_is_not_null
|
706
|
-
assert_equal false, @myung.where(@age.is_not_null).blank?
|
707
|
-
assert_equal true, @test.where(@age.is_not_null).blank?
|
686
|
+
assert_equal false , @myung.where(@age.is_not_null).blank?
|
687
|
+
assert_equal true , @test.where(@age.is_not_null).blank?
|
708
688
|
end
|
709
689
|
|
710
690
|
def test_not_in_with_nil
|
711
|
-
assert_equal false, @myung.where(@age.not_in(1)).blank?
|
712
|
-
assert_equal true, @myung.where(@age.not_in(23)).blank?
|
713
|
-
assert_equal false, @myung.where(@age.not_in([1])).blank?
|
714
|
-
assert_equal false, @myung.where(@age.not_in([1,2])).blank?
|
715
|
-
assert_equal true, @myung.where(@age.not_in([1,23])).blank?
|
716
|
-
assert_equal false, @myung.where(@age.not_in(nil)).blank?
|
717
|
-
assert_equal false, @myung.where(@age.not_in([nil])).blank?
|
718
|
-
assert_equal false, @myung.where(@age.not_in([nil,1])).blank?
|
719
|
-
assert_equal true, @myung.where(@age.not_in([nil,23])).blank?
|
720
|
-
assert_equal false, @myung.where(@age.not_in([nil,1,2])).blank?
|
721
|
-
assert_equal true, @myung.where(@age.not_in([nil,1,23])).blank?
|
722
|
-
|
723
|
-
assert_equal false, @myung.where(@age.not_in(1..2)).blank?
|
724
|
-
|
725
|
-
#
|
726
|
-
#
|
727
|
-
#
|
728
|
-
#
|
729
|
-
#
|
730
|
-
#
|
731
|
-
#
|
732
|
-
#
|
733
|
-
end
|
734
|
-
|
735
|
-
def test_in_on_grouping
|
736
|
-
skip "We should modify the visitor of IN to make it work" if $sqlite || @env_db == 'mssql'
|
737
|
-
assert_equal 2, User.where(Arel.tuple(@name,@age).in(Arel.tuple('Myung',23),Arel.tuple('Arthur',21))).count
|
738
|
-
assert_equal 1, User.where(Arel.tuple(@name,@age).in(Arel.tuple('Myung',23))).count
|
739
|
-
assert_equal 0, User.where(Arel.tuple(@name,@age).in([])).count
|
691
|
+
assert_equal false , @myung.where(@age.not_in(1)).blank?
|
692
|
+
assert_equal true , @myung.where(@age.not_in(23)).blank?
|
693
|
+
assert_equal false , @myung.where(@age.not_in([1])).blank?
|
694
|
+
assert_equal false , @myung.where(@age.not_in([1,2])).blank?
|
695
|
+
assert_equal true , @myung.where(@age.not_in([1,23])).blank?
|
696
|
+
assert_equal false , @myung.where(@age.not_in(nil)).blank?
|
697
|
+
assert_equal false , @myung.where(@age.not_in([nil])).blank?
|
698
|
+
assert_equal false , @myung.where(@age.not_in([nil,1])).blank?
|
699
|
+
assert_equal true , @myung.where(@age.not_in([nil,23])).blank?
|
700
|
+
assert_equal false , @myung.where(@age.not_in([nil,1,2])).blank?
|
701
|
+
assert_equal true , @myung.where(@age.not_in([nil,1,23])).blank?
|
702
|
+
|
703
|
+
assert_equal false , @myung.where(@age.not_in(1..2)).blank?
|
704
|
+
|
705
|
+
#if the column is null, the entry will never be selected with not in (like every DBMS does)
|
706
|
+
#assert_equal false , @test.where(@age.not_in(1)).blank?
|
707
|
+
#assert_equal false , @test.where(@age.not_in([1])).blank?
|
708
|
+
#assert_equal false , @test.where(@age.not_in([1,2])).blank?
|
709
|
+
#assert_equal true , @test.where(@age.not_in(nil)).blank?
|
710
|
+
#assert_equal true , @test.where(@age.not_in([nil])).blank?
|
711
|
+
#assert_equal true , @test.where(@age.not_in([nil,1])).blank?
|
712
|
+
#assert_equal true , @test.where(@age.not_in([nil,1,2])).blank?
|
740
713
|
end
|
741
714
|
|
742
715
|
def test_alias_shortened
|
@@ -745,29 +718,24 @@ module ArelExtensions
|
|
745
718
|
at = User.arel_table.alias('azerty' * 15)
|
746
719
|
assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
|
747
720
|
assert_equal '"user_tests" "u"'.downcase, User.arel_table.alias('u').to_sql.downcase
|
748
|
-
assert_equal
|
749
|
-
|
721
|
+
assert_equal %Q[SELECT "#{new_alias}"."id" FROM "user_tests" "#{new_alias}"].downcase,
|
722
|
+
User.select(at[:id]).from(at).to_sql.downcase
|
750
723
|
end
|
751
724
|
end
|
752
725
|
|
753
726
|
def test_stat_functions
|
754
727
|
skip "SQLite doesn't work for most on this functions" if $sqlite
|
755
|
-
#
|
756
|
-
#
|
757
|
-
#
|
758
|
-
#
|
759
|
-
#
|
760
|
-
|
761
|
-
assert ( 15.
|
762
|
-
assert (
|
763
|
-
assert (
|
764
|
-
assert (
|
765
|
-
assert (
|
766
|
-
skip "Not Yet Implemented" # if !['postgresql'].include?(@env_db)
|
767
|
-
assert_equal 2, User.select(@score.std(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
768
|
-
assert_equal 2, User.select(@score.variance(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
769
|
-
assert_equal 2, User.select(@score.sum(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
770
|
-
assert_equal 2, User.select(@comments.group_concat(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
728
|
+
#puts t(User.where(nil), @score.average)
|
729
|
+
#puts t(User.where(nil), @score.variance(true))
|
730
|
+
#puts t(User.where(nil), @score.variance(false))
|
731
|
+
#puts t(User.where(nil), @score.std(true))
|
732
|
+
#puts t(User.where(nil), @score.std(false))
|
733
|
+
|
734
|
+
assert ( 15.98625 - t(User.where(nil), @score.average)).abs < 0.01
|
735
|
+
assert (613.75488 - t(User.where(nil), @score.variance)).abs < 0.01
|
736
|
+
assert ( 537.0355 - t(User.where(nil), @score.variance(false))).abs < 0.01
|
737
|
+
assert ( 24.77408 - t(User.where(nil), @score.std)).abs < 0.01
|
738
|
+
assert ( 23.17403 - t(User.where(nil), @score.std(false))).abs < 0.01
|
771
739
|
end
|
772
740
|
|
773
741
|
def test_levenshtein_distance
|
@@ -779,7 +747,7 @@ module ArelExtensions
|
|
779
747
|
|
780
748
|
def test_json
|
781
749
|
skip "Can't be tested on travis"
|
782
|
-
#
|
750
|
+
#creation
|
783
751
|
assert_equal 'Arthur', t(@arthur,Arel.json(@name))
|
784
752
|
assert_equal ["Arthur","Arthur"], parse_json(t(@arthur,Arel.json(@name,@name)))
|
785
753
|
assert_equal ({"Arthur" => "Arthur", "Arthur2" => "ArthurArthur"}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => @name+@name})))
|
@@ -788,29 +756,29 @@ module ArelExtensions
|
|
788
756
|
|
789
757
|
# aggregate
|
790
758
|
assert_equal ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure"}),
|
791
|
-
|
759
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name}).group(false)))
|
792
760
|
assert_equal ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure", "Laure"=>25, "Lucas"=>5, "Myung"=>23, "Sophie"=>15}),
|
793
|
-
|
761
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name,@name => @age}).group(false)))
|
794
762
|
assert_equal ([{"5" => "Lucas"},{ "15" => "Sophie"},{ "23" => "Myung"},{ "25" => "Laure"}]),
|
795
|
-
|
763
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score),Arel.json({@age => @name}).group(true,[@age])))
|
796
764
|
|
797
|
-
#
|
798
|
-
#
|
765
|
+
#puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_sql
|
766
|
+
#puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_a
|
799
767
|
|
800
768
|
skip "Not Yet Implemented" if $sqlite || ['oracle','mssql'].include?(@env_db)
|
801
|
-
#
|
769
|
+
#get
|
802
770
|
h1 = Arel.json({@name => @name+@name,@name+"2" => 1})
|
803
771
|
assert_equal "ArthurArthur", parse_json(t(@arthur,h1.get(@name)))
|
804
772
|
h2 = Arel.json([{age: @age},{name: @name,score: @score}])
|
805
773
|
assert_equal ({"age" => 21}), parse_json(t(@arthur,h2.get(0)))
|
806
774
|
assert_equal 21, parse_json(t(@arthur,h2.get(0).get('age')))
|
807
775
|
assert_nil t(@arthur,h2.get('age'))
|
808
|
-
#
|
776
|
+
#set
|
809
777
|
assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1}), parse_json(t(@arthur,h1.set(@name, ['toto','tata'])))
|
810
778
|
assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.set(@name+"3",2)))
|
811
779
|
assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => nil}), parse_json(t(@arthur,h1.set(@name+"3",nil)))
|
812
780
|
assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => {"a" => 2}}), parse_json(t(@arthur,h1.set(@name+"3",{a: 2})))
|
813
|
-
#
|
781
|
+
#merge
|
814
782
|
assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata']},{@name+"3" => 2})))
|
815
783
|
assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata'], @name+"3" => 2})))
|
816
784
|
assert_equal ({"Arthur" => "ArthurArthur","Arthur2" => 1}), parse_json(t(@arthur,h1.merge({})))
|
@@ -818,14 +786,11 @@ module ArelExtensions
|
|
818
786
|
|
819
787
|
def test_as_on_everything
|
820
788
|
name = @arthur.select(@name.as('NaMe')).first.attributes
|
821
|
-
assert_equal 'Arthur', name["NaMe"] || name["name"] #
|
789
|
+
assert_equal 'Arthur', name["NaMe"] || name["name"] #because of Oracle
|
822
790
|
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes["Na Me"]
|
823
791
|
assert_equal 'ArthurArthur', @arthur.select((@name+@name).as('Na-Me')).first.attributes["Na-Me"]
|
824
792
|
end
|
825
793
|
|
826
|
-
def test_exists_in_subquery
|
827
|
-
assert User.where(User.where(nil).arel.exists).first
|
828
|
-
end
|
829
794
|
end
|
830
795
|
end
|
831
796
|
end
|