arel_extensions 2.1.3 → 2.1.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 +1 -2
  7. data/Rakefile +4 -4
  8. data/TODO +0 -1
  9. data/arel_extensions.gemspec +11 -11
  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 +0 -0
  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 +127 -121
  84. data/lib/arel_extensions/visitors/mysql.rb +137 -145
  85. data/lib/arel_extensions/visitors/oracle.rb +128 -128
  86. data/lib/arel_extensions/visitors/oracle12.rb +16 -16
  87. data/lib/arel_extensions/visitors/postgresql.rb +139 -137
  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 -13
  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 +364 -299
  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
@@ -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::Nodes.build_quoted('2022-02-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', { tz['utc'] => 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['utc'] => tz['paris'] }))
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::Nodes.build_quoted('2022-02-01 10:42:00').cast(:datetime).format('%Y/%m/%d %H:%M:%S', tz['paris']))
437
- 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']))
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::Nodes.build_quoted(date).cast(:datetime).format('%V'))
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::Nodes.build_quoted(date).cast(:datetime).format('%G'))
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, "default") + ' concat')
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("Myung").coalesce('ignored'))
480
- 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'))
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 "Camille", t(@camille, @name.coalesce(nil, "default"))
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("Laure") + 10)
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("2012")).count
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("05")).count
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("05")).count
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("05")).count
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 "not yet implemented" if $sqlite
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,"2026-05-23"], t(@test,(@created_at + durPos).cast(:date))
567
- 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))
568
633
 
569
634
  # puts (@created_at + @created_at.day).cast(:date).to_sql
570
- 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))
571
636
  # puts (@created_at - @created_at.day).cast(:date).to_sql
572
- 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))
573
638
 
574
- assert_includes [datetime1 + 42.seconds,"2014-03-03 12:42:42 UTC"], t(@lucas,(@updated_at + @updated_at.minute))
575
- 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))
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,"2014-03-03 12:54:42 UTC"],
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,"2014-03-04 01:24:00 UTC"],
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 ["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'))
585
650
  # puts (@updated_at - durPos).to_sql
586
- 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'))
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 "5", t(@lucas, @age.cast(:string))
596
- 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
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("12:42:21")).then(1).else(0)) unless @env_db == 'oracle' || @env_db == 'mssql'
599
- assert_equal 1, t(@laure,Arel.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle'
600
- assert_equal "20.16", t(@laure,@score.cast(:string)).gsub(/[0]*\z/,'')
601
- assert_equal "20.161", t(@laure,@score.cast(:string)+1).gsub(/[0]*1\z/,'1')
602
- assert_equal 21.16, t(@laure,@score.cast(:string).cast(:decimal)+1)
603
- 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)
604
669
 
605
- assert_equal String, t(@lucas,@updated_at.cast(:string)).class
670
+ assert_equal String, t(@lucas, @updated_at.cast(:string)).class
606
671
 
607
- assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
608
- assert_equal Time, t(@lucas,@updated_at.cast(:string).cast(:datetime)).class
609
- 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
610
675
 
611
676
  # mysql adapter in rails7 adds some infos we just squeeze here
612
- assert_equal "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)).split('.').first unless @env_db == 'mssql' # locale dependent
613
- assert_equal Date.parse("2014-03-03"), t(@lucas,Arel::Nodes.build_quoted('2014-03-03').cast(:date))
614
- assert_equal Date.parse("5014-03-03"), t(@lucas,(@age.cast(:string) + '014-03-03').cast(:date))
615
- assert_equal Time.parse("2014-03-03 12:42:00 UTC"), t(@lucas,@updated_at.cast(:string).cast(:datetime))
616
- assert_equal Date.parse("2014-03-03"), t(@lucas,@updated_at.cast(:date))
617
- 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
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 "Test", User.select(@name).where(@age.is_null).first.name
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 "SophiePhan", t(@sophie, @name + "Phan")
634
- assert_equal "Sophie2", t(@sophie, @name + 2)
635
- assert_equal "Sophie1997-06-15", t(@sophie, @name + d)
636
- assert_equal "Sophie15", t(@sophie, @name + @age)
637
- assert_equal "SophieSophie", t(@sophie, @name + @name)
638
- assert_equal "SophieSophieSophie", t(@sophie, @name + @name + @name)
639
- assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name.concat(@name)))
640
- 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))
641
706
  # FIXME: should work as expected in Oracle
642
- 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'
643
708
  # concat Integer
644
709
  assert_equal 1, User.where((@age + 10).eq(33)).count
645
- assert_equal 1, User.where((@age + "1").eq(6)).count
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 "2016-05-24", t(@myung, @created_at + 1).to_date.to_s
650
- 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
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("res")).first.res.abs.to_i
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 - "10").eq(50)).count
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 - "9.5").eq(50.5)).count
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 "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
701
- assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
702
- 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
703
768
  end
704
769
 
705
770
  def test_format_numbers
706
771
  # score of Arthur = 65.62
707
- skip " Works with SQLite if the version used knows printf" if $sqlite
708
-
709
- assert_equal "Wrong Format", t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
710
- assert_equal "AZERTY65,62", t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
711
- assert_equal "65,62AZERTY", t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
712
- assert_equal "$ 65.62 €", t(@arthur, @score.format_number("$ %.2f €","en_US"))
713
- assert_equal "$ 66 €", t(@arthur, @score.format_number("$ %.0f €","en_US"))
714
- assert_equal "$ 0065,62 €", t(@arthur, @score.format_number("$ %07.2f €","fr_FR"))
715
- assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ %-07.2f €","fr_FR"))
716
- assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
717
- assert_equal "$ 65,62 €", t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
718
- assert_equal "$ 65,6 €", t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
719
- assert_equal "$ +65,62 €", t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
720
- assert_equal "$ +065,62 €", t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
721
- assert_includes ["$ 6,56e1 €","$ 6,56e+01 €"], t(@arthur, @score.format_number("$ %.2e €","fr_FR"))
722
- assert_includes ["$ 6,56E1 €","$ 6,56E+01 €"], t(@arthur, @score.format_number("$ %.2E €","fr_FR"))
723
- assert_includes ["$ 6,562E1 €","$ 6,562E+01 €"], t(@arthur, @score.format_number("$ %.3E €","fr_FR"))
724
- 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)
725
- assert_equal "123456765,6", t(@arthur, (@score+123456700).format_number("%.1f","fr_FR")).gsub("\u00A0","") # because SqlServer does it like no one else
726
- assert_equal "123,456,765.6", t(@arthur, (@score+123456700).format_number("%.1f","en_US"))
727
- assert_equal " 123,456,765.6", t(@arthur, (@score+123456700).format_number("%16.1f","en_US"))
728
- assert_equal "$ 0,00 €", t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
729
- 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'))
730
795
  end
731
796
 
732
797
  def test_accent_insensitive
733
- skip "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
734
- 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'
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 "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrêté")).then("1").else("0"))
739
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
740
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("àrrétè")).then("1").else("0"))
741
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
742
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrete")).then("1").else("0"))
743
- 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'))
744
809
  # AI & CS
745
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrêté")).then("1").else("0"))
746
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
747
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
748
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arretez")).then("1").else("0"))
749
- if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
750
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrete")).then("1").else("0"))
751
- 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'))
752
817
  end
753
818
  end
754
819
  # AS & CI
755
- 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'))
756
821
  if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
757
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
758
- 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'))
759
824
  end
760
- 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'))
761
826
  if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
762
- 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'))
763
828
  end
764
- 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'))
765
830
  # AS & CS
766
- assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrêté")).then("1").else("0"))
767
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
768
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
769
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arretez")).then("1").else("0"))
770
- assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrete")).then("1").else("0"))
771
- 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'))
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(:name => User.select(:name).order(:name)).count
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) # MySql can't have limit in IN subquery
779
- assert_equal 2, User.where(:name => User.select(:name).order(:name).limit(2)).count
780
- # 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
781
846
  end
782
847
  end
783
848
 
784
849
  def test_in_with_nil
785
- assert_equal true, @myung.where(@age.in(1)).blank?
786
- assert_equal false, @myung.where(@age.in(23)).blank?
787
- assert_equal true, @myung.where(@age.in([1])).blank?
788
- assert_equal true, @myung.where(@age.in([1,2])).blank?
789
- assert_equal false, @myung.where(@age.in([1,23])).blank?
790
- assert_equal true, @myung.where(@age.in(nil)).blank?
791
- assert_equal true, @myung.where(@age.in([nil])).blank?
792
- assert_equal true, @myung.where(@age.in([nil,1])).blank?
793
- assert_equal false, @myung.where(@age.in([nil,23])).blank?
794
- assert_equal true, @myung.where(@age.in([nil,1,2])).blank?
795
- assert_equal false, @myung.where(@age.in([nil,1,23])).blank?
796
- assert_equal true, @test.where(@age.in(1)).blank?
797
- assert_equal true, @test.where(@age.in([1])).blank?
798
- assert_equal true, @test.where(@age.in([1,2])).blank?
799
- assert_equal false, @test.where(@age.in(nil)).blank?
800
- assert_equal false, @test.where(@age.in([nil])).blank?
801
- assert_equal false, @test.where(@age.in([nil,1])).blank?
802
- 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?
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
- assert_equal false, @myung.where(@age.is_not_null).blank?
817
- 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?
818
883
  end
819
884
 
820
885
  def test_not_in_with_nil
821
- assert_equal false, @myung.where(@age.not_in(1)).blank?
822
- assert_equal true, @myung.where(@age.not_in(23)).blank?
823
- assert_equal false, @myung.where(@age.not_in([1])).blank?
824
- assert_equal false, @myung.where(@age.not_in([1,2])).blank?
825
- assert_equal true, @myung.where(@age.not_in([1,23])).blank?
826
- assert_equal false, @myung.where(@age.not_in(nil)).blank?
827
- assert_equal false, @myung.where(@age.not_in([nil])).blank?
828
- assert_equal false, @myung.where(@age.not_in([nil,1])).blank?
829
- assert_equal true, @myung.where(@age.not_in([nil,23])).blank?
830
- assert_equal false, @myung.where(@age.not_in([nil,1,2])).blank?
831
- assert_equal true, @myung.where(@age.not_in([nil,1,23])).blank?
832
-
833
- 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?
834
899
 
835
900
  # if the column is null, the entry will never be selected with not in (like every DBMS does)
836
- # assert_equal false , @test.where(@age.not_in(1)).blank?
837
- # assert_equal false , @test.where(@age.not_in([1])).blank?
838
- # assert_equal false , @test.where(@age.not_in([1,2])).blank?
839
- # assert_equal true , @test.where(@age.not_in(nil)).blank?
840
- # assert_equal true , @test.where(@age.not_in([nil])).blank?
841
- # assert_equal true , @test.where(@age.not_in([nil,1])).blank?
842
- # 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?
843
908
  end
844
909
 
845
910
  def test_in_on_grouping
846
- skip "We should modify the visitor of IN to make it work" if $sqlite || @env_db == 'mssql'
847
- assert_equal 2, User.where(Arel.tuple(@name,@age).in(Arel.tuple('Myung',23),Arel.tuple('Arthur',21))).count
848
- assert_equal 1, User.where(Arel.tuple(@name,@age).in(Arel.tuple('Myung',23))).count
849
- 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
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 "Not Yet Implemented" # if !['postgresql'].include?(@env_db)
877
- assert_equal 2, User.select(@score.std(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
878
- assert_equal 2, User.select(@score.variance(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
879
- assert_equal 2, User.select(@score.sum(group: Arel.when(@name > "M").then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
880
- 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
881
946
  end
882
947
 
883
948
  def test_levenshtein_distance
884
- skip "Not Yet Implemented" if $sqlite
885
- assert_equal 0, t(@arthur,@name.levenshtein_distance("Arthur"))
886
- assert_equal 2, t(@arthur,@name.levenshtein_distance("Artoor"))
887
- 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'))
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 ["Arthur","Arthur"], parse_json(t(@arthur,Arel.json(@name,@name)))
895
- assert_equal ({"Arthur" => "Arthur", "Arthur2" => "ArthurArthur"}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => @name+@name})))
896
- assert_equal ({"Arthur" => "Arthur","Arthur2" => 1}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => 1})))
897
- 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}])))
898
963
 
899
964
  # aggregate
900
- assert_equal ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure"}),
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 ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure", "Laure"=>25, "Lucas"=>5, "Myung"=>23, "Sophie"=>15}),
903
- parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name,@name => @age}).group(false)))
904
- assert_equal ([{"5" => "Lucas"},{ "15" => "Sophie"},{ "23" => "Myung"},{ "25" => "Laure"}]),
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 "Not Yet Implemented" if $sqlite || ['oracle','mssql'].include?(@env_db)
975
+ skip 'Not Yet Implemented' if $sqlite || ['oracle', 'mssql'].include?(@env_db)
911
976
  # get
912
- h1 = Arel.json({@name => @name+@name,@name+"2" => 1})
913
- assert_equal "ArthurArthur", parse_json(t(@arthur,h1.get(@name)))
914
- h2 = Arel.json([{age: @age},{name: @name,score: @score}])
915
- assert_equal ({"age" => 21}), parse_json(t(@arthur,h2.get(0)))
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 ({"Arthur" => ["toto", "tata"], "Arthur2" => 1}), parse_json(t(@arthur,h1.set(@name, ['toto','tata'])))
920
- assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.set(@name+"3",2)))
921
- assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => nil}), parse_json(t(@arthur,h1.set(@name+"3",nil)))
922
- 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})))
923
988
  # merge
924
- assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata']},{@name+"3" => 2})))
925
- assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata'], @name+"3" => 2})))
926
- 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({})))
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["NaMe"] || name["name"] # because of Oracle
932
- assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes["Na Me"]
933
- 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']
934
999
  end
935
1000
 
936
1001
  def test_exists_in_subquery