arel_extensions 1.3.8 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +2 -1
  3. data/.gitignore +6 -7
  4. data/.rubocop.yml +3 -67
  5. data/.travis/oracle/download.js +152 -0
  6. data/.travis/oracle/download.sh +30 -0
  7. data/.travis/oracle/download_ojdbc.js +116 -0
  8. data/.travis/oracle/install.sh +34 -0
  9. data/.travis/setup_accounts.sh +9 -0
  10. data/.travis/sqlite3/extension-functions.sh +6 -0
  11. data/.travis.yml +223 -0
  12. data/Gemfile +21 -16
  13. data/README.md +90 -282
  14. data/Rakefile +30 -48
  15. data/TODO +1 -0
  16. data/appveyor.yml +22 -60
  17. data/arel_extensions.gemspec +14 -13
  18. data/functions.html +3 -3
  19. data/gemfiles/rails3.gemfile +10 -10
  20. data/gemfiles/rails4.gemfile +29 -0
  21. data/gemfiles/rails5_0.gemfile +29 -0
  22. data/gemfiles/rails5_1_4.gemfile +14 -14
  23. data/gemfiles/rails5_2.gemfile +14 -16
  24. data/init/mssql.sql +4 -4
  25. data/init/mysql.sql +38 -38
  26. data/init/oracle.sql +0 -0
  27. data/init/postgresql.sql +21 -21
  28. data/init/sqlite.sql +0 -0
  29. data/lib/arel_extensions/attributes.rb +3 -4
  30. data/lib/arel_extensions/boolean_functions.rb +14 -53
  31. data/lib/arel_extensions/common_sql_functions.rb +17 -16
  32. data/lib/arel_extensions/comparators.rb +28 -27
  33. data/lib/arel_extensions/date_duration.rb +13 -17
  34. data/lib/arel_extensions/insert_manager.rb +15 -18
  35. data/lib/arel_extensions/math.rb +53 -55
  36. data/lib/arel_extensions/math_functions.rb +39 -46
  37. data/lib/arel_extensions/nodes/abs.rb +1 -0
  38. data/lib/arel_extensions/nodes/blank.rb +2 -1
  39. data/lib/arel_extensions/nodes/case.rb +19 -20
  40. data/lib/arel_extensions/nodes/cast.rb +8 -10
  41. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  42. data/lib/arel_extensions/nodes/coalesce.rb +4 -3
  43. data/lib/arel_extensions/nodes/collate.rb +10 -9
  44. data/lib/arel_extensions/nodes/concat.rb +18 -9
  45. data/lib/arel_extensions/nodes/date_diff.rb +26 -42
  46. data/lib/arel_extensions/nodes/duration.rb +3 -0
  47. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  48. data/lib/arel_extensions/nodes/floor.rb +1 -1
  49. data/lib/arel_extensions/nodes/format.rb +8 -35
  50. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  51. data/lib/arel_extensions/nodes/function.rb +37 -42
  52. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  53. data/lib/arel_extensions/nodes/json.rb +39 -52
  54. data/lib/arel_extensions/nodes/length.rb +0 -5
  55. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  56. data/lib/arel_extensions/nodes/locate.rb +2 -1
  57. data/lib/arel_extensions/nodes/log10.rb +2 -1
  58. data/lib/arel_extensions/nodes/matches.rb +7 -5
  59. data/lib/arel_extensions/nodes/md5.rb +1 -0
  60. data/lib/arel_extensions/nodes/power.rb +5 -5
  61. data/lib/arel_extensions/nodes/rand.rb +1 -0
  62. data/lib/arel_extensions/nodes/repeat.rb +5 -3
  63. data/lib/arel_extensions/nodes/replace.rb +8 -16
  64. data/lib/arel_extensions/nodes/round.rb +6 -5
  65. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  66. data/lib/arel_extensions/nodes/std.rb +21 -18
  67. data/lib/arel_extensions/nodes/substring.rb +16 -8
  68. data/lib/arel_extensions/nodes/then.rb +1 -1
  69. data/lib/arel_extensions/nodes/trim.rb +6 -4
  70. data/lib/arel_extensions/nodes/union.rb +8 -5
  71. data/lib/arel_extensions/nodes/union_all.rb +7 -4
  72. data/lib/arel_extensions/nodes/wday.rb +4 -0
  73. data/lib/arel_extensions/nodes.rb +1 -1
  74. data/lib/arel_extensions/null_functions.rb +5 -19
  75. data/lib/arel_extensions/predications.rb +43 -44
  76. data/lib/arel_extensions/railtie.rb +5 -5
  77. data/lib/arel_extensions/set_functions.rb +7 -5
  78. data/lib/arel_extensions/string_functions.rb +29 -59
  79. data/lib/arel_extensions/tasks.rb +6 -6
  80. data/lib/arel_extensions/version.rb +1 -1
  81. data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
  82. data/lib/arel_extensions/visitors/mssql.rb +180 -375
  83. data/lib/arel_extensions/visitors/mysql.rb +212 -350
  84. data/lib/arel_extensions/visitors/oracle.rb +178 -220
  85. data/lib/arel_extensions/visitors/oracle12.rb +31 -18
  86. data/lib/arel_extensions/visitors/postgresql.rb +173 -256
  87. data/lib/arel_extensions/visitors/sqlite.rb +126 -140
  88. data/lib/arel_extensions/visitors/to_sql.rb +237 -298
  89. data/lib/arel_extensions/visitors.rb +58 -82
  90. data/lib/arel_extensions.rb +31 -182
  91. data/test/database.yml +7 -15
  92. data/test/helper.rb +18 -0
  93. data/test/real_db_test.rb +116 -105
  94. data/test/support/fake_record.rb +3 -3
  95. data/test/test_comparators.rb +17 -14
  96. data/test/visitors/test_bulk_insert_oracle.rb +11 -11
  97. data/test/visitors/test_bulk_insert_sqlite.rb +13 -12
  98. data/test/visitors/test_bulk_insert_to_sql.rb +13 -11
  99. data/test/visitors/test_oracle.rb +55 -55
  100. data/test/visitors/test_to_sql.rb +226 -419
  101. data/test/with_ar/all_agnostic_test.rb +365 -709
  102. data/test/with_ar/insert_agnostic_test.rb +21 -27
  103. data/test/with_ar/test_bulk_sqlite.rb +16 -17
  104. data/test/with_ar/test_math_sqlite.rb +26 -26
  105. data/test/with_ar/test_string_mysql.rb +33 -31
  106. data/test/with_ar/test_string_sqlite.rb +34 -30
  107. metadata +34 -33
  108. data/.github/workflows/ruby.yml +0 -360
  109. data/NEWS.md +0 -32
  110. data/dev/compose.yaml +0 -29
  111. data/gemfiles/rails4_2.gemfile +0 -38
  112. data/gemfiles/rails5.gemfile +0 -29
  113. data/gemfiles/rails6.gemfile +0 -30
  114. data/gemfiles/rails6_1.gemfile +0 -30
  115. data/gemfiles/rails7.gemfile +0 -23
  116. data/gemfiles/rails7_1.gemfile +0 -22
  117. data/gemspecs/arel_extensions-v1.gemspec +0 -27
  118. data/gemspecs/arel_extensions-v2.gemspec +0 -27
  119. data/generate_gems.sh +0 -15
  120. data/lib/arel_extensions/aliases.rb +0 -14
  121. data/lib/arel_extensions/helpers.rb +0 -57
  122. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  123. data/lib/arel_extensions/nodes/formatted_date.rb +0 -42
  124. data/lib/arel_extensions/nodes/rollup.rb +0 -36
  125. data/lib/arel_extensions/nodes/select.rb +0 -10
  126. data/lib/arel_extensions/nodes/sum.rb +0 -7
  127. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  128. data/test/arelx_test_helper.rb +0 -67
  129. data/version_v1.rb +0 -3
  130. data/version_v2.rb +0 -3
@@ -1,24 +1,21 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
  require 'date'
3
3
 
4
4
  module ArelExtensions
5
- module WithAr
5
+ module WthAr
6
+
6
7
  class ListTest < Minitest::Test
7
8
  require 'minitest/pride'
8
9
  def connect_db
9
10
  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 (DB: #{ENV['DB']}, RUBY_ENGINE: #{RUBY_ENGINE}, RUBY_PLATFORM: #{RUBY_PLATFORM})"
11
+ if ENV['DB'] == 'oracle' && ((defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx") || (RUBY_PLATFORM == 'java')) # not supported
12
+ @env_db = (RUBY_PLATFORM == 'java' ? "jdbc-sqlite" : 'sqlite')
13
+ skip "Platform not supported"
13
14
  else
14
15
  @env_db = ENV['DB']
15
16
  end
16
17
  ActiveRecord::Base.establish_connection(@env_db.try(:to_sym) || (RUBY_PLATFORM == 'java' ? :"jdbc-sqlite" : :sqlite))
17
- if ActiveRecord::VERSION::MAJOR >= 7
18
- ActiveRecord.default_timezone = :utc
19
- else
20
- ActiveRecord::Base.default_timezone = :utc
21
- end
18
+ ActiveRecord::Base.default_timezone = :utc
22
19
  @cnx = ActiveRecord::Base.connection
23
20
  $sqlite = @cnx.adapter_name =~ /sqlite/i
24
21
  $load_extension_disabled ||= false
@@ -33,14 +30,14 @@ module ArelExtensions
33
30
  t.column :name, :string
34
31
  t.column :comments, :text
35
32
  t.column :created_at, :date
36
- t.column :updated_at, :datetime, precision: nil
33
+ t.column :updated_at, :datetime
37
34
  t.column :duration, :time
38
35
  t.column :other, :string
39
- t.column :score, :decimal, precision: 20, scale: 10
36
+ t.column :score, :decimal, :precision => 20, :scale => 10
40
37
  end
41
38
  @cnx.drop_table(:product_tests) rescue nil
42
39
  @cnx.create_table :product_tests do |t|
43
- t.column :price, :decimal, precision: 20, scale: 10
40
+ t.column :price, :decimal, :precision => 20, :scale => 10
44
41
  end
45
42
  end
46
43
 
@@ -55,26 +52,23 @@ module ArelExtensions
55
52
  d = Date.new(2016, 5, 23)
56
53
  connect_db
57
54
  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)
76
- u = User.create age: nil, name: 'nilly', created_at: nil, score: nil
77
- @nilly = User.where(id: u.id)
55
+ u = User.create :age => 5, :name => "Lucas", :created_at => d, :score => 20.16, :updated_at => Time.utc(2014, 3, 3, 12, 42, 0)
56
+ @lucas = User.where(:id => u.id)
57
+ u = User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
58
+ @sophie = User.where(:id => u.id)
59
+ u = User.create :age => 20, :name => "Camille", :created_at => d, :score => -20.16, :comments => ''
60
+ @camille = User.where(:id => u.id)
61
+ u = User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62, :comments => 'arrêté'
62
+ @arthur = User.where(:id => u.id)
63
+ u = User.create :age => 23, :name => "Myung", :created_at => d, :score => 20.16, :comments => ' '
64
+ @myung = User.where(:id => u.id)
65
+ 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)
66
+ @laure = User.where(:id => u.id)
67
+ u = User.create :age => nil, :name => "Test", :created_at => d, :score => 1.62, :other => 'toto'
68
+ @test = User.where(:id => u.id)
69
+ u = User.create :age => -42, :name => "Negatif", :comments => '1,22,3,42,2', :created_at => d, :updated_at => d.to_time, :score => 0.17
70
+ @neg = User.where(:id => u.id)
71
+
78
72
 
79
73
  @age = User.arel_table[:age]
80
74
  @name = User.arel_table[:name]
@@ -100,7 +94,7 @@ module ArelExtensions
100
94
  scope.select(node.as('res')).to_a.first.res
101
95
  end
102
96
 
103
- # manage the difference between adapters that handle or not json type
97
+ #manage the difference between adapters that handle or not json type
104
98
  def parse_json(h)
105
99
  if @env_db != 'postgresql'
106
100
  JSON.parse("{\"res\":#{h}}")['res']
@@ -109,14 +103,6 @@ module ArelExtensions
109
103
  end
110
104
  end
111
105
 
112
- # Connection and column info
113
- def test_column_of
114
- assert_nil Arel.column_of('chupa', 'maflavla'), 'Non-existent table and column should return nil'
115
- assert_nil Arel.column_of('chupa', 'updated_at'), 'Non-existent table but existent column should return nil'
116
- assert_nil Arel.column_of('user_tests', 'maflavla'), 'Existent table but non-existent column should return nil'
117
- assert_equal 'updated_at', Arel.column_of('user_tests', 'updated_at').name, 'An existing column name should be returned'
118
- end
119
-
120
106
  # Math Functions
121
107
  def test_classical_arel
122
108
  assert_in_epsilon 42.16, t(@laure, @score + 22), 0.01
@@ -130,28 +116,25 @@ module ArelExtensions
130
116
  end
131
117
 
132
118
  def test_ceil
133
- # skip "Sqlite version can't load extension for ceil" if $sqlite && $load_extension_disabled
119
+ # skip "Sqlite version can't load extension for ceil" if $sqlite && $load_extension_disabled
134
120
  assert_equal 2, t(@test, @score.ceil) # 1.62
135
121
  assert_equal(-20, t(@camille, @score.ceil)) # -20.16
136
122
  assert_equal(-20, t(@camille, (@score - 0.5).ceil)) # -20.16
137
123
  assert_equal 63, t(@arthur, @age.ceil + 42)
138
- assert_equal 11, t(@justin, @score.ceil) # 11.0
139
124
  end
140
125
 
141
126
  def test_floor
142
- # skip "Sqlite version can't load extension for floor" if $sqlite && $load_extension_disabled
127
+ # skip "Sqlite version can't load extension for floor" if $sqlite && $load_extension_disabled
143
128
  assert_equal 0, t(@neg, @score.floor)
144
129
  assert_equal 1, t(@test, @score.floor) # 1.62
145
130
  assert_equal(-9, t(@test, (@score - 10).floor)) # 1.62
146
131
  assert_equal 42, t(@arthur, @score.floor - 23)
147
- assert_equal 11, t(@justin, @score.floor) # 11.0
148
- assert_equal(-21, t(@camille, @score.floor)) # # -20.16
149
132
  end
150
133
 
151
134
  def test_rand
152
135
  assert 42 != User.select(Arel.rand.as('res')).first.res
153
136
  assert 0 <= User.select(Arel.rand.abs.as('res')).first.res
154
- assert_equal 10, User.order(Arel.rand).limit(50).count
137
+ assert_equal 8, User.order(Arel.rand).limit(50).count
155
138
  end
156
139
 
157
140
  def test_round
@@ -163,91 +146,35 @@ module ArelExtensions
163
146
 
164
147
  def test_sum
165
148
  if @env_db == 'mssql'
166
- skip 'SQL Server forces order?' # TODO
167
- assert_equal 83, User.select((@age.sum + 1).as('res'), User.arel_table[:id].sum).take(50).reorder(@age).first.res
168
- assert_equal 164, User.reorder(nil).select((@age.sum + @age.sum).as('res'), User.arel_table[:id].sum).take(50).first.res
169
- assert_equal 246, User.reorder(nil).select(((@age * 3).sum).as('res'), User.arel_table[:id].sum).take(50).first.res
170
- assert_equal 4234, User.reorder(nil).select(((@age * @age).sum).as('res'), User.arel_table[:id].sum).take(50).first.res
149
+ skip "SQL Server forces order?" # TODO
150
+ assert_equal 68, User.select((@age.sum + 1).as("res"), User.arel_table[:id].sum).take(50).reorder(@age).first.res
151
+ assert_equal 134, User.reorder(nil).select((@age.sum + @age.sum).as("res"), User.arel_table[:id].sum).take(50).first.res
152
+ assert_equal 201, User.reorder(nil).select(((@age * 3).sum).as("res"), User.arel_table[:id].sum).take(50).first.res
153
+ assert_equal 4009, User.reorder(nil).select(((@age * @age).sum).as("res"), User.arel_table[:id].sum).take(50).first.res
171
154
  else
172
- assert_equal 83, User.select((@age.sum + 1).as('res')).take(50).first.res
173
- assert_equal 164, User.select((@age.sum + @age.sum).as('res')).take(50).first.res
174
- assert_equal 246, User.select((@age * 3).sum.as('res')).take(50).first.res
175
- assert_equal 4234, User.select(((@age * @age).sum).as('res')).take(50).first.res
155
+ assert_equal 68, User.select((@age.sum + 1).as("res")).take(50).first.res
156
+ assert_equal 134, User.select((@age.sum + @age.sum).as("res")).take(50).first.res
157
+ assert_equal 201, User.select(((@age * 3).sum).as("res")).take(50).first.res
158
+ assert_equal 4009, User.select(((@age * @age).sum).as("res")).take(50).first.res
176
159
  end
177
160
  end
178
161
 
179
- def test_aggregation_with_ar_calculation
180
- # Since Arel10 (Rails6.1), some unwanted behaviors on aggregated calculation were present.
181
- # This should works no matter which version of rails is used
182
- assert User.group(:score).average(:id).values.all?{|e| !e.nil?}
183
-
184
- # Since Rails 7, a patch to calculations.rb has tirggered a double
185
- # quoting of the alias name. See https://github.com/rails/rails/commit/7e6e9091e55c3357b0162d44b6ab955ed0c718d5
186
- # Before the patch that fixed this the following error would occur:
187
- # ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
188
- assert User.group(:score).count(:id).values.all?{|e| !e.nil?}
189
- end
190
-
191
- def test_rollup
192
- skip "sqlite not supported" if $sqlite
193
- at = User.arel_table
194
- # single
195
- q = User.select(at[:name], at[:age].sum).group(Arel::Nodes::RollUp.new([at[:name]]))
196
- assert q.to_a.length > 0
197
-
198
- # multi
199
- q = User.select(at[:name], at[:score], at[:age].sum).group(Arel::Nodes::RollUp.new([at[:score], at[:name]]))
200
- assert q.to_a.length > 0
201
-
202
- # hybrid
203
- q = User.select(at[:name], at[:score], at[:age].sum).group(at[:score], Arel::Nodes::RollUp.new([at[:name]]))
204
- assert q.to_a.length > 0
205
-
206
- ## Using Arel.rollup which is less verbose than the original way
207
-
208
- # simple
209
- q = User.select(at[:name], at[:age].sum).group(Arel.rollup(at[:name]))
210
- assert q.to_a.length > 0
211
-
212
- # multi
213
- q = User.select(at[:name], at[:score], at[:age].sum).group(Arel.rollup([at[:score], at[:name]]))
214
- assert q.to_a.length > 0
215
-
216
- # hybrid
217
- q = User.select(at[:name], at[:score], at[:age].sum).group(at[:score], Arel.rollup([at[:name]]))
218
- assert q.to_a.length > 0
219
-
220
- ## Using at[:col].rollup which is handy for single column rollups
221
-
222
- # simple
223
- q = User.select(at[:name], at[:age].sum).group(at[:name].rollup)
224
- assert q.to_a.length > 0
225
-
226
- q = User.select(at[:name], at[:score], at[:age].sum).group(at[:name].rollup, at[:score].rollup)
227
- assert q.to_a.length > 0
228
-
229
- # hybrid
230
- q = User.select(at[:name], at[:score], at[:age].sum).group(at[:name], at[:score].rollup)
231
- assert q.to_a.length > 0
232
- end
233
-
234
162
  # String Functions
235
163
  def test_concat
236
164
  assert_equal 'Camille Camille', t(@camille, @name + ' ' + @name)
237
165
  assert_equal 'Laure 2', t(@laure, @name + ' ' + 2)
238
- assert_equal 'Test Laure', t(@laure, Arel.quoted('Test ') + @name)
239
-
240
- skip 'No group_concat in SqlServer before 2017' if @env_db == 'mssql'
241
- assert_equal 'Lucas Sophie', t(User.where(name: %w[Lucas Sophie]), @name.group_concat(' '))
242
- assert_equal 'Lucas,Sophie', t(User.where(name: %w[Lucas Sophie]), @name.group_concat(','))
243
- assert_equal 'Lucas,Sophie', t(User.where(name: %w[Lucas Sophie]), @name.group_concat)
244
-
245
- skip 'No order in group_concat in SqlLite' if $sqlite
246
- assert_equal 'Arthur,Lucas,Sophie', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', @name.asc))
247
- assert_equal 'Sophie,Lucas,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', @name.desc))
248
- assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', [@score.asc, @name.asc]))
249
- assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', @score.asc, @name.asc))
250
- assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', order: [@score.asc, @name.asc]))
166
+ assert_equal 'Test Laure', t(@laure, Arel::Nodes.build_quoted('Test ') + @name)
167
+
168
+ skip "No group_concat in SqlServer before 2017" if @env_db == 'mssql'
169
+ assert_equal "Lucas Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat(' '))
170
+ assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat(','))
171
+ assert_equal "Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie']), @name.group_concat)
172
+
173
+ skip "No order in group_concat in SqlLite" if $sqlite
174
+ assert_equal "Arthur,Lucas,Sophie", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@name.asc))
175
+ assert_equal "Sophie,Lucas,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@name.desc))
176
+ assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',[@score.asc,@name.asc]))
177
+ assert_equal "Lucas,Sophie,Arthur", t(User.where(:name => ['Lucas', 'Sophie','Arthur']), @name.group_concat(',',@score.asc,@name.asc))
251
178
  end
252
179
 
253
180
  def test_length
@@ -258,15 +185,15 @@ module ArelExtensions
258
185
 
259
186
  def test_md5
260
187
  skip "Sqlite can't do md5" if $sqlite
261
- assert_equal 'e2cf99ca82a7e829d2a4ac85c48154d0', t(@camille, @name.md5)
262
- assert_equal 'c3d41bf5efb468a1bcce53bd53726c85', t(@lucas, @name.md5)
188
+ assert_equal "e2cf99ca82a7e829d2a4ac85c48154d0", t(@camille, @name.md5)
189
+ assert_equal "c3d41bf5efb468a1bcce53bd53726c85", t(@lucas, @name.md5)
263
190
  end
264
191
 
265
192
  def test_locate
266
193
  skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
267
- assert_equal 1, t(@camille, @name.locate('C'))
268
- assert_equal 0, t(@lucas, @name.locate('z'))
269
- assert_equal 5, t(@lucas, @name.locate('s'))
194
+ assert_equal 1, t(@camille, @name.locate("C"))
195
+ assert_equal 0, t(@lucas, @name.locate("z"))
196
+ assert_equal 5, t(@lucas, @name.locate("s"))
270
197
  end
271
198
 
272
199
  def test_substring
@@ -276,7 +203,7 @@ module ArelExtensions
276
203
  else
277
204
  assert_equal('', t(@lucas, @name.substring(42)))
278
205
  end
279
- assert_equal 'Lu', t(@lucas, @name.substring(1, 2))
206
+ assert_equal 'Lu', t(@lucas, @name.substring(1,2))
280
207
 
281
208
  assert_equal 'C', t(@camille, @name[0, 1])
282
209
  assert_equal 'C', t(@camille, @name[0])
@@ -285,32 +212,32 @@ module ArelExtensions
285
212
  else
286
213
  assert_equal('', t(@lucas, @name[42]))
287
214
  end
288
- assert_equal 'Lu', t(@lucas, @name[0, 2])
215
+ assert_equal 'Lu', t(@lucas, @name[0,2])
289
216
  assert_equal 'Lu', t(@lucas, @name[0..1])
290
217
 
291
- # substring should accept string function
218
+ #substring should accept string function
292
219
  assert_equal 'Ce', t(@camille, @name.substring(1, 1).concat('e'))
293
- assert_equal 'Ce', t(@camille, @name.substring(1, 1) + 'e')
220
+ assert_equal 'Ce', t(@camille, @name.substring(1, 1)+'e')
294
221
  end
295
222
 
296
223
  def test_find_in_set
297
224
  skip "Sqlite version can't load extension for find_in_set" if $sqlite && $load_extension_disabled
298
- skip 'SQL Server does not know about FIND_IN_SET' if @env_db == 'mssql'
225
+ skip "SQL Server does not know about FIND_IN_SET" if @env_db == 'mssql'
299
226
  assert_equal 5, t(@neg, @comments & 2)
300
227
  assert_equal 0, t(@neg, @comments & 6) # not found
301
228
  end
302
229
 
303
230
  def test_string_comparators
304
- # skip "Oracle can't use math operators to compare strings" if @env_db == 'oracle' # use GREATEST ?
231
+ #skip "Oracle can't use math operators to compare strings" if @env_db == 'oracle' # use GREATEST ?
305
232
  skip "SQL Server can't use math operators to compare strings" if @env_db == 'mssql' # use GREATEST ?
306
233
  if @env_db == 'postgresql' # may return real boolean
307
234
  assert t(@neg, @name >= 'Mest') == true || t(@neg, @name >= 'Mest') == 't' # depends of ar version
308
235
  assert t(@neg, @name <= (@name + 'Z')) == true || t(@neg, @name <= (@name + 'Z')) == 't'
309
236
  elsif @env_db == 'oracle'
310
- assert_equal 1, t(@neg, Arel.when(@name >= 'Mest').then(1).else(0))
311
- assert_equal 1, t(@neg, Arel.when(@name <= (@name + 'Z')).then(1).else(0))
312
- assert_equal 1, t(@neg, Arel.when(@name > 'Mest').then(1).else(0))
313
- assert_equal 1, t(@neg, Arel.when(@name < (@name + 'Z')).then(1).else(0))
237
+ assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name >= 'Mest').then(1).else(0))
238
+ assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name <= (@name + 'Z')).then(1).else(0))
239
+ assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name > 'Mest').then(1).else(0))
240
+ assert_equal 1, t(@neg, ArelExtensions::Nodes::Case.new.when(@name < (@name + 'Z')).then(1).else(0))
314
241
  else
315
242
  assert_equal 1, t(@neg, @name >= 'Mest')
316
243
  assert_equal 1, t(@neg, @name <= (@name + 'Z'))
@@ -322,75 +249,69 @@ module ArelExtensions
322
249
  def test_compare_on_date_time_types
323
250
  skip "Sqlite can't compare time" if $sqlite
324
251
  skip "Oracle can't compare time" if @env_db == 'oracle'
325
- # @created_at == 2016-05-23
326
- assert_includes [true, 't', 1], t(@laure, Arel.when(@created_at >= '2014-01-01').then(1).else(0))
327
- assert_includes [false, 'f', 0], t(@laure, Arel.when(@created_at >= '2018-01-01').then(1).else(0))
328
- # @updated_at == 2014-03-03 12:42:00
329
- assert_includes [true, 't', 1], t(@laure, Arel.when(@updated_at >= '2014-03-03 10:10:10').then(1).else(0))
330
- assert_includes [false, 'f', 0], t(@laure, Arel.when(@updated_at >= '2014-03-03 13:10:10').then(1).else(0))
331
- # @duration == 12:42:21
332
- # puts @laure.select(Arel.when(@duration >= '10:10:10').then(1).else(0)).to_sql
333
- # puts @laure.select(Arel.when(@duration >= '14:10:10').then(1).else(0)).to_sql
334
- assert_includes [true, 't', 1], t(@laure, Arel.when(@duration >= '10:10:10').then(1).else(0))
335
- assert_includes [false, 'f', 0], t(@laure, Arel.when(@duration >= '14:10:10').then(1).else(0))
252
+ #@created_at == 2016-05-23
253
+ assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2014-01-01').then(1).else(0))
254
+ assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@created_at >= '2018-01-01').then(1).else(0))
255
+ #@updated_at == 2014-03-03 12:42:00
256
+ assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 10:10:10').then(1).else(0))
257
+ assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@updated_at >= '2014-03-03 13:10:10').then(1).else(0))
258
+ #@duration == 12:42:21
259
+ #puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0)).to_sql
260
+ #puts @laure.select(ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0)).to_sql
261
+ assert_includes [true,'t',1], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '10:10:10').then(1).else(0))
262
+ assert_includes [false,'f',0], t(@laure, ArelExtensions::Nodes::Case.new.when(@duration >= '14:10:10').then(1).else(0))
336
263
  end
337
264
 
265
+
338
266
  def test_regexp_not_regexp
339
267
  skip "Sqlite version can't load extension for regexp" if $sqlite && $load_extension_disabled
340
- skip 'SQL Server does not know about REGEXP without extensions' if @env_db == 'mssql'
268
+ skip "SQL Server does not know about REGEXP without extensions" if @env_db == 'mssql'
341
269
  assert_equal 1, User.where(@name =~ '^M').count
342
- assert_equal 8, User.where(@name !~ '^L').count
270
+ assert_equal 6, User.where(@name !~ '^L').count
343
271
  assert_equal 1, User.where(@name =~ /^M/).count
344
- assert_equal 8, User.where(@name !~ /^L/).count
272
+ assert_equal 6, User.where(@name !~ /^L/).count
345
273
  end
346
274
 
347
275
  def test_imatches
348
- # puts User.where(@name.imatches('m%')).to_sql
276
+ #puts User.where(@name.imatches('m%')).to_sql
349
277
  assert_equal 1, User.where(@name.imatches('m%')).count
350
278
  assert_equal 4, User.where(@name.imatches_any(['L%', '%e'])).count
351
- assert_equal 8, User.where(@name.idoes_not_match('L%')).count
279
+ assert_equal 6, User.where(@name.idoes_not_match('L%')).count
352
280
  end
353
281
 
354
282
  def test_replace
355
- assert_equal 'LucaX', t(@lucas, @name.replace('s', 'X'))
356
- assert_equal 'replace', t(@lucas, @name.replace(@name, 'replace'))
357
-
358
- skip 'Sqlite does not seem to support regexp_replace' if $sqlite
359
- skip 'SQL Server does not know about REGEXP without extensions' if @env_db == 'mssql'
360
- skip 'Travis mysql version does not support REGEXP_REPLACE' if @env_db == 'mysql'
361
- assert_equal 'LXcXs', t(@lucas, @name.replace(/[ua]/, 'X'))
362
- assert_equal 'LXcXs', t(@lucas, @name.regexp_replace(/[ua]/, 'X'))
363
- assert_equal 'LXcXs', t(@lucas, @name.regexp_replace('[ua]', 'X'))
283
+ assert_equal "LucaX", t(@lucas, @name.replace("s", "X"))
284
+ assert_equal "replace", t(@lucas, @name.replace(@name, "replace"))
364
285
  end
365
286
 
366
287
  def test_replace_once
367
- skip 'TODO'
368
- # skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
369
- assert_equal 'LuCas', t(@lucas, @name.substring(1, @name.locate('c') - 1) + 'C' + @name.substring(@name.locate('c') + 1, @name.length))
288
+ skip "TODO"
289
+ #skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
290
+ assert_equal "LuCas", t(@lucas, @name.substring(1, @name.locate('c') - 1) + 'C' + @name.substring(@name.locate('c') + 1, @name.length))
370
291
  end
371
292
 
372
293
  def test_soundex
373
294
  skip "Sqlite version can't load extension for soundex" if $sqlite && $load_extension_disabled
374
295
  skip "PostgreSql version can't load extension for soundex" if @env_db == 'postgresql'
375
- assert_equal 'C540', t(@camille, @name.soundex)
376
- assert_equal 10, User.where(@name.soundex.eq(@name.soundex)).count
377
- assert_equal 10, User.where(@name.soundex == @name.soundex).count
296
+ assert_equal "C540", t(@camille, @name.soundex)
297
+ assert_equal 8, User.where(@name.soundex.eq(@name.soundex)).count
298
+ assert_equal 8, User.where(@name.soundex == @name.soundex).count
378
299
  end
379
300
 
380
301
  def test_change_case
381
- assert_equal 'myung', t(@myung, @name.downcase)
382
- assert_equal 'MYUNG', t(@myung, @name.upcase)
383
- assert_equal 'myung', t(@myung, @name.upcase.downcase)
302
+ assert_equal "myung", t(@myung, @name.downcase)
303
+ assert_equal "MYUNG", t(@myung, @name.upcase)
304
+ assert_equal "myung", t(@myung, @name.upcase.downcase)
384
305
  end
385
306
 
386
307
  def test_trim
387
- assert_equal 'Myung', t(@myung, @name.trim)
388
- assert_equal 'Myung', t(@myung, @name.trim.ltrim.rtrim)
389
- assert_equal 'Myun', t(@myung, @name.rtrim('g'))
390
- assert_equal 'yung', t(@myung, @name.ltrim('M'))
391
- assert_equal 'yung', t(@myung, (@name + 'M').trim('M'))
392
- skip 'Oracle does not accept multi char trim' if @env_db == 'oracle'
393
- assert_equal '', t(@myung, @name.rtrim(@name))
308
+ assert_equal "Myung", t(@myung, @name.trim)
309
+ assert_equal "Myung", t(@myung, @name.trim.ltrim.rtrim)
310
+ assert_equal "Myun", t(@myung, @name.rtrim("g"))
311
+ assert_equal "yung", t(@myung, @name.ltrim("M"))
312
+ assert_equal "yung", t(@myung, (@name + "M").trim("M"))
313
+ skip "Oracle does not accept multi char trim" if @env_db == 'oracle'
314
+ assert_equal "", t(@myung, @name.rtrim(@name))
394
315
  end
395
316
 
396
317
  def test_blank
@@ -419,244 +340,32 @@ module ArelExtensions
419
340
  assert_equal 'true', t(@neg, @comments.not_blank.then('true', 'false'))
420
341
  end
421
342
 
422
- # This test repeats a lot of `test_blank` cases.
423
- def test_present
424
- if @env_db == 'postgresql'
425
- assert_includes [true, 't'], t(@myung, @name.present) # depends of adapter
426
- assert_includes [false, 'f'], t(@myung, @comments.present)
427
- end
428
- assert_equal 1, @myung.where(@name.present).count
429
- assert_equal 0, @myung.where(@comments.present).count
430
- assert_equal 0, @sophie.where(@comments.present).count
431
- assert_equal 0, @camille.where(@comments.present).count
432
-
433
- assert_equal 1, @neg.where(@comments.present).count
434
- assert_equal 'true', t(@myung, @name.present.then('true', 'false'))
435
- assert_equal 'false', t(@myung, @comments.present.then('true', 'false'))
436
- assert_equal 'true', t(@neg, @comments.present.then('true', 'false'))
437
- end
438
-
439
343
  def test_format
440
- %i[format format_date].each do |method|
441
- assert_equal '2016-05-23', t(@lucas, @created_at.send(method, '%Y-%m-%d'))
442
- assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.send(method, '%Y/%m/%d %H:%M:%S'))
443
- assert_equal '12:42%', t(@lucas, @updated_at.send(method, '%R%%'))
444
-
445
- # The following tests will ensure proper conversion of timestamps to
446
- # requested timezones.
447
- #
448
- # The names of the timezones is highly dependant on the underlying
449
- # operating system, and this is why we need to handle each database
450
- # separately: the images we're using to test these databases are
451
- # different. So don't rely on the provided examples. Your setup is your
452
- # reference.
453
- #
454
- # One could always have portable code if s/he uses standard
455
- # abbreviations, like:
456
- #
457
- # 1. CET => Central European Time
458
- # 2. CEST => Central European Summer Time
459
- #
460
- # Which implies that the caller should handle daylight saving detection.
461
- # In fact, CET will handle daylight saving in MySQL but not Postgres.
462
- #
463
- # It looks like the posix convention is supported by mysql and
464
- # postgresql, e.g.:
465
- #
466
- # posix/Europe/Paris
467
- # posix/America/Nipigon
468
- #
469
- # so it looks like a more reliably portable way of specifying it.
470
- time_zones = {
471
- 'mssql' => {
472
- 'utc' => 'UTC',
473
- 'sao_paulo' => 'Argentina Standard Time',
474
- 'tahiti' => 'Hawaiian Standard Time',
475
- 'paris' => 'Central European Standard Time'
476
- },
477
- 'posix' => {
478
- 'utc' => 'UTC',
479
- 'sao_paulo' => 'America/Sao_Paulo',
480
- 'tahiti' => 'Pacific/Tahiti',
481
- 'paris' => 'Europe/Paris'
482
- }
483
- }
484
-
485
- skip "Unsupported timezone conversion for DB=#{ENV['DB']}" if !%w[mssql mysql oracle postgresql].include?(ENV['DB'])
486
- # TODO: Standarize timezone conversion across all databases.
487
- # This test case will be refactored and should work the same across all vendors.
488
- if ENV['DB'] == 'mssql' && /Microsoft SQL Server (\d+)/.match(ActiveRecord::Base.connection.select_value('SELECT @@version'))[1].to_i < 2016
489
- skip "SQL Server < 2016 is not currently supported"
490
- end
491
-
492
- tz = ENV['DB'] == 'mssql' ? time_zones['mssql'] : time_zones['posix']
493
-
494
- assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.send(method, '%Y/%m/%d %H:%M:%S', tz['utc']))
495
- assert_equal '2014/03/03 09:42:00', t(@lucas, @updated_at.send(method, '%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['sao_paulo']}))
496
- assert_equal '2014/03/03 02:42:00', t(@lucas, @updated_at.send(method, '%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['tahiti']}))
497
-
498
- # Skipping conversion from UTC to the desired timezones fails in SQL
499
- # Server and Postgres. This is mainly due to the fact that timezone
500
- # information is not preserved in the column itself.
501
- #
502
- # MySQL is happy to consider that times by default are in UTC.
503
- assert_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.send(method, '%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
504
- refute_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.send(method, '%Y/%m/%d %H:%M:%S', tz['paris'])) if !['mysql'].include?(ENV['DB'])
505
-
506
- # Winter/Summer time
507
- assert_equal '2014/08/03 14:42:00', t(@lucas, (@updated_at + 5.months).send(method, '%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
508
- if ENV['DB'] == 'mssql'
509
- assert_equal '2022/02/01 11:42:00', t(@lucas, Arel.quoted('2022-02-01 10:42:00').cast(:datetime).send(method, '%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
510
- assert_equal '2022/08/01 12:42:00', t(@lucas, Arel.quoted('2022-08-01 10:42:00').cast(:datetime).send(method, '%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
511
- else
512
- assert_equal '2022/02/01 11:42:00', t(@lucas, Arel.quoted('2022-02-01 10:42:00').cast(:datetime).send(method, '%Y/%m/%d %H:%M:%S', tz['paris']))
513
- assert_equal '2022/08/01 12:42:00', t(@lucas, Arel.quoted('2022-08-01 10:42:00').cast(:datetime).send(method, '%Y/%m/%d %H:%M:%S', tz['paris']))
514
- end
515
- end
516
- end
517
-
518
- def test_format_iso_week
519
- %i[format format_date].each do |method|
520
- skip "Unsupported ISO week number for DB=#{ENV['DB']}" if ['sqlite'].include?(ENV['DB'])
521
- assert_equal '10', t(@lucas, @updated_at.send(method, '%V'))
522
- {
523
- '2024-01-01 10:42:00' => '01', # Monday
524
- '2030-01-01 10:42:00' => '01', # Tuesday
525
- '2025-01-01 10:42:00' => '01', # Wednesday
526
- '2026-01-01 10:42:00' => '01', # Thursday
527
- '2027-01-01 10:42:00' => '53', # Friday
528
- '2028-01-01 10:42:00' => '52', # Saturday
529
- '2034-01-01 10:42:00' => '52', # Sunday
530
- }.each do |date, exp|
531
- assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).send(method, '%V'))
532
- end
533
- end
534
- end
535
-
536
- def test_format_iso_year_of_week
537
- skip "Unsupported ISO year of week for DB=#{ENV['DB']}" if %w[mssql sqlite].include?(ENV['DB'])
538
- %i[format format_date].each do |method|
539
- assert_equal '2014', t(@lucas, @updated_at.send(method, '%G'))
540
-
541
- {
542
- '2024-01-01 10:42:00' => '2024', # Monday
543
- '2030-01-01 10:42:00' => '2030', # Tuesday
544
- '2025-01-01 10:42:00' => '2025', # Wednesday
545
- '2026-01-01 10:42:00' => '2026', # Thursday
546
- '2027-01-01 10:42:00' => '2026', # Friday
547
- '2028-01-01 10:42:00' => '2027', # Saturday
548
- '2034-01-01 10:42:00' => '2033', # Sunday
549
- }.each do |date, exp|
550
- assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).send(method, '%G'))
551
- end
552
- end
553
- end
554
-
555
- def test_format_date_with_names
556
- skip "#{ENV['DB']} does not support a variety of word-based formatting for month and day names" if %w[mssql sqlite].include?(ENV['DB'])
557
- %i[format format_date].each do |method|
558
- assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.send(method, '%a, %d %b %y'))
559
- assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.send(method, '%A, %d %B %y'))
560
- end
561
-
562
- skip "#{ENV['DB']} does not support ALLCAPS month and day names" if ['mysql'].include?(ENV['DB'])
563
- %i[format format_date].each do |method|
564
-
565
- assert_equal 'Mon, 03 MAR 14', t(@lucas, @updated_at.send(method, '%a, %d %^b %y'))
566
- assert_equal 'Monday, 03 MARCH 14', t(@lucas, @updated_at.send(method, '%A, %d %^B %y'))
567
- end
568
- end
569
-
570
- def switch_to_lang(lang)
571
- languages = {
572
- 'mssql' => {en: 'English', fr: 'French'},
573
- 'mysql' => {en: 'en_US', fr: 'fr_FR'},
574
- 'postgresql' => {en: 'en_US.utf8', fr: 'fr_FR.utf8'}
575
- }
576
-
577
- sql = {
578
- 'mssql' => ->(l) { "SET LANGUAGE #{l};" },
579
- 'mysql' => ->(l) { "SET lc_time_names = '#{l}';" },
580
- 'postgresql' => ->(l) { "SET lc_time to '#{l}';" }
581
- }
582
-
583
- User.connection.execute(sql[ENV['DB']][languages[ENV['DB']][lang]])
584
- end
585
-
586
- def test_format_date_with_names_and_lang_switch
587
- skip "#{ENV['DB']} does not support word-based formatting for month and day names" if ['sqlite'].include?(ENV['DB'])
588
-
589
- # the begin-rescue block is here to make sure we set the db back to en_US
590
- # if we fail, so that other tests don't get contaminated.
591
- #
592
- # Tests should assert one single thing in principle, but until we
593
- # refactor this whole thing, we'll have to do tricks of this sort.
594
- %i[format format_date].each do |method|
595
- begin
596
- switch_to_lang(:en)
597
- case ENV['DB']
598
- when 'mysql', 'postgresql'
599
- assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.send(method, '%a, %d %b %y'))
600
- assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.send(method, '%A, %d %B %y'))
601
- when 'mssql'
602
- assert_equal 'Monday, 03 March 2014', t(@lucas, @updated_at.send(method, '%A, %d %B %y'))
603
- end
604
- switch_to_lang(:fr)
605
- case ENV['DB']
606
- when 'mysql'
607
- assert_equal 'lun, 03 mar 14', t(@lucas, @updated_at.send(method, '%a, %d %b %y'))
608
- assert_equal 'lundi, 03 mars 14', t(@lucas, @updated_at.send(method, '%A, %d %B %y'))
609
- when 'postgresql'
610
- assert_equal 'Lun., 03 Mars 14', t(@lucas, @updated_at.send(method, '%a, %d %b %y'))
611
- assert_equal 'Lundi, 03 Mars 14', t(@lucas, @updated_at.send(method, '%A, %d %B %y'))
612
- when 'mssql'
613
- assert_equal 'lundi, 03 mars 2014', t(@lucas, @updated_at.send(method, '%A, %d %B %y'))
614
- end
615
- ensure
616
- switch_to_lang(:en)
617
- end
618
- end
344
+ assert_equal '2016-05-23', t(@lucas, @created_at.format('%Y-%m-%d'))
345
+ skip "SQL Server does not accept any format" if @env_db == 'mssql'
346
+ assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S'))
619
347
  end
620
348
 
621
349
  def test_coalesce
622
- assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, 'default') + ' concat')
350
+ assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, "default") + ' concat')
623
351
 
624
- assert_equal 'toto', t(@test, @other.coalesce(''))
352
+ assert_equal 'toto', t(@test, @other.coalesce(""))
625
353
 
626
- assert_equal ' ', t(@myung, @comments.coalesce('Myung').coalesce('ignored'))
627
- assert_equal 'Laure', t(@laure, @comments.coalesce('Laure'))
354
+ assert_equal ' ', t(@myung, @comments.coalesce("Myung").coalesce('ignored'))
355
+ assert_equal 'Laure', t(@laure, @comments.coalesce("Laure"))
628
356
  if @env_db == 'oracle'
629
- assert_nil t(@laure, @comments.coalesce(''))
630
- assert_nil t(@camille, @other.coalesce(''))
357
+ assert_nil t(@laure, @comments.coalesce(""))
358
+ assert_nil t(@camille, @other.coalesce(""))
631
359
  else
632
- assert_equal('', t(@laure, @comments.coalesce('')))
633
- assert_equal '', t(@camille, @other.coalesce(''))
360
+ assert_equal('', t(@laure, @comments.coalesce("")))
361
+ assert_equal '', t(@camille, @other.coalesce(""))
634
362
  end
635
363
  assert_equal 100, t(@test, @age.coalesce(100))
636
- assert_equal 'Camille', t(@camille, @name.coalesce(nil, 'default'))
364
+ assert_equal "Camille", t(@camille, @name.coalesce(nil, "default"))
637
365
  assert_equal 20, t(@test, @age.coalesce(nil, 20))
638
366
 
639
- assert_equal 20, t(@test, @age.coalesce(10) + 10)
640
- assert_equal 'Laure10', t(@laure, @comments.coalesce('Laure') + 10)
641
- end
642
-
643
- def test_coalesce_blank
644
- assert_equal 'Myung', t(@myung, @comments.coalesce_blank('Myung').coalesce_blank('ignored'))
645
- assert_equal 'Myung', t(@myung, @comments.coalesce_blank('', ' ', ' ').coalesce_blank('Myung'))
646
- assert_equal 'Myung', t(@myung, @comments.coalesce_blank('', ' ', ' ', 'Myung'))
647
- assert_equal '2016-05-23', t(@myung, @created_at.coalesce_blank(Date.new(2022, 1, 1)).format('%Y-%m-%d'))
648
- assert_equal '2016-05-23', t(@myung, @created_at.coalesce_blank(Date.new(2022, 1, 1)).format_date('%Y-%m-%d'))
649
- assert_equal 'Laure', t(@laure, @comments.coalesce_blank('Laure'))
650
- assert_equal 100, t(@test, @age.coalesce_blank(100))
651
- assert_equal 20, t(@test, @age.coalesce_blank(20))
652
- assert_equal 20, t(@test, @age.coalesce_blank(10) + 10)
653
- assert_equal 'Laure10', t(@laure, @comments.coalesce_blank('Laure') + 10)
654
-
655
- skip 'mssql does not support null in case results' if @env_db == 'mssql'
656
-
657
- assert_equal 'Camille concat', t(@camille, @name.coalesce_blank(Arel.null, 'default') + ' concat')
658
- assert_equal 'Myung', t(@myung, @comments.coalesce_blank(Arel.null, 'Myung'))
659
- assert_equal 'Camille', t(@camille, @name.coalesce_blank(Arel.null, 'default'))
367
+ assert_equal 20, t(@test, @age.coalesce(10)+10)
368
+ assert_equal 'Laure10', t(@laure, @comments.coalesce("Laure") + 10)
660
369
  end
661
370
 
662
371
  # Comparators
@@ -665,44 +374,37 @@ module ArelExtensions
665
374
  assert_equal 2, User.where(@age <= 10).count
666
375
  assert_equal 3, User.where(@age > 20).count
667
376
  assert_equal 4, User.where(@age >= 20).count
668
- assert_equal 2, User.where(@age > 5).where(@age < 20).count
377
+ assert_equal 1, User.where(@age > 5).where(@age < 20).count
669
378
  end
670
379
 
671
380
  def test_date_comparator
672
381
  d = Date.new(2016, 5, 23)
673
382
  assert_equal 0, User.where(@created_at < d).count
674
- assert_equal 10, User.where(@created_at >= d).count
383
+ assert_equal 8, User.where(@created_at >= d).count
675
384
  end
676
385
 
677
386
  def test_date_duration
678
- # When user `nilly` is created, with an explicit `created_at: nil`,
679
- # activerecord will give it the current date.
680
- #
681
- # So depending on the month when we run this test, we will get different
682
- # results for `User.where(@created_at.month.eq('05'))`.
683
- count_for_may = Time.now.month == 5 ? 10 : 9
684
-
685
- # Year
387
+ #Year
686
388
  assert_equal 2016, t(@lucas, @created_at.year).to_i
687
- assert_equal 0, User.where(@created_at.year.eq('2012')).count
688
- # Month
389
+ assert_equal 0, User.where(@created_at.year.eq("2012")).count
390
+ #Month
689
391
  assert_equal 5, t(@camille, @created_at.month).to_i
690
- assert_equal count_for_may, User.where(@created_at.month.eq('05')).count
691
- # Week
392
+ assert_equal 8, User.where(@created_at.month.eq("05")).count
393
+ #Week
692
394
  assert_equal(@env_db == 'mssql' ? 22 : 21, t(@arthur, @created_at.week).to_i)
693
- assert_equal count_for_may, User.where(@created_at.month.eq('05')).count
694
- # Day
395
+ assert_equal 8, User.where(@created_at.month.eq("05")).count
396
+ #Day
695
397
  assert_equal 23, t(@laure, @created_at.day).to_i
696
- assert_equal 0, User.where(@created_at.day.eq('05')).count
398
+ assert_equal 0, User.where(@created_at.day.eq("05")).count
697
399
 
698
- # skip "manage DATE" if @env_db == 'oracle'
699
- # Hour
400
+ #skip "manage DATE" if @env_db == 'oracle'
401
+ #Hour
700
402
  assert_equal 0, t(@laure, @created_at.hour).to_i
701
403
  assert_equal 12, t(@lucas, @updated_at.hour).to_i
702
- # Minute
404
+ #Minute
703
405
  assert_equal 0, t(@laure, @created_at.minute).to_i
704
406
  assert_equal 42, t(@lucas, @updated_at.minute).to_i
705
- # Second
407
+ #Second
706
408
  assert_equal 0, t(@laure, @created_at.second).to_i
707
409
  assert_equal 0, t(@lucas, @updated_at.second).to_i
708
410
  end
@@ -710,8 +412,8 @@ module ArelExtensions
710
412
  def test_datetime_diff
711
413
  assert_equal 0, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 42)).to_i
712
414
  if @env_db == 'oracle' && Arel::VERSION.to_i > 6 # in rails 5, result is multiplied by 24*60*60 = 86400...
713
- assert_equal 42 * 86_400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
714
- assert_equal(-3600 * 86_400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
415
+ assert_equal 42 * 86400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
416
+ assert_equal(-3600 * 86400, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
715
417
  else
716
418
  assert_equal 42, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
717
419
  assert_equal(-3600, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
@@ -722,8 +424,7 @@ module ArelExtensions
722
424
  end
723
425
  end
724
426
 
725
-
726
- skip 'not yet implemented' if $sqlite
427
+ skip "not yet implemented" if $sqlite
727
428
 
728
429
  date1 = Date.new(2016, 5, 23)
729
430
  durPos = 10.years
@@ -735,134 +436,106 @@ module ArelExtensions
735
436
 
736
437
  datetime1 = Time.utc(2014, 3, 3, 12, 42, 0)
737
438
  # Pull Request #5 tests
738
- # puts (@created_at + durPos).cast(:date).to_sql
739
- assert_includes [date2, '2026-05-23'], t(@test, (@created_at + durPos).cast(:date))
740
- assert_includes [date3, '2006-05-23'], t(@test, (@created_at + durNeg).cast(:date))
439
+ #puts (@created_at + durPos).cast(:date).to_sql
440
+ assert_includes [date2,"2026-05-23"], t(@test,(@created_at + durPos).cast(:date))
441
+ assert_includes [date3,"2006-05-23"], t(@test,(@created_at + durNeg).cast(:date))
741
442
 
742
- # puts (@created_at + @created_at.day).cast(:date).to_sql
743
- assert_includes [date4, '2016-06-15'], t(@test, (@created_at + @created_at.day).cast(:date))
744
- # puts (@created_at - @created_at.day).cast(:date).to_sql
745
- assert_includes [date5, '2016-04-30'], t(@test, (@created_at - @created_at.day).cast(:date))
443
+ #puts (@created_at + @created_at.day).cast(:date).to_sql
444
+ assert_includes [date4,"2016-06-15"], t(@test,(@created_at + @created_at.day).cast(:date))
445
+ #puts (@created_at - @created_at.day).cast(:date).to_sql
446
+ assert_includes [date5,"2016-04-30"], t(@test,(@created_at - @created_at.day).cast(:date))
746
447
 
747
- assert_includes [datetime1 + 42.seconds, '2014-03-03 12:42:42 UTC'], t(@lucas, (@updated_at + @updated_at.minute))
748
- assert_includes [datetime1 - 42.seconds, '2014-03-03 12:41:18 UTC'], t(@lucas, (@updated_at - @updated_at.minute))
448
+ assert_includes [datetime1 + 42.seconds,"2014-03-03 12:42:42 UTC"], t(@lucas,(@updated_at + @updated_at.minute))
449
+ assert_includes [datetime1 - 42.seconds,"2014-03-03 12:41:18 UTC"], t(@lucas,(@updated_at - @updated_at.minute))
749
450
 
750
451
  # (@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))).to_sql
751
- assert_includes [datetime1 + (12 * 60 + 42).seconds, '2014-03-03 12:54:42 UTC'],
752
- t(@lucas, (@updated_at + Arel.duration('s', (@updated_at.hour * 60 + @updated_at.minute))))
452
+ assert_includes [datetime1 + (12*60+42).seconds,"2014-03-03 12:54:42 UTC"],
453
+ t(@lucas,(@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))))
753
454
 
754
- assert_includes [datetime1 + (12 * 60 + 42).minutes, '2014-03-04 01:24:00 UTC'],
755
- t(@lucas, (@updated_at + Arel.duration('mn', (@updated_at.hour * 60 + @updated_at.minute))))
455
+ assert_includes [datetime1 + (12*60+42).minutes,"2014-03-04 01:24:00 UTC"],
456
+ t(@lucas,(@updated_at + Arel.duration('mn',(@updated_at.hour*60 + @updated_at.minute))))
756
457
 
757
- assert_includes ['2024-03-03'], t(@lucas, (@updated_at + durPos).format('%Y-%m-%d'))
758
- assert_includes ['2024-03-03'], t(@lucas, (@updated_at + durPos).format_date('%Y-%m-%d'))
759
- # puts (@updated_at - durPos).to_sql
760
- assert_includes ['2004-03-03'], t(@lucas, (@updated_at - durPos).format('%Y-%m-%d'))
761
- assert_includes ['2004-03-03'], t(@lucas, (@updated_at - durPos).format_date('%Y-%m-%d'))
458
+ assert_includes ["2024-03-03"], t(@lucas,(@updated_at + durPos).format('%Y-%m-%d'))
459
+ #puts (@updated_at - durPos).to_sql
460
+ assert_includes ["2004-03-03"], t(@lucas,(@updated_at - durPos).format('%Y-%m-%d'))
762
461
 
763
462
 
764
463
  # we test with the ruby object or the string because some adapters don't return an object Date
765
- # end
464
+ # end
766
465
  end
767
466
 
768
467
  # TODO; cast types
769
468
  def test_cast_types
770
- assert_equal '5', t(@lucas, @age.cast(:string))
771
- skip 'jdbc adapters does not work properly here (v52 works fine)' if RUBY_PLATFORM.match?(/java/i)
469
+ assert_equal "5", t(@lucas, @age.cast(:string))
772
470
  if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
773
- 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'
774
- assert_equal 1, t(@laure, Arel.when(@duration.cast(:time).eq('12:42:21')).then(1).else(0)) unless @env_db == 'oracle'
775
- assert_equal '20.16', t(@laure, @score.cast(:string)).gsub(/0*\z/, '')
776
- assert_equal '20.161', t(@laure, @score.cast(:string) + 1).gsub(/0*1\z/, '1')
777
- assert_equal 21.16, t(@laure, @score.cast(:string).cast(:decimal) + 1)
778
- assert_equal 21, t(@laure, @score.cast(:string).cast(:int) + 1)
779
-
780
- assert_equal String, t(@lucas, @updated_at.cast(:string)).class
781
-
782
- assert_equal Date, t(@lucas, @updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
783
- assert_equal Time, t(@lucas, @updated_at.cast(:string).cast(:datetime)).class
784
- assert_equal Time, t(@lucas, @updated_at.cast(:time)).class
785
-
786
- # mysql adapter in rails7 adds some infos we just squeeze here
787
- assert_equal '2014-03-03 12:42:00', t(@lucas, @updated_at.cast(:string)).split('.').first unless @env_db == 'mssql' # locale dependent
788
- assert_equal Date.parse('2014-03-03'), t(@lucas, Arel.quoted('2014-03-03').cast(:date))
789
- assert_equal Date.parse('5014-03-03'), t(@lucas, (@age.cast(:string) + '014-03-03').cast(:date))
790
- assert_equal Time.parse('2014-03-03 12:42:00 UTC'), t(@lucas, @updated_at.cast(:string).cast(:datetime))
791
- assert_equal Date.parse('2014-03-03'), t(@lucas, @updated_at.cast(:date))
792
- assert_equal '12:42:00', t(@lucas, @updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' # DateTime
793
- end
794
- end
795
-
796
- def test_if_present
797
- assert_nil t(@myung, @comments.if_present)
798
- assert_equal 0, t(@myung, @comments.if_present.count)
799
- assert_equal 20.16, t(@myung, @score.if_present)
800
- assert_equal '2016-05-23', t(@myung, @created_at.if_present.format('%Y-%m-%d'))
801
- assert_equal '2016-05-23', t(@myung, @created_at.if_present.format_date('%Y-%m-%d'))
802
- assert_nil t(@laure, @comments.if_present)
803
-
804
- assert_nil t(@nilly, @duration.if_present.format('%Y-%m-%d'))
805
- assert_nil t(@nilly, @duration.if_present.format_date('%Y-%m-%d'))
806
-
807
- # NOTE: here we're testing the capacity to format a nil value,
808
- # however, @comments is a text field, and not a date/datetime field,
809
- # so Postgres will rightfully complain when we format the text:
810
- # we need to cast it first.
811
- if @env_db == 'postgresql'
812
- assert_nil t(@laure, @comments.cast(:date).if_present.format('%Y-%m-%d'))
813
- assert_nil t(@laure, @comments.cast(:date).if_present.format_date('%Y-%m-%d'))
814
- else
815
- assert_nil t(@laure, @comments.if_present.format('%Y-%m-%d'))
816
- assert_nil t(@laure, @comments.if_present.format_date('%Y-%m-%d'))
471
+ assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).cast(:string).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle' || @env_db == 'mssql'
472
+ assert_equal 1, t(@laure,ArelExtensions::Nodes::Case.new.when(@duration.cast(:time).eq("12:42:21")).then(1).else(0)) unless @env_db == 'oracle'
473
+ assert_equal "20.16", t(@laure,@score.cast(:string)).gsub(/[0]*\z/,'')
474
+ assert_equal "20.161", t(@laure,@score.cast(:string)+1).gsub(/[0]*1\z/,'1')
475
+ assert_equal 21.16, t(@laure,@score.cast(:string).cast(:decimal)+1)
476
+ assert_equal 21, t(@laure,@score.cast(:string).cast(:int)+1)
477
+
478
+ assert_equal String, t(@lucas,@updated_at.cast(:string)).class
479
+ assert_equal Date, t(@lucas,@updated_at.cast(:date)).class unless @env_db == 'oracle' # DateTime
480
+ assert_equal Time, t(@lucas,@updated_at.cast(:string).cast(:datetime)).class
481
+ assert_equal Time, t(@lucas,@updated_at.cast(:time)).class
482
+
483
+ assert_equal "2014-03-03 12:42:00", t(@lucas,@updated_at.cast(:string)) unless @env_db == 'mssql' #locale dependent
484
+ assert_equal Date.parse("2014-03-03"), t(@lucas,Arel::Nodes.build_quoted('2014-03-03').cast(:date))
485
+ assert_equal Date.parse("5014-03-03"), t(@lucas,(@age.cast(:string) + '014-03-03').cast(:date))
486
+ assert_equal Time.parse("2014-03-03 12:42:00 UTC"), t(@lucas,@updated_at.cast(:string).cast(:datetime))
487
+ assert_equal Date.parse("2014-03-03"), t(@lucas,@updated_at.cast(:date))
488
+ assert_equal "12:42:00", t(@lucas,@updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' #DateTime
817
489
  end
818
490
  end
819
491
 
820
492
  def test_is_null
821
- # puts User.where(@age.is_null).select(@name).to_sql
822
- # puts @age.is_null
823
- # puts @age.is_null.inspect
824
- # puts @age.is_null.to_sql
825
- # puts @age=='34'
826
- assert_equal 'Test', User.select(@name).where(@age.is_null).first.name
493
+ #puts User.where(@age.is_null).select(@name).to_sql
494
+ #puts @age.is_null
495
+ #puts @age.is_null.inspect
496
+ #puts @age.is_null.to_sql
497
+ #puts @age=='34'
498
+ assert_equal "Test", User.select(@name).where(@age.is_null.to_sql).first.name
827
499
  end
828
500
 
829
501
  def test_math_plus
830
502
  d = Date.new(1997, 6, 15)
831
- # Concat String
832
- assert_equal 'SophiePhan', t(@sophie, @name + 'Phan')
833
- assert_equal 'Sophie2', t(@sophie, @name + 2)
834
- assert_equal 'Sophie1997-06-15', t(@sophie, @name + d)
835
- assert_equal 'Sophie15', t(@sophie, @name + @age)
836
- assert_equal 'SophieSophie', t(@sophie, @name + @name)
837
- assert_equal 'SophieSophieSophie', t(@sophie, @name + @name + @name)
838
- assert_equal 'SophieSophieSophie', t(@sophie, @name.concat(@name.concat(@name)))
839
- assert_equal 'SophieSophieSophie', t(@sophie, @name.concat(@name).concat(@name))
840
- # FIXME: should work as expected in Oracle
841
- assert_equal 'Sophie2016-05-23', t(@sophie, @name + @created_at) unless @env_db == 'oracle'
842
- # concat Integer
503
+ #Concat String
504
+ assert_equal "SophiePhan", t(@sophie, @name + "Phan")
505
+ assert_equal "Sophie2", t(@sophie, @name + 2)
506
+ assert_equal "Sophie1997-06-15", t(@sophie, @name + d)
507
+ assert_equal "Sophie15", t(@sophie, @name + @age)
508
+ assert_equal "SophieSophie", t(@sophie, @name + @name)
509
+ assert_equal "SophieSophieSophie", t(@sophie, @name + @name + @name)
510
+ assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name.concat(@name)))
511
+ assert_equal "SophieSophieSophie", t(@sophie, @name.concat(@name).concat(@name))
512
+ #FIXME: should work as expected in Oracle
513
+ assert_equal "Sophie2016-05-23", t(@sophie, @name + @created_at) unless @env_db == 'oracle'
514
+ #concat Integer
843
515
  assert_equal 1, User.where((@age + 10).eq(33)).count
844
- assert_equal 1, User.where((@age + '1').eq(6)).count
516
+ assert_equal 1, User.where((@age + "1").eq(6)).count
845
517
  assert_equal 1, User.where((@age + @age).eq(10)).count
846
- # concat Date
847
- # puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
848
- assert_equal '2016-05-24', t(@myung, @created_at + 1).to_date.to_s
849
- assert_equal '2016-05-25', t(@myung, @created_at + 2.day).to_date.to_s
518
+ #concat Date
519
+ #puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
520
+ assert_equal "2016-05-24", t(@myung, @created_at + 1).to_date.to_s
521
+ assert_equal "2016-05-25", t(@myung, @created_at + 2.day).to_date.to_s
850
522
  end
851
523
 
524
+
852
525
  def test_math_minus
853
526
  d = Date.new(2016, 5, 20)
854
- # Datediff
855
- assert_equal 10, User.where((@created_at - @created_at).eq(0)).count
856
- assert_equal 3, @laure.select((@created_at - d).as('res')).first.res.abs.to_i
857
- # Substraction
527
+ #Datediff
528
+ assert_equal 8, User.where((@created_at - @created_at).eq(0)).count
529
+ assert_equal 3, @laure.select((@created_at - d).as("res")).first.res.abs.to_i
530
+ #Substraction
858
531
  assert_equal 0, User.where((@age - 10).eq(50)).count
859
- assert_equal 0, User.where((@age - '10').eq(50)).count
532
+ assert_equal 0, User.where((@age - "10").eq(50)).count
860
533
  # assert_equal 0, User.where((@age - 9.5).eq(50.5)).count # should work: TODO
861
- assert_equal 0, User.where((@age - '9.5').eq(50.5)).count
534
+ assert_equal 0, User.where((@age - "9.5").eq(50.5)).count
862
535
  end
863
536
 
864
537
  def test_wday
865
- # d = Date.new(2016, 6, 26)
538
+ #d = Date.new(2016, 6, 26)
866
539
  assert_equal(@env_db == 'oracle' || @env_db == 'mssql' ? 2 : 1, t(@myung, @created_at.wday).to_i) # monday
867
540
  end
868
541
 
@@ -881,8 +554,8 @@ module ArelExtensions
881
554
  assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(23)) + @ut.project(@age).where(@age.eq(21))).as('my_union')).length
882
555
  assert_equal 2, User.select('*').from((@ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(20)) + @ut.project(@age).where(@age.eq(21))).as('my_union')).length
883
556
 
884
- assert_equal 3, User.find_by_sql(@ut.project(@age).where(@age.gt(22)).union_all(@ut.project(@age).where(@age.lt(0))).to_sql).length
885
- assert_equal 3, User.find_by_sql(@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(20))).union_all(@ut.project(@age).where(@age.eq(21))).to_sql).length
557
+ assert_equal 3, User.find_by_sql((@ut.project(@age).where(@age.gt(22)).union_all(@ut.project(@age).where(@age.lt(0)))).to_sql).length
558
+ assert_equal 3, User.find_by_sql((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(20))).union_all(@ut.project(@age).where(@age.eq(21)))).to_sql).length
886
559
  assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.gt(22)).union_all(@ut.project(@age).where(@age.lt(0)))).as('my_union')).length
887
560
  assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(23))).union_all(@ut.project(@age).where(@age.eq(21)))).as('my_union')).length
888
561
  assert_equal 3, User.select('*').from((@ut.project(@age).where(@age.eq(20)).union_all(@ut.project(@age).where(@age.eq(20))).union_all(@ut.project(@age).where(@age.eq(21)))).as('my_union')).length
@@ -893,118 +566,116 @@ module ArelExtensions
893
566
  # Case clause
894
567
  def test_case
895
568
  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)
896
- assert_equal 4, User.where(@score.when(20.16).then(1).else(0).eq(1)).count
897
- assert_equal 2, t(@arthur, @score.when(65.62, 1).else(0) + 1)
898
- assert_equal 0, t(@arthur, @score.when(65.62, 1).else(0) - 1)
899
- assert_equal '11', t(@arthur, @score.when(65.62).then('1').else('0') + '1')
900
- assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score) + 1)
901
- 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
569
+ assert_equal 2, t(@arthur, @score.when(65.62,1).else(0)+1)
570
+ assert_equal 0, t(@arthur, @score.when(65.62,1).else(0)-1)
571
+ assert_equal "11", t(@arthur, @score.when(65.62).then("1").else("0")+"1")
572
+ assert_equal 66.62, t(@arthur, @score.when(65.62).then(@score).else(@score)+1)
573
+ 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
902
574
  end
903
575
 
904
576
  def test_format_numbers
905
- # score of Arthur = 65.62
906
- skip ' Works with SQLite if the version used knows printf' if $sqlite
907
-
908
- assert_equal 'Wrong Format', t(@arthur, @score.format_number('$ %...234.6F €', 'fr_FR'))
909
- assert_equal 'AZERTY65,62', t(@arthur, @score.format_number('AZERTY%.2f', 'fr_FR'))
910
- assert_equal '65,62AZERTY', t(@arthur, @score.format_number('%.2fAZERTY', 'fr_FR'))
911
- assert_equal '$ 65.62 €', t(@arthur, @score.format_number('$ %.2f €', 'en_US'))
912
- assert_equal '$ 66 €', t(@arthur, @score.format_number('$ %.0f €', 'en_US'))
913
- assert_equal '$ 0065,62 €', t(@arthur, @score.format_number('$ %07.2f €', 'fr_FR'))
914
- assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ %-07.2f €', 'fr_FR'))
915
- assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ %-7.2f €', 'fr_FR'))
916
- assert_equal '$ 65,62 €', t(@arthur, @score.format_number('$ % 7.2f €', 'fr_FR'))
917
- assert_equal '$ 65,6 €', t(@arthur, @score.format_number('$ % 7.1f €', 'fr_FR'))
918
- assert_equal '$ +65,62 €', t(@arthur, @score.format_number('$ % +7.2f €', 'fr_FR'))
919
- assert_equal '$ +065,62 €', t(@arthur, @score.format_number('$ %0+7.2f €', 'fr_FR'))
920
- assert_includes ['$ 6,56e1 €', '$ 6,56e+01 €'], t(@arthur, @score.format_number('$ %.2e €', 'fr_FR'))
921
- assert_includes ['$ 6,56E1 €', '$ 6,56E+01 €'], t(@arthur, @score.format_number('$ %.2E €', 'fr_FR'))
922
- assert_includes ['$ 6,562E1 €', '$ 6,562E+01 €'], t(@arthur, @score.format_number('$ %.3E €', 'fr_FR'))
923
- assert_equal '123 456 765,6', t(@arthur, (@score + 123_456_700).format_number('%.1f', 'sv_SE')).tr("\u00A0", ' ') # some DBMS put no-break space here (it makes sense thus)
924
- assert_equal '123456765,6', t(@arthur, (@score + 123_456_700).format_number('%.1f', 'fr_FR')).delete("\u00A0") # because SqlServer does it like no one else
925
- assert_equal '123,456,765.6', t(@arthur, (@score + 123_456_700).format_number('%.1f', 'en_US'))
926
- assert_equal ' 123,456,765.6', t(@arthur, (@score + 123_456_700).format_number('%16.1f', 'en_US'))
927
- assert_equal '$ 0,00 €', t(@arthur, @score.when(65.62).then(Arel.sql('null')).else(1).format_number('$ %.2f €', 'fr_FR'))
928
- assert_equal '$ 0,00 €', t(@arthur, (@score - 65.62).format_number('$ %.2f €', 'fr_FR'))
577
+ #score of Arthur = 65.62
578
+ skip " Works with SQLite if the version used knows printf" if $sqlite
579
+
580
+ assert_equal "Wrong Format" , t(@arthur, @score.format_number("$ %...234.6F €","fr_FR"))
581
+ assert_equal "AZERTY65,62" , t(@arthur, @score.format_number("AZERTY%.2f","fr_FR"))
582
+ assert_equal "65,62AZERTY" , t(@arthur, @score.format_number("%.2fAZERTY","fr_FR"))
583
+ assert_equal "$ 65.62 €" , t(@arthur, @score.format_number("$ %.2f €","en_US"))
584
+ assert_equal "$ 66 €" , t(@arthur, @score.format_number("$ %.0f €","en_US"))
585
+ assert_equal "$ 0065,62 €" , t(@arthur, @score.format_number("$ %07.2f €","fr_FR"))
586
+ assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-07.2f €","fr_FR"))
587
+ assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ %-7.2f €","fr_FR"))
588
+ assert_equal "$ 65,62 €" , t(@arthur, @score.format_number("$ % 7.2f €","fr_FR"))
589
+ assert_equal "$ 65,6 €" , t(@arthur, @score.format_number("$ % 7.1f €","fr_FR"))
590
+ assert_equal "$ +65,62 €" , t(@arthur, @score.format_number("$ % +7.2f €","fr_FR"))
591
+ assert_equal "$ +065,62 €" , t(@arthur, @score.format_number("$ %0+7.2f €","fr_FR"))
592
+ assert_includes ["$ 6,56e1 €","$ 6,56e+01 €"], t(@arthur, @score.format_number("$ %.2e €","fr_FR"))
593
+ assert_includes ["$ 6,56E1 €","$ 6,56E+01 €"], t(@arthur, @score.format_number("$ %.2E €","fr_FR"))
594
+ assert_includes ["$ 6,562E1 €","$ 6,562E+01 €"], t(@arthur, @score.format_number("$ %.3E €","fr_FR"))
595
+ 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)
596
+ assert_equal "123456765,6" , t(@arthur, (@score+123456700).format_number("%.1f","fr_FR")).gsub("\u00A0","") #because SqlServer does it like no one else
597
+ assert_equal "123,456,765.6" , t(@arthur, (@score+123456700).format_number("%.1f","en_US"))
598
+ assert_equal " 123,456,765.6" , t(@arthur, (@score+123456700).format_number("%16.1f","en_US"))
599
+ assert_equal "$ 0,00 €" , t(@arthur, @score.when(65.62).then(Arel.sql("null")).else(1).format_number("$ %.2f €","fr_FR"))
600
+ assert_equal "$ 0,00 €" , t(@arthur, (@score-65.62).format_number("$ %.2f €","fr_FR"))
929
601
  end
930
602
 
931
603
  def test_accent_insensitive
932
- skip 'SQLite is natively Case Insensitive and Accent Sensitive' if $sqlite
933
- skip 'Not finished' if @env_db == 'mysql'
604
+ skip "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
605
+ skip "Not finished" if @env_db == 'mysql'
934
606
  # actual comments value: "arrêté"
935
- # AI & CI
607
+ #AI & CI
936
608
  if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
937
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('arrêté')).then('1').else('0'))
938
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('arrete')).then('1').else('0'))
939
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('àrrétè')).then('1').else('0'))
940
- assert_equal '0', t(@arthur, Arel.when(@comments.ai_imatches('arretez')).then('1').else('0'))
941
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('Arrete')).then('1').else('0'))
942
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_imatches('Arrêté')).then('1').else('0'))
943
- # AI & CS
944
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('arrêté')).then('1').else('0'))
945
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('arrete')).then('1').else('0'))
946
- assert_equal '1', t(@arthur, Arel.when(@comments.ai_matches('àrrétè')).then('1').else('0'))
947
- assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('arretez')).then('1').else('0'))
948
- if !%w[oracle postgresql mysql].include?(@env_db) # AI => CI
949
- assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('Arrete')).then('1').else('0'))
950
- assert_equal '0', t(@arthur, Arel.when(@comments.ai_matches('Arrêté')).then('1').else('0'))
609
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrêté")).then("1").else("0"))
610
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arrete")).then("1").else("0"))
611
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("àrrétè")).then("1").else("0"))
612
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("arretez")).then("1").else("0"))
613
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrete")).then("1").else("0"))
614
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_imatches("Arrêté")).then("1").else("0"))
615
+ #AI & CS
616
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrêté")).then("1").else("0"))
617
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arrete")).then("1").else("0"))
618
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("àrrétè")).then("1").else("0"))
619
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("arretez")).then("1").else("0"))
620
+ if !['oracle','postgresql','mysql'].include?(@env_db) # AI => CI
621
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrete")).then("1").else("0"))
622
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.ai_matches("Arrêté")).then("1").else("0"))
951
623
  end
952
624
  end
953
- # AS & CI
954
- assert_equal '1', t(@arthur, Arel.when(@comments.imatches('arrêté')).then('1').else('0'))
625
+ #AS & CI
626
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrêté")).then("1").else("0"))
955
627
  if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
956
- assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arrete')).then('1').else('0'))
957
- assert_equal '0', t(@arthur, Arel.when(@comments.imatches('àrrétè')).then('1').else('0'))
628
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrete")).then("1").else("0"))
629
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("àrrétè")).then("1").else("0"))
958
630
  end
959
- assert_equal '0', t(@arthur, Arel.when(@comments.imatches('arretez')).then('1').else('0'))
631
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arretez")).then("1").else("0"))
960
632
  if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
961
- assert_equal '0', t(@arthur, Arel.when(@comments.imatches('Arrete')).then('1').else('0'))
633
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrete")).then("1").else("0"))
962
634
  end
963
- assert_equal '1', t(@arthur, Arel.when(@comments.imatches('Arrêté')).then('1').else('0'))
964
- # AS & CS
965
- assert_equal '1', t(@arthur, Arel.when(@comments.smatches('arrêté')).then('1').else('0'))
966
- assert_equal '0', t(@arthur, Arel.when(@comments.smatches('arrete')).then('1').else('0'))
967
- assert_equal '0', t(@arthur, Arel.when(@comments.smatches('àrrétè')).then('1').else('0'))
968
- assert_equal '0', t(@arthur, Arel.when(@comments.smatches('arretez')).then('1').else('0'))
969
- assert_equal '0', t(@arthur, Arel.when(@comments.smatches('Arrete')).then('1').else('0'))
970
- assert_equal '0', t(@arthur, Arel.when(@comments.smatches('Arrêté')).then('1').else('0'))
635
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrêté")).then("1").else("0"))
636
+ #AS & CS
637
+ assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrêté")).then("1").else("0"))
638
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arrete")).then("1").else("0"))
639
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("àrrétè")).then("1").else("0"))
640
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("arretez")).then("1").else("0"))
641
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrete")).then("1").else("0"))
642
+ assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.smatches("Arrêté")).then("1").else("0"))
971
643
  end
972
644
 
973
645
  def test_subquery_with_order
974
- skip if ['mssql'].include?(@env_db) && Arel::VERSION.to_i < 10
975
- assert_equal 10, User.where(name: User.select(:name).order(:name)).count
976
- assert_equal 10, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
977
- if !['mysql'].include?(@env_db) # MySql can't have limit in IN subquery
978
- assert_equal 2, User.where(name: User.select(:name).order(:name).limit(2)).count
979
- # assert_equal 6, User.where(name: User.select(:name).order(:name).offset(2)).count
646
+ assert_equal 8, User.where(:name => User.select(:name).order(:name)).count
647
+ assert_equal 8, User.where(@ut[:name].in(@ut.project(@ut[:name]).order(@ut[:name]))).count
648
+ if !['mysql'].include?(@env_db) # MySql can't have limit in IN subquery
649
+ assert_equal 2, User.where(:name => User.select(:name).order(:name).limit(2)).count
650
+ #assert_equal 6, User.where(:name => User.select(:name).order(:name).offset(2)).count
980
651
  end
981
652
  end
982
653
 
983
654
  def test_in_with_nil
984
- assert_predicate @myung.where(@age.in(1)), :blank?
985
- assert_predicate @myung.where(@age.in(23)), :present?
986
- assert_predicate @myung.where(@age.in([1])), :blank?
987
- assert_predicate @myung.where(@age.in([1, 2])), :blank?
988
- assert_predicate @myung.where(@age.in([1, 23])), :present?
989
- assert_predicate @myung.where(@age.in(nil)), :blank?
990
- assert_predicate @myung.where(@age.in([nil])), :blank?
991
- assert_predicate @myung.where(@age.in([nil, 1])), :blank?
992
- assert_predicate @myung.where(@age.in([nil, 23])), :present?
993
- assert_predicate @myung.where(@age.in([nil, 1, 2])), :blank?
994
- assert_predicate @myung.where(@age.in([nil, 1, 23])), :present?
995
- assert_predicate @test.where(@age.in(1)), :blank?
996
- assert_predicate @test.where(@age.in([1])), :blank?
997
- assert_predicate @test.where(@age.in([1, 2])), :blank?
998
- assert_predicate @test.where(@age.in(nil)), :present?
999
- assert_predicate @test.where(@age.in([nil])), :present?
1000
- assert_predicate @test.where(@age.in([nil, 1])), :present?
1001
- assert_predicate @test.where(@age.in([nil, 1, 2])), :present?
655
+ assert_equal true , @myung.where(@age.in(1)).blank?
656
+ assert_equal false , @myung.where(@age.in(23)).blank?
657
+ assert_equal true , @myung.where(@age.in([1])).blank?
658
+ assert_equal true , @myung.where(@age.in([1,2])).blank?
659
+ assert_equal false , @myung.where(@age.in([1,23])).blank?
660
+ assert_equal true , @myung.where(@age.in(nil)).blank?
661
+ assert_equal true , @myung.where(@age.in([nil])).blank?
662
+ assert_equal true , @myung.where(@age.in([nil,1])).blank?
663
+ assert_equal false , @myung.where(@age.in([nil,23])).blank?
664
+ assert_equal true , @myung.where(@age.in([nil,1,2])).blank?
665
+ assert_equal false , @myung.where(@age.in([nil,1,23])).blank?
666
+ assert_equal true , @test.where(@age.in(1)).blank?
667
+ assert_equal true , @test.where(@age.in([1])).blank?
668
+ assert_equal true , @test.where(@age.in([1,2])).blank?
669
+ assert_equal false , @test.where(@age.in(nil)).blank?
670
+ assert_equal false , @test.where(@age.in([nil])).blank?
671
+ assert_equal false , @test.where(@age.in([nil,1])).blank?
672
+ assert_equal false , @test.where(@age.in([nil,1,2])).blank?
1002
673
  end
1003
674
 
1004
675
  def test_scope_with_in_plus_new
1005
676
  begin
1006
- @test.where(@age.in([1, 2])).new
1007
- @test.where(@age.not_in([1, 2])).new
677
+ @test.where(@age.in([1,2])).new
678
+ @test.where(@age.not_in([1,2])).new
1008
679
  assert true
1009
680
  rescue
1010
681
  assert false
@@ -1012,129 +683,114 @@ module ArelExtensions
1012
683
  end
1013
684
 
1014
685
  def test_is_not_null
1015
- assert_predicate @myung.where(@age.is_not_null), :present?
1016
- assert_predicate @test.where(@age.is_not_null), :blank?
686
+ assert_equal false , @myung.where(@age.is_not_null).blank?
687
+ assert_equal true , @test.where(@age.is_not_null).blank?
1017
688
  end
1018
689
 
1019
690
  def test_not_in_with_nil
1020
- assert_predicate @myung.where(@age.not_in(1)), :present?
1021
- assert_predicate @myung.where(@age.not_in(23)), :blank?
1022
- assert_predicate @myung.where(@age.not_in([1])), :present?
1023
- assert_predicate @myung.where(@age.not_in([1, 2])), :present?
1024
- assert_predicate @myung.where(@age.not_in([1, 23])), :blank?
1025
- assert_predicate @myung.where(@age.not_in(nil)), :present?
1026
- assert_predicate @myung.where(@age.not_in([nil])), :present?
1027
- assert_predicate @myung.where(@age.not_in([nil, 1])), :present?
1028
- assert_predicate @myung.where(@age.not_in([nil, 23])), :blank?
1029
- assert_predicate @myung.where(@age.not_in([nil, 1, 2])), :present?
1030
- assert_predicate @myung.where(@age.not_in([nil, 1, 23])), :blank?
1031
-
1032
- assert_predicate @myung.where(@age.not_in(1..2)), :present?
1033
-
1034
- # if the column is null, the entry will never be selected with not in (like every DBMS does)
1035
- # assert_predicate @test.where(@age.not_in(1)), :present?
1036
- # assert_predicate @test.where(@age.not_in([1])), :present?
1037
- # assert_predicate @test.where(@age.not_in([1,2])), :present?
1038
- # assert_predicate @test.where(@age.not_in(nil)), :blank?
1039
- # assert_predicate @test.where(@age.not_in([nil])), :blank?
1040
- # assert_predicate @test.where(@age.not_in([nil,1])), :blank?
1041
- # assert_predicate @test.where(@age.not_in([nil,1,2])), :blank?
1042
- end
1043
-
1044
- def test_in_on_grouping
1045
- skip 'We should modify the visitor of IN to make it work' if $sqlite || @env_db == 'mssql'
1046
- assert_equal 2, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23), Arel.tuple('Arthur', 21))).count
1047
- assert_equal 1, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23))).count
1048
- assert_equal 0, User.where(Arel.tuple(@name, @age).in([])).count
691
+ assert_equal false , @myung.where(@age.not_in(1)).blank?
692
+ assert_equal true , @myung.where(@age.not_in(23)).blank?
693
+ assert_equal false , @myung.where(@age.not_in([1])).blank?
694
+ assert_equal false , @myung.where(@age.not_in([1,2])).blank?
695
+ assert_equal true , @myung.where(@age.not_in([1,23])).blank?
696
+ assert_equal false , @myung.where(@age.not_in(nil)).blank?
697
+ assert_equal false , @myung.where(@age.not_in([nil])).blank?
698
+ assert_equal false , @myung.where(@age.not_in([nil,1])).blank?
699
+ assert_equal true , @myung.where(@age.not_in([nil,23])).blank?
700
+ assert_equal false , @myung.where(@age.not_in([nil,1,2])).blank?
701
+ assert_equal true , @myung.where(@age.not_in([nil,1,23])).blank?
702
+
703
+ assert_equal false , @myung.where(@age.not_in(1..2)).blank?
704
+
705
+ #if the column is null, the entry will never be selected with not in (like every DBMS does)
706
+ #assert_equal false , @test.where(@age.not_in(1)).blank?
707
+ #assert_equal false , @test.where(@age.not_in([1])).blank?
708
+ #assert_equal false , @test.where(@age.not_in([1,2])).blank?
709
+ #assert_equal true , @test.where(@age.not_in(nil)).blank?
710
+ #assert_equal true , @test.where(@age.not_in([nil])).blank?
711
+ #assert_equal true , @test.where(@age.not_in([nil,1])).blank?
712
+ #assert_equal true , @test.where(@age.not_in([nil,1,2])).blank?
1049
713
  end
1050
714
 
1051
715
  def test_alias_shortened
1052
- if %w[postgresql oracle].include?(@env_db)
716
+ if ['postgresql','oracle'].include?(@env_db)
1053
717
  new_alias = Arel.shorten('azerty' * 15)
1054
718
  at = User.arel_table.alias('azerty' * 15)
1055
719
  assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
1056
720
  assert_equal '"user_tests" "u"'.downcase, User.arel_table.alias('u').to_sql.downcase
1057
- assert_equal %Q[SELECT "#{new_alias}"."id" FROM "user_tests" "#{new_alias}"].downcase,
1058
- User.select(at[:id]).from(at).to_sql.downcase
721
+ assert_equal %Q[SELECT "#{new_alias}"."id" FROM "user_tests" "#{new_alias}"].downcase,
722
+ User.select(at[:id]).from(at).to_sql.downcase
1059
723
  end
1060
724
  end
1061
725
 
1062
726
  def test_stat_functions
1063
727
  skip "SQLite doesn't work for most on this functions" if $sqlite
1064
- # puts t(User.where(nil), @score.average)
1065
- # puts t(User.where(nil), @score.variance(unbiased: true))
1066
- # puts t(User.where(nil), @score.variance(unbiased: false))
1067
- # puts t(User.where(nil), @score.std(unbiased: true))
1068
- # puts t(User.where(nil), @score.std(unbiased: false))
1069
-
1070
- assert ( 15.43222 - t(User.where(nil), @score.average)).abs < 0.01
1071
- assert (539.79804 - t(User.where(nil), @score.variance)).abs < 0.01
1072
- assert (479.82048 - t(User.where(nil), @score.variance(unbiased: false))).abs < 0.01
1073
- assert ( 23.23355 - t(User.where(nil), @score.std)).abs < 0.01
1074
- assert ( 21.90480 - t(User.where(nil), @score.std(unbiased: false))).abs < 0.01
1075
- skip 'Not Yet Implemented' # if !['postgresql'].include?(@env_db)
1076
- assert_equal 2, User.select(@score.std(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
1077
- assert_equal 2, User.select(@score.variance(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
1078
- assert_equal 2, User.select(@score.sum(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
1079
- 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
728
+ #puts t(User.where(nil), @score.average)
729
+ #puts t(User.where(nil), @score.variance(true))
730
+ #puts t(User.where(nil), @score.variance(false))
731
+ #puts t(User.where(nil), @score.std(true))
732
+ #puts t(User.where(nil), @score.std(false))
733
+
734
+ assert ( 15.98625 - t(User.where(nil), @score.average)).abs < 0.01
735
+ assert (613.75488 - t(User.where(nil), @score.variance)).abs < 0.01
736
+ assert ( 537.0355 - t(User.where(nil), @score.variance(false))).abs < 0.01
737
+ assert ( 24.77408 - t(User.where(nil), @score.std)).abs < 0.01
738
+ assert ( 23.17403 - t(User.where(nil), @score.std(false))).abs < 0.01
1080
739
  end
1081
740
 
1082
741
  def test_levenshtein_distance
1083
- skip 'Not Yet Implemented' if $sqlite
1084
- assert_equal 0, t(@arthur, @name.levenshtein_distance('Arthur'))
1085
- assert_equal 2, t(@arthur, @name.levenshtein_distance('Artoor'))
1086
- assert_equal 1, t(@arthur, @name.levenshtein_distance('Artehur'))
742
+ skip "Not Yet Implemented" if $sqlite
743
+ assert_equal 0, t(@arthur,@name.levenshtein_distance("Arthur"))
744
+ assert_equal 2, t(@arthur,@name.levenshtein_distance("Artoor"))
745
+ assert_equal 1, t(@arthur,@name.levenshtein_distance("Artehur"))
1087
746
  end
1088
747
 
1089
748
  def test_json
1090
749
  skip "Can't be tested on travis"
1091
- # creation
1092
- assert_equal 'Arthur', t(@arthur, Arel.json(@name))
1093
- assert_equal %w[Arthur Arthur], parse_json(t(@arthur, Arel.json(@name, @name)))
1094
- assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 'ArthurArthur'}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => @name + @name})))
1095
- assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 1}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => 1})))
1096
- assert_equal [{'age' => 21}, {'name' => 'Arthur', 'score' => 65.62}], parse_json(t(@arthur, Arel.json([{age: @age}, {name: @name, score: @score}])))
750
+ #creation
751
+ assert_equal 'Arthur', t(@arthur,Arel.json(@name))
752
+ assert_equal ["Arthur","Arthur"], parse_json(t(@arthur,Arel.json(@name,@name)))
753
+ assert_equal ({"Arthur" => "Arthur", "Arthur2" => "ArthurArthur"}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => @name+@name})))
754
+ assert_equal ({"Arthur" => "Arthur","Arthur2" => 1}), parse_json(t(@arthur,Arel.json({@name => @name,@name+"2" => 1})))
755
+ assert_equal ([{"age" => 21},{"name" => "Arthur","score" => 65.62}]), parse_json(t(@arthur,Arel.json([{age: @age},{name: @name,score: @score}])))
1097
756
 
1098
757
  # aggregate
1099
- assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure'}),
1100
- parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16), Arel.json({@age => @name}).group(false)))
1101
- assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure', 'Laure' => 25, 'Lucas' => 5, 'Myung' => 23, 'Sophie' => 15}),
1102
- parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16), Arel.json({@age => @name, @name => @age}).group(false)))
1103
- assert_equal [{'5' => 'Lucas'}, {'15' => 'Sophie'}, {'23' => 'Myung'}, {'25' => 'Laure'}],
1104
- parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score), Arel.json({@age => @name}).group(true, [@age])))
1105
-
1106
- # puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score, Arel.json({@age => @name}).group(true,[@age])).to_sql
1107
- # puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score, Arel.json({@age => @name}).group(true,[@age])).to_a
1108
-
1109
- skip 'Not Yet Implemented' if $sqlite || %w[oracle mssql].include?(@env_db)
1110
- # get
1111
- h1 = Arel.json({@name => @name + @name, @name + '2' => 1})
1112
- assert_equal 'ArthurArthur', parse_json(t(@arthur, h1.get(@name)))
1113
- h2 = Arel.json([{age: @age}, {name: @name, score: @score}])
1114
- assert_equal ({'age' => 21}), parse_json(t(@arthur, h2.get(0)))
1115
- assert_equal 21, parse_json(t(@arthur, h2.get(0).get('age')))
1116
- assert_nil t(@arthur, h2.get('age'))
1117
- # set
1118
- assert_equal ({'Arthur' => %w[toto tata], 'Arthur2' => 1}), parse_json(t(@arthur, h1.set(@name, %w[toto tata])))
1119
- assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.set(@name + '3', 2)))
1120
- assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => nil}), parse_json(t(@arthur, h1.set(@name + '3', nil)))
1121
- assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1, 'Arthur3' => {'a' => 2}}), parse_json(t(@arthur, h1.set(@name + '3', {a: 2})))
1122
- # merge
1123
- assert_equal ({'Arthur' => %w[toto tata], 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.merge({@name => %w[toto tata]}, {@name + '3' => 2})))
1124
- assert_equal ({'Arthur' => %w[toto tata], 'Arthur2' => 1, 'Arthur3' => 2}), parse_json(t(@arthur, h1.merge({@name => %w[toto tata], @name + '3' => 2})))
1125
- assert_equal ({'Arthur' => 'ArthurArthur', 'Arthur2' => 1}), parse_json(t(@arthur, h1.merge({})))
758
+ assert_equal ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure"}),
759
+ parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name}).group(false)))
760
+ assert_equal ({"5" => "Lucas", "15" => "Sophie", "23" => "Myung", "25" => "Laure", "Laure"=>25, "Lucas"=>5, "Myung"=>23, "Sophie"=>15}),
761
+ parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16),Arel.json({@age => @name,@name => @age}).group(false)))
762
+ assert_equal ([{"5" => "Lucas"},{ "15" => "Sophie"},{ "23" => "Myung"},{ "25" => "Laure"}]),
763
+ parse_json(t(User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score),Arel.json({@age => @name}).group(true,[@age])))
764
+
765
+ #puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_sql
766
+ #puts User.group(:score).where(@age.is_not_null).where(@score == 20.16).select(@score,Arel.json({@age => @name}).group(true,[@age])).to_a
767
+
768
+ skip "Not Yet Implemented" if $sqlite || ['oracle','mssql'].include?(@env_db)
769
+ #get
770
+ h1 = Arel.json({@name => @name+@name,@name+"2" => 1})
771
+ assert_equal "ArthurArthur", parse_json(t(@arthur,h1.get(@name)))
772
+ h2 = Arel.json([{age: @age},{name: @name,score: @score}])
773
+ assert_equal ({"age" => 21}), parse_json(t(@arthur,h2.get(0)))
774
+ assert_equal 21, parse_json(t(@arthur,h2.get(0).get('age')))
775
+ assert_nil t(@arthur,h2.get('age'))
776
+ #set
777
+ assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1}), parse_json(t(@arthur,h1.set(@name, ['toto','tata'])))
778
+ assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.set(@name+"3",2)))
779
+ assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => nil}), parse_json(t(@arthur,h1.set(@name+"3",nil)))
780
+ assert_equal ({"Arthur" => "ArthurArthur", "Arthur2" => 1, "Arthur3" => {"a" => 2}}), parse_json(t(@arthur,h1.set(@name+"3",{a: 2})))
781
+ #merge
782
+ assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata']},{@name+"3" => 2})))
783
+ assert_equal ({"Arthur" => ["toto", "tata"], "Arthur2" => 1, "Arthur3" => 2}), parse_json(t(@arthur,h1.merge({@name => ['toto','tata'], @name+"3" => 2})))
784
+ assert_equal ({"Arthur" => "ArthurArthur","Arthur2" => 1}), parse_json(t(@arthur,h1.merge({})))
1126
785
  end
1127
786
 
1128
787
  def test_as_on_everything
1129
788
  name = @arthur.select(@name.as('NaMe')).first.attributes
1130
- assert_equal 'Arthur', name['NaMe'] || name['name'] # because of Oracle
1131
- assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes['Na Me']
1132
- assert_equal 'ArthurArthur', @arthur.select((@name + @name).as('Na-Me')).first.attributes['Na-Me']
789
+ assert_equal 'Arthur', name["NaMe"] || name["name"] #because of Oracle
790
+ assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes["Na Me"]
791
+ assert_equal 'ArthurArthur', @arthur.select((@name+@name).as('Na-Me')).first.attributes["Na-Me"]
1133
792
  end
1134
793
 
1135
- def test_exists_in_subquery
1136
- assert User.where(User.where(nil).arel.exists).first
1137
- end
1138
794
  end
1139
795
  end
1140
796
  end