arel_extensions 1.3.1 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -2
  3. data/.github/workflows/ruby.yml +4 -4
  4. data/.rubocop.yml +25 -1
  5. data/Gemfile +10 -10
  6. data/README.md +24 -4
  7. data/Rakefile +4 -4
  8. data/TODO +0 -1
  9. data/arel_extensions.gemspec +12 -12
  10. data/gemfiles/rails3.gemfile +10 -10
  11. data/gemfiles/rails4.gemfile +13 -13
  12. data/gemfiles/rails5_0.gemfile +12 -12
  13. data/gemfiles/rails5_1_4.gemfile +12 -12
  14. data/gemfiles/rails5_2.gemfile +13 -13
  15. data/gemfiles/rails6.gemfile +11 -11
  16. data/gemfiles/rails6_1.gemfile +11 -11
  17. data/gemfiles/rails7.gemfile +7 -7
  18. data/gemspecs/arel_extensions-v1.gemspec +12 -12
  19. data/gemspecs/arel_extensions-v2.gemspec +11 -11
  20. data/init/mssql.sql +0 -0
  21. data/init/mysql.sql +0 -0
  22. data/init/oracle.sql +0 -0
  23. data/init/postgresql.sql +0 -0
  24. data/init/sqlite.sql +0 -0
  25. data/lib/arel_extensions/attributes.rb +2 -2
  26. data/lib/arel_extensions/boolean_functions.rb +2 -4
  27. data/lib/arel_extensions/common_sql_functions.rb +10 -10
  28. data/lib/arel_extensions/comparators.rb +14 -14
  29. data/lib/arel_extensions/date_duration.rb +7 -7
  30. data/lib/arel_extensions/helpers.rb +16 -13
  31. data/lib/arel_extensions/insert_manager.rb +1 -1
  32. data/lib/arel_extensions/math.rb +16 -18
  33. data/lib/arel_extensions/math_functions.rb +18 -18
  34. data/lib/arel_extensions/nodes/abs.rb +0 -0
  35. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -0
  36. data/lib/arel_extensions/nodes/blank.rb +1 -1
  37. data/lib/arel_extensions/nodes/case.rb +4 -6
  38. data/lib/arel_extensions/nodes/cast.rb +3 -3
  39. data/lib/arel_extensions/nodes/ceil.rb +0 -0
  40. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  41. data/lib/arel_extensions/nodes/coalesce.rb +0 -0
  42. data/lib/arel_extensions/nodes/collate.rb +9 -9
  43. data/lib/arel_extensions/nodes/concat.rb +2 -2
  44. data/lib/arel_extensions/nodes/date_diff.rb +6 -6
  45. data/lib/arel_extensions/nodes/duration.rb +0 -0
  46. data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
  47. data/lib/arel_extensions/nodes/floor.rb +0 -0
  48. data/lib/arel_extensions/nodes/format.rb +1 -1
  49. data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
  50. data/lib/arel_extensions/nodes/function.rb +21 -21
  51. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  52. data/lib/arel_extensions/nodes/json.rb +7 -7
  53. data/lib/arel_extensions/nodes/length.rb +0 -0
  54. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  55. data/lib/arel_extensions/nodes/locate.rb +0 -0
  56. data/lib/arel_extensions/nodes/log10.rb +0 -0
  57. data/lib/arel_extensions/nodes/matches.rb +1 -1
  58. data/lib/arel_extensions/nodes/md5.rb +0 -0
  59. data/lib/arel_extensions/nodes/power.rb +0 -0
  60. data/lib/arel_extensions/nodes/rand.rb +0 -0
  61. data/lib/arel_extensions/nodes/repeat.rb +0 -0
  62. data/lib/arel_extensions/nodes/replace.rb +2 -2
  63. data/lib/arel_extensions/nodes/round.rb +0 -0
  64. data/lib/arel_extensions/nodes/soundex.rb +2 -2
  65. data/lib/arel_extensions/nodes/std.rb +0 -0
  66. data/lib/arel_extensions/nodes/substring.rb +0 -0
  67. data/lib/arel_extensions/nodes/sum.rb +0 -0
  68. data/lib/arel_extensions/nodes/then.rb +0 -0
  69. data/lib/arel_extensions/nodes/trim.rb +0 -0
  70. data/lib/arel_extensions/nodes/union.rb +4 -4
  71. data/lib/arel_extensions/nodes/union_all.rb +3 -3
  72. data/lib/arel_extensions/nodes/wday.rb +0 -0
  73. data/lib/arel_extensions/nodes.rb +0 -0
  74. data/lib/arel_extensions/null_functions.rb +0 -0
  75. data/lib/arel_extensions/predications.rb +10 -10
  76. data/lib/arel_extensions/railtie.rb +1 -1
  77. data/lib/arel_extensions/set_functions.rb +3 -3
  78. data/lib/arel_extensions/string_functions.rb +8 -8
  79. data/lib/arel_extensions/tasks.rb +2 -2
  80. data/lib/arel_extensions/version.rb +1 -1
  81. data/lib/arel_extensions/visitors/convert_format.rb +0 -0
  82. data/lib/arel_extensions/visitors/ibm_db.rb +19 -19
  83. data/lib/arel_extensions/visitors/mssql.rb +139 -118
  84. data/lib/arel_extensions/visitors/mysql.rb +145 -144
  85. data/lib/arel_extensions/visitors/oracle.rb +136 -128
  86. data/lib/arel_extensions/visitors/oracle12.rb +16 -16
  87. data/lib/arel_extensions/visitors/postgresql.rb +147 -136
  88. data/lib/arel_extensions/visitors/sqlite.rb +83 -85
  89. data/lib/arel_extensions/visitors/to_sql.rb +143 -148
  90. data/lib/arel_extensions/visitors.rb +7 -7
  91. data/lib/arel_extensions.rb +46 -32
  92. data/test/arelx_test_helper.rb +14 -14
  93. data/test/database.yml +5 -5
  94. data/test/real_db_test.rb +78 -78
  95. data/test/support/fake_record.rb +1 -1
  96. data/test/test_comparators.rb +5 -5
  97. data/test/visitors/test_bulk_insert_oracle.rb +4 -4
  98. data/test/visitors/test_bulk_insert_sqlite.rb +4 -4
  99. data/test/visitors/test_bulk_insert_to_sql.rb +4 -4
  100. data/test/visitors/test_oracle.rb +14 -14
  101. data/test/visitors/test_to_sql.rb +82 -82
  102. data/test/with_ar/all_agnostic_test.rb +415 -313
  103. data/test/with_ar/insert_agnostic_test.rb +17 -14
  104. data/test/with_ar/test_bulk_sqlite.rb +4 -4
  105. data/test/with_ar/test_math_sqlite.rb +12 -12
  106. data/test/with_ar/test_string_mysql.rb +20 -20
  107. data/test/with_ar/test_string_sqlite.rb +20 -20
  108. data/version_v1.rb +1 -1
  109. data/version_v2.rb +1 -1
  110. metadata +2 -9
  111. data/.travis/oracle/download.js +0 -152
  112. data/.travis/oracle/download.sh +0 -30
  113. data/.travis/oracle/download_ojdbc.js +0 -116
  114. data/.travis/oracle/install.sh +0 -34
  115. data/.travis/setup_accounts.sh +0 -9
  116. data/.travis/sqlite3/extension-functions.sh +0 -6
  117. 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 == "rbx") || (RUBY_PLATFORM == 'java')) # not supported
11
- @env_db = (RUBY_PLATFORM == 'java' ? "jdbc-sqlite" : 'sqlite')
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, :precision => 20, :scale => 10
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, :precision => 20, :scale => 10
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 :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)
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 "SQL Server forces order?" # TODO
157
- assert_equal 83, User.select((@age.sum + 1).as("res"), User.arel_table[:id].sum).take(50).reorder(@age).first.res
158
- assert_equal 164, User.reorder(nil).select((@age.sum + @age.sum).as("res"), User.arel_table[:id].sum).take(50).first.res
159
- assert_equal 246, User.reorder(nil).select(((@age * 3).sum).as("res"), User.arel_table[:id].sum).take(50).first.res
160
- assert_equal 4234, User.reorder(nil).select(((@age * @age).sum).as("res"), User.arel_table[:id].sum).take(50).first.res
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("res")).take(50).first.res
163
- assert_equal 164, User.select((@age.sum + @age.sum).as("res")).take(50).first.res
164
- assert_equal 246, User.select((@age * 3).sum.as("res")).take(50).first.res
165
- assert_equal 4234, User.select(((@age * @age).sum).as("res")).take(50).first.res
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::Nodes.build_quoted('Test ') + @name)
193
+ assert_equal 'Test Laure', t(@laure, Arel.quoted('Test ') + @name)
186
194
 
187
- skip "No group_concat in SqlServer before 2017" if @env_db == 'mssql'
188
- assert_equal "Lucas Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat(' '))
189
- assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat(','))
190
- assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat)
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 "No order in group_concat in SqlLite" if $sqlite
193
- assert_equal "Arthur,Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@name.asc))
194
- assert_equal "Sophie,Lucas,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@name.desc))
195
- assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',[@score.asc,@name.asc]))
196
- assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@score.asc,@name.asc))
197
- assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',order: [@score.asc,@name.asc]))
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 "e2cf99ca82a7e829d2a4ac85c48154d0", t(@camille, @name.md5)
209
- assert_equal "c3d41bf5efb468a1bcce53bd53726c85", t(@lucas, @name.md5)
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("C"))
215
- assert_equal 0, t(@lucas, @name.locate("z"))
216
- assert_equal 5, t(@lucas, @name.locate("s"))
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 "SQL Server does not know about FIND_IN_SET" if @env_db == 'mssql'
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, ArelExtensions::Nodes::Case.new.when(@name >= 'Mest').then(1).else(0))
258
- assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name <= (@name + 'Z')).then(1).else(0))
259
- assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name > 'Mest').then(1).else(0))
260
- assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name < (@name + 'Z')).then(1).else(0))
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, ArelExtensions::Nodes::Case.new.when(@created_at >= '2014-01-01').then(1).else(0))
274
- assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2018-01-01').then(1).else(0))
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, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 10:10:10').then(1).else(0))
277
- assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 13:10:10').then(1).else(0))
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(ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0)).to_sql
280
- # puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0)).to_sql
281
- assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0))
282
- assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0))
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 "SQL Server does not know about REGEXP without extensions" if @env_db == 'mssql'
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 "LucaX", t(@lucas, @name.replace("s", "X"))
303
- assert_equal "replace", t(@lucas, @name.replace(@name, "replace"))
310
+ assert_equal 'LucaX', t(@lucas, @name.replace('s', 'X'))
311
+ assert_equal 'replace', t(@lucas, @name.replace(@name, 'replace'))
304
312
 
305
- skip "Sqlite does not seem to support regexp_replace" if $sqlite
306
- skip "SQL Server does not know about REGEXP without extensions" if @env_db == 'mssql'
307
- skip "Travis mysql version does not support REGEXP_REPLACE" if @env_db == 'mysql'
308
- assert_equal "LXcXs", t(@lucas, @name.replace(/[ua]/, "X"))
309
- assert_equal "LXcXs", t(@lucas, @name.regexp_replace(/[ua]/, "X"))
310
- assert_equal "LXcXs", t(@lucas, @name.regexp_replace('[ua]', "X"))
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 "TODO"
322
+ skip 'TODO'
315
323
  # skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
316
- assert_equal "LuCas", t(@lucas, @name.substring(1, @name.locate('c') - 1) + 'C' + @name.substring(@name.locate('c') + 1, @name.length))
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 "C540", t(@camille, @name.soundex)
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 "myung", t(@myung, @name.downcase)
329
- assert_equal "MYUNG", t(@myung, @name.upcase)
330
- assert_equal "myung", t(@myung, @name.upcase.downcase)
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 "Myung", t(@myung, @name.trim)
335
- assert_equal "Myung", t(@myung, @name.trim.ltrim.rtrim)
336
- assert_equal "Myun", t(@myung, @name.rtrim("g"))
337
- assert_equal "yung", t(@myung, @name.ltrim("M"))
338
- assert_equal "yung", t(@myung, (@name + "M").trim("M"))
339
- skip "Oracle does not accept multi char trim" if @env_db == 'oracle'
340
- assert_equal "", t(@myung, @name.rtrim(@name))
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
- 'mysql' => {
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
- tz = time_zones[ENV['DB']]
427
- skip "Unsupported timezone conversion for DB=#{ENV['DB']}" if tz.nil?
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 '2022/02/01 11:42:00', t(@lucas, Arel::Nodes.build_quoted('2022-02-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', tz['paris']))
434
- assert_equal '2022/08/01 12:42:00', t(@lucas, Arel::Nodes.build_quoted('2022-08-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', tz['paris']))
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, "default") + ' concat')
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("Myung").coalesce('ignored'))
443
- assert_equal 'Laure', t(@laure, @comments.coalesce("Laure"))
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 "Camille", t(@camille, @name.coalesce(nil, "default"))
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("Laure") + 10)
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("2012")).count
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("05")).count
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("05")).count
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("05")).count
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 "not yet implemented" if $sqlite
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,"2026-05-23"], t(@test,(@created_at + durPos).cast(:date))
530
- assert_includes [date3,"2006-05-23"], t(@test,(@created_at + durNeg).cast(:date))
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,"2016-06-15"], t(@test,(@created_at + @created_at.day).cast(:date))
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,"2016-04-30"], t(@test,(@created_at - @created_at.day).cast(:date))
637
+ assert_includes [date5, '2016-04-30'], t(@test, (@created_at - @created_at.day).cast(:date))
536
638
 
537
- assert_includes [datetime1 + 42.seconds,"2014-03-03 12:42:42 UTC"], t(@lucas,(@updated_at + @updated_at.minute))
538
- assert_includes [datetime1 - 42.seconds,"2014-03-03 12:41:18 UTC"], t(@lucas,(@updated_at - @updated_at.minute))
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,"2014-03-03 12:54:42 UTC"],
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,"2014-03-04 01:24:00 UTC"],
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 ["2024-03-03"], t(@lucas,(@updated_at + durPos).format('%Y-%m-%d'))
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 ["2004-03-03"], t(@lucas,(@updated_at - durPos).format('%Y-%m-%d'))
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 "5", t(@lucas, @age.cast(:string))
559
- skip "jdbc adapters does not work properly here (v52 works fine)" if RUBY_PLATFORM =~ /java/i
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("12:42:21")).then(1).else(0)) unless @env_db == 'oracle' || @env_db == 'mssql'
562
- assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle'
563
- assert_equal "20.16", t(@laure,@score.cast(:string)).gsub(/[0]*\z/,'')
564
- assert_equal "20.161", t(@laure,@score.cast(:string)+1).gsub(/[0]*1\z/,'1')
565
- assert_equal 21.16, t(@laure,@score.cast(:string).cast(:decimal)+1)
566
- assert_equal 21, t(@laure,@score.cast(:string).cast(:int)+1)
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,@updated_at.cast(:string)).class
670
+ assert_equal String, t(@lucas, @updated_at.cast(:string)).class
569
671
 
570
- assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
571
- assert_equal Time, t(@lucas,@updated_at.cast(:string).cast(:datetime)).class
572
- assert_equal Time, t(@lucas,@updated_at.cast(:time)).class
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 "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)).split('.').first unless @env_db == 'mssql' # locale dependent
576
- assert_equal Date.parse("2014-03-03"), t(@lucas,Arel::Nodes.build_quoted('2014-03-03').cast(:date))
577
- assert_equal Date.parse("5014-03-03"), t(@lucas,(@age.cast(:string) + '014-03-03').cast(:date))
578
- assert_equal Time.parse("2014-03-03 12:42:00 UTC"), t(@lucas,@updated_at.cast(:string).cast(:datetime))
579
- assert_equal Date.parse("2014-03-03"), t(@lucas,@updated_at.cast(:date))
580
- assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' # DateTime
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 "Test", User.select(@name).where(@age.is_null).first.name
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 "SophiePhan", t(@sophie, @name + "Phan")
597
- assert_equal "Sophie2", t(@sophie, @name + 2)
598
- assert_equal "Sophie1997-06-15", t(@sophie, @name + d)
599
- assert_equal "Sophie15", t(@sophie, @name + @age)
600
- assert_equal "SophieSophie", t(@sophie, @name + @name)
601
- assert_equal "SophieSophieSophie", t(@sophie, @name + @name + @name)
602
- assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name.concat(@name)))
603
- assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name).concat(@name))
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 "Sophie2016-05-23", t(@sophie, @name + @created_at) unless @env_db == 'oracle'
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 + "1").eq(6)).count
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 "2016-05-24", t(@myung, @created_at + 1).to_date.to_s
613
- assert_equal "2016-05-25", t(@myung, @created_at + 2.day).to_date.to_s
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("res")).first.res.abs.to_i
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 - "10").eq(50)).count
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 - "9.5").eq(50.5)).count
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 "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
664
- assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
665
- 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
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 " Works with SQLite if the version used knows printf" if $sqlite
671
-
672
- assert_equal "Wrong Format", t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
673
- assert_equal "AZERTY65,62", t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
674
- assert_equal "65,62AZERTY", t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
675
- assert_equal "$ 65.62 €", t(@arthur, @score.format_number("$ %.2f €","en_US"))
676
- assert_equal "$ 66 €", t(@arthur, @score.format_number("$ %.0f €","en_US"))
677
- assert_equal "$ 0065,62 €", t(@arthur, @score.format_number("$ %07.2f €","fr_FR"))
678
- assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ %-07.2f €","fr_FR"))
679
- assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
680
- assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
681
- assert_equal "$ 65,6 €", t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
682
- assert_equal "$ +65,62 €", t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
683
- assert_equal "$ +065,62 €", t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
684
- assert_includes ["$ 6,56e1 €","$ 6,56e+01 €"], t(@arthur, @score.format_number("$ %.2e €","fr_FR"))
685
- assert_includes ["$ 6,56E1 €","$ 6,56E+01 €"], t(@arthur, @score.format_number("$ %.2E €","fr_FR"))
686
- assert_includes ["$ 6,562E1 €","$ 6,562E+01 €"], t(@arthur, @score.format_number("$ %.3E €","fr_FR"))
687
- 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)
688
- assert_equal "123456765,6", t(@arthur, (@score+123456700).format_number("%.1f","fr_FR")).gsub("\u00A0","") # because SqlServer does it like no one else
689
- assert_equal "123,456,765.6", t(@arthur, (@score+123456700).format_number("%.1f","en_US"))
690
- assert_equal " 123,456,765.6", t(@arthur, (@score+123456700).format_number("%16.1f","en_US"))
691
- assert_equal "$ 0,00 €", t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
692
- assert_equal "$ 0,00 €", t(@arthur, (@score-65.62).format_number("$ %.2f €","fr_FR"))
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 "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
697
- skip "Not finished" if @env_db == 'mysql'
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 "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrêté")).then("1").else("0"))
702
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
703
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("àrrétè")).then("1").else("0"))
704
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
705
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrete")).then("1").else("0"))
706
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrêté")).then("1").else("0"))
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 "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrêté")).then("1").else("0"))
709
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
710
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
711
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arretez")).then("1").else("0"))
712
- if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
713
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrete")).then("1").else("0"))
714
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrêté")).then("1").else("0"))
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 "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrêté")).then("1").else("0"))
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 "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
721
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("àrrétè")).then("1").else("0"))
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 "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arretez")).then("1").else("0"))
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 "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrete")).then("1").else("0"))
827
+ assert_equal '0', t(@arthur, Arel.when(@comments.imatches('Arrete')).then('1').else('0'))
726
828
  end
727
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrêté")).then("1").else("0"))
829
+ assert_equal '1', t(@arthur, Arel.when(@comments.imatches('Arrêté')).then('1').else('0'))
728
830
  # AS & CS
729
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrêté")).then("1").else("0"))
730
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
731
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
732
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arretez")).then("1").else("0"))
733
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrete")).then("1").else("0"))
734
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrêté")).then("1").else("0"))
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(:name => User.select(:name).order(:name)).count
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) # MySql can't have limit in IN subquery
742
- assert_equal 2, User.where(:name => User.select(:name).order(:name).limit(2)).count
743
- # assert_equal 6, User.where(:name => User.select(:name).order(:name).offset(2)).count
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
- assert_equal true, @myung.where(@age.in(1)).blank?
749
- assert_equal false, @myung.where(@age.in(23)).blank?
750
- assert_equal true, @myung.where(@age.in([1])).blank?
751
- assert_equal true, @myung.where(@age.in([1,2])).blank?
752
- assert_equal false, @myung.where(@age.in([1,23])).blank?
753
- assert_equal true, @myung.where(@age.in(nil)).blank?
754
- assert_equal true, @myung.where(@age.in([nil])).blank?
755
- assert_equal true, @myung.where(@age.in([nil,1])).blank?
756
- assert_equal false, @myung.where(@age.in([nil,23])).blank?
757
- assert_equal true, @myung.where(@age.in([nil,1,2])).blank?
758
- assert_equal false, @myung.where(@age.in([nil,1,23])).blank?
759
- assert_equal true, @test.where(@age.in(1)).blank?
760
- assert_equal true, @test.where(@age.in([1])).blank?
761
- assert_equal true, @test.where(@age.in([1,2])).blank?
762
- assert_equal false, @test.where(@age.in(nil)).blank?
763
- assert_equal false, @test.where(@age.in([nil])).blank?
764
- assert_equal false, @test.where(@age.in([nil,1])).blank?
765
- assert_equal false, @test.where(@age.in([nil,1,2])).blank?
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
- assert_equal false, @myung.where(@age.is_not_null).blank?
780
- assert_equal true, @test.where(@age.is_not_null).blank?
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
- assert_equal false, @myung.where(@age.not_in(1)).blank?
785
- assert_equal true, @myung.where(@age.not_in(23)).blank?
786
- assert_equal false, @myung.where(@age.not_in([1])).blank?
787
- assert_equal false, @myung.where(@age.not_in([1,2])).blank?
788
- assert_equal true, @myung.where(@age.not_in([1,23])).blank?
789
- assert_equal false, @myung.where(@age.not_in(nil)).blank?
790
- assert_equal false, @myung.where(@age.not_in([nil])).blank?
791
- assert_equal false, @myung.where(@age.not_in([nil,1])).blank?
792
- assert_equal true, @myung.where(@age.not_in([nil,23])).blank?
793
- assert_equal false, @myung.where(@age.not_in([nil,1,2])).blank?
794
- assert_equal true, @myung.where(@age.not_in([nil,1,23])).blank?
795
-
796
- assert_equal false, @myung.where(@age.not_in(1..2)).blank?
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
- # assert_equal false , @test.where(@age.not_in(1)).blank?
800
- # assert_equal false , @test.where(@age.not_in([1])).blank?
801
- # assert_equal false , @test.where(@age.not_in([1,2])).blank?
802
- # assert_equal true , @test.where(@age.not_in(nil)).blank?
803
- # assert_equal true , @test.where(@age.not_in([nil])).blank?
804
- # assert_equal true , @test.where(@age.not_in([nil,1])).blank?
805
- # assert_equal true , @test.where(@age.not_in([nil,1,2])).blank?
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 "We should modify the visitor of IN to make it work" if $sqlite || @env_db == 'mssql'
810
- assert_equal 2, User.where(Arel.tuple(@name,@age).in(Arel.tuple('Myung',23),Arel.tuple('Arthur',21))).count
811
- assert_equal 1, User.where(Arel.tuple(@name,@age).in(Arel.tuple('Myung',23))).count
812
- assert_equal 0, User.where(Arel.tuple(@name,@age).in([])).count
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 "Not Yet Implemented" # if !['postgresql'].include?(@env_db)
840
- assert_equal 2, User.select(@score.std(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
841
- assert_equal 2, User.select(@score.variance(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
842
- assert_equal 2, User.select(@score.sum(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
843
- 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
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 "Not Yet Implemented" if $sqlite
848
- assert_equal 0, t(@arthur,@name.levenshtein_distance("Arthur"))
849
- assert_equal 2, t(@arthur,@name.levenshtein_distance("Artoor"))
850
- assert_equal 1, t(@arthur,@name.levenshtein_distance("Artehur"))
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 ["Arthur","Arthur"], parse_json(t(@arthur,Arel.json(@name,@name)))
858
- assert_equal ({"Arthur" => "Arthur", "Arthur2" => "ArthurArthur"}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => @name+@name})))
859
- assert_equal ({"Arthur" => "Arthur","Arthur2" => 1}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => 1})))
860
- assert_equal ([{"age" => 21},{"name" => "Arthur","score" => 65.62}]), parse_json(t(@arthur,Arel.json([{age: @age},{name: @name,score: @score}])))
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 ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure"}),
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 ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure", "Laure"=>25, "Lucas"=>5, "Myung"=>23, "Sophie"=>15}),
866
- parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name,@name => @age}).group(false)))
867
- assert_equal ([{"5" => "Lucas"},{ "15" => "Sophie"},{ "23" => "Myung"},{ "25" => "Laure"}]),
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 "Not Yet Implemented" if $sqlite || ['oracle','mssql'].include?(@env_db)
975
+ skip 'Not Yet Implemented' if $sqlite || ['oracle', 'mssql'].include?(@env_db)
874
976
  # get
875
- h1 = Arel.json({@name => @name+@name,@name+"2" => 1})
876
- assert_equal "ArthurArthur", parse_json(t(@arthur,h1.get(@name)))
877
- h2 = Arel.json([{age: @age},{name: @name,score: @score}])
878
- assert_equal ({"age" => 21}), parse_json(t(@arthur,h2.get(0)))
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 ({"Arthur" => ["toto", "tata"], "Arthur2" => 1}), parse_json(t(@arthur,h1.set(@name, ['toto','tata'])))
883
- assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.set(@name+"3",2)))
884
- assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => nil}), parse_json(t(@arthur,h1.set(@name+"3",nil)))
885
- assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => {"a" => 2}}), parse_json(t(@arthur,h1.set(@name+"3",{a: 2})))
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 ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata']},{@name+"3" => 2})))
888
- assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata'], @name+"3" => 2})))
889
- assert_equal ({"Arthur" => "ArthurArthur","Arthur2" => 1}), parse_json(t(@arthur,h1.merge({})))
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["NaMe"] || name["name"] # because of Oracle
895
- assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes["Na Me"]
896
- assert_equal 'ArthurArthur', @arthur.select((@name+@name).as('Na-Me')).first.attributes["Na-Me"]
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