arel_extensions 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +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 +1 -2
- 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 +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 +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 +127 -121
- data/lib/arel_extensions/visitors/mysql.rb +137 -145
- data/lib/arel_extensions/visitors/oracle.rb +128 -128
- data/lib/arel_extensions/visitors/oracle12.rb +16 -16
- data/lib/arel_extensions/visitors/postgresql.rb +139 -137
- 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 -13
- 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 +364 -299
- 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
|
|
@@ -430,11 +438,11 @@ module ArelExtensions
|
|
|
430
438
|
# Winter/Summer time
|
|
431
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'] }))
|
|
432
440
|
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
|
|
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'] }))
|
|
435
443
|
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
|
|
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']))
|
|
438
446
|
end
|
|
439
447
|
end
|
|
440
448
|
|
|
@@ -450,7 +458,7 @@ module ArelExtensions
|
|
|
450
458
|
'2028-01-01 10:42:00' => '52', # Saturday
|
|
451
459
|
'2034-01-01 10:42:00' => '52', # Sunday
|
|
452
460
|
}.each do |date, exp|
|
|
453
|
-
assert_equal exp, t(@lucas, Arel
|
|
461
|
+
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%V'))
|
|
454
462
|
end
|
|
455
463
|
end
|
|
456
464
|
|
|
@@ -467,30 +475,87 @@ module ArelExtensions
|
|
|
467
475
|
'2028-01-01 10:42:00' => '2027', # Saturday
|
|
468
476
|
'2034-01-01 10:42:00' => '2033', # Sunday
|
|
469
477
|
}.each do |date, exp|
|
|
470
|
-
assert_equal exp, t(@lucas, Arel
|
|
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'))
|
|
471
534
|
end
|
|
535
|
+
ensure
|
|
536
|
+
switch_to_lang(:en)
|
|
472
537
|
end
|
|
473
538
|
|
|
474
539
|
def test_coalesce
|
|
475
|
-
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil,
|
|
540
|
+
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, 'default') + ' concat')
|
|
476
541
|
|
|
477
|
-
assert_equal 'toto', t(@test, @other.coalesce(
|
|
542
|
+
assert_equal 'toto', t(@test, @other.coalesce(''))
|
|
478
543
|
|
|
479
|
-
assert_equal ' ', t(@myung, @comments.coalesce(
|
|
480
|
-
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'))
|
|
481
546
|
if @env_db == 'oracle'
|
|
482
|
-
assert_nil t(@laure, @comments.coalesce(
|
|
483
|
-
assert_nil t(@camille, @other.coalesce(
|
|
547
|
+
assert_nil t(@laure, @comments.coalesce(''))
|
|
548
|
+
assert_nil t(@camille, @other.coalesce(''))
|
|
484
549
|
else
|
|
485
|
-
assert_equal('', t(@laure, @comments.coalesce(
|
|
486
|
-
assert_equal '', t(@camille, @other.coalesce(
|
|
550
|
+
assert_equal('', t(@laure, @comments.coalesce('')))
|
|
551
|
+
assert_equal '', t(@camille, @other.coalesce(''))
|
|
487
552
|
end
|
|
488
553
|
assert_equal 100, t(@test, @age.coalesce(100))
|
|
489
|
-
assert_equal
|
|
554
|
+
assert_equal 'Camille', t(@camille, @name.coalesce(nil, 'default'))
|
|
490
555
|
assert_equal 20, t(@test, @age.coalesce(nil, 20))
|
|
491
556
|
|
|
492
|
-
assert_equal 20, t(@test, @age.coalesce(10)+10)
|
|
493
|
-
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)
|
|
494
559
|
end
|
|
495
560
|
|
|
496
561
|
# Comparators
|
|
@@ -511,16 +576,16 @@ module ArelExtensions
|
|
|
511
576
|
def test_date_duration
|
|
512
577
|
# Year
|
|
513
578
|
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
|
514
|
-
assert_equal 0, User.where(@created_at.year.eq(
|
|
579
|
+
assert_equal 0, User.where(@created_at.year.eq('2012')).count
|
|
515
580
|
# Month
|
|
516
581
|
assert_equal 5, t(@camille, @created_at.month).to_i
|
|
517
|
-
assert_equal 9, User.where(@created_at.month.eq(
|
|
582
|
+
assert_equal 9, User.where(@created_at.month.eq('05')).count
|
|
518
583
|
# Week
|
|
519
584
|
assert_equal(@env_db == 'mssql' ? 22 : 21, t(@arthur, @created_at.week).to_i)
|
|
520
|
-
assert_equal 9, User.where(@created_at.month.eq(
|
|
585
|
+
assert_equal 9, User.where(@created_at.month.eq('05')).count
|
|
521
586
|
# Day
|
|
522
587
|
assert_equal 23, t(@laure, @created_at.day).to_i
|
|
523
|
-
assert_equal 0, User.where(@created_at.day.eq(
|
|
588
|
+
assert_equal 0, User.where(@created_at.day.eq('05')).count
|
|
524
589
|
|
|
525
590
|
# skip "manage DATE" if @env_db == 'oracle'
|
|
526
591
|
# Hour
|
|
@@ -550,7 +615,7 @@ module ArelExtensions
|
|
|
550
615
|
end
|
|
551
616
|
|
|
552
617
|
|
|
553
|
-
skip
|
|
618
|
+
skip 'not yet implemented' if $sqlite
|
|
554
619
|
|
|
555
620
|
date1 = Date.new(2016, 5, 23)
|
|
556
621
|
durPos = 10.years
|
|
@@ -563,27 +628,27 @@ module ArelExtensions
|
|
|
563
628
|
datetime1 = Time.utc(2014, 3, 3, 12, 42, 0)
|
|
564
629
|
# Pull Request #5 tests
|
|
565
630
|
# puts (@created_at + durPos).cast(:date).to_sql
|
|
566
|
-
assert_includes [date2,
|
|
567
|
-
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))
|
|
568
633
|
|
|
569
634
|
# puts (@created_at + @created_at.day).cast(:date).to_sql
|
|
570
|
-
assert_includes [date4,
|
|
635
|
+
assert_includes [date4, '2016-06-15'], t(@test, (@created_at + @created_at.day).cast(:date))
|
|
571
636
|
# puts (@created_at - @created_at.day).cast(:date).to_sql
|
|
572
|
-
assert_includes [date5,
|
|
637
|
+
assert_includes [date5, '2016-04-30'], t(@test, (@created_at - @created_at.day).cast(:date))
|
|
573
638
|
|
|
574
|
-
assert_includes [datetime1 + 42.seconds,
|
|
575
|
-
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))
|
|
576
641
|
|
|
577
642
|
# (@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))).to_sql
|
|
578
|
-
assert_includes [datetime1 + (12*60+42).seconds,
|
|
579
|
-
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))))
|
|
580
645
|
|
|
581
|
-
assert_includes [datetime1 + (12*60+42).minutes,
|
|
582
|
-
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))))
|
|
583
648
|
|
|
584
|
-
assert_includes [
|
|
649
|
+
assert_includes ['2024-03-03'], t(@lucas, (@updated_at + durPos).format('%Y-%m-%d'))
|
|
585
650
|
# puts (@updated_at - durPos).to_sql
|
|
586
|
-
assert_includes [
|
|
651
|
+
assert_includes ['2004-03-03'], t(@lucas, (@updated_at - durPos).format('%Y-%m-%d'))
|
|
587
652
|
|
|
588
653
|
|
|
589
654
|
# we test with the ruby object or the string because some adapters don't return an object Date
|
|
@@ -592,29 +657,29 @@ module ArelExtensions
|
|
|
592
657
|
|
|
593
658
|
# TODO; cast types
|
|
594
659
|
def test_cast_types
|
|
595
|
-
assert_equal
|
|
596
|
-
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
|
|
597
662
|
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
|
|
598
|
-
assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).cast(:string).eq(
|
|
599
|
-
assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).eq(
|
|
600
|
-
assert_equal
|
|
601
|
-
assert_equal
|
|
602
|
-
assert_equal 21.16, t(@laure
|
|
603
|
-
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)
|
|
604
669
|
|
|
605
|
-
assert_equal String, t(@lucas
|
|
670
|
+
assert_equal String, t(@lucas, @updated_at.cast(:string)).class
|
|
606
671
|
|
|
607
|
-
assert_equal Date, t(@lucas
|
|
608
|
-
assert_equal Time, t(@lucas
|
|
609
|
-
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
|
|
610
675
|
|
|
611
676
|
# mysql adapter in rails7 adds some infos we just squeeze here
|
|
612
|
-
assert_equal
|
|
613
|
-
assert_equal Date.parse(
|
|
614
|
-
assert_equal Date.parse(
|
|
615
|
-
assert_equal Time.parse(
|
|
616
|
-
assert_equal Date.parse(
|
|
617
|
-
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
|
|
618
683
|
end
|
|
619
684
|
end
|
|
620
685
|
|
|
@@ -624,42 +689,42 @@ module ArelExtensions
|
|
|
624
689
|
# puts @age.is_null.inspect
|
|
625
690
|
# puts @age.is_null.to_sql
|
|
626
691
|
# puts @age=='34'
|
|
627
|
-
assert_equal
|
|
692
|
+
assert_equal 'Test', User.select(@name).where(@age.is_null).first.name
|
|
628
693
|
end
|
|
629
694
|
|
|
630
695
|
def test_math_plus
|
|
631
696
|
d = Date.new(1997, 6, 15)
|
|
632
697
|
# Concat String
|
|
633
|
-
assert_equal
|
|
634
|
-
assert_equal
|
|
635
|
-
assert_equal
|
|
636
|
-
assert_equal
|
|
637
|
-
assert_equal
|
|
638
|
-
assert_equal
|
|
639
|
-
assert_equal
|
|
640
|
-
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))
|
|
641
706
|
# FIXME: should work as expected in Oracle
|
|
642
|
-
assert_equal
|
|
707
|
+
assert_equal 'Sophie2016-05-23', t(@sophie, @name + @created_at) unless @env_db == 'oracle'
|
|
643
708
|
# concat Integer
|
|
644
709
|
assert_equal 1, User.where((@age + 10).eq(33)).count
|
|
645
|
-
assert_equal 1, User.where((@age +
|
|
710
|
+
assert_equal 1, User.where((@age + '1').eq(6)).count
|
|
646
711
|
assert_equal 1, User.where((@age + @age).eq(10)).count
|
|
647
712
|
# concat Date
|
|
648
713
|
# puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
|
|
649
|
-
assert_equal
|
|
650
|
-
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
|
|
651
716
|
end
|
|
652
717
|
|
|
653
718
|
def test_math_minus
|
|
654
719
|
d = Date.new(2016, 5, 20)
|
|
655
720
|
# Datediff
|
|
656
721
|
assert_equal 9, User.where((@created_at - @created_at).eq(0)).count
|
|
657
|
-
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
|
|
658
723
|
# Substraction
|
|
659
724
|
assert_equal 0, User.where((@age - 10).eq(50)).count
|
|
660
|
-
assert_equal 0, User.where((@age -
|
|
725
|
+
assert_equal 0, User.where((@age - '10').eq(50)).count
|
|
661
726
|
# assert_equal 0, User.where((@age - 9.5).eq(50.5)).count # should work: TODO
|
|
662
|
-
assert_equal 0, User.where((@age -
|
|
727
|
+
assert_equal 0, User.where((@age - '9.5').eq(50.5)).count
|
|
663
728
|
end
|
|
664
729
|
|
|
665
730
|
def test_wday
|
|
@@ -695,117 +760,117 @@ module ArelExtensions
|
|
|
695
760
|
def test_case
|
|
696
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)
|
|
697
762
|
assert_equal 4, User.where(@score.when(20.16).then(1).else(0).eq(1)).count
|
|
698
|
-
assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
|
|
699
|
-
assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
|
|
700
|
-
assert_equal
|
|
701
|
-
assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
|
|
702
|
-
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
|
|
703
768
|
end
|
|
704
769
|
|
|
705
770
|
def test_format_numbers
|
|
706
771
|
# score of Arthur = 65.62
|
|
707
|
-
skip
|
|
708
|
-
|
|
709
|
-
assert_equal
|
|
710
|
-
assert_equal
|
|
711
|
-
assert_equal
|
|
712
|
-
assert_equal
|
|
713
|
-
assert_equal
|
|
714
|
-
assert_equal
|
|
715
|
-
assert_equal
|
|
716
|
-
assert_equal
|
|
717
|
-
assert_equal
|
|
718
|
-
assert_equal
|
|
719
|
-
assert_equal
|
|
720
|
-
assert_equal
|
|
721
|
-
assert_includes [
|
|
722
|
-
assert_includes [
|
|
723
|
-
assert_includes [
|
|
724
|
-
assert_equal
|
|
725
|
-
assert_equal
|
|
726
|
-
assert_equal
|
|
727
|
-
assert_equal
|
|
728
|
-
assert_equal
|
|
729
|
-
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'))
|
|
730
795
|
end
|
|
731
796
|
|
|
732
797
|
def test_accent_insensitive
|
|
733
|
-
skip
|
|
734
|
-
skip
|
|
798
|
+
skip 'SQLite is natively Case Insensitive and Accent Sensitive' if $sqlite
|
|
799
|
+
skip 'Not finished' if @env_db == 'mysql'
|
|
735
800
|
# actual comments value: "arrêté"
|
|
736
801
|
# AI & CI
|
|
737
802
|
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
|
738
|
-
assert_equal
|
|
739
|
-
assert_equal
|
|
740
|
-
assert_equal
|
|
741
|
-
assert_equal
|
|
742
|
-
assert_equal
|
|
743
|
-
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'))
|
|
744
809
|
# AI & CS
|
|
745
|
-
assert_equal
|
|
746
|
-
assert_equal
|
|
747
|
-
assert_equal
|
|
748
|
-
assert_equal
|
|
749
|
-
if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
|
|
750
|
-
assert_equal
|
|
751
|
-
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'))
|
|
752
817
|
end
|
|
753
818
|
end
|
|
754
819
|
# AS & CI
|
|
755
|
-
assert_equal
|
|
820
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.imatches('arrêté')).then('1').else('0'))
|
|
756
821
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
|
757
|
-
assert_equal
|
|
758
|
-
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'))
|
|
759
824
|
end
|
|
760
|
-
assert_equal
|
|
825
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arretez')).then('1').else('0'))
|
|
761
826
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
|
762
|
-
assert_equal
|
|
827
|
+
assert_equal '0', t(@arthur, Arel.when(@comments.imatches('Arrete')).then('1').else('0'))
|
|
763
828
|
end
|
|
764
|
-
assert_equal
|
|
829
|
+
assert_equal '1', t(@arthur, Arel.when(@comments.imatches('Arrêté')).then('1').else('0'))
|
|
765
830
|
# AS & CS
|
|
766
|
-
assert_equal
|
|
767
|
-
assert_equal
|
|
768
|
-
assert_equal
|
|
769
|
-
assert_equal
|
|
770
|
-
assert_equal
|
|
771
|
-
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'))
|
|
772
837
|
end
|
|
773
838
|
|
|
774
839
|
def test_subquery_with_order
|
|
775
840
|
skip if ['mssql'].include?(@env_db) && Arel::VERSION.to_i < 10
|
|
776
|
-
assert_equal 9, User.where(:
|
|
841
|
+
assert_equal 9, User.where(name: User.select(:name).order(:name)).count
|
|
777
842
|
assert_equal 9, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
|
|
778
|
-
if !['mysql'].include?(@env_db)
|
|
779
|
-
assert_equal 2, User.where(:
|
|
780
|
-
# 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
|
|
781
846
|
end
|
|
782
847
|
end
|
|
783
848
|
|
|
784
849
|
def test_in_with_nil
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
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?
|
|
803
868
|
end
|
|
804
869
|
|
|
805
870
|
def test_scope_with_in_plus_new
|
|
806
871
|
begin
|
|
807
|
-
@test.where(@age.in([1,2])).new
|
|
808
|
-
@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
|
|
809
874
|
assert true
|
|
810
875
|
rescue
|
|
811
876
|
assert false
|
|
@@ -813,44 +878,44 @@ module ArelExtensions
|
|
|
813
878
|
end
|
|
814
879
|
|
|
815
880
|
def test_is_not_null
|
|
816
|
-
|
|
817
|
-
|
|
881
|
+
assert_predicate @myung.where(@age.is_not_null), :present?
|
|
882
|
+
assert_predicate @test.where(@age.is_not_null), :blank?
|
|
818
883
|
end
|
|
819
884
|
|
|
820
885
|
def test_not_in_with_nil
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
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?
|
|
834
899
|
|
|
835
900
|
# if the column is null, the entry will never be selected with not in (like every DBMS does)
|
|
836
|
-
#
|
|
837
|
-
#
|
|
838
|
-
#
|
|
839
|
-
#
|
|
840
|
-
#
|
|
841
|
-
#
|
|
842
|
-
#
|
|
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?
|
|
843
908
|
end
|
|
844
909
|
|
|
845
910
|
def test_in_on_grouping
|
|
846
|
-
skip
|
|
847
|
-
assert_equal 2, User.where(Arel.tuple(@name
|
|
848
|
-
assert_equal 1, User.where(Arel.tuple(@name
|
|
849
|
-
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
|
|
850
915
|
end
|
|
851
916
|
|
|
852
917
|
def test_alias_shortened
|
|
853
|
-
if ['postgresql','oracle'].include?(@env_db)
|
|
918
|
+
if ['postgresql', 'oracle'].include?(@env_db)
|
|
854
919
|
new_alias = Arel.shorten('azerty' * 15)
|
|
855
920
|
at = User.arel_table.alias('azerty' * 15)
|
|
856
921
|
assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
|
|
@@ -873,64 +938,64 @@ module ArelExtensions
|
|
|
873
938
|
assert (479.82048 - t(User.where(nil), @score.variance(unbiased: false))).abs < 0.01
|
|
874
939
|
assert ( 23.23355 - t(User.where(nil), @score.std)).abs < 0.01
|
|
875
940
|
assert ( 21.90480 - t(User.where(nil), @score.std(unbiased: false))).abs < 0.01
|
|
876
|
-
skip
|
|
877
|
-
assert_equal 2, User.select(@score.std(group: Arel.when(@name >
|
|
878
|
-
assert_equal 2, User.select(@score.variance(group: Arel.when(@name >
|
|
879
|
-
assert_equal 2, User.select(@score.sum(group: Arel.when(@name >
|
|
880
|
-
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
|
|
881
946
|
end
|
|
882
947
|
|
|
883
948
|
def test_levenshtein_distance
|
|
884
|
-
skip
|
|
885
|
-
assert_equal 0, t(@arthur
|
|
886
|
-
assert_equal 2, t(@arthur
|
|
887
|
-
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'))
|
|
888
953
|
end
|
|
889
954
|
|
|
890
955
|
def test_json
|
|
891
956
|
skip "Can't be tested on travis"
|
|
892
957
|
# creation
|
|
893
|
-
assert_equal 'Arthur', t(@arthur,Arel.json(@name))
|
|
894
|
-
assert_equal [
|
|
895
|
-
assert_equal ({
|
|
896
|
-
assert_equal ({
|
|
897
|
-
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}])))
|
|
898
963
|
|
|
899
964
|
# aggregate
|
|
900
|
-
assert_equal ({
|
|
901
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name}).group(false)))
|
|
902
|
-
assert_equal ({
|
|
903
|
-
parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name
|
|
904
|
-
assert_equal ([{
|
|
905
|
-
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])))
|
|
906
971
|
|
|
907
|
-
# puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_sql
|
|
908
|
-
# 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
|
|
909
974
|
|
|
910
|
-
skip
|
|
975
|
+
skip 'Not Yet Implemented' if $sqlite || ['oracle', 'mssql'].include?(@env_db)
|
|
911
976
|
# get
|
|
912
|
-
h1 = Arel.json({@name => @name
|
|
913
|
-
assert_equal
|
|
914
|
-
h2 = Arel.json([{age: @age},{name: @name,score: @score}])
|
|
915
|
-
assert_equal ({
|
|
916
|
-
assert_equal 21, parse_json(t(@arthur,h2.get(0).get('age')))
|
|
917
|
-
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'))
|
|
918
983
|
# set
|
|
919
|
-
assert_equal ({
|
|
920
|
-
assert_equal ({
|
|
921
|
-
assert_equal ({
|
|
922
|
-
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})))
|
|
923
988
|
# merge
|
|
924
|
-
assert_equal ({
|
|
925
|
-
assert_equal ({
|
|
926
|
-
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({})))
|
|
927
992
|
end
|
|
928
993
|
|
|
929
994
|
def test_as_on_everything
|
|
930
995
|
name = @arthur.select(@name.as('NaMe')).first.attributes
|
|
931
|
-
assert_equal 'Arthur', name[
|
|
932
|
-
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes[
|
|
933
|
-
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']
|
|
934
999
|
end
|
|
935
1000
|
|
|
936
1001
|
def test_exists_in_subquery
|