arel_extensions 2.1.1 → 2.1.4
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 +4 -4
- data/.rubocop.yml +25 -1
- data/Gemfile +10 -10
- data/README.md +24 -4
- data/Rakefile +4 -4
- data/TODO +0 -1
- data/arel_extensions.gemspec +11 -11
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +13 -13
- data/gemfiles/rails5_0.gemfile +12 -12
- data/gemfiles/rails5_1_4.gemfile +12 -12
- data/gemfiles/rails5_2.gemfile +13 -13
- data/gemfiles/rails6.gemfile +11 -11
- data/gemfiles/rails6_1.gemfile +11 -11
- data/gemfiles/rails7.gemfile +7 -7
- 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 +10 -10
- data/lib/arel_extensions/comparators.rb +14 -14
- data/lib/arel_extensions/date_duration.rb +7 -7
- data/lib/arel_extensions/helpers.rb +16 -13
- data/lib/arel_extensions/insert_manager.rb +1 -1
- data/lib/arel_extensions/math.rb +16 -18
- 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 +3 -3
- 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 +0 -0
- 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 +6 -6
- 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 +1 -1
- 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 +0 -0
- 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 +0 -0
- 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 +0 -0
- data/lib/arel_extensions/nodes/sum.rb +0 -0
- data/lib/arel_extensions/nodes/then.rb +0 -0
- data/lib/arel_extensions/nodes/trim.rb +0 -0
- 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 +19 -19
- data/lib/arel_extensions/visitors/mssql.rb +139 -118
- data/lib/arel_extensions/visitors/mysql.rb +145 -144
- data/lib/arel_extensions/visitors/oracle.rb +136 -128
- data/lib/arel_extensions/visitors/oracle12.rb +16 -16
- data/lib/arel_extensions/visitors/postgresql.rb +147 -136
- data/lib/arel_extensions/visitors/sqlite.rb +83 -85
- data/lib/arel_extensions/visitors/to_sql.rb +143 -148
- data/lib/arel_extensions/visitors.rb +7 -7
- data/lib/arel_extensions.rb +46 -32
- data/test/arelx_test_helper.rb +14 -14
- data/test/database.yml +5 -5
- data/test/real_db_test.rb +78 -78
- data/test/support/fake_record.rb +1 -1
- data/test/test_comparators.rb +5 -5
- data/test/visitors/test_bulk_insert_oracle.rb +4 -4
- data/test/visitors/test_bulk_insert_sqlite.rb +4 -4
- data/test/visitors/test_bulk_insert_to_sql.rb +4 -4
- data/test/visitors/test_oracle.rb +14 -14
- data/test/visitors/test_to_sql.rb +82 -82
- data/test/with_ar/all_agnostic_test.rb +415 -313
- data/test/with_ar/insert_agnostic_test.rb +17 -14
- data/test/with_ar/test_bulk_sqlite.rb +4 -4
- 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 +2 -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: ['Lucas', 'Sophie']), @name.group_concat(' '))
|
197
|
+
assert_equal 'Lucas,Sophie', t(User.where(name: ['Lucas', 'Sophie']), @name.group_concat(','))
|
198
|
+
assert_equal 'Lucas,Sophie', t(User.where(name: ['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: ['Lucas', 'Sophie', 'Arthur']), @name.group_concat(',', @name.asc))
|
202
|
+
assert_equal 'Sophie,Lucas,Arthur', t(User.where(name: ['Lucas', 'Sophie', 'Arthur']), @name.group_concat(',', @name.desc))
|
203
|
+
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: ['Lucas', 'Sophie', 'Arthur']), @name.group_concat(',', [@score.asc, @name.asc]))
|
204
|
+
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: ['Lucas', 'Sophie', 'Arthur']), @name.group_concat(',', @score.asc, @name.asc))
|
205
|
+
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: ['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
|
@@ -403,57 +411,151 @@ module ArelExtensions
|
|
403
411
|
'tahiti' => 'Hawaiian Standard Time',
|
404
412
|
'paris' => 'Central European Standard Time'
|
405
413
|
},
|
406
|
-
'
|
414
|
+
'posix' => {
|
407
415
|
'utc' => 'UTC',
|
408
416
|
'sao_paulo' => 'America/Sao_Paulo',
|
409
417
|
'tahiti' => 'Pacific/Tahiti',
|
410
418
|
'paris' => 'Europe/Paris'
|
411
|
-
}
|
412
|
-
'oracle' => {
|
413
|
-
'utc' => 'UTC',
|
414
|
-
'sao_paulo' => 'America/Sao_Paulo',
|
415
|
-
'tahiti' => 'Pacific/Tahiti',
|
416
|
-
'paris' => 'Europe/Paris'
|
417
|
-
},
|
418
|
-
'postgresql' => {
|
419
|
-
'utc' => 'UTC',
|
420
|
-
'sao_paulo' => 'America/Sao Paulo (-03)',
|
421
|
-
'tahiti' => 'Pacific/Tahiti (-10)',
|
422
|
-
'paris' => 'Europe/Paris'
|
423
|
-
},
|
419
|
+
}
|
424
420
|
}
|
425
421
|
|
426
|
-
|
427
|
-
|
422
|
+
skip "Unsupported timezone conversion for DB=#{ENV['DB']}" if !['mssql', 'mysql', 'oracle', 'postgresql'].include?(ENV['DB'])
|
423
|
+
|
424
|
+
tz = ENV['DB'] == 'mssql' ? time_zones['mssql'] : time_zones['posix']
|
425
|
+
|
428
426
|
assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['utc']))
|
429
|
-
assert_equal '2014/03/03 09:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['sao_paulo']))
|
430
|
-
assert_equal '2014/03/03 02:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['tahiti']))
|
427
|
+
assert_equal '2014/03/03 09:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', { tz['utc'] => tz['sao_paulo'] }))
|
428
|
+
assert_equal '2014/03/03 02:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', { tz['utc'] => tz['tahiti'] }))
|
429
|
+
|
430
|
+
# Skipping conversion from UTC to the desired timezones fails in SQL
|
431
|
+
# Server and Postgres. This is mainly due to the fact that timezone
|
432
|
+
# information is not preserved in the column itself.
|
433
|
+
#
|
434
|
+
# MySQL is happy to consider that times by default are in UTC.
|
435
|
+
assert_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', { tz['utc'] => tz['paris'] }))
|
436
|
+
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'])
|
431
437
|
|
432
438
|
# Winter/Summer time
|
433
|
-
assert_equal '
|
434
|
-
|
439
|
+
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'] }))
|
440
|
+
if ENV['DB'] == 'mssql'
|
441
|
+
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'] }))
|
442
|
+
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'] }))
|
443
|
+
else
|
444
|
+
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']))
|
445
|
+
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']))
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
def test_format_iso_week
|
450
|
+
skip "Unsupported ISO week number for DB=#{ENV['DB']}" if ['sqlite'].include?(ENV['DB'])
|
451
|
+
assert_equal '10', t(@lucas, @updated_at.format('%V'))
|
452
|
+
{
|
453
|
+
'2024-01-01 10:42:00' => '01', # Monday
|
454
|
+
'2030-01-01 10:42:00' => '01', # Tuesday
|
455
|
+
'2025-01-01 10:42:00' => '01', # Wednesday
|
456
|
+
'2026-01-01 10:42:00' => '01', # Thursday
|
457
|
+
'2027-01-01 10:42:00' => '53', # Friday
|
458
|
+
'2028-01-01 10:42:00' => '52', # Saturday
|
459
|
+
'2034-01-01 10:42:00' => '52', # Sunday
|
460
|
+
}.each do |date, exp|
|
461
|
+
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%V'))
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_format_iso_year_of_week
|
466
|
+
skip "Unsupported ISO year of week for DB=#{ENV['DB']}" if ['mssql', 'sqlite'].include?(ENV['DB'])
|
467
|
+
assert_equal '2014', t(@lucas, @updated_at.format('%G'))
|
468
|
+
|
469
|
+
{
|
470
|
+
'2024-01-01 10:42:00' => '2024', # Monday
|
471
|
+
'2030-01-01 10:42:00' => '2030', # Tuesday
|
472
|
+
'2025-01-01 10:42:00' => '2025', # Wednesday
|
473
|
+
'2026-01-01 10:42:00' => '2026', # Thursday
|
474
|
+
'2027-01-01 10:42:00' => '2026', # Friday
|
475
|
+
'2028-01-01 10:42:00' => '2027', # Saturday
|
476
|
+
'2034-01-01 10:42:00' => '2033', # Sunday
|
477
|
+
}.each do |date, exp|
|
478
|
+
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%G'))
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_format_date_with_names
|
483
|
+
skip "#{ENV['DB']} does not support a variety of word-based formatting for month and day names" if ['mssql'].include?(ENV['DB'])
|
484
|
+
assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
485
|
+
assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
486
|
+
|
487
|
+
skip "#{ENV['DB']} does not support ALLCAPS month and day names" if ['mysql'].include?(ENV['DB'])
|
488
|
+
assert_equal 'Mon, 03 MAR 14', t(@lucas, @updated_at.format('%a, %d %^b %y'))
|
489
|
+
assert_equal 'Monday, 03 MARCH 14', t(@lucas, @updated_at.format('%A, %d %^B %y'))
|
490
|
+
end
|
491
|
+
|
492
|
+
def switch_to_lang(lang)
|
493
|
+
languages = {
|
494
|
+
'mssql' => { :en => 'English', :fr => 'French' },
|
495
|
+
'mysql' => { :en => 'en_US', :fr => 'fr_FR' },
|
496
|
+
'postgresql' => { :en => 'en_US.utf8', :fr => 'fr_FR.utf8' }
|
497
|
+
}
|
498
|
+
|
499
|
+
sql = {
|
500
|
+
'mssql' => ->(l) { "SET LANGUAGE #{l};" },
|
501
|
+
'mysql' => ->(l) { "SET lc_time_names = '#{l}';" },
|
502
|
+
'postgresql' => ->(l) { "SET lc_time to '#{l}';" }
|
503
|
+
}
|
504
|
+
|
505
|
+
User.connection.execute(sql[ENV['DB']][languages[ENV['DB']][lang]])
|
506
|
+
end
|
507
|
+
|
508
|
+
def test_format_date_with_names_and_lang_switch
|
509
|
+
skip "#{ENV['DB']} does not support word-based formatting for month and day names" if ['sqlite'].include?(ENV['DB'])
|
510
|
+
|
511
|
+
# the begin-rescue block is here to make sure we set the db back to en_US
|
512
|
+
# if we fail, so that other tests don't get contaminated.
|
513
|
+
#
|
514
|
+
# Tests should assert one single thing in principle, but until we
|
515
|
+
# refactor this whole thing, we'll have to do tricks of this sort.
|
516
|
+
switch_to_lang(:en)
|
517
|
+
case ENV['DB']
|
518
|
+
when 'mysql', 'postgresql'
|
519
|
+
assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
520
|
+
assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
521
|
+
when 'mssql'
|
522
|
+
assert_equal 'Monday, 03 March 2014', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
523
|
+
end
|
524
|
+
switch_to_lang(:fr)
|
525
|
+
case ENV['DB']
|
526
|
+
when 'mysql'
|
527
|
+
assert_equal 'lun, 03 mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
528
|
+
assert_equal 'lundi, 03 mars 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
529
|
+
when 'postgresql'
|
530
|
+
assert_equal 'Lun., 03 Mars 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
531
|
+
assert_equal 'Lundi, 03 Mars 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
532
|
+
when 'mssql'
|
533
|
+
assert_equal 'lundi, 03 mars 2014', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
534
|
+
end
|
535
|
+
ensure
|
536
|
+
switch_to_lang(:en)
|
435
537
|
end
|
436
538
|
|
437
539
|
def test_coalesce
|
438
|
-
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil,
|
540
|
+
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, 'default') + ' concat')
|
439
541
|
|
440
|
-
assert_equal 'toto', t(@test, @other.coalesce(
|
542
|
+
assert_equal 'toto', t(@test, @other.coalesce(''))
|
441
543
|
|
442
|
-
assert_equal ' ', t(@myung, @comments.coalesce(
|
443
|
-
assert_equal 'Laure', t(@laure, @comments.coalesce(
|
544
|
+
assert_equal ' ', t(@myung, @comments.coalesce('Myung').coalesce('ignored'))
|
545
|
+
assert_equal 'Laure', t(@laure, @comments.coalesce('Laure'))
|
444
546
|
if @env_db == 'oracle'
|
445
|
-
assert_nil t(@laure, @comments.coalesce(
|
446
|
-
assert_nil t(@camille, @other.coalesce(
|
547
|
+
assert_nil t(@laure, @comments.coalesce(''))
|
548
|
+
assert_nil t(@camille, @other.coalesce(''))
|
447
549
|
else
|
448
|
-
assert_equal('', t(@laure, @comments.coalesce(
|
449
|
-
assert_equal '', t(@camille, @other.coalesce(
|
550
|
+
assert_equal('', t(@laure, @comments.coalesce('')))
|
551
|
+
assert_equal '', t(@camille, @other.coalesce(''))
|
450
552
|
end
|
451
553
|
assert_equal 100, t(@test, @age.coalesce(100))
|
452
|
-
assert_equal
|
554
|
+
assert_equal 'Camille', t(@camille, @name.coalesce(nil, 'default'))
|
453
555
|
assert_equal 20, t(@test, @age.coalesce(nil, 20))
|
454
556
|
|
455
|
-
assert_equal 20, t(@test, @age.coalesce(10)+10)
|
456
|
-
assert_equal 'Laure10', t(@laure, @comments.coalesce(
|
557
|
+
assert_equal 20, t(@test, @age.coalesce(10) + 10)
|
558
|
+
assert_equal 'Laure10', t(@laure, @comments.coalesce('Laure') + 10)
|
457
559
|
end
|
458
560
|
|
459
561
|
# Comparators
|
@@ -474,16 +576,16 @@ module ArelExtensions
|
|
474
576
|
def test_date_duration
|
475
577
|
# Year
|
476
578
|
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
477
|
-
assert_equal 0, User.where(@created_at.year.eq(
|
579
|
+
assert_equal 0, User.where(@created_at.year.eq('2012')).count
|
478
580
|
# Month
|
479
581
|
assert_equal 5, t(@camille, @created_at.month).to_i
|
480
|
-
assert_equal 9, User.where(@created_at.month.eq(
|
582
|
+
assert_equal 9, User.where(@created_at.month.eq('05')).count
|
481
583
|
# Week
|
482
584
|
assert_equal(@env_db == 'mssql' ? 22 : 21, t(@arthur, @created_at.week).to_i)
|
483
|
-
assert_equal 9, User.where(@created_at.month.eq(
|
585
|
+
assert_equal 9, User.where(@created_at.month.eq('05')).count
|
484
586
|
# Day
|
485
587
|
assert_equal 23, t(@laure, @created_at.day).to_i
|
486
|
-
assert_equal 0, User.where(@created_at.day.eq(
|
588
|
+
assert_equal 0, User.where(@created_at.day.eq('05')).count
|
487
589
|
|
488
590
|
# skip "manage DATE" if @env_db == 'oracle'
|
489
591
|
# Hour
|
@@ -513,7 +615,7 @@ module ArelExtensions
|
|
513
615
|
end
|
514
616
|
|
515
617
|
|
516
|
-
skip
|
618
|
+
skip 'not yet implemented' if $sqlite
|
517
619
|
|
518
620
|
date1 = Date.new(2016, 5, 23)
|
519
621
|
durPos = 10.years
|
@@ -526,27 +628,27 @@ module ArelExtensions
|
|
526
628
|
datetime1 = Time.utc(2014, 3, 3, 12, 42, 0)
|
527
629
|
# Pull Request #5 tests
|
528
630
|
# puts (@created_at + durPos).cast(:date).to_sql
|
529
|
-
assert_includes [date2,
|
530
|
-
assert_includes [date3,
|
631
|
+
assert_includes [date2, '2026-05-23'], t(@test, (@created_at + durPos).cast(:date))
|
632
|
+
assert_includes [date3, '2006-05-23'], t(@test, (@created_at + durNeg).cast(:date))
|
531
633
|
|
532
634
|
# puts (@created_at + @created_at.day).cast(:date).to_sql
|
533
|
-
assert_includes [date4,
|
635
|
+
assert_includes [date4, '2016-06-15'], t(@test, (@created_at + @created_at.day).cast(:date))
|
534
636
|
# puts (@created_at - @created_at.day).cast(:date).to_sql
|
535
|
-
assert_includes [date5,
|
637
|
+
assert_includes [date5, '2016-04-30'], t(@test, (@created_at - @created_at.day).cast(:date))
|
536
638
|
|
537
|
-
assert_includes [datetime1 + 42.seconds,
|
538
|
-
assert_includes [datetime1 - 42.seconds,
|
639
|
+
assert_includes [datetime1 + 42.seconds, '2014-03-03 12:42:42 UTC'], t(@lucas, (@updated_at + @updated_at.minute))
|
640
|
+
assert_includes [datetime1 - 42.seconds, '2014-03-03 12:41:18 UTC'], t(@lucas, (@updated_at - @updated_at.minute))
|
539
641
|
|
540
642
|
# (@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))).to_sql
|
541
|
-
assert_includes [datetime1 + (12*60+42).seconds,
|
542
|
-
t(@lucas,(@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))))
|
643
|
+
assert_includes [datetime1 + (12 * 60 + 42).seconds, '2014-03-03 12:54:42 UTC'],
|
644
|
+
t(@lucas, (@updated_at + Arel.duration('s', (@updated_at.hour * 60 + @updated_at.minute))))
|
543
645
|
|
544
|
-
assert_includes [datetime1 + (12*60+42).minutes,
|
545
|
-
t(@lucas,(@updated_at + Arel.duration('mn',(@updated_at.hour*60 + @updated_at.minute))))
|
646
|
+
assert_includes [datetime1 + (12 * 60 + 42).minutes, '2014-03-04 01:24:00 UTC'],
|
647
|
+
t(@lucas, (@updated_at + Arel.duration('mn', (@updated_at.hour * 60 + @updated_at.minute))))
|
546
648
|
|
547
|
-
assert_includes [
|
649
|
+
assert_includes ['2024-03-03'], t(@lucas, (@updated_at + durPos).format('%Y-%m-%d'))
|
548
650
|
# puts (@updated_at - durPos).to_sql
|
549
|
-
assert_includes [
|
651
|
+
assert_includes ['2004-03-03'], t(@lucas, (@updated_at - durPos).format('%Y-%m-%d'))
|
550
652
|
|
551
653
|
|
552
654
|
# we test with the ruby object or the string because some adapters don't return an object Date
|
@@ -555,29 +657,29 @@ module ArelExtensions
|
|
555
657
|
|
556
658
|
# TODO; cast types
|
557
659
|
def test_cast_types
|
558
|
-
assert_equal
|
559
|
-
skip
|
660
|
+
assert_equal '5', t(@lucas, @age.cast(:string))
|
661
|
+
skip 'jdbc adapters does not work properly here (v52 works fine)' if RUBY_PLATFORM =~ /java/i
|
560
662
|
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
|
561
|
-
assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).cast(:string).eq(
|
562
|
-
assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).eq(
|
563
|
-
assert_equal
|
564
|
-
assert_equal
|
565
|
-
assert_equal 21.16, t(@laure
|
566
|
-
assert_equal 21, t(@laure
|
663
|
+
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'
|
664
|
+
assert_equal 1, t(@laure, Arel.when(@duration.cast(:time).eq('12:42:21')).then(1).else(0)) unless @env_db == 'oracle'
|
665
|
+
assert_equal '20.16', t(@laure, @score.cast(:string)).gsub(/[0]*\z/, '')
|
666
|
+
assert_equal '20.161', t(@laure, @score.cast(:string) + 1).gsub(/[0]*1\z/, '1')
|
667
|
+
assert_equal 21.16, t(@laure, @score.cast(:string).cast(:decimal) + 1)
|
668
|
+
assert_equal 21, t(@laure, @score.cast(:string).cast(:int) + 1)
|
567
669
|
|
568
|
-
assert_equal String, t(@lucas
|
670
|
+
assert_equal String, t(@lucas, @updated_at.cast(:string)).class
|
569
671
|
|
570
|
-
assert_equal Date, t(@lucas
|
571
|
-
assert_equal Time, t(@lucas
|
572
|
-
assert_equal Time, t(@lucas
|
672
|
+
assert_equal Date, t(@lucas, @updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
|
673
|
+
assert_equal Time, t(@lucas, @updated_at.cast(:string).cast(:datetime)).class
|
674
|
+
assert_equal Time, t(@lucas, @updated_at.cast(:time)).class
|
573
675
|
|
574
676
|
# mysql adapter in rails7 adds some infos we just squeeze here
|
575
|
-
assert_equal
|
576
|
-
assert_equal Date.parse(
|
577
|
-
assert_equal Date.parse(
|
578
|
-
assert_equal Time.parse(
|
579
|
-
assert_equal Date.parse(
|
580
|
-
assert_equal
|
677
|
+
assert_equal '2014-03-03 12:42:00', t(@lucas, @updated_at.cast(:string)).split('.').first unless @env_db == 'mssql' # locale dependent
|
678
|
+
assert_equal Date.parse('2014-03-03'), t(@lucas, Arel.quoted('2014-03-03').cast(:date))
|
679
|
+
assert_equal Date.parse('5014-03-03'), t(@lucas, (@age.cast(:string) + '014-03-03').cast(:date))
|
680
|
+
assert_equal Time.parse('2014-03-03 12:42:00 UTC'), t(@lucas, @updated_at.cast(:string).cast(:datetime))
|
681
|
+
assert_equal Date.parse('2014-03-03'), t(@lucas, @updated_at.cast(:date))
|
682
|
+
assert_equal '12:42:00', t(@lucas, @updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' # DateTime
|
581
683
|
end
|
582
684
|
end
|
583
685
|
|
@@ -587,42 +689,42 @@ module ArelExtensions
|
|
587
689
|
# puts @age.is_null.inspect
|
588
690
|
# puts @age.is_null.to_sql
|
589
691
|
# puts @age=='34'
|
590
|
-
assert_equal
|
692
|
+
assert_equal 'Test', User.select(@name).where(@age.is_null).first.name
|
591
693
|
end
|
592
694
|
|
593
695
|
def test_math_plus
|
594
696
|
d = Date.new(1997, 6, 15)
|
595
697
|
# Concat String
|
596
|
-
assert_equal
|
597
|
-
assert_equal
|
598
|
-
assert_equal
|
599
|
-
assert_equal
|
600
|
-
assert_equal
|
601
|
-
assert_equal
|
602
|
-
assert_equal
|
603
|
-
assert_equal
|
698
|
+
assert_equal 'SophiePhan', t(@sophie, @name + 'Phan')
|
699
|
+
assert_equal 'Sophie2', t(@sophie, @name + 2)
|
700
|
+
assert_equal 'Sophie1997-06-15', t(@sophie, @name + d)
|
701
|
+
assert_equal 'Sophie15', t(@sophie, @name + @age)
|
702
|
+
assert_equal 'SophieSophie', t(@sophie, @name + @name)
|
703
|
+
assert_equal 'SophieSophieSophie', t(@sophie, @name + @name + @name)
|
704
|
+
assert_equal 'SophieSophieSophie', t(@sophie, @name.concat(@name.concat(@name)))
|
705
|
+
assert_equal 'SophieSophieSophie', t(@sophie, @name.concat(@name).concat(@name))
|
604
706
|
# FIXME: should work as expected in Oracle
|
605
|
-
assert_equal
|
707
|
+
assert_equal 'Sophie2016-05-23', t(@sophie, @name + @created_at) unless @env_db == 'oracle'
|
606
708
|
# concat Integer
|
607
709
|
assert_equal 1, User.where((@age + 10).eq(33)).count
|
608
|
-
assert_equal 1, User.where((@age +
|
710
|
+
assert_equal 1, User.where((@age + '1').eq(6)).count
|
609
711
|
assert_equal 1, User.where((@age + @age).eq(10)).count
|
610
712
|
# concat Date
|
611
713
|
# puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
612
|
-
assert_equal
|
613
|
-
assert_equal
|
714
|
+
assert_equal '2016-05-24', t(@myung, @created_at + 1).to_date.to_s
|
715
|
+
assert_equal '2016-05-25', t(@myung, @created_at + 2.day).to_date.to_s
|
614
716
|
end
|
615
717
|
|
616
718
|
def test_math_minus
|
617
719
|
d = Date.new(2016, 5, 20)
|
618
720
|
# Datediff
|
619
721
|
assert_equal 9, User.where((@created_at - @created_at).eq(0)).count
|
620
|
-
assert_equal 3, @laure.select((@created_at - d).as(
|
722
|
+
assert_equal 3, @laure.select((@created_at - d).as('res')).first.res.abs.to_i
|
621
723
|
# Substraction
|
622
724
|
assert_equal 0, User.where((@age - 10).eq(50)).count
|
623
|
-
assert_equal 0, User.where((@age -
|
725
|
+
assert_equal 0, User.where((@age - '10').eq(50)).count
|
624
726
|
# assert_equal 0, User.where((@age - 9.5).eq(50.5)).count # should work: TODO
|
625
|
-
assert_equal 0, User.where((@age -
|
727
|
+
assert_equal 0, User.where((@age - '9.5').eq(50.5)).count
|
626
728
|
end
|
627
729
|
|
628
730
|
def test_wday
|
@@ -658,117 +760,117 @@ module ArelExtensions
|
|
658
760
|
def test_case
|
659
761
|
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)
|
660
762
|
assert_equal 4, User.where(@score.when(20.16).then(1).else(0).eq(1)).count
|
661
|
-
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
662
|
-
assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
|
663
|
-
assert_equal
|
664
|
-
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
|
665
|
-
assert_equal
|
763
|
+
assert_equal 2, t(@arthur, @score.when(65.62, 1).else(0) + 1)
|
764
|
+
assert_equal 0, t(@arthur, @score.when(65.62, 1).else(0) - 1)
|
765
|
+
assert_equal '11', t(@arthur, @score.when(65.62).then('1').else('0') + '1')
|
766
|
+
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score) + 1)
|
767
|
+
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
|
666
768
|
end
|
667
769
|
|
668
770
|
def test_format_numbers
|
669
771
|
# score of Arthur = 65.62
|
670
|
-
skip
|
671
|
-
|
672
|
-
assert_equal
|
673
|
-
assert_equal
|
674
|
-
assert_equal
|
675
|
-
assert_equal
|
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_includes [
|
685
|
-
assert_includes [
|
686
|
-
assert_includes [
|
687
|
-
assert_equal
|
688
|
-
assert_equal
|
689
|
-
assert_equal
|
690
|
-
assert_equal
|
691
|
-
assert_equal
|
692
|
-
assert_equal
|
772
|
+
skip ' Works with SQLite if the version used knows printf' if $sqlite
|
773
|
+
|
774
|
+
assert_equal 'Wrong Format', t(@arthur, @score.format_number('$ %...234.6F €', 'fr_FR'))
|
775
|
+
assert_equal 'AZERTY65,62', t(@arthur, @score.format_number('AZERTY%.2f', 'fr_FR'))
|
776
|
+
assert_equal '65,62AZERTY', t(@arthur, @score.format_number('%.2fAZERTY', 'fr_FR'))
|
777
|
+
assert_equal '$ 65.62 €', t(@arthur, @score.format_number('$ %.2f €', 'en_US'))
|
778
|
+
assert_equal '$ 66 €', t(@arthur, @score.format_number('$ %.0f €', 'en_US'))
|
779
|
+
assert_equal '$ 0065,62 €', t(@arthur, @score.format_number('$ %07.2f €', 'fr_FR'))
|
780
|
+
assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ %-07.2f €', 'fr_FR'))
|
781
|
+
assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ %-7.2f €', 'fr_FR'))
|
782
|
+
assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ % 7.2f €', 'fr_FR'))
|
783
|
+
assert_equal '$ 65,6 €', t(@arthur, @score.format_number('$ % 7.1f €', 'fr_FR'))
|
784
|
+
assert_equal '$ +65,62 €', t(@arthur, @score.format_number('$ % +7.2f €', 'fr_FR'))
|
785
|
+
assert_equal '$ +065,62 €', t(@arthur, @score.format_number('$ %0+7.2f €', 'fr_FR'))
|
786
|
+
assert_includes ['$ 6,56e1 €', '$ 6,56e+01 €'], t(@arthur, @score.format_number('$ %.2e €', 'fr_FR'))
|
787
|
+
assert_includes ['$ 6,56E1 €', '$ 6,56E+01 €'], t(@arthur, @score.format_number('$ %.2E €', 'fr_FR'))
|
788
|
+
assert_includes ['$ 6,562E1 €', '$ 6,562E+01 €'], t(@arthur, @score.format_number('$ %.3E €', 'fr_FR'))
|
789
|
+
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)
|
790
|
+
assert_equal '123456765,6', t(@arthur, (@score + 123456700).format_number('%.1f', 'fr_FR')).gsub("\u00A0", '') # because SqlServer does it like no one else
|
791
|
+
assert_equal '123,456,765.6', t(@arthur, (@score + 123456700).format_number('%.1f', 'en_US'))
|
792
|
+
assert_equal ' 123,456,765.6', t(@arthur, (@score + 123456700).format_number('%16.1f', 'en_US'))
|
793
|
+
assert_equal '$ 0,00 €', t(@arthur, @score.when(65.62).then(Arel.sql('null')).else(1).format_number('$ %.2f €', 'fr_FR'))
|
794
|
+
assert_equal '$ 0,00 €', t(@arthur, (@score - 65.62).format_number('$ %.2f €', 'fr_FR'))
|
693
795
|
end
|
694
796
|
|
695
797
|
def test_accent_insensitive
|
696
|
-
skip
|
697
|
-
skip
|
798
|
+
skip 'SQLite is natively Case Insensitive and Accent Sensitive' if $sqlite
|
799
|
+
skip 'Not finished' if @env_db == 'mysql'
|
698
800
|
# actual comments value: "arrêté"
|
699
801
|
# AI & CI
|
700
802
|
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
701
|
-
assert_equal
|
702
|
-
assert_equal
|
703
|
-
assert_equal
|
704
|
-
assert_equal
|
705
|
-
assert_equal
|
706
|
-
assert_equal
|
803
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('arrêté')).then('1').else('0'))
|
804
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('arrete')).then('1').else('0'))
|
805
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('àrrétè')).then('1').else('0'))
|
806
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_imatches('arretez')).then('1').else('0'))
|
807
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('Arrete')).then('1').else('0'))
|
808
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('Arrêté')).then('1').else('0'))
|
707
809
|
# AI & CS
|
708
|
-
assert_equal
|
709
|
-
assert_equal
|
710
|
-
assert_equal
|
711
|
-
assert_equal
|
712
|
-
if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
|
713
|
-
assert_equal
|
714
|
-
assert_equal
|
810
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('arrêté')).then('1').else('0'))
|
811
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('arrete')).then('1').else('0'))
|
812
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('àrrétè')).then('1').else('0'))
|
813
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('arretez')).then('1').else('0'))
|
814
|
+
if !['oracle', 'postgresql', 'mysql'].include?(@env_db) # AI => CI
|
815
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('Arrete')).then('1').else('0'))
|
816
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('Arrêté')).then('1').else('0'))
|
715
817
|
end
|
716
818
|
end
|
717
819
|
# AS & CI
|
718
|
-
assert_equal
|
820
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.imatches('arrêté')).then('1').else('0'))
|
719
821
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
720
|
-
assert_equal
|
721
|
-
assert_equal
|
822
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arrete')).then('1').else('0'))
|
823
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('àrrétè')).then('1').else('0'))
|
722
824
|
end
|
723
|
-
assert_equal
|
825
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arretez')).then('1').else('0'))
|
724
826
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
725
|
-
assert_equal
|
827
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('Arrete')).then('1').else('0'))
|
726
828
|
end
|
727
|
-
assert_equal
|
829
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.imatches('Arrêté')).then('1').else('0'))
|
728
830
|
# AS & CS
|
729
|
-
assert_equal
|
730
|
-
assert_equal
|
731
|
-
assert_equal
|
732
|
-
assert_equal
|
733
|
-
assert_equal
|
734
|
-
assert_equal
|
831
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.smatches('arrêté')).then('1').else('0'))
|
832
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('arrete')).then('1').else('0'))
|
833
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('àrrétè')).then('1').else('0'))
|
834
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('arretez')).then('1').else('0'))
|
835
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('Arrete')).then('1').else('0'))
|
836
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.smatches('Arrêté')).then('1').else('0'))
|
735
837
|
end
|
736
838
|
|
737
839
|
def test_subquery_with_order
|
738
840
|
skip if ['mssql'].include?(@env_db) && Arel::VERSION.to_i < 10
|
739
|
-
assert_equal 9, User.where(:
|
841
|
+
assert_equal 9, User.where(name: User.select(:name).order(:name)).count
|
740
842
|
assert_equal 9, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
|
741
|
-
if !['mysql'].include?(@env_db)
|
742
|
-
assert_equal 2, User.where(:
|
743
|
-
# assert_equal 6, User.where(:
|
843
|
+
if !['mysql'].include?(@env_db) # MySql can't have limit in IN subquery
|
844
|
+
assert_equal 2, User.where(name: User.select(:name).order(:name).limit(2)).count
|
845
|
+
# assert_equal 6, User.where(name: User.select(:name).order(:name).offset(2)).count
|
744
846
|
end
|
745
847
|
end
|
746
848
|
|
747
849
|
def test_in_with_nil
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
850
|
+
assert_predicate @myung.where(@age.in(1)), :blank?
|
851
|
+
assert_predicate @myung.where(@age.in(23)), :present?
|
852
|
+
assert_predicate @myung.where(@age.in([1])), :blank?
|
853
|
+
assert_predicate @myung.where(@age.in([1, 2])), :blank?
|
854
|
+
assert_predicate @myung.where(@age.in([1, 23])), :present?
|
855
|
+
assert_predicate @myung.where(@age.in(nil)), :blank?
|
856
|
+
assert_predicate @myung.where(@age.in([nil])), :blank?
|
857
|
+
assert_predicate @myung.where(@age.in([nil, 1])), :blank?
|
858
|
+
assert_predicate @myung.where(@age.in([nil, 23])), :present?
|
859
|
+
assert_predicate @myung.where(@age.in([nil, 1, 2])), :blank?
|
860
|
+
assert_predicate @myung.where(@age.in([nil, 1, 23])), :present?
|
861
|
+
assert_predicate @test.where(@age.in(1)), :blank?
|
862
|
+
assert_predicate @test.where(@age.in([1])), :blank?
|
863
|
+
assert_predicate @test.where(@age.in([1, 2])), :blank?
|
864
|
+
assert_predicate @test.where(@age.in(nil)), :present?
|
865
|
+
assert_predicate @test.where(@age.in([nil])), :present?
|
866
|
+
assert_predicate @test.where(@age.in([nil, 1])), :present?
|
867
|
+
assert_predicate @test.where(@age.in([nil, 1, 2])), :present?
|
766
868
|
end
|
767
869
|
|
768
870
|
def test_scope_with_in_plus_new
|
769
871
|
begin
|
770
|
-
@test.where(@age.in([1,2])).new
|
771
|
-
@test.where(@age.not_in([1,2])).new
|
872
|
+
@test.where(@age.in([1, 2])).new
|
873
|
+
@test.where(@age.not_in([1, 2])).new
|
772
874
|
assert true
|
773
875
|
rescue
|
774
876
|
assert false
|
@@ -776,44 +878,44 @@ module ArelExtensions
|
|
776
878
|
end
|
777
879
|
|
778
880
|
def test_is_not_null
|
779
|
-
|
780
|
-
|
881
|
+
assert_predicate @myung.where(@age.is_not_null), :present?
|
882
|
+
assert_predicate @test.where(@age.is_not_null), :blank?
|
781
883
|
end
|
782
884
|
|
783
885
|
def test_not_in_with_nil
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
886
|
+
assert_predicate @myung.where(@age.not_in(1)), :present?
|
887
|
+
assert_predicate @myung.where(@age.not_in(23)), :blank?
|
888
|
+
assert_predicate @myung.where(@age.not_in([1])), :present?
|
889
|
+
assert_predicate @myung.where(@age.not_in([1, 2])), :present?
|
890
|
+
assert_predicate @myung.where(@age.not_in([1, 23])), :blank?
|
891
|
+
assert_predicate @myung.where(@age.not_in(nil)), :present?
|
892
|
+
assert_predicate @myung.where(@age.not_in([nil])), :present?
|
893
|
+
assert_predicate @myung.where(@age.not_in([nil, 1])), :present?
|
894
|
+
assert_predicate @myung.where(@age.not_in([nil, 23])), :blank?
|
895
|
+
assert_predicate @myung.where(@age.not_in([nil, 1, 2])), :present?
|
896
|
+
assert_predicate @myung.where(@age.not_in([nil, 1, 23])), :blank?
|
897
|
+
|
898
|
+
assert_predicate @myung.where(@age.not_in(1..2)), :present?
|
797
899
|
|
798
900
|
# if the column is null, the entry will never be selected with not in (like every DBMS does)
|
799
|
-
#
|
800
|
-
#
|
801
|
-
#
|
802
|
-
#
|
803
|
-
#
|
804
|
-
#
|
805
|
-
#
|
901
|
+
# assert_predicate @test.where(@age.not_in(1)), :present?
|
902
|
+
# assert_predicate @test.where(@age.not_in([1])), :present?
|
903
|
+
# assert_predicate @test.where(@age.not_in([1,2])), :present?
|
904
|
+
# assert_predicate @test.where(@age.not_in(nil)), :blank?
|
905
|
+
# assert_predicate @test.where(@age.not_in([nil])), :blank?
|
906
|
+
# assert_predicate @test.where(@age.not_in([nil,1])), :blank?
|
907
|
+
# assert_predicate @test.where(@age.not_in([nil,1,2])), :blank?
|
806
908
|
end
|
807
909
|
|
808
910
|
def test_in_on_grouping
|
809
|
-
skip
|
810
|
-
assert_equal 2, User.where(Arel.tuple(@name
|
811
|
-
assert_equal 1, User.where(Arel.tuple(@name
|
812
|
-
assert_equal 0, User.where(Arel.tuple(@name
|
911
|
+
skip 'We should modify the visitor of IN to make it work' if $sqlite || @env_db == 'mssql'
|
912
|
+
assert_equal 2, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23), Arel.tuple('Arthur', 21))).count
|
913
|
+
assert_equal 1, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23))).count
|
914
|
+
assert_equal 0, User.where(Arel.tuple(@name, @age).in([])).count
|
813
915
|
end
|
814
916
|
|
815
917
|
def test_alias_shortened
|
816
|
-
if ['postgresql','oracle'].include?(@env_db)
|
918
|
+
if ['postgresql', 'oracle'].include?(@env_db)
|
817
919
|
new_alias = Arel.shorten('azerty' * 15)
|
818
920
|
at = User.arel_table.alias('azerty' * 15)
|
819
921
|
assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
|
@@ -836,64 +938,64 @@ module ArelExtensions
|
|
836
938
|
assert (479.82048 - t(User.where(nil), @score.variance(unbiased: false))).abs < 0.01
|
837
939
|
assert ( 23.23355 - t(User.where(nil), @score.std)).abs < 0.01
|
838
940
|
assert ( 21.90480 - t(User.where(nil), @score.std(unbiased: false))).abs < 0.01
|
839
|
-
skip
|
840
|
-
assert_equal 2, User.select(@score.std(group: Arel.when(@name >
|
841
|
-
assert_equal 2, User.select(@score.variance(group: Arel.when(@name >
|
842
|
-
assert_equal 2, User.select(@score.sum(group: Arel.when(@name >
|
843
|
-
assert_equal 2, User.select(@comments.group_concat(group: Arel.when(@name >
|
941
|
+
skip 'Not Yet Implemented' # if !['postgresql'].include?(@env_db)
|
942
|
+
assert_equal 2, User.select(@score.std(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
943
|
+
assert_equal 2, User.select(@score.variance(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
944
|
+
assert_equal 2, User.select(@score.sum(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
945
|
+
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
|
844
946
|
end
|
845
947
|
|
846
948
|
def test_levenshtein_distance
|
847
|
-
skip
|
848
|
-
assert_equal 0, t(@arthur
|
849
|
-
assert_equal 2, t(@arthur
|
850
|
-
assert_equal 1, t(@arthur
|
949
|
+
skip 'Not Yet Implemented' if $sqlite
|
950
|
+
assert_equal 0, t(@arthur, @name.levenshtein_distance('Arthur'))
|
951
|
+
assert_equal 2, t(@arthur, @name.levenshtein_distance('Artoor'))
|
952
|
+
assert_equal 1, t(@arthur, @name.levenshtein_distance('Artehur'))
|
851
953
|
end
|
852
954
|
|
853
955
|
def test_json
|
854
956
|
skip "Can't be tested on travis"
|
855
957
|
# creation
|
856
|
-
assert_equal 'Arthur', t(@arthur,Arel.json(@name))
|
857
|
-
assert_equal [
|
858
|
-
assert_equal ({
|
859
|
-
assert_equal ({
|
860
|
-
assert_equal ([{
|
958
|
+
assert_equal 'Arthur', t(@arthur, Arel.json(@name))
|
959
|
+
assert_equal ['Arthur', 'Arthur'], parse_json(t(@arthur, Arel.json(@name, @name)))
|
960
|
+
assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 'ArthurArthur'}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => @name + @name})))
|
961
|
+
assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 1}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => 1})))
|
962
|
+
assert_equal ([{'age' => 21}, {'name' => 'Arthur', 'score' => 65.62}]), parse_json(t(@arthur, Arel.json([{age: @age}, {name: @name, score: @score}])))
|
861
963
|
|
862
964
|
# aggregate
|
863
|
-
assert_equal ({
|
864
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name}).group(false)))
|
865
|
-
assert_equal ({
|
866
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name
|
867
|
-
assert_equal ([{
|
868
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score),Arel.json({@age => @name}).group(true,[@age])))
|
965
|
+
assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure'}),
|
966
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16), Arel.json({@age => @name}).group(false)))
|
967
|
+
assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure', 'Laure' => 25, 'Lucas' => 5, 'Myung' => 23, 'Sophie' => 15}),
|
968
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16), Arel.json({@age => @name, @name => @age}).group(false)))
|
969
|
+
assert_equal ([{'5' => 'Lucas'}, { '15' => 'Sophie'}, { '23' => 'Myung'}, { '25' => 'Laure'}]),
|
970
|
+
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score), Arel.json({@age => @name}).group(true, [@age])))
|
869
971
|
|
870
|
-
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_sql
|
871
|
-
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_a
|
972
|
+
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score, Arel.json({@age => @name}).group(true,[@age])).to_sql
|
973
|
+
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score, Arel.json({@age => @name}).group(true,[@age])).to_a
|
872
974
|
|
873
|
-
skip
|
975
|
+
skip 'Not Yet Implemented' if $sqlite || ['oracle', 'mssql'].include?(@env_db)
|
874
976
|
# get
|
875
|
-
h1 = Arel.json({@name => @name
|
876
|
-
assert_equal
|
877
|
-
h2 = Arel.json([{age: @age},{name: @name,score: @score}])
|
878
|
-
assert_equal ({
|
879
|
-
assert_equal 21, parse_json(t(@arthur,h2.get(0).get('age')))
|
880
|
-
assert_nil t(@arthur,h2.get('age'))
|
977
|
+
h1 = Arel.json({@name => @name + @name, @name + '2' => 1})
|
978
|
+
assert_equal 'ArthurArthur', parse_json(t(@arthur, h1.get(@name)))
|
979
|
+
h2 = Arel.json([{age: @age}, {name: @name, score: @score}])
|
980
|
+
assert_equal ({'age' => 21}), parse_json(t(@arthur, h2.get(0)))
|
981
|
+
assert_equal 21, parse_json(t(@arthur, h2.get(0).get('age')))
|
982
|
+
assert_nil t(@arthur, h2.get('age'))
|
881
983
|
# set
|
882
|
-
assert_equal ({
|
883
|
-
assert_equal ({
|
884
|
-
assert_equal ({
|
885
|
-
assert_equal ({
|
984
|
+
assert_equal ({'Arthur' => ['toto', 'tata'], 'Arthur2' => 1}), parse_json(t(@arthur, h1.set(@name, ['toto', 'tata'])))
|
985
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.set(@name + '3', 2)))
|
986
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => nil}), parse_json(t(@arthur, h1.set(@name + '3', nil)))
|
987
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => {'a' => 2}}), parse_json(t(@arthur, h1.set(@name + '3', {a: 2})))
|
886
988
|
# merge
|
887
|
-
assert_equal ({
|
888
|
-
assert_equal ({
|
889
|
-
assert_equal ({
|
989
|
+
assert_equal ({'Arthur' => ['toto', 'tata'], 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.merge({@name => ['toto', 'tata']}, {@name + '3' => 2})))
|
990
|
+
assert_equal ({'Arthur' => ['toto', 'tata'], 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.merge({@name => ['toto', 'tata'], @name + '3' => 2})))
|
991
|
+
assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1}), parse_json(t(@arthur, h1.merge({})))
|
890
992
|
end
|
891
993
|
|
892
994
|
def test_as_on_everything
|
893
995
|
name = @arthur.select(@name.as('NaMe')).first.attributes
|
894
|
-
assert_equal 'Arthur', name[
|
895
|
-
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes[
|
896
|
-
assert_equal 'ArthurArthur', @arthur.select((@name
|
996
|
+
assert_equal 'Arthur', name['NaMe'] || name['name'] # because of Oracle
|
997
|
+
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes['Na Me']
|
998
|
+
assert_equal 'ArthurArthur', @arthur.select((@name + @name).as('Na-Me')).first.attributes['Na-Me']
|
897
999
|
end
|
898
1000
|
|
899
1001
|
def test_exists_in_subquery
|