arel_extensions 1.3.2 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -2
- data/.github/workflows/ruby.yml +43 -91
- data/.gitignore +7 -6
- data/.rubocop.yml +62 -1
- data/Gemfile +11 -11
- data/README.md +1 -1
- data/Rakefile +4 -4
- data/TODO +0 -1
- data/appveyor.yml +73 -0
- data/arel_extensions.gemspec +12 -12
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +14 -14
- data/gemfiles/rails5_0.gemfile +13 -13
- data/gemfiles/rails5_1_4.gemfile +13 -13
- data/gemfiles/rails5_2.gemfile +15 -14
- data/gemfiles/rails6.gemfile +13 -12
- data/gemfiles/rails6_1.gemfile +13 -12
- data/gemfiles/rails7.gemfile +9 -8
- data/gemspecs/arel_extensions-v1.gemspec +12 -12
- data/gemspecs/arel_extensions-v2.gemspec +11 -11
- data/init/mssql.sql +0 -0
- data/init/mysql.sql +0 -0
- data/init/oracle.sql +0 -0
- data/init/postgresql.sql +0 -0
- data/init/sqlite.sql +0 -0
- data/lib/arel_extensions/attributes.rb +2 -2
- data/lib/arel_extensions/boolean_functions.rb +2 -4
- data/lib/arel_extensions/common_sql_functions.rb +12 -12
- data/lib/arel_extensions/comparators.rb +14 -14
- data/lib/arel_extensions/date_duration.rb +7 -7
- data/lib/arel_extensions/helpers.rb +19 -16
- data/lib/arel_extensions/insert_manager.rb +1 -1
- data/lib/arel_extensions/math.rb +44 -31
- data/lib/arel_extensions/math_functions.rb +18 -18
- data/lib/arel_extensions/nodes/abs.rb +0 -0
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -1
- data/lib/arel_extensions/nodes/case.rb +4 -6
- data/lib/arel_extensions/nodes/cast.rb +5 -5
- data/lib/arel_extensions/nodes/ceil.rb +0 -0
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +1 -1
- data/lib/arel_extensions/nodes/collate.rb +9 -9
- data/lib/arel_extensions/nodes/concat.rb +2 -2
- data/lib/arel_extensions/nodes/date_diff.rb +10 -10
- data/lib/arel_extensions/nodes/duration.rb +0 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
- data/lib/arel_extensions/nodes/floor.rb +0 -0
- data/lib/arel_extensions/nodes/format.rb +0 -0
- data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
- data/lib/arel_extensions/nodes/function.rb +21 -21
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +7 -7
- data/lib/arel_extensions/nodes/length.rb +0 -0
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
- data/lib/arel_extensions/nodes/locate.rb +1 -1
- data/lib/arel_extensions/nodes/log10.rb +0 -0
- data/lib/arel_extensions/nodes/matches.rb +1 -1
- data/lib/arel_extensions/nodes/md5.rb +0 -0
- data/lib/arel_extensions/nodes/power.rb +0 -0
- data/lib/arel_extensions/nodes/rand.rb +0 -0
- data/lib/arel_extensions/nodes/repeat.rb +2 -2
- data/lib/arel_extensions/nodes/replace.rb +2 -2
- data/lib/arel_extensions/nodes/round.rb +0 -0
- data/lib/arel_extensions/nodes/soundex.rb +2 -2
- data/lib/arel_extensions/nodes/std.rb +0 -0
- data/lib/arel_extensions/nodes/substring.rb +1 -1
- data/lib/arel_extensions/nodes/sum.rb +0 -0
- data/lib/arel_extensions/nodes/then.rb +1 -1
- data/lib/arel_extensions/nodes/trim.rb +2 -2
- data/lib/arel_extensions/nodes/union.rb +4 -4
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/nodes/wday.rb +0 -0
- data/lib/arel_extensions/nodes.rb +0 -0
- data/lib/arel_extensions/null_functions.rb +0 -0
- data/lib/arel_extensions/predications.rb +10 -10
- data/lib/arel_extensions/railtie.rb +1 -1
- data/lib/arel_extensions/set_functions.rb +3 -3
- data/lib/arel_extensions/string_functions.rb +8 -8
- data/lib/arel_extensions/tasks.rb +2 -2
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/convert_format.rb +0 -0
- data/lib/arel_extensions/visitors/ibm_db.rb +20 -20
- data/lib/arel_extensions/visitors/mssql.rb +150 -130
- data/lib/arel_extensions/visitors/mysql.rb +147 -149
- data/lib/arel_extensions/visitors/oracle.rb +141 -135
- data/lib/arel_extensions/visitors/oracle12.rb +16 -16
- data/lib/arel_extensions/visitors/postgresql.rb +147 -139
- data/lib/arel_extensions/visitors/sqlite.rb +85 -87
- data/lib/arel_extensions/visitors/to_sql.rb +146 -148
- data/lib/arel_extensions/visitors.rb +7 -7
- data/lib/arel_extensions.rb +56 -32
- data/test/arelx_test_helper.rb +14 -13
- data/test/database.yml +5 -5
- data/test/real_db_test.rb +81 -81
- data/test/support/fake_record.rb +2 -2
- data/test/test_comparators.rb +5 -5
- data/test/visitors/test_bulk_insert_oracle.rb +5 -5
- data/test/visitors/test_bulk_insert_sqlite.rb +5 -5
- data/test/visitors/test_bulk_insert_to_sql.rb +5 -5
- data/test/visitors/test_oracle.rb +14 -14
- data/test/visitors/test_to_sql.rb +87 -87
- data/test/with_ar/all_agnostic_test.rb +411 -306
- data/test/with_ar/insert_agnostic_test.rb +19 -16
- data/test/with_ar/test_bulk_sqlite.rb +5 -5
- data/test/with_ar/test_math_sqlite.rb +12 -12
- data/test/with_ar/test_string_mysql.rb +20 -20
- data/test/with_ar/test_string_sqlite.rb +20 -20
- data/version_v1.rb +1 -1
- data/version_v2.rb +1 -1
- metadata +3 -9
- data/.travis/oracle/download.js +0 -152
- data/.travis/oracle/download.sh +0 -30
- data/.travis/oracle/download_ojdbc.js +0 -116
- data/.travis/oracle/install.sh +0 -34
- data/.travis/setup_accounts.sh +0 -9
- data/.travis/sqlite3/extension-functions.sh +0 -6
- data/.travis.yml +0 -193
@@ -7,9 +7,9 @@ module ArelExtensions
|
|
7
7
|
require 'minitest/pride'
|
8
8
|
def connect_db
|
9
9
|
ActiveRecord::Base.configurations = YAML.load_file('test/database.yml')
|
10
|
-
if ENV['DB'] == 'oracle' && ((defined?(RUBY_ENGINE) && RUBY_ENGINE ==
|
11
|
-
@env_db = (RUBY_PLATFORM == 'java' ?
|
12
|
-
skip "Platform not supported"
|
10
|
+
if ENV['DB'] == 'oracle' && ((defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx') || (RUBY_PLATFORM == 'java')) # not supported
|
11
|
+
@env_db = (RUBY_PLATFORM == 'java' ? 'jdbc-sqlite' : 'sqlite')
|
12
|
+
skip "Platform not supported (DB: #{ENV['DB']}, RUBY_ENGINE: #{RUBY_ENGINE}, RUBY_PLATFORM: #{RUBY_PLATFORM})"
|
13
13
|
else
|
14
14
|
@env_db = ENV['DB']
|
15
15
|
end
|
@@ -36,11 +36,11 @@ module ArelExtensions
|
|
36
36
|
t.column :updated_at, :datetime, precision: nil
|
37
37
|
t.column :duration, :time
|
38
38
|
t.column :other, :string
|
39
|
-
t.column :score, :decimal, :
|
39
|
+
t.column :score, :decimal, precision: 20, scale: 10
|
40
40
|
end
|
41
41
|
@cnx.drop_table(:product_tests) rescue nil
|
42
42
|
@cnx.create_table :product_tests do |t|
|
43
|
-
t.column :price, :decimal, :
|
43
|
+
t.column :price, :decimal, precision: 20, scale: 10
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -55,24 +55,24 @@ module ArelExtensions
|
|
55
55
|
d = Date.new(2016, 5, 23)
|
56
56
|
connect_db
|
57
57
|
setup_db
|
58
|
-
u = User.create :
|
59
|
-
@lucas = User.where(:
|
60
|
-
u = User.create :
|
61
|
-
@sophie = User.where(:
|
62
|
-
u = User.create :
|
63
|
-
@camille = User.where(:
|
64
|
-
u = User.create :
|
65
|
-
@arthur = User.where(:
|
66
|
-
u = User.create :
|
67
|
-
@myung = User.where(:
|
68
|
-
u = User.create :
|
69
|
-
@laure = User.where(:
|
70
|
-
u = User.create :
|
71
|
-
@test = User.where(:
|
72
|
-
u = User.create :
|
73
|
-
@neg = User.where(:
|
74
|
-
u = User.create :
|
75
|
-
@justin = User.where(:
|
58
|
+
u = User.create age: 5, name: 'Lucas', created_at: d, score: 20.16, updated_at: Time.utc(2014, 3, 3, 12, 42, 0)
|
59
|
+
@lucas = User.where(id: u.id)
|
60
|
+
u = User.create age: 15, name: 'Sophie', created_at: d, score: 20.16
|
61
|
+
@sophie = User.where(id: u.id)
|
62
|
+
u = User.create age: 20, name: 'Camille', created_at: d, score: -20.16, comments: ''
|
63
|
+
@camille = User.where(id: u.id)
|
64
|
+
u = User.create age: 21, name: 'Arthur', created_at: d, score: 65.62, comments: 'arrêté'
|
65
|
+
@arthur = User.where(id: u.id)
|
66
|
+
u = User.create age: 23, name: 'Myung', created_at: d, score: 20.16, comments: ' '
|
67
|
+
@myung = User.where(id: u.id)
|
68
|
+
u = User.create age: 25, name: 'Laure', created_at: d, score: 20.16, duration: Time.utc(2001, 1, 1, 12, 42, 21), updated_at: Time.utc(2014, 3, 3, 12, 42, 0)
|
69
|
+
@laure = User.where(id: u.id)
|
70
|
+
u = User.create age: nil, name: 'Test', created_at: d, score: 1.62, other: 'toto'
|
71
|
+
@test = User.where(id: u.id)
|
72
|
+
u = User.create age: -42, name: 'Negatif', comments: '1,22,3,42,2', created_at: d, updated_at: d.to_time, score: 0.17
|
73
|
+
@neg = User.where(id: u.id)
|
74
|
+
u = User.create age: 15, name: 'Justin', created_at: d, score: 11.0
|
75
|
+
@justin = User.where(id: u.id)
|
76
76
|
|
77
77
|
@age = User.arel_table[:age]
|
78
78
|
@name = User.arel_table[:name]
|
@@ -107,6 +107,14 @@ module ArelExtensions
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
+
# Connection and column info
|
111
|
+
def test_column_of
|
112
|
+
assert_nil Arel.column_of('chupa', 'maflavla'), 'Non-existent table and column should return nil'
|
113
|
+
assert_nil Arel.column_of('chupa', 'updated_at'), 'Non-existent table but existent column should return nil'
|
114
|
+
assert_nil Arel.column_of('user_tests', 'maflavla'), 'Existent table but non-existent column should return nil'
|
115
|
+
assert_equal 'updated_at', Arel.column_of('user_tests', 'updated_at').name, 'An existing column name should be returned'
|
116
|
+
end
|
117
|
+
|
110
118
|
# Math Functions
|
111
119
|
def test_classical_arel
|
112
120
|
assert_in_epsilon 42.16, t(@laure, @score + 22), 0.01
|
@@ -153,16 +161,16 @@ module ArelExtensions
|
|
153
161
|
|
154
162
|
def test_sum
|
155
163
|
if @env_db == 'mssql'
|
156
|
-
skip
|
157
|
-
assert_equal 83, User.select((@age.sum + 1).as(
|
158
|
-
assert_equal 164, User.reorder(nil).select((@age.sum + @age.sum).as(
|
159
|
-
assert_equal 246, User.reorder(nil).select(((@age * 3).sum).as(
|
160
|
-
assert_equal 4234, User.reorder(nil).select(((@age * @age).sum).as(
|
164
|
+
skip 'SQL Server forces order?' # TODO
|
165
|
+
assert_equal 83, User.select((@age.sum + 1).as('res'), User.arel_table[:id].sum).take(50).reorder(@age).first.res
|
166
|
+
assert_equal 164, User.reorder(nil).select((@age.sum + @age.sum).as('res'), User.arel_table[:id].sum).take(50).first.res
|
167
|
+
assert_equal 246, User.reorder(nil).select(((@age * 3).sum).as('res'), User.arel_table[:id].sum).take(50).first.res
|
168
|
+
assert_equal 4234, User.reorder(nil).select(((@age * @age).sum).as('res'), User.arel_table[:id].sum).take(50).first.res
|
161
169
|
else
|
162
|
-
assert_equal 83, User.select((@age.sum + 1).as(
|
163
|
-
assert_equal 164, User.select((@age.sum + @age.sum).as(
|
164
|
-
assert_equal 246, User.select((@age * 3).sum.as(
|
165
|
-
assert_equal 4234, User.select(((@age * @age).sum).as(
|
170
|
+
assert_equal 83, User.select((@age.sum + 1).as('res')).take(50).first.res
|
171
|
+
assert_equal 164, User.select((@age.sum + @age.sum).as('res')).take(50).first.res
|
172
|
+
assert_equal 246, User.select((@age * 3).sum.as('res')).take(50).first.res
|
173
|
+
assert_equal 4234, User.select(((@age * @age).sum).as('res')).take(50).first.res
|
166
174
|
end
|
167
175
|
end
|
168
176
|
|
@@ -182,19 +190,19 @@ module ArelExtensions
|
|
182
190
|
def test_concat
|
183
191
|
assert_equal 'Camille Camille', t(@camille, @name + ' ' + @name)
|
184
192
|
assert_equal 'Laure 2', t(@laure, @name + ' ' + 2)
|
185
|
-
assert_equal 'Test Laure', t(@laure, Arel
|
193
|
+
assert_equal 'Test Laure', t(@laure, Arel.quoted('Test ') + @name)
|
186
194
|
|
187
|
-
skip
|
188
|
-
assert_equal
|
189
|
-
assert_equal
|
190
|
-
assert_equal
|
195
|
+
skip 'No group_concat in SqlServer before 2017' if @env_db == 'mssql'
|
196
|
+
assert_equal 'Lucas Sophie', t(User.where(name: %w[Lucas Sophie]), @name.group_concat(' '))
|
197
|
+
assert_equal 'Lucas,Sophie', t(User.where(name: %w[Lucas Sophie]), @name.group_concat(','))
|
198
|
+
assert_equal 'Lucas,Sophie', t(User.where(name: %w[Lucas Sophie]), @name.group_concat)
|
191
199
|
|
192
|
-
skip
|
193
|
-
assert_equal
|
194
|
-
assert_equal
|
195
|
-
assert_equal
|
196
|
-
assert_equal
|
197
|
-
assert_equal
|
200
|
+
skip 'No order in group_concat in SqlLite' if $sqlite
|
201
|
+
assert_equal 'Arthur,Lucas,Sophie', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', @name.asc))
|
202
|
+
assert_equal 'Sophie,Lucas,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', @name.desc))
|
203
|
+
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', [@score.asc, @name.asc]))
|
204
|
+
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', @score.asc, @name.asc))
|
205
|
+
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', order: [@score.asc, @name.asc]))
|
198
206
|
end
|
199
207
|
|
200
208
|
def test_length
|
@@ -205,15 +213,15 @@ module ArelExtensions
|
|
205
213
|
|
206
214
|
def test_md5
|
207
215
|
skip "Sqlite can't do md5" if $sqlite
|
208
|
-
assert_equal
|
209
|
-
assert_equal
|
216
|
+
assert_equal 'e2cf99ca82a7e829d2a4ac85c48154d0', t(@camille, @name.md5)
|
217
|
+
assert_equal 'c3d41bf5efb468a1bcce53bd53726c85', t(@lucas, @name.md5)
|
210
218
|
end
|
211
219
|
|
212
220
|
def test_locate
|
213
221
|
skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
|
214
|
-
assert_equal 1, t(@camille, @name.locate(
|
215
|
-
assert_equal 0, t(@lucas, @name.locate(
|
216
|
-
assert_equal 5, t(@lucas, @name.locate(
|
222
|
+
assert_equal 1, t(@camille, @name.locate('C'))
|
223
|
+
assert_equal 0, t(@lucas, @name.locate('z'))
|
224
|
+
assert_equal 5, t(@lucas, @name.locate('s'))
|
217
225
|
end
|
218
226
|
|
219
227
|
def test_substring
|
@@ -223,7 +231,7 @@ module ArelExtensions
|
|
223
231
|
else
|
224
232
|
assert_equal('', t(@lucas, @name.substring(42)))
|
225
233
|
end
|
226
|
-
assert_equal 'Lu', t(@lucas, @name.substring(1,2))
|
234
|
+
assert_equal 'Lu', t(@lucas, @name.substring(1, 2))
|
227
235
|
|
228
236
|
assert_equal 'C', t(@camille, @name[0, 1])
|
229
237
|
assert_equal 'C', t(@camille, @name[0])
|
@@ -232,17 +240,17 @@ module ArelExtensions
|
|
232
240
|
else
|
233
241
|
assert_equal('', t(@lucas, @name[42]))
|
234
242
|
end
|
235
|
-
assert_equal 'Lu', t(@lucas, @name[0,2])
|
243
|
+
assert_equal 'Lu', t(@lucas, @name[0, 2])
|
236
244
|
assert_equal 'Lu', t(@lucas, @name[0..1])
|
237
245
|
|
238
246
|
# substring should accept string function
|
239
247
|
assert_equal 'Ce', t(@camille, @name.substring(1, 1).concat('e'))
|
240
|
-
assert_equal 'Ce', t(@camille, @name.substring(1, 1)+'e')
|
248
|
+
assert_equal 'Ce', t(@camille, @name.substring(1, 1) + 'e')
|
241
249
|
end
|
242
250
|
|
243
251
|
def test_find_in_set
|
244
252
|
skip "Sqlite version can't load extension for find_in_set" if $sqlite && $load_extension_disabled
|
245
|
-
skip
|
253
|
+
skip 'SQL Server does not know about FIND_IN_SET' if @env_db == 'mssql'
|
246
254
|
assert_equal 5, t(@neg, @comments & 2)
|
247
255
|
assert_equal 0, t(@neg, @comments & 6) # not found
|
248
256
|
end
|
@@ -254,10 +262,10 @@ module ArelExtensions
|
|
254
262
|
assert t(@neg, @name >= 'Mest') == true || t(@neg, @name >= 'Mest') == 't' # depends of ar version
|
255
263
|
assert t(@neg, @name <= (@name + 'Z')) == true || t(@neg, @name <= (@name + 'Z')) == 't'
|
256
264
|
elsif @env_db == 'oracle'
|
257
|
-
assert_equal 1, t(@neg,
|
258
|
-
assert_equal 1, t(@neg,
|
259
|
-
assert_equal 1, t(@neg,
|
260
|
-
assert_equal 1, t(@neg,
|
265
|
+
assert_equal 1, t(@neg, Arel.when(@name >= 'Mest').then(1).else(0))
|
266
|
+
assert_equal 1, t(@neg, Arel.when(@name <= (@name + 'Z')).then(1).else(0))
|
267
|
+
assert_equal 1, t(@neg, Arel.when(@name > 'Mest').then(1).else(0))
|
268
|
+
assert_equal 1, t(@neg, Arel.when(@name < (@name + 'Z')).then(1).else(0))
|
261
269
|
else
|
262
270
|
assert_equal 1, t(@neg, @name >= 'Mest')
|
263
271
|
assert_equal 1, t(@neg, @name <= (@name + 'Z'))
|
@@ -270,21 +278,21 @@ module ArelExtensions
|
|
270
278
|
skip "Sqlite can't compare time" if $sqlite
|
271
279
|
skip "Oracle can't compare time" if @env_db == 'oracle'
|
272
280
|
# @created_at == 2016-05-23
|
273
|
-
assert_includes [true,'t',1], t(@laure,
|
274
|
-
assert_includes [false,'f',0], t(@laure,
|
281
|
+
assert_includes [true, 't', 1], t(@laure, Arel.when(@created_at >= '2014-01-01').then(1).else(0))
|
282
|
+
assert_includes [false, 'f', 0], t(@laure, Arel.when(@created_at >= '2018-01-01').then(1).else(0))
|
275
283
|
# @updated_at == 2014-03-03 12:42:00
|
276
|
-
assert_includes [true,'t',1], t(@laure,
|
277
|
-
assert_includes [false,'f',0], t(@laure,
|
284
|
+
assert_includes [true, 't', 1], t(@laure, Arel.when(@updated_at >= '2014-03-03 10:10:10').then(1).else(0))
|
285
|
+
assert_includes [false, 'f', 0], t(@laure, Arel.when(@updated_at >= '2014-03-03 13:10:10').then(1).else(0))
|
278
286
|
# @duration == 12:42:21
|
279
|
-
# puts @laure.select(
|
280
|
-
# puts @laure.select(
|
281
|
-
assert_includes [true,'t',1], t(@laure,
|
282
|
-
assert_includes [false,'f',0], t(@laure,
|
287
|
+
# puts @laure.select(Arel.when(@duration >= '10:10:10').then(1).else(0)).to_sql
|
288
|
+
# puts @laure.select(Arel.when(@duration >= '14:10:10').then(1).else(0)).to_sql
|
289
|
+
assert_includes [true, 't', 1], t(@laure, Arel.when(@duration >= '10:10:10').then(1).else(0))
|
290
|
+
assert_includes [false, 'f', 0], t(@laure, Arel.when(@duration >= '14:10:10').then(1).else(0))
|
283
291
|
end
|
284
292
|
|
285
293
|
def test_regexp_not_regexp
|
286
294
|
skip "Sqlite version can't load extension for regexp" if $sqlite && $load_extension_disabled
|
287
|
-
skip
|
295
|
+
skip 'SQL Server does not know about REGEXP without extensions' if @env_db == 'mssql'
|
288
296
|
assert_equal 1, User.where(@name =~ '^M').count
|
289
297
|
assert_equal 7, User.where(@name !~ '^L').count
|
290
298
|
assert_equal 1, User.where(@name =~ /^M/).count
|
@@ -299,45 +307,45 @@ module ArelExtensions
|
|
299
307
|
end
|
300
308
|
|
301
309
|
def test_replace
|
302
|
-
assert_equal
|
303
|
-
assert_equal
|
310
|
+
assert_equal 'LucaX', t(@lucas, @name.replace('s', 'X'))
|
311
|
+
assert_equal 'replace', t(@lucas, @name.replace(@name, 'replace'))
|
304
312
|
|
305
|
-
skip
|
306
|
-
skip
|
307
|
-
skip
|
308
|
-
assert_equal
|
309
|
-
assert_equal
|
310
|
-
assert_equal
|
313
|
+
skip 'Sqlite does not seem to support regexp_replace' if $sqlite
|
314
|
+
skip 'SQL Server does not know about REGEXP without extensions' if @env_db == 'mssql'
|
315
|
+
skip 'Travis mysql version does not support REGEXP_REPLACE' if @env_db == 'mysql'
|
316
|
+
assert_equal 'LXcXs', t(@lucas, @name.replace(/[ua]/, 'X'))
|
317
|
+
assert_equal 'LXcXs', t(@lucas, @name.regexp_replace(/[ua]/, 'X'))
|
318
|
+
assert_equal 'LXcXs', t(@lucas, @name.regexp_replace('[ua]', 'X'))
|
311
319
|
end
|
312
320
|
|
313
321
|
def test_replace_once
|
314
|
-
skip
|
322
|
+
skip 'TODO'
|
315
323
|
# skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
|
316
|
-
assert_equal
|
324
|
+
assert_equal 'LuCas', t(@lucas, @name.substring(1, @name.locate('c') - 1) + 'C' + @name.substring(@name.locate('c') + 1, @name.length))
|
317
325
|
end
|
318
326
|
|
319
327
|
def test_soundex
|
320
328
|
skip "Sqlite version can't load extension for soundex" if $sqlite && $load_extension_disabled
|
321
329
|
skip "PostgreSql version can't load extension for soundex" if @env_db == 'postgresql'
|
322
|
-
assert_equal
|
330
|
+
assert_equal 'C540', t(@camille, @name.soundex)
|
323
331
|
assert_equal 9, User.where(@name.soundex.eq(@name.soundex)).count
|
324
332
|
assert_equal 9, User.where(@name.soundex == @name.soundex).count
|
325
333
|
end
|
326
334
|
|
327
335
|
def test_change_case
|
328
|
-
assert_equal
|
329
|
-
assert_equal
|
330
|
-
assert_equal
|
336
|
+
assert_equal 'myung', t(@myung, @name.downcase)
|
337
|
+
assert_equal 'MYUNG', t(@myung, @name.upcase)
|
338
|
+
assert_equal 'myung', t(@myung, @name.upcase.downcase)
|
331
339
|
end
|
332
340
|
|
333
341
|
def test_trim
|
334
|
-
assert_equal
|
335
|
-
assert_equal
|
336
|
-
assert_equal
|
337
|
-
assert_equal
|
338
|
-
assert_equal
|
339
|
-
skip
|
340
|
-
assert_equal
|
342
|
+
assert_equal 'Myung', t(@myung, @name.trim)
|
343
|
+
assert_equal 'Myung', t(@myung, @name.trim.ltrim.rtrim)
|
344
|
+
assert_equal 'Myun', t(@myung, @name.rtrim('g'))
|
345
|
+
assert_equal 'yung', t(@myung, @name.ltrim('M'))
|
346
|
+
assert_equal 'yung', t(@myung, (@name + 'M').trim('M'))
|
347
|
+
skip 'Oracle does not accept multi char trim' if @env_db == 'oracle'
|
348
|
+
assert_equal '', t(@myung, @name.rtrim(@name))
|
341
349
|
end
|
342
350
|
|
343
351
|
def test_blank
|
@@ -411,53 +419,150 @@ module ArelExtensions
|
|
411
419
|
}
|
412
420
|
}
|
413
421
|
|
414
|
-
skip "Unsupported timezone conversion for DB=#{ENV['DB']}" if
|
422
|
+
skip "Unsupported timezone conversion for DB=#{ENV['DB']}" if !%w[mssql mysql oracle postgresql].include?(ENV['DB'])
|
423
|
+
# TODO: Standarize timezone conversion across all databases.
|
424
|
+
# This test case will be refactored and should work the same across all vendors.
|
425
|
+
if ENV['DB'] == 'mssql' && /Microsoft SQL Server (\d+)/.match(ActiveRecord::Base.connection.select_value('SELECT @@version'))[1].to_i < 2016
|
426
|
+
skip "SQL Server < 2016 is not currently supported"
|
427
|
+
end
|
415
428
|
|
416
429
|
tz = ENV['DB'] == 'mssql' ? time_zones['mssql'] : time_zones['posix']
|
417
430
|
|
418
431
|
assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['utc']))
|
419
|
-
assert_equal '2014/03/03 09:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {
|
420
|
-
assert_equal '2014/03/03 02:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {
|
432
|
+
assert_equal '2014/03/03 09:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['sao_paulo']}))
|
433
|
+
assert_equal '2014/03/03 02:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['tahiti']}))
|
421
434
|
|
422
435
|
# Skipping conversion from UTC to the desired timezones fails in SQL
|
423
436
|
# Server and Postgres. This is mainly due to the fact that timezone
|
424
437
|
# information is not preserved in the column itself.
|
425
438
|
#
|
426
439
|
# MySQL is happy to consider that times by default are in UTC.
|
427
|
-
assert_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {
|
440
|
+
assert_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
|
428
441
|
refute_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['paris'])) if !['mysql'].include?(ENV['DB'])
|
429
442
|
|
430
443
|
# Winter/Summer time
|
431
|
-
assert_equal '2014/08/03 14:42:00', t(@lucas, (@updated_at + 5.months).format('%Y/%m/%d %H:%M:%S', {
|
444
|
+
assert_equal '2014/08/03 14:42:00', t(@lucas, (@updated_at + 5.months).format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
|
432
445
|
if ENV['DB'] == 'mssql'
|
433
|
-
assert_equal '2022/02/01 11:42:00', t(@lucas, Arel
|
434
|
-
assert_equal '2022/08/01 12:42:00', t(@lucas, Arel
|
446
|
+
assert_equal '2022/02/01 11:42:00', t(@lucas, Arel.quoted('2022-02-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
|
447
|
+
assert_equal '2022/08/01 12:42:00', t(@lucas, Arel.quoted('2022-08-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
|
435
448
|
else
|
436
|
-
assert_equal '2022/02/01 11:42:00', t(@lucas, Arel
|
437
|
-
assert_equal '2022/08/01 12:42:00', t(@lucas, Arel
|
449
|
+
assert_equal '2022/02/01 11:42:00', t(@lucas, Arel.quoted('2022-02-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', tz['paris']))
|
450
|
+
assert_equal '2022/08/01 12:42:00', t(@lucas, Arel.quoted('2022-08-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', tz['paris']))
|
451
|
+
end
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_format_iso_week
|
455
|
+
skip "Unsupported ISO week number for DB=#{ENV['DB']}" if ['sqlite'].include?(ENV['DB'])
|
456
|
+
assert_equal '10', t(@lucas, @updated_at.format('%V'))
|
457
|
+
{
|
458
|
+
'2024-01-01 10:42:00' => '01', # Monday
|
459
|
+
'2030-01-01 10:42:00' => '01', # Tuesday
|
460
|
+
'2025-01-01 10:42:00' => '01', # Wednesday
|
461
|
+
'2026-01-01 10:42:00' => '01', # Thursday
|
462
|
+
'2027-01-01 10:42:00' => '53', # Friday
|
463
|
+
'2028-01-01 10:42:00' => '52', # Saturday
|
464
|
+
'2034-01-01 10:42:00' => '52', # Sunday
|
465
|
+
}.each do |date, exp|
|
466
|
+
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%V'))
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
def test_format_iso_year_of_week
|
471
|
+
skip "Unsupported ISO year of week for DB=#{ENV['DB']}" if %w[mssql sqlite].include?(ENV['DB'])
|
472
|
+
assert_equal '2014', t(@lucas, @updated_at.format('%G'))
|
473
|
+
|
474
|
+
{
|
475
|
+
'2024-01-01 10:42:00' => '2024', # Monday
|
476
|
+
'2030-01-01 10:42:00' => '2030', # Tuesday
|
477
|
+
'2025-01-01 10:42:00' => '2025', # Wednesday
|
478
|
+
'2026-01-01 10:42:00' => '2026', # Thursday
|
479
|
+
'2027-01-01 10:42:00' => '2026', # Friday
|
480
|
+
'2028-01-01 10:42:00' => '2027', # Saturday
|
481
|
+
'2034-01-01 10:42:00' => '2033', # Sunday
|
482
|
+
}.each do |date, exp|
|
483
|
+
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%G'))
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_format_date_with_names
|
488
|
+
skip "#{ENV['DB']} does not support a variety of word-based formatting for month and day names" if %w[mssql sqlite].include?(ENV['DB'])
|
489
|
+
assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
490
|
+
assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
491
|
+
|
492
|
+
skip "#{ENV['DB']} does not support ALLCAPS month and day names" if ['mysql'].include?(ENV['DB'])
|
493
|
+
assert_equal 'Mon, 03 MAR 14', t(@lucas, @updated_at.format('%a, %d %^b %y'))
|
494
|
+
assert_equal 'Monday, 03 MARCH 14', t(@lucas, @updated_at.format('%A, %d %^B %y'))
|
495
|
+
end
|
496
|
+
|
497
|
+
def switch_to_lang(lang)
|
498
|
+
languages = {
|
499
|
+
'mssql' => {en: 'English', fr: 'French'},
|
500
|
+
'mysql' => {en: 'en_US', fr: 'fr_FR'},
|
501
|
+
'postgresql' => {en: 'en_US.utf8', fr: 'fr_FR.utf8'}
|
502
|
+
}
|
503
|
+
|
504
|
+
sql = {
|
505
|
+
'mssql' => ->(l) { "SET LANGUAGE #{l};" },
|
506
|
+
'mysql' => ->(l) { "SET lc_time_names = '#{l}';" },
|
507
|
+
'postgresql' => ->(l) { "SET lc_time to '#{l}';" }
|
508
|
+
}
|
509
|
+
|
510
|
+
User.connection.execute(sql[ENV['DB']][languages[ENV['DB']][lang]])
|
511
|
+
end
|
512
|
+
|
513
|
+
def test_format_date_with_names_and_lang_switch
|
514
|
+
skip "#{ENV['DB']} does not support word-based formatting for month and day names" if ['sqlite'].include?(ENV['DB'])
|
515
|
+
|
516
|
+
# the begin-rescue block is here to make sure we set the db back to en_US
|
517
|
+
# if we fail, so that other tests don't get contaminated.
|
518
|
+
#
|
519
|
+
# Tests should assert one single thing in principle, but until we
|
520
|
+
# refactor this whole thing, we'll have to do tricks of this sort.
|
521
|
+
begin
|
522
|
+
switch_to_lang(:en)
|
523
|
+
case ENV['DB']
|
524
|
+
when 'mysql', 'postgresql'
|
525
|
+
assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
526
|
+
assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
527
|
+
when 'mssql'
|
528
|
+
assert_equal 'Monday, 03 March 2014', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
529
|
+
end
|
530
|
+
switch_to_lang(:fr)
|
531
|
+
case ENV['DB']
|
532
|
+
when 'mysql'
|
533
|
+
assert_equal 'lun, 03 mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
534
|
+
assert_equal 'lundi, 03 mars 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
535
|
+
when 'postgresql'
|
536
|
+
assert_equal 'Lun., 03 Mars 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
537
|
+
assert_equal 'Lundi, 03 Mars 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
538
|
+
when 'mssql'
|
539
|
+
assert_equal 'lundi, 03 mars 2014', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
540
|
+
end
|
541
|
+
ensure
|
542
|
+
switch_to_lang(:en)
|
438
543
|
end
|
439
544
|
end
|
440
545
|
|
441
546
|
def test_coalesce
|
442
|
-
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil,
|
547
|
+
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, 'default') + ' concat')
|
443
548
|
|
444
|
-
assert_equal 'toto', t(@test, @other.coalesce(
|
549
|
+
assert_equal 'toto', t(@test, @other.coalesce(''))
|
445
550
|
|
446
|
-
assert_equal ' ', t(@myung, @comments.coalesce(
|
447
|
-
assert_equal 'Laure', t(@laure, @comments.coalesce(
|
551
|
+
assert_equal ' ', t(@myung, @comments.coalesce('Myung').coalesce('ignored'))
|
552
|
+
assert_equal 'Laure', t(@laure, @comments.coalesce('Laure'))
|
448
553
|
if @env_db == 'oracle'
|
449
|
-
assert_nil t(@laure, @comments.coalesce(
|
450
|
-
assert_nil t(@camille, @other.coalesce(
|
554
|
+
assert_nil t(@laure, @comments.coalesce(''))
|
555
|
+
assert_nil t(@camille, @other.coalesce(''))
|
451
556
|
else
|
452
|
-
assert_equal('', t(@laure, @comments.coalesce(
|
453
|
-
assert_equal '', t(@camille, @other.coalesce(
|
557
|
+
assert_equal('', t(@laure, @comments.coalesce('')))
|
558
|
+
assert_equal '', t(@camille, @other.coalesce(''))
|
454
559
|
end
|
455
560
|
assert_equal 100, t(@test, @age.coalesce(100))
|
456
|
-
assert_equal
|
561
|
+
assert_equal 'Camille', t(@camille, @name.coalesce(nil, 'default'))
|
457
562
|
assert_equal 20, t(@test, @age.coalesce(nil, 20))
|
458
563
|
|
459
|
-
assert_equal 20, t(@test, @age.coalesce(10)+10)
|
460
|
-
assert_equal 'Laure10', t(@laure, @comments.coalesce(
|
564
|
+
assert_equal 20, t(@test, @age.coalesce(10) + 10)
|
565
|
+
assert_equal 'Laure10', t(@laure, @comments.coalesce('Laure') + 10)
|
461
566
|
end
|
462
567
|
|
463
568
|
# Comparators
|
@@ -478,16 +583,16 @@ module ArelExtensions
|
|
478
583
|
def test_date_duration
|
479
584
|
# Year
|
480
585
|
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
481
|
-
assert_equal 0, User.where(@created_at.year.eq(
|
586
|
+
assert_equal 0, User.where(@created_at.year.eq('2012')).count
|
482
587
|
# Month
|
483
588
|
assert_equal 5, t(@camille, @created_at.month).to_i
|
484
|
-
assert_equal 9, User.where(@created_at.month.eq(
|
589
|
+
assert_equal 9, User.where(@created_at.month.eq('05')).count
|
485
590
|
# Week
|
486
591
|
assert_equal(@env_db == 'mssql' ? 22 : 21, t(@arthur, @created_at.week).to_i)
|
487
|
-
assert_equal 9, User.where(@created_at.month.eq(
|
592
|
+
assert_equal 9, User.where(@created_at.month.eq('05')).count
|
488
593
|
# Day
|
489
594
|
assert_equal 23, t(@laure, @created_at.day).to_i
|
490
|
-
assert_equal 0, User.where(@created_at.day.eq(
|
595
|
+
assert_equal 0, User.where(@created_at.day.eq('05')).count
|
491
596
|
|
492
597
|
# skip "manage DATE" if @env_db == 'oracle'
|
493
598
|
# Hour
|
@@ -504,8 +609,8 @@ module ArelExtensions
|
|
504
609
|
def test_datetime_diff
|
505
610
|
assert_equal 0, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 42)).to_i
|
506
611
|
if @env_db == 'oracle' && Arel::VERSION.to_i > 6 # in rails 5, result is multiplied by 24*60*60 = 86400...
|
507
|
-
assert_equal 42 *
|
508
|
-
assert_equal(-3600 *
|
612
|
+
assert_equal 42 * 86_400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
|
613
|
+
assert_equal(-3600 * 86_400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
|
509
614
|
else
|
510
615
|
assert_equal 42, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
|
511
616
|
assert_equal(-3600, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
|
@@ -517,7 +622,7 @@ module ArelExtensions
|
|
517
622
|
end
|
518
623
|
|
519
624
|
|
520
|
-
skip
|
625
|
+
skip 'not yet implemented' if $sqlite
|
521
626
|
|
522
627
|
date1 = Date.new(2016, 5, 23)
|
523
628
|
durPos = 10.years
|
@@ -530,27 +635,27 @@ module ArelExtensions
|
|
530
635
|
datetime1 = Time.utc(2014, 3, 3, 12, 42, 0)
|
531
636
|
# Pull Request #5 tests
|
532
637
|
# puts (@created_at + durPos).cast(:date).to_sql
|
533
|
-
assert_includes [date2,
|
534
|
-
assert_includes [date3,
|
638
|
+
assert_includes [date2, '2026-05-23'], t(@test, (@created_at + durPos).cast(:date))
|
639
|
+
assert_includes [date3, '2006-05-23'], t(@test, (@created_at + durNeg).cast(:date))
|
535
640
|
|
536
641
|
# puts (@created_at + @created_at.day).cast(:date).to_sql
|
537
|
-
assert_includes [date4,
|
642
|
+
assert_includes [date4, '2016-06-15'], t(@test, (@created_at + @created_at.day).cast(:date))
|
538
643
|
# puts (@created_at - @created_at.day).cast(:date).to_sql
|
539
|
-
assert_includes [date5,
|
644
|
+
assert_includes [date5, '2016-04-30'], t(@test, (@created_at - @created_at.day).cast(:date))
|
540
645
|
|
541
|
-
assert_includes [datetime1 + 42.seconds,
|
542
|
-
assert_includes [datetime1 - 42.seconds,
|
646
|
+
assert_includes [datetime1 + 42.seconds, '2014-03-03 12:42:42 UTC'], t(@lucas, (@updated_at + @updated_at.minute))
|
647
|
+
assert_includes [datetime1 - 42.seconds, '2014-03-03 12:41:18 UTC'], t(@lucas, (@updated_at - @updated_at.minute))
|
543
648
|
|
544
649
|
# (@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))).to_sql
|
545
|
-
assert_includes [datetime1 + (12*60+42).seconds,
|
546
|
-
t(@lucas,(@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))))
|
650
|
+
assert_includes [datetime1 + (12 * 60 + 42).seconds, '2014-03-03 12:54:42 UTC'],
|
651
|
+
t(@lucas, (@updated_at + Arel.duration('s', (@updated_at.hour * 60 + @updated_at.minute))))
|
547
652
|
|
548
|
-
assert_includes [datetime1 + (12*60+42).minutes,
|
549
|
-
t(@lucas,(@updated_at + Arel.duration('mn',(@updated_at.hour*60 + @updated_at.minute))))
|
653
|
+
assert_includes [datetime1 + (12 * 60 + 42).minutes, '2014-03-04 01:24:00 UTC'],
|
654
|
+
t(@lucas, (@updated_at + Arel.duration('mn', (@updated_at.hour * 60 + @updated_at.minute))))
|
550
655
|
|
551
|
-
assert_includes [
|
656
|
+
assert_includes ['2024-03-03'], t(@lucas, (@updated_at + durPos).format('%Y-%m-%d'))
|
552
657
|
# puts (@updated_at - durPos).to_sql
|
553
|
-
assert_includes [
|
658
|
+
assert_includes ['2004-03-03'], t(@lucas, (@updated_at - durPos).format('%Y-%m-%d'))
|
554
659
|
|
555
660
|
|
556
661
|
# we test with the ruby object or the string because some adapters don't return an object Date
|
@@ -559,29 +664,29 @@ module ArelExtensions
|
|
559
664
|
|
560
665
|
# TODO; cast types
|
561
666
|
def test_cast_types
|
562
|
-
assert_equal
|
563
|
-
skip
|
667
|
+
assert_equal '5', t(@lucas, @age.cast(:string))
|
668
|
+
skip 'jdbc adapters does not work properly here (v52 works fine)' if RUBY_PLATFORM.match?(/java/i)
|
564
669
|
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
|
565
|
-
assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).cast(:string).eq(
|
566
|
-
assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).eq(
|
567
|
-
assert_equal
|
568
|
-
assert_equal
|
569
|
-
assert_equal 21.16, t(@laure
|
570
|
-
assert_equal 21, t(@laure
|
670
|
+
assert_equal 1, t(@laure, Arel.when(@duration.cast(:time).cast(:string).eq('12:42:21')).then(1).else(0)) unless @env_db == 'oracle' || @env_db == 'mssql'
|
671
|
+
assert_equal 1, t(@laure, Arel.when(@duration.cast(:time).eq('12:42:21')).then(1).else(0)) unless @env_db == 'oracle'
|
672
|
+
assert_equal '20.16', t(@laure, @score.cast(:string)).gsub(/0*\z/, '')
|
673
|
+
assert_equal '20.161', t(@laure, @score.cast(:string) + 1).gsub(/0*1\z/, '1')
|
674
|
+
assert_equal 21.16, t(@laure, @score.cast(:string).cast(:decimal) + 1)
|
675
|
+
assert_equal 21, t(@laure, @score.cast(:string).cast(:int) + 1)
|
571
676
|
|
572
|
-
assert_equal String, t(@lucas
|
677
|
+
assert_equal String, t(@lucas, @updated_at.cast(:string)).class
|
573
678
|
|
574
|
-
assert_equal Date, t(@lucas
|
575
|
-
assert_equal Time, t(@lucas
|
576
|
-
assert_equal Time, t(@lucas
|
679
|
+
assert_equal Date, t(@lucas, @updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
|
680
|
+
assert_equal Time, t(@lucas, @updated_at.cast(:string).cast(:datetime)).class
|
681
|
+
assert_equal Time, t(@lucas, @updated_at.cast(:time)).class
|
577
682
|
|
578
683
|
# mysql adapter in rails7 adds some infos we just squeeze here
|
579
|
-
assert_equal
|
580
|
-
assert_equal Date.parse(
|
581
|
-
assert_equal Date.parse(
|
582
|
-
assert_equal Time.parse(
|
583
|
-
assert_equal Date.parse(
|
584
|
-
assert_equal
|
684
|
+
assert_equal '2014-03-03 12:42:00', t(@lucas, @updated_at.cast(:string)).split('.').first unless @env_db == 'mssql' # locale dependent
|
685
|
+
assert_equal Date.parse('2014-03-03'), t(@lucas, Arel.quoted('2014-03-03').cast(:date))
|
686
|
+
assert_equal Date.parse('5014-03-03'), t(@lucas, (@age.cast(:string) + '014-03-03').cast(:date))
|
687
|
+
assert_equal Time.parse('2014-03-03 12:42:00 UTC'), t(@lucas, @updated_at.cast(:string).cast(:datetime))
|
688
|
+
assert_equal Date.parse('2014-03-03'), t(@lucas, @updated_at.cast(:date))
|
689
|
+
assert_equal '12:42:00', t(@lucas, @updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' # DateTime
|
585
690
|
end
|
586
691
|
end
|
587
692
|
|
@@ -591,42 +696,42 @@ module ArelExtensions
|
|
591
696
|
# puts @age.is_null.inspect
|
592
697
|
# puts @age.is_null.to_sql
|
593
698
|
# puts @age=='34'
|
594
|
-
assert_equal
|
699
|
+
assert_equal 'Test', User.select(@name).where(@age.is_null).first.name
|
595
700
|
end
|
596
701
|
|
597
702
|
def test_math_plus
|
598
703
|
d = Date.new(1997, 6, 15)
|
599
704
|
# Concat String
|
600
|
-
assert_equal
|
601
|
-
assert_equal
|
602
|
-
assert_equal
|
603
|
-
assert_equal
|
604
|
-
assert_equal
|
605
|
-
assert_equal
|
606
|
-
assert_equal
|
607
|
-
assert_equal
|
705
|
+
assert_equal 'SophiePhan', t(@sophie, @name + 'Phan')
|
706
|
+
assert_equal 'Sophie2', t(@sophie, @name + 2)
|
707
|
+
assert_equal 'Sophie1997-06-15', t(@sophie, @name + d)
|
708
|
+
assert_equal 'Sophie15', t(@sophie, @name + @age)
|
709
|
+
assert_equal 'SophieSophie', t(@sophie, @name + @name)
|
710
|
+
assert_equal 'SophieSophieSophie', t(@sophie, @name + @name + @name)
|
711
|
+
assert_equal 'SophieSophieSophie', t(@sophie, @name.concat(@name.concat(@name)))
|
712
|
+
assert_equal 'SophieSophieSophie', t(@sophie, @name.concat(@name).concat(@name))
|
608
713
|
# FIXME: should work as expected in Oracle
|
609
|
-
assert_equal
|
714
|
+
assert_equal 'Sophie2016-05-23', t(@sophie, @name + @created_at) unless @env_db == 'oracle'
|
610
715
|
# concat Integer
|
611
716
|
assert_equal 1, User.where((@age + 10).eq(33)).count
|
612
|
-
assert_equal 1, User.where((@age +
|
717
|
+
assert_equal 1, User.where((@age + '1').eq(6)).count
|
613
718
|
assert_equal 1, User.where((@age + @age).eq(10)).count
|
614
719
|
# concat Date
|
615
720
|
# puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
616
|
-
assert_equal
|
617
|
-
assert_equal
|
721
|
+
assert_equal '2016-05-24', t(@myung, @created_at + 1).to_date.to_s
|
722
|
+
assert_equal '2016-05-25', t(@myung, @created_at + 2.day).to_date.to_s
|
618
723
|
end
|
619
724
|
|
620
725
|
def test_math_minus
|
621
726
|
d = Date.new(2016, 5, 20)
|
622
727
|
# Datediff
|
623
728
|
assert_equal 9, User.where((@created_at - @created_at).eq(0)).count
|
624
|
-
assert_equal 3, @laure.select((@created_at - d).as(
|
729
|
+
assert_equal 3, @laure.select((@created_at - d).as('res')).first.res.abs.to_i
|
625
730
|
# Substraction
|
626
731
|
assert_equal 0, User.where((@age - 10).eq(50)).count
|
627
|
-
assert_equal 0, User.where((@age -
|
732
|
+
assert_equal 0, User.where((@age - '10').eq(50)).count
|
628
733
|
# assert_equal 0, User.where((@age - 9.5).eq(50.5)).count # should work: TODO
|
629
|
-
assert_equal 0, User.where((@age -
|
734
|
+
assert_equal 0, User.where((@age - '9.5').eq(50.5)).count
|
630
735
|
end
|
631
736
|
|
632
737
|
def test_wday
|
@@ -649,8 +754,8 @@ module ArelExtensions
|
|
649
754
|
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
|
650
755
|
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
|
651
756
|
|
652
|
-
assert_equal 3, User.find_by_sql(
|
653
|
-
assert_equal 3, User.find_by_sql(
|
757
|
+
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
|
758
|
+
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
|
654
759
|
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
|
655
760
|
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
|
656
761
|
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
|
@@ -662,117 +767,117 @@ module ArelExtensions
|
|
662
767
|
def test_case
|
663
768
|
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)
|
664
769
|
assert_equal 4, User.where(@score.when(20.16).then(1).else(0).eq(1)).count
|
665
|
-
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
666
|
-
assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
|
667
|
-
assert_equal
|
668
|
-
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
|
669
|
-
assert_equal
|
770
|
+
assert_equal 2, t(@arthur, @score.when(65.62, 1).else(0) + 1)
|
771
|
+
assert_equal 0, t(@arthur, @score.when(65.62, 1).else(0) - 1)
|
772
|
+
assert_equal '11', t(@arthur, @score.when(65.62).then('1').else('0') + '1')
|
773
|
+
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score) + 1)
|
774
|
+
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
|
670
775
|
end
|
671
776
|
|
672
777
|
def test_format_numbers
|
673
778
|
# score of Arthur = 65.62
|
674
|
-
skip
|
675
|
-
|
676
|
-
assert_equal
|
677
|
-
assert_equal
|
678
|
-
assert_equal
|
679
|
-
assert_equal
|
680
|
-
assert_equal
|
681
|
-
assert_equal
|
682
|
-
assert_equal
|
683
|
-
assert_equal
|
684
|
-
assert_equal
|
685
|
-
assert_equal
|
686
|
-
assert_equal
|
687
|
-
assert_equal
|
688
|
-
assert_includes [
|
689
|
-
assert_includes [
|
690
|
-
assert_includes [
|
691
|
-
assert_equal
|
692
|
-
assert_equal
|
693
|
-
assert_equal
|
694
|
-
assert_equal
|
695
|
-
assert_equal
|
696
|
-
assert_equal
|
779
|
+
skip ' Works with SQLite if the version used knows printf' if $sqlite
|
780
|
+
|
781
|
+
assert_equal 'Wrong Format', t(@arthur, @score.format_number('$ %...234.6F €', 'fr_FR'))
|
782
|
+
assert_equal 'AZERTY65,62', t(@arthur, @score.format_number('AZERTY%.2f', 'fr_FR'))
|
783
|
+
assert_equal '65,62AZERTY', t(@arthur, @score.format_number('%.2fAZERTY', 'fr_FR'))
|
784
|
+
assert_equal '$ 65.62 €', t(@arthur, @score.format_number('$ %.2f €', 'en_US'))
|
785
|
+
assert_equal '$ 66 €', t(@arthur, @score.format_number('$ %.0f €', 'en_US'))
|
786
|
+
assert_equal '$ 0065,62 €', t(@arthur, @score.format_number('$ %07.2f €', 'fr_FR'))
|
787
|
+
assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ %-07.2f €', 'fr_FR'))
|
788
|
+
assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ %-7.2f €', 'fr_FR'))
|
789
|
+
assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ % 7.2f €', 'fr_FR'))
|
790
|
+
assert_equal '$ 65,6 €', t(@arthur, @score.format_number('$ % 7.1f €', 'fr_FR'))
|
791
|
+
assert_equal '$ +65,62 €', t(@arthur, @score.format_number('$ % +7.2f €', 'fr_FR'))
|
792
|
+
assert_equal '$ +065,62 €', t(@arthur, @score.format_number('$ %0+7.2f €', 'fr_FR'))
|
793
|
+
assert_includes ['$ 6,56e1 €', '$ 6,56e+01 €'], t(@arthur, @score.format_number('$ %.2e €', 'fr_FR'))
|
794
|
+
assert_includes ['$ 6,56E1 €', '$ 6,56E+01 €'], t(@arthur, @score.format_number('$ %.2E €', 'fr_FR'))
|
795
|
+
assert_includes ['$ 6,562E1 €', '$ 6,562E+01 €'], t(@arthur, @score.format_number('$ %.3E €', 'fr_FR'))
|
796
|
+
assert_equal '123 456 765,6', t(@arthur, (@score + 123_456_700).format_number('%.1f', 'sv_SE')).tr("\u00A0", ' ') # some DBMS put no-break space here (it makes sense thus)
|
797
|
+
assert_equal '123456765,6', t(@arthur, (@score + 123_456_700).format_number('%.1f', 'fr_FR')).delete("\u00A0") # because SqlServer does it like no one else
|
798
|
+
assert_equal '123,456,765.6', t(@arthur, (@score + 123_456_700).format_number('%.1f', 'en_US'))
|
799
|
+
assert_equal ' 123,456,765.6', t(@arthur, (@score + 123_456_700).format_number('%16.1f', 'en_US'))
|
800
|
+
assert_equal '$ 0,00 €', t(@arthur, @score.when(65.62).then(Arel.sql('null')).else(1).format_number('$ %.2f €', 'fr_FR'))
|
801
|
+
assert_equal '$ 0,00 €', t(@arthur, (@score - 65.62).format_number('$ %.2f €', 'fr_FR'))
|
697
802
|
end
|
698
803
|
|
699
804
|
def test_accent_insensitive
|
700
|
-
skip
|
701
|
-
skip
|
805
|
+
skip 'SQLite is natively Case Insensitive and Accent Sensitive' if $sqlite
|
806
|
+
skip 'Not finished' if @env_db == 'mysql'
|
702
807
|
# actual comments value: "arrêté"
|
703
808
|
# AI & CI
|
704
809
|
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
705
|
-
assert_equal
|
706
|
-
assert_equal
|
707
|
-
assert_equal
|
708
|
-
assert_equal
|
709
|
-
assert_equal
|
710
|
-
assert_equal
|
810
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('arrêté')).then('1').else('0'))
|
811
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('arrete')).then('1').else('0'))
|
812
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('àrrétè')).then('1').else('0'))
|
813
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_imatches('arretez')).then('1').else('0'))
|
814
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('Arrete')).then('1').else('0'))
|
815
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('Arrêté')).then('1').else('0'))
|
711
816
|
# AI & CS
|
712
|
-
assert_equal
|
713
|
-
assert_equal
|
714
|
-
assert_equal
|
715
|
-
assert_equal
|
716
|
-
if
|
717
|
-
assert_equal
|
718
|
-
assert_equal
|
817
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('arrêté')).then('1').else('0'))
|
818
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('arrete')).then('1').else('0'))
|
819
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('àrrétè')).then('1').else('0'))
|
820
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('arretez')).then('1').else('0'))
|
821
|
+
if !%w[oracle postgresql mysql].include?(@env_db) # AI => CI
|
822
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('Arrete')).then('1').else('0'))
|
823
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('Arrêté')).then('1').else('0'))
|
719
824
|
end
|
720
825
|
end
|
721
826
|
# AS & CI
|
722
|
-
assert_equal
|
827
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.imatches('arrêté')).then('1').else('0'))
|
723
828
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
724
|
-
assert_equal
|
725
|
-
assert_equal
|
829
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arrete')).then('1').else('0'))
|
830
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('àrrétè')).then('1').else('0'))
|
726
831
|
end
|
727
|
-
assert_equal
|
832
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arretez')).then('1').else('0'))
|
728
833
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
729
|
-
assert_equal
|
834
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('Arrete')).then('1').else('0'))
|
730
835
|
end
|
731
|
-
assert_equal
|
836
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.imatches('Arrêté')).then('1').else('0'))
|
732
837
|
# AS & CS
|
733
|
-
assert_equal
|
734
|
-
assert_equal
|
735
|
-
assert_equal
|
736
|
-
assert_equal
|
737
|
-
assert_equal
|
738
|
-
assert_equal
|
838
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.smatches('arrêté')).then('1').else('0'))
|
839
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('arrete')).then('1').else('0'))
|
840
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('àrrétè')).then('1').else('0'))
|
841
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('arretez')).then('1').else('0'))
|
842
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('Arrete')).then('1').else('0'))
|
843
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('Arrêté')).then('1').else('0'))
|
739
844
|
end
|
740
845
|
|
741
846
|
def test_subquery_with_order
|
742
847
|
skip if ['mssql'].include?(@env_db) && Arel::VERSION.to_i < 10
|
743
|
-
assert_equal 9, User.where(:
|
848
|
+
assert_equal 9, User.where(name: User.select(:name).order(:name)).count
|
744
849
|
assert_equal 9, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
|
745
|
-
if !['mysql'].include?(@env_db)
|
746
|
-
assert_equal 2, User.where(:
|
747
|
-
# assert_equal 6, User.where(:
|
850
|
+
if !['mysql'].include?(@env_db) # MySql can't have limit in IN subquery
|
851
|
+
assert_equal 2, User.where(name: User.select(:name).order(:name).limit(2)).count
|
852
|
+
# assert_equal 6, User.where(name: User.select(:name).order(:name).offset(2)).count
|
748
853
|
end
|
749
854
|
end
|
750
855
|
|
751
856
|
def test_in_with_nil
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
857
|
+
assert_predicate @myung.where(@age.in(1)), :blank?
|
858
|
+
assert_predicate @myung.where(@age.in(23)), :present?
|
859
|
+
assert_predicate @myung.where(@age.in([1])), :blank?
|
860
|
+
assert_predicate @myung.where(@age.in([1, 2])), :blank?
|
861
|
+
assert_predicate @myung.where(@age.in([1, 23])), :present?
|
862
|
+
assert_predicate @myung.where(@age.in(nil)), :blank?
|
863
|
+
assert_predicate @myung.where(@age.in([nil])), :blank?
|
864
|
+
assert_predicate @myung.where(@age.in([nil, 1])), :blank?
|
865
|
+
assert_predicate @myung.where(@age.in([nil, 23])), :present?
|
866
|
+
assert_predicate @myung.where(@age.in([nil, 1, 2])), :blank?
|
867
|
+
assert_predicate @myung.where(@age.in([nil, 1, 23])), :present?
|
868
|
+
assert_predicate @test.where(@age.in(1)), :blank?
|
869
|
+
assert_predicate @test.where(@age.in([1])), :blank?
|
870
|
+
assert_predicate @test.where(@age.in([1, 2])), :blank?
|
871
|
+
assert_predicate @test.where(@age.in(nil)), :present?
|
872
|
+
assert_predicate @test.where(@age.in([nil])), :present?
|
873
|
+
assert_predicate @test.where(@age.in([nil, 1])), :present?
|
874
|
+
assert_predicate @test.where(@age.in([nil, 1, 2])), :present?
|
770
875
|
end
|
771
876
|
|
772
877
|
def test_scope_with_in_plus_new
|
773
878
|
begin
|
774
|
-
@test.where(@age.in([1,2])).new
|
775
|
-
@test.where(@age.not_in([1,2])).new
|
879
|
+
@test.where(@age.in([1, 2])).new
|
880
|
+
@test.where(@age.not_in([1, 2])).new
|
776
881
|
assert true
|
777
882
|
rescue
|
778
883
|
assert false
|
@@ -780,44 +885,44 @@ module ArelExtensions
|
|
780
885
|
end
|
781
886
|
|
782
887
|
def test_is_not_null
|
783
|
-
|
784
|
-
|
888
|
+
assert_predicate @myung.where(@age.is_not_null), :present?
|
889
|
+
assert_predicate @test.where(@age.is_not_null), :blank?
|
785
890
|
end
|
786
891
|
|
787
892
|
def test_not_in_with_nil
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
893
|
+
assert_predicate @myung.where(@age.not_in(1)), :present?
|
894
|
+
assert_predicate @myung.where(@age.not_in(23)), :blank?
|
895
|
+
assert_predicate @myung.where(@age.not_in([1])), :present?
|
896
|
+
assert_predicate @myung.where(@age.not_in([1, 2])), :present?
|
897
|
+
assert_predicate @myung.where(@age.not_in([1, 23])), :blank?
|
898
|
+
assert_predicate @myung.where(@age.not_in(nil)), :present?
|
899
|
+
assert_predicate @myung.where(@age.not_in([nil])), :present?
|
900
|
+
assert_predicate @myung.where(@age.not_in([nil, 1])), :present?
|
901
|
+
assert_predicate @myung.where(@age.not_in([nil, 23])), :blank?
|
902
|
+
assert_predicate @myung.where(@age.not_in([nil, 1, 2])), :present?
|
903
|
+
assert_predicate @myung.where(@age.not_in([nil, 1, 23])), :blank?
|
904
|
+
|
905
|
+
assert_predicate @myung.where(@age.not_in(1..2)), :present?
|
801
906
|
|
802
907
|
# if the column is null, the entry will never be selected with not in (like every DBMS does)
|
803
|
-
#
|
804
|
-
#
|
805
|
-
#
|
806
|
-
#
|
807
|
-
#
|
808
|
-
#
|
809
|
-
#
|
908
|
+
# assert_predicate @test.where(@age.not_in(1)), :present?
|
909
|
+
# assert_predicate @test.where(@age.not_in([1])), :present?
|
910
|
+
# assert_predicate @test.where(@age.not_in([1,2])), :present?
|
911
|
+
# assert_predicate @test.where(@age.not_in(nil)), :blank?
|
912
|
+
# assert_predicate @test.where(@age.not_in([nil])), :blank?
|
913
|
+
# assert_predicate @test.where(@age.not_in([nil,1])), :blank?
|
914
|
+
# assert_predicate @test.where(@age.not_in([nil,1,2])), :blank?
|
810
915
|
end
|
811
916
|
|
812
917
|
def test_in_on_grouping
|
813
|
-
skip
|
814
|
-
assert_equal 2, User.where(Arel.tuple(@name
|
815
|
-
assert_equal 1, User.where(Arel.tuple(@name
|
816
|
-
assert_equal 0, User.where(Arel.tuple(@name
|
918
|
+
skip 'We should modify the visitor of IN to make it work' if $sqlite || @env_db == 'mssql'
|
919
|
+
assert_equal 2, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23), Arel.tuple('Arthur', 21))).count
|
920
|
+
assert_equal 1, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23))).count
|
921
|
+
assert_equal 0, User.where(Arel.tuple(@name, @age).in([])).count
|
817
922
|
end
|
818
923
|
|
819
924
|
def test_alias_shortened
|
820
|
-
if [
|
925
|
+
if %w[postgresql oracle].include?(@env_db)
|
821
926
|
new_alias = Arel.shorten('azerty' * 15)
|
822
927
|
at = User.arel_table.alias('azerty' * 15)
|
823
928
|
assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
|
@@ -840,64 +945,64 @@ module ArelExtensions
|
|
840
945
|
assert (479.82048 - t(User.where(nil), @score.variance(unbiased: false))).abs < 0.01
|
841
946
|
assert ( 23.23355 - t(User.where(nil), @score.std)).abs < 0.01
|
842
947
|
assert ( 21.90480 - t(User.where(nil), @score.std(unbiased: false))).abs < 0.01
|
843
|
-
skip
|
844
|
-
assert_equal 2, User.select(@score.std(group: Arel.when(@name >
|
845
|
-
assert_equal 2, User.select(@score.variance(group: Arel.when(@name >
|
846
|
-
assert_equal 2, User.select(@score.sum(group: Arel.when(@name >
|
847
|
-
assert_equal 2, User.select(@comments.group_concat(group: Arel.when(@name >
|
948
|
+
skip 'Not Yet Implemented' # if !['postgresql'].include?(@env_db)
|
949
|
+
assert_equal 2, User.select(@score.std(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
950
|
+
assert_equal 2, User.select(@score.variance(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
951
|
+
assert_equal 2, User.select(@score.sum(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
952
|
+
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
|
848
953
|
end
|
849
954
|
|
850
955
|
def test_levenshtein_distance
|
851
|
-
skip
|
852
|
-
assert_equal 0, t(@arthur
|
853
|
-
assert_equal 2, t(@arthur
|
854
|
-
assert_equal 1, t(@arthur
|
956
|
+
skip 'Not Yet Implemented' if $sqlite
|
957
|
+
assert_equal 0, t(@arthur, @name.levenshtein_distance('Arthur'))
|
958
|
+
assert_equal 2, t(@arthur, @name.levenshtein_distance('Artoor'))
|
959
|
+
assert_equal 1, t(@arthur, @name.levenshtein_distance('Artehur'))
|
855
960
|
end
|
856
961
|
|
857
962
|
def test_json
|
858
963
|
skip "Can't be tested on travis"
|
859
964
|
# creation
|
860
|
-
assert_equal 'Arthur', t(@arthur,Arel.json(@name))
|
861
|
-
assert_equal [
|
862
|
-
assert_equal ({
|
863
|
-
assert_equal ({
|
864
|
-
assert_equal
|
965
|
+
assert_equal 'Arthur', t(@arthur, Arel.json(@name))
|
966
|
+
assert_equal %w[Arthur Arthur], parse_json(t(@arthur, Arel.json(@name, @name)))
|
967
|
+
assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 'ArthurArthur'}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => @name + @name})))
|
968
|
+
assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 1}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => 1})))
|
969
|
+
assert_equal [{'age' => 21}, {'name' => 'Arthur', 'score' => 65.62}], parse_json(t(@arthur, Arel.json([{age: @age}, {name: @name, score: @score}])))
|
865
970
|
|
866
971
|
# aggregate
|
867
|
-
assert_equal ({
|
868
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name}).group(false)))
|
869
|
-
assert_equal ({
|
870
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name
|
871
|
-
assert_equal
|
872
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score),Arel.json({@age => @name}).group(true,[@age])))
|
972
|
+
assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure'}),
|
973
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16), Arel.json({@age => @name}).group(false)))
|
974
|
+
assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure', 'Laure' => 25, 'Lucas' => 5, 'Myung' => 23, 'Sophie' => 15}),
|
975
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16), Arel.json({@age => @name, @name => @age}).group(false)))
|
976
|
+
assert_equal [{'5' => 'Lucas'}, {'15' => 'Sophie'}, {'23' => 'Myung'}, {'25' => 'Laure'}],
|
977
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score), Arel.json({@age => @name}).group(true, [@age])))
|
873
978
|
|
874
|
-
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_sql
|
875
|
-
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_a
|
979
|
+
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score, Arel.json({@age => @name}).group(true,[@age])).to_sql
|
980
|
+
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score, Arel.json({@age => @name}).group(true,[@age])).to_a
|
876
981
|
|
877
|
-
skip
|
982
|
+
skip 'Not Yet Implemented' if $sqlite || %w[oracle mssql].include?(@env_db)
|
878
983
|
# get
|
879
|
-
h1 = Arel.json({@name => @name
|
880
|
-
assert_equal
|
881
|
-
h2 = Arel.json([{age: @age},{name: @name,score: @score}])
|
882
|
-
assert_equal ({
|
883
|
-
assert_equal 21, parse_json(t(@arthur,h2.get(0).get('age')))
|
884
|
-
assert_nil t(@arthur,h2.get('age'))
|
984
|
+
h1 = Arel.json({@name => @name + @name, @name + '2' => 1})
|
985
|
+
assert_equal 'ArthurArthur', parse_json(t(@arthur, h1.get(@name)))
|
986
|
+
h2 = Arel.json([{age: @age}, {name: @name, score: @score}])
|
987
|
+
assert_equal ({'age' => 21}), parse_json(t(@arthur, h2.get(0)))
|
988
|
+
assert_equal 21, parse_json(t(@arthur, h2.get(0).get('age')))
|
989
|
+
assert_nil t(@arthur, h2.get('age'))
|
885
990
|
# set
|
886
|
-
assert_equal ({
|
887
|
-
assert_equal ({
|
888
|
-
assert_equal ({
|
889
|
-
assert_equal ({
|
991
|
+
assert_equal ({'Arthur' => %w[toto tata], 'Arthur2' => 1}), parse_json(t(@arthur, h1.set(@name, %w[toto tata])))
|
992
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.set(@name + '3', 2)))
|
993
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => nil}), parse_json(t(@arthur, h1.set(@name + '3', nil)))
|
994
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => {'a' => 2}}), parse_json(t(@arthur, h1.set(@name + '3', {a: 2})))
|
890
995
|
# merge
|
891
|
-
assert_equal ({
|
892
|
-
assert_equal ({
|
893
|
-
assert_equal ({
|
996
|
+
assert_equal ({'Arthur' => %w[toto tata], 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.merge({@name => %w[toto tata]}, {@name + '3' => 2})))
|
997
|
+
assert_equal ({'Arthur' => %w[toto tata], 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.merge({@name => %w[toto tata], @name + '3' => 2})))
|
998
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1}), parse_json(t(@arthur, h1.merge({})))
|
894
999
|
end
|
895
1000
|
|
896
1001
|
def test_as_on_everything
|
897
1002
|
name = @arthur.select(@name.as('NaMe')).first.attributes
|
898
|
-
assert_equal 'Arthur', name[
|
899
|
-
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes[
|
900
|
-
assert_equal 'ArthurArthur', @arthur.select((@name
|
1003
|
+
assert_equal 'Arthur', name['NaMe'] || name['name'] # because of Oracle
|
1004
|
+
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes['Na Me']
|
1005
|
+
assert_equal 'ArthurArthur', @arthur.select((@name + @name).as('Na-Me')).first.attributes['Na-Me']
|
901
1006
|
end
|
902
1007
|
|
903
1008
|
def test_exists_in_subquery
|