arel_extensions 1.3.5 → 2.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +2 -1
- data/.gitignore +6 -7
- data/.rubocop.yml +3 -67
- data/.travis/oracle/download.js +152 -0
- data/.travis/oracle/download.sh +30 -0
- data/.travis/oracle/download_ojdbc.js +116 -0
- data/.travis/oracle/install.sh +34 -0
- data/.travis/setup_accounts.sh +9 -0
- data/.travis/sqlite3/extension-functions.sh +6 -0
- data/.travis.yml +223 -0
- data/Gemfile +21 -16
- data/README.md +13 -125
- data/Rakefile +30 -41
- data/TODO +1 -0
- data/appveyor.yml +22 -51
- data/arel_extensions.gemspec +14 -14
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +14 -14
- data/gemfiles/rails5_0.gemfile +14 -14
- data/gemfiles/rails5_1_4.gemfile +14 -14
- data/gemfiles/rails5_2.gemfile +14 -16
- data/init/mssql.sql +4 -4
- data/init/mysql.sql +38 -38
- data/init/oracle.sql +0 -0
- data/init/postgresql.sql +21 -21
- data/init/sqlite.sql +0 -0
- data/lib/arel_extensions/attributes.rb +3 -4
- data/lib/arel_extensions/boolean_functions.rb +14 -53
- data/lib/arel_extensions/common_sql_functions.rb +17 -16
- data/lib/arel_extensions/comparators.rb +28 -27
- data/lib/arel_extensions/date_duration.rb +14 -13
- data/lib/arel_extensions/insert_manager.rb +15 -18
- data/lib/arel_extensions/math.rb +53 -55
- data/lib/arel_extensions/math_functions.rb +39 -46
- data/lib/arel_extensions/nodes/abs.rb +1 -0
- data/lib/arel_extensions/nodes/blank.rb +2 -1
- data/lib/arel_extensions/nodes/case.rb +16 -16
- data/lib/arel_extensions/nodes/cast.rb +8 -10
- data/lib/arel_extensions/nodes/ceil.rb +1 -1
- data/lib/arel_extensions/nodes/coalesce.rb +4 -3
- data/lib/arel_extensions/nodes/collate.rb +10 -9
- data/lib/arel_extensions/nodes/concat.rb +18 -9
- data/lib/arel_extensions/nodes/date_diff.rb +26 -42
- data/lib/arel_extensions/nodes/duration.rb +3 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
- data/lib/arel_extensions/nodes/floor.rb +1 -1
- data/lib/arel_extensions/nodes/format.rb +8 -35
- data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
- data/lib/arel_extensions/nodes/function.rb +37 -42
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +39 -48
- data/lib/arel_extensions/nodes/length.rb +0 -5
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
- data/lib/arel_extensions/nodes/locate.rb +2 -1
- data/lib/arel_extensions/nodes/log10.rb +2 -1
- data/lib/arel_extensions/nodes/matches.rb +7 -5
- data/lib/arel_extensions/nodes/md5.rb +1 -0
- data/lib/arel_extensions/nodes/power.rb +5 -5
- data/lib/arel_extensions/nodes/rand.rb +1 -0
- data/lib/arel_extensions/nodes/repeat.rb +5 -3
- data/lib/arel_extensions/nodes/replace.rb +8 -16
- data/lib/arel_extensions/nodes/round.rb +6 -5
- data/lib/arel_extensions/nodes/soundex.rb +15 -15
- data/lib/arel_extensions/nodes/std.rb +21 -18
- data/lib/arel_extensions/nodes/substring.rb +16 -8
- data/lib/arel_extensions/nodes/then.rb +1 -1
- data/lib/arel_extensions/nodes/trim.rb +6 -4
- data/lib/arel_extensions/nodes/union.rb +8 -5
- data/lib/arel_extensions/nodes/union_all.rb +7 -4
- data/lib/arel_extensions/nodes/wday.rb +4 -0
- data/lib/arel_extensions/nodes.rb +1 -1
- data/lib/arel_extensions/null_functions.rb +7 -5
- data/lib/arel_extensions/predications.rb +43 -44
- data/lib/arel_extensions/railtie.rb +5 -5
- data/lib/arel_extensions/set_functions.rb +7 -5
- data/lib/arel_extensions/string_functions.rb +29 -58
- data/lib/arel_extensions/tasks.rb +6 -6
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
- data/lib/arel_extensions/visitors/mssql.rb +181 -279
- data/lib/arel_extensions/visitors/mysql.rb +210 -280
- data/lib/arel_extensions/visitors/oracle.rb +180 -201
- data/lib/arel_extensions/visitors/oracle12.rb +31 -18
- data/lib/arel_extensions/visitors/postgresql.rb +173 -252
- data/lib/arel_extensions/visitors/sqlite.rb +126 -140
- data/lib/arel_extensions/visitors/to_sql.rb +237 -272
- data/lib/arel_extensions/visitors.rb +59 -75
- data/lib/arel_extensions.rb +31 -159
- data/test/database.yml +7 -15
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +116 -105
- data/test/support/fake_record.rb +3 -3
- data/test/test_comparators.rb +17 -14
- data/test/visitors/test_bulk_insert_oracle.rb +11 -11
- data/test/visitors/test_bulk_insert_sqlite.rb +13 -12
- data/test/visitors/test_bulk_insert_to_sql.rb +13 -11
- data/test/visitors/test_oracle.rb +55 -55
- data/test/visitors/test_to_sql.rb +226 -419
- data/test/with_ar/all_agnostic_test.rb +361 -578
- data/test/with_ar/insert_agnostic_test.rb +21 -27
- data/test/with_ar/test_bulk_sqlite.rb +16 -17
- data/test/with_ar/test_math_sqlite.rb +26 -26
- data/test/with_ar/test_string_mysql.rb +33 -31
- data/test/with_ar/test_string_sqlite.rb +34 -30
- metadata +22 -29
- data/.github/workflows/ruby.yml +0 -341
- data/gemfiles/rails6.gemfile +0 -30
- data/gemfiles/rails6_1.gemfile +0 -30
- data/gemfiles/rails7.gemfile +0 -23
- data/gemspecs/arel_extensions-v1.gemspec +0 -28
- data/gemspecs/arel_extensions-v2.gemspec +0 -28
- data/generate_gems.sh +0 -15
- data/lib/arel_extensions/aliases.rb +0 -14
- data/lib/arel_extensions/helpers.rb +0 -51
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
- data/lib/arel_extensions/nodes/sum.rb +0 -7
- data/lib/arel_extensions/visitors/convert_format.rb +0 -37
- data/test/arelx_test_helper.rb +0 -71
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
@@ -1,24 +1,21 @@
|
|
1
|
-
require '
|
1
|
+
require 'helper'
|
2
2
|
require 'date'
|
3
3
|
|
4
4
|
module ArelExtensions
|
5
|
-
module
|
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 ==
|
11
|
-
@env_db = (RUBY_PLATFORM == 'java' ?
|
12
|
-
skip "Platform not supported
|
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
|
-
|
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
|
33
|
+
t.column :updated_at, :datetime
|
37
34
|
t.column :duration, :time
|
38
35
|
t.column :other, :string
|
39
|
-
t.column :score, :decimal, precision
|
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
|
40
|
+
t.column :price, :decimal, :precision => 20, :scale => 10
|
44
41
|
end
|
45
42
|
end
|
46
43
|
|
@@ -55,24 +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
|
59
|
-
@lucas = User.where(id
|
60
|
-
u = User.create age
|
61
|
-
@sophie = User.where(id
|
62
|
-
u = User.create age
|
63
|
-
@camille = User.where(id
|
64
|
-
u = User.create age
|
65
|
-
@arthur = User.where(id
|
66
|
-
u = User.create age
|
67
|
-
@myung = User.where(id
|
68
|
-
u = User.create age
|
69
|
-
@laure = User.where(id
|
70
|
-
u = User.create age
|
71
|
-
@test = User.where(id
|
72
|
-
u = User.create age
|
73
|
-
@neg = User.where(id
|
74
|
-
|
75
|
-
@justin = 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
|
+
|
76
72
|
|
77
73
|
@age = User.arel_table[:age]
|
78
74
|
@name = User.arel_table[:name]
|
@@ -98,7 +94,7 @@ module ArelExtensions
|
|
98
94
|
scope.select(node.as('res')).to_a.first.res
|
99
95
|
end
|
100
96
|
|
101
|
-
#
|
97
|
+
#manage the difference between adapters that handle or not json type
|
102
98
|
def parse_json(h)
|
103
99
|
if @env_db != 'postgresql'
|
104
100
|
JSON.parse("{\"res\":#{h}}")['res']
|
@@ -107,14 +103,6 @@ module ArelExtensions
|
|
107
103
|
end
|
108
104
|
end
|
109
105
|
|
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
|
-
|
118
106
|
# Math Functions
|
119
107
|
def test_classical_arel
|
120
108
|
assert_in_epsilon 42.16, t(@laure, @score + 22), 0.01
|
@@ -128,28 +116,25 @@ module ArelExtensions
|
|
128
116
|
end
|
129
117
|
|
130
118
|
def test_ceil
|
131
|
-
|
119
|
+
# skip "Sqlite version can't load extension for ceil" if $sqlite && $load_extension_disabled
|
132
120
|
assert_equal 2, t(@test, @score.ceil) # 1.62
|
133
121
|
assert_equal(-20, t(@camille, @score.ceil)) # -20.16
|
134
122
|
assert_equal(-20, t(@camille, (@score - 0.5).ceil)) # -20.16
|
135
123
|
assert_equal 63, t(@arthur, @age.ceil + 42)
|
136
|
-
assert_equal 11, t(@justin, @score.ceil) # 11.0
|
137
124
|
end
|
138
125
|
|
139
126
|
def test_floor
|
140
|
-
|
127
|
+
# skip "Sqlite version can't load extension for floor" if $sqlite && $load_extension_disabled
|
141
128
|
assert_equal 0, t(@neg, @score.floor)
|
142
129
|
assert_equal 1, t(@test, @score.floor) # 1.62
|
143
130
|
assert_equal(-9, t(@test, (@score - 10).floor)) # 1.62
|
144
131
|
assert_equal 42, t(@arthur, @score.floor - 23)
|
145
|
-
assert_equal 11, t(@justin, @score.floor) # 11.0
|
146
|
-
assert_equal(-21, t(@camille, @score.floor)) # # -20.16
|
147
132
|
end
|
148
133
|
|
149
134
|
def test_rand
|
150
135
|
assert 42 != User.select(Arel.rand.as('res')).first.res
|
151
136
|
assert 0 <= User.select(Arel.rand.abs.as('res')).first.res
|
152
|
-
assert_equal
|
137
|
+
assert_equal 8, User.order(Arel.rand).limit(50).count
|
153
138
|
end
|
154
139
|
|
155
140
|
def test_round
|
@@ -161,48 +146,35 @@ module ArelExtensions
|
|
161
146
|
|
162
147
|
def test_sum
|
163
148
|
if @env_db == 'mssql'
|
164
|
-
skip
|
165
|
-
assert_equal
|
166
|
-
assert_equal
|
167
|
-
assert_equal
|
168
|
-
assert_equal
|
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
|
169
154
|
else
|
170
|
-
assert_equal
|
171
|
-
assert_equal
|
172
|
-
assert_equal
|
173
|
-
assert_equal
|
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
|
174
159
|
end
|
175
160
|
end
|
176
161
|
|
177
|
-
def test_aggregation_with_ar_calculation
|
178
|
-
# Since Arel10 (Rails6.1), some unwanted behaviors on aggregated calculation were present.
|
179
|
-
# This should works no matter which version of rails is used
|
180
|
-
assert User.group(:score).average(:id).values.all?{|e| !e.nil?}
|
181
|
-
|
182
|
-
# Since Rails 7, a patch to calculations.rb has tirggered a double
|
183
|
-
# quoting of the alias name. See https://github.com/rails/rails/commit/7e6e9091e55c3357b0162d44b6ab955ed0c718d5
|
184
|
-
# Before the patch that fixed this the following error would occur:
|
185
|
-
# ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
|
186
|
-
assert User.group(:score).count(:id).values.all?{|e| !e.nil?}
|
187
|
-
end
|
188
|
-
|
189
162
|
# String Functions
|
190
163
|
def test_concat
|
191
164
|
assert_equal 'Camille Camille', t(@camille, @name + ' ' + @name)
|
192
165
|
assert_equal 'Laure 2', t(@laure, @name + ' ' + 2)
|
193
|
-
assert_equal 'Test Laure', t(@laure, Arel.
|
166
|
+
assert_equal 'Test Laure', t(@laure, Arel::Nodes.build_quoted('Test ') + @name)
|
194
167
|
|
195
|
-
skip
|
196
|
-
assert_equal
|
197
|
-
assert_equal
|
198
|
-
assert_equal
|
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)
|
199
172
|
|
200
|
-
skip
|
201
|
-
assert_equal
|
202
|
-
assert_equal
|
203
|
-
assert_equal
|
204
|
-
assert_equal
|
205
|
-
assert_equal 'Lucas,Sophie,Arthur', t(User.where(name: %w[Lucas Sophie Arthur]), @name.group_concat(',', order: [@score.asc, @name.asc]))
|
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))
|
206
178
|
end
|
207
179
|
|
208
180
|
def test_length
|
@@ -213,15 +185,15 @@ module ArelExtensions
|
|
213
185
|
|
214
186
|
def test_md5
|
215
187
|
skip "Sqlite can't do md5" if $sqlite
|
216
|
-
assert_equal
|
217
|
-
assert_equal
|
188
|
+
assert_equal "e2cf99ca82a7e829d2a4ac85c48154d0", t(@camille, @name.md5)
|
189
|
+
assert_equal "c3d41bf5efb468a1bcce53bd53726c85", t(@lucas, @name.md5)
|
218
190
|
end
|
219
191
|
|
220
192
|
def test_locate
|
221
193
|
skip "Sqlite version can't load extension for locate" if $sqlite && $load_extension_disabled
|
222
|
-
assert_equal 1, t(@camille, @name.locate(
|
223
|
-
assert_equal 0, t(@lucas, @name.locate(
|
224
|
-
assert_equal 5, t(@lucas, @name.locate(
|
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"))
|
225
197
|
end
|
226
198
|
|
227
199
|
def test_substring
|
@@ -231,7 +203,7 @@ module ArelExtensions
|
|
231
203
|
else
|
232
204
|
assert_equal('', t(@lucas, @name.substring(42)))
|
233
205
|
end
|
234
|
-
assert_equal 'Lu', t(@lucas, @name.substring(1,
|
206
|
+
assert_equal 'Lu', t(@lucas, @name.substring(1,2))
|
235
207
|
|
236
208
|
assert_equal 'C', t(@camille, @name[0, 1])
|
237
209
|
assert_equal 'C', t(@camille, @name[0])
|
@@ -240,32 +212,32 @@ module ArelExtensions
|
|
240
212
|
else
|
241
213
|
assert_equal('', t(@lucas, @name[42]))
|
242
214
|
end
|
243
|
-
assert_equal 'Lu', t(@lucas, @name[0,
|
215
|
+
assert_equal 'Lu', t(@lucas, @name[0,2])
|
244
216
|
assert_equal 'Lu', t(@lucas, @name[0..1])
|
245
217
|
|
246
|
-
#
|
218
|
+
#substring should accept string function
|
247
219
|
assert_equal 'Ce', t(@camille, @name.substring(1, 1).concat('e'))
|
248
|
-
assert_equal 'Ce', t(@camille, @name.substring(1, 1)
|
220
|
+
assert_equal 'Ce', t(@camille, @name.substring(1, 1)+'e')
|
249
221
|
end
|
250
222
|
|
251
223
|
def test_find_in_set
|
252
224
|
skip "Sqlite version can't load extension for find_in_set" if $sqlite && $load_extension_disabled
|
253
|
-
skip
|
225
|
+
skip "SQL Server does not know about FIND_IN_SET" if @env_db == 'mssql'
|
254
226
|
assert_equal 5, t(@neg, @comments & 2)
|
255
227
|
assert_equal 0, t(@neg, @comments & 6) # not found
|
256
228
|
end
|
257
229
|
|
258
230
|
def test_string_comparators
|
259
|
-
#
|
231
|
+
#skip "Oracle can't use math operators to compare strings" if @env_db == 'oracle' # use GREATEST ?
|
260
232
|
skip "SQL Server can't use math operators to compare strings" if @env_db == 'mssql' # use GREATEST ?
|
261
233
|
if @env_db == 'postgresql' # may return real boolean
|
262
234
|
assert t(@neg, @name >= 'Mest') == true || t(@neg, @name >= 'Mest') == 't' # depends of ar version
|
263
235
|
assert t(@neg, @name <= (@name + 'Z')) == true || t(@neg, @name <= (@name + 'Z')) == 't'
|
264
236
|
elsif @env_db == 'oracle'
|
265
|
-
assert_equal 1, t(@neg,
|
266
|
-
assert_equal 1, t(@neg,
|
267
|
-
assert_equal 1, t(@neg,
|
268
|
-
assert_equal 1, t(@neg,
|
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))
|
269
241
|
else
|
270
242
|
assert_equal 1, t(@neg, @name >= 'Mest')
|
271
243
|
assert_equal 1, t(@neg, @name <= (@name + 'Z'))
|
@@ -277,75 +249,69 @@ module ArelExtensions
|
|
277
249
|
def test_compare_on_date_time_types
|
278
250
|
skip "Sqlite can't compare time" if $sqlite
|
279
251
|
skip "Oracle can't compare time" if @env_db == 'oracle'
|
280
|
-
|
281
|
-
assert_includes [true,
|
282
|
-
assert_includes [false,
|
283
|
-
|
284
|
-
assert_includes [true,
|
285
|
-
assert_includes [false,
|
286
|
-
|
287
|
-
#
|
288
|
-
#
|
289
|
-
assert_includes [true,
|
290
|
-
assert_includes [false,
|
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))
|
291
263
|
end
|
292
264
|
|
265
|
+
|
293
266
|
def test_regexp_not_regexp
|
294
267
|
skip "Sqlite version can't load extension for regexp" if $sqlite && $load_extension_disabled
|
295
|
-
skip
|
268
|
+
skip "SQL Server does not know about REGEXP without extensions" if @env_db == 'mssql'
|
296
269
|
assert_equal 1, User.where(@name =~ '^M').count
|
297
|
-
assert_equal
|
270
|
+
assert_equal 6, User.where(@name !~ '^L').count
|
298
271
|
assert_equal 1, User.where(@name =~ /^M/).count
|
299
|
-
assert_equal
|
272
|
+
assert_equal 6, User.where(@name !~ /^L/).count
|
300
273
|
end
|
301
274
|
|
302
275
|
def test_imatches
|
303
|
-
#
|
276
|
+
#puts User.where(@name.imatches('m%')).to_sql
|
304
277
|
assert_equal 1, User.where(@name.imatches('m%')).count
|
305
278
|
assert_equal 4, User.where(@name.imatches_any(['L%', '%e'])).count
|
306
|
-
assert_equal
|
279
|
+
assert_equal 6, User.where(@name.idoes_not_match('L%')).count
|
307
280
|
end
|
308
281
|
|
309
282
|
def test_replace
|
310
|
-
assert_equal
|
311
|
-
assert_equal
|
312
|
-
|
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'))
|
283
|
+
assert_equal "LucaX", t(@lucas, @name.replace("s", "X"))
|
284
|
+
assert_equal "replace", t(@lucas, @name.replace(@name, "replace"))
|
319
285
|
end
|
320
286
|
|
321
287
|
def test_replace_once
|
322
|
-
skip
|
323
|
-
#
|
324
|
-
assert_equal
|
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))
|
325
291
|
end
|
326
292
|
|
327
293
|
def test_soundex
|
328
294
|
skip "Sqlite version can't load extension for soundex" if $sqlite && $load_extension_disabled
|
329
295
|
skip "PostgreSql version can't load extension for soundex" if @env_db == 'postgresql'
|
330
|
-
assert_equal
|
331
|
-
assert_equal
|
332
|
-
assert_equal
|
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
|
333
299
|
end
|
334
300
|
|
335
301
|
def test_change_case
|
336
|
-
assert_equal
|
337
|
-
assert_equal
|
338
|
-
assert_equal
|
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)
|
339
305
|
end
|
340
306
|
|
341
307
|
def test_trim
|
342
|
-
assert_equal
|
343
|
-
assert_equal
|
344
|
-
assert_equal
|
345
|
-
assert_equal
|
346
|
-
assert_equal
|
347
|
-
skip
|
348
|
-
assert_equal
|
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))
|
349
315
|
end
|
350
316
|
|
351
317
|
def test_blank
|
@@ -376,193 +342,30 @@ module ArelExtensions
|
|
376
342
|
|
377
343
|
def test_format
|
378
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'
|
379
346
|
assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S'))
|
380
|
-
assert_equal '12:42%', t(@lucas, @updated_at.format('%R%%'))
|
381
|
-
|
382
|
-
# The following tests will ensure proper conversion of timestamps to
|
383
|
-
# requested timezones.
|
384
|
-
#
|
385
|
-
# The names of the timezones is highly dependant on the underlying
|
386
|
-
# operating system, and this is why we need to handle each database
|
387
|
-
# separately: the images we're using to test these databases are
|
388
|
-
# different. So don't rely on the provided examples. Your setup is your
|
389
|
-
# reference.
|
390
|
-
#
|
391
|
-
# One could always have portable code if s/he uses standard
|
392
|
-
# abbreviations, like:
|
393
|
-
#
|
394
|
-
# 1. CET => Central European Time
|
395
|
-
# 2. CEST => Central European Summer Time
|
396
|
-
#
|
397
|
-
# Which implies that the caller should handle daylight saving detection.
|
398
|
-
# In fact, CET will handle daylight saving in MySQL but not Postgres.
|
399
|
-
#
|
400
|
-
# It looks like the posix convention is supported by mysql and
|
401
|
-
# postgresql, e.g.:
|
402
|
-
#
|
403
|
-
# posix/Europe/Paris
|
404
|
-
# posix/America/Nipigon
|
405
|
-
#
|
406
|
-
# so it looks like a more reliably portable way of specifying it.
|
407
|
-
time_zones = {
|
408
|
-
'mssql' => {
|
409
|
-
'utc' => 'UTC',
|
410
|
-
'sao_paulo' => 'Argentina Standard Time',
|
411
|
-
'tahiti' => 'Hawaiian Standard Time',
|
412
|
-
'paris' => 'Central European Standard Time'
|
413
|
-
},
|
414
|
-
'posix' => {
|
415
|
-
'utc' => 'UTC',
|
416
|
-
'sao_paulo' => 'America/Sao_Paulo',
|
417
|
-
'tahiti' => 'Pacific/Tahiti',
|
418
|
-
'paris' => 'Europe/Paris'
|
419
|
-
}
|
420
|
-
}
|
421
|
-
|
422
|
-
skip "Unsupported timezone conversion for DB=#{ENV['DB']}" if !%w[mssql mysql oracle postgresql].include?(ENV['DB'])
|
423
|
-
# TODO: Standarize timezone conversion across all databases.
|
424
|
-
# This test case will be refactored and should work the same across all vendors.
|
425
|
-
if ENV['DB'] == 'mssql' && /Microsoft SQL Server (\d+)/.match(ActiveRecord::Base.connection.select_value('SELECT @@version'))[1].to_i < 2016
|
426
|
-
skip "SQL Server < 2016 is not currently supported"
|
427
|
-
end
|
428
|
-
|
429
|
-
tz = ENV['DB'] == 'mssql' ? time_zones['mssql'] : time_zones['posix']
|
430
|
-
|
431
|
-
assert_equal '2014/03/03 12:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['utc']))
|
432
|
-
assert_equal '2014/03/03 09:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['sao_paulo']}))
|
433
|
-
assert_equal '2014/03/03 02:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['tahiti']}))
|
434
|
-
|
435
|
-
# Skipping conversion from UTC to the desired timezones fails in SQL
|
436
|
-
# Server and Postgres. This is mainly due to the fact that timezone
|
437
|
-
# information is not preserved in the column itself.
|
438
|
-
#
|
439
|
-
# MySQL is happy to consider that times by default are in UTC.
|
440
|
-
assert_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', {tz['utc'] => tz['paris']}))
|
441
|
-
refute_equal '2014/03/03 13:42:00', t(@lucas, @updated_at.format('%Y/%m/%d %H:%M:%S', tz['paris'])) if !['mysql'].include?(ENV['DB'])
|
442
|
-
|
443
|
-
# Winter/Summer time
|
444
|
-
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']}))
|
445
|
-
if ENV['DB'] == 'mssql'
|
446
|
-
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']}))
|
447
|
-
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']}))
|
448
|
-
else
|
449
|
-
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']))
|
450
|
-
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']))
|
451
|
-
end
|
452
|
-
end
|
453
|
-
|
454
|
-
def test_format_iso_week
|
455
|
-
skip "Unsupported ISO week number for DB=#{ENV['DB']}" if ['sqlite'].include?(ENV['DB'])
|
456
|
-
assert_equal '10', t(@lucas, @updated_at.format('%V'))
|
457
|
-
{
|
458
|
-
'2024-01-01 10:42:00' => '01', # Monday
|
459
|
-
'2030-01-01 10:42:00' => '01', # Tuesday
|
460
|
-
'2025-01-01 10:42:00' => '01', # Wednesday
|
461
|
-
'2026-01-01 10:42:00' => '01', # Thursday
|
462
|
-
'2027-01-01 10:42:00' => '53', # Friday
|
463
|
-
'2028-01-01 10:42:00' => '52', # Saturday
|
464
|
-
'2034-01-01 10:42:00' => '52', # Sunday
|
465
|
-
}.each do |date, exp|
|
466
|
-
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%V'))
|
467
|
-
end
|
468
|
-
end
|
469
|
-
|
470
|
-
def test_format_iso_year_of_week
|
471
|
-
skip "Unsupported ISO year of week for DB=#{ENV['DB']}" if %w[mssql sqlite].include?(ENV['DB'])
|
472
|
-
assert_equal '2014', t(@lucas, @updated_at.format('%G'))
|
473
|
-
|
474
|
-
{
|
475
|
-
'2024-01-01 10:42:00' => '2024', # Monday
|
476
|
-
'2030-01-01 10:42:00' => '2030', # Tuesday
|
477
|
-
'2025-01-01 10:42:00' => '2025', # Wednesday
|
478
|
-
'2026-01-01 10:42:00' => '2026', # Thursday
|
479
|
-
'2027-01-01 10:42:00' => '2026', # Friday
|
480
|
-
'2028-01-01 10:42:00' => '2027', # Saturday
|
481
|
-
'2034-01-01 10:42:00' => '2033', # Sunday
|
482
|
-
}.each do |date, exp|
|
483
|
-
assert_equal exp, t(@lucas, Arel.quoted(date).cast(:datetime).format('%G'))
|
484
|
-
end
|
485
|
-
end
|
486
|
-
|
487
|
-
def test_format_date_with_names
|
488
|
-
skip "#{ENV['DB']} does not support a variety of word-based formatting for month and day names" if %w[mssql sqlite].include?(ENV['DB'])
|
489
|
-
assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
490
|
-
assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
491
|
-
|
492
|
-
skip "#{ENV['DB']} does not support ALLCAPS month and day names" if ['mysql'].include?(ENV['DB'])
|
493
|
-
assert_equal 'Mon, 03 MAR 14', t(@lucas, @updated_at.format('%a, %d %^b %y'))
|
494
|
-
assert_equal 'Monday, 03 MARCH 14', t(@lucas, @updated_at.format('%A, %d %^B %y'))
|
495
|
-
end
|
496
|
-
|
497
|
-
def switch_to_lang(lang)
|
498
|
-
languages = {
|
499
|
-
'mssql' => {en: 'English', fr: 'French'},
|
500
|
-
'mysql' => {en: 'en_US', fr: 'fr_FR'},
|
501
|
-
'postgresql' => {en: 'en_US.utf8', fr: 'fr_FR.utf8'}
|
502
|
-
}
|
503
|
-
|
504
|
-
sql = {
|
505
|
-
'mssql' => ->(l) { "SET LANGUAGE #{l};" },
|
506
|
-
'mysql' => ->(l) { "SET lc_time_names = '#{l}';" },
|
507
|
-
'postgresql' => ->(l) { "SET lc_time to '#{l}';" }
|
508
|
-
}
|
509
|
-
|
510
|
-
User.connection.execute(sql[ENV['DB']][languages[ENV['DB']][lang]])
|
511
|
-
end
|
512
|
-
|
513
|
-
def test_format_date_with_names_and_lang_switch
|
514
|
-
skip "#{ENV['DB']} does not support word-based formatting for month and day names" if ['sqlite'].include?(ENV['DB'])
|
515
|
-
|
516
|
-
# the begin-rescue block is here to make sure we set the db back to en_US
|
517
|
-
# if we fail, so that other tests don't get contaminated.
|
518
|
-
#
|
519
|
-
# Tests should assert one single thing in principle, but until we
|
520
|
-
# refactor this whole thing, we'll have to do tricks of this sort.
|
521
|
-
begin
|
522
|
-
switch_to_lang(:en)
|
523
|
-
case ENV['DB']
|
524
|
-
when 'mysql', 'postgresql'
|
525
|
-
assert_equal 'Mon, 03 Mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
526
|
-
assert_equal 'Monday, 03 March 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
527
|
-
when 'mssql'
|
528
|
-
assert_equal 'Monday, 03 March 2014', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
529
|
-
end
|
530
|
-
switch_to_lang(:fr)
|
531
|
-
case ENV['DB']
|
532
|
-
when 'mysql'
|
533
|
-
assert_equal 'lun, 03 mar 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
534
|
-
assert_equal 'lundi, 03 mars 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
535
|
-
when 'postgresql'
|
536
|
-
assert_equal 'Lun., 03 Mars 14', t(@lucas, @updated_at.format('%a, %d %b %y'))
|
537
|
-
assert_equal 'Lundi, 03 Mars 14', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
538
|
-
when 'mssql'
|
539
|
-
assert_equal 'lundi, 03 mars 2014', t(@lucas, @updated_at.format('%A, %d %B %y'))
|
540
|
-
end
|
541
|
-
ensure
|
542
|
-
switch_to_lang(:en)
|
543
|
-
end
|
544
347
|
end
|
545
348
|
|
546
349
|
def test_coalesce
|
547
|
-
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil,
|
350
|
+
assert_equal 'Camille concat', t(@camille, @name.coalesce(nil, "default") + ' concat')
|
548
351
|
|
549
|
-
assert_equal 'toto', t(@test, @other.coalesce(
|
352
|
+
assert_equal 'toto', t(@test, @other.coalesce(""))
|
550
353
|
|
551
|
-
assert_equal ' ', t(@myung, @comments.coalesce(
|
552
|
-
assert_equal 'Laure', t(@laure, @comments.coalesce(
|
354
|
+
assert_equal ' ', t(@myung, @comments.coalesce("Myung").coalesce('ignored'))
|
355
|
+
assert_equal 'Laure', t(@laure, @comments.coalesce("Laure"))
|
553
356
|
if @env_db == 'oracle'
|
554
|
-
assert_nil t(@laure, @comments.coalesce(
|
555
|
-
assert_nil t(@camille, @other.coalesce(
|
357
|
+
assert_nil t(@laure, @comments.coalesce(""))
|
358
|
+
assert_nil t(@camille, @other.coalesce(""))
|
556
359
|
else
|
557
|
-
assert_equal('', t(@laure, @comments.coalesce(
|
558
|
-
assert_equal '', t(@camille, @other.coalesce(
|
360
|
+
assert_equal('', t(@laure, @comments.coalesce("")))
|
361
|
+
assert_equal '', t(@camille, @other.coalesce(""))
|
559
362
|
end
|
560
363
|
assert_equal 100, t(@test, @age.coalesce(100))
|
561
|
-
assert_equal
|
364
|
+
assert_equal "Camille", t(@camille, @name.coalesce(nil, "default"))
|
562
365
|
assert_equal 20, t(@test, @age.coalesce(nil, 20))
|
563
366
|
|
564
|
-
assert_equal 20, t(@test, @age.coalesce(10)
|
565
|
-
assert_equal 'Laure10', t(@laure, @comments.coalesce(
|
367
|
+
assert_equal 20, t(@test, @age.coalesce(10)+10)
|
368
|
+
assert_equal 'Laure10', t(@laure, @comments.coalesce("Laure") + 10)
|
566
369
|
end
|
567
370
|
|
568
371
|
# Comparators
|
@@ -571,37 +374,37 @@ module ArelExtensions
|
|
571
374
|
assert_equal 2, User.where(@age <= 10).count
|
572
375
|
assert_equal 3, User.where(@age > 20).count
|
573
376
|
assert_equal 4, User.where(@age >= 20).count
|
574
|
-
assert_equal
|
377
|
+
assert_equal 1, User.where(@age > 5).where(@age < 20).count
|
575
378
|
end
|
576
379
|
|
577
380
|
def test_date_comparator
|
578
381
|
d = Date.new(2016, 5, 23)
|
579
382
|
assert_equal 0, User.where(@created_at < d).count
|
580
|
-
assert_equal
|
383
|
+
assert_equal 8, User.where(@created_at >= d).count
|
581
384
|
end
|
582
385
|
|
583
386
|
def test_date_duration
|
584
|
-
#
|
387
|
+
#Year
|
585
388
|
assert_equal 2016, t(@lucas, @created_at.year).to_i
|
586
|
-
assert_equal 0, User.where(@created_at.year.eq(
|
587
|
-
#
|
389
|
+
assert_equal 0, User.where(@created_at.year.eq("2012")).count
|
390
|
+
#Month
|
588
391
|
assert_equal 5, t(@camille, @created_at.month).to_i
|
589
|
-
assert_equal
|
590
|
-
#
|
392
|
+
assert_equal 8, User.where(@created_at.month.eq("05")).count
|
393
|
+
#Week
|
591
394
|
assert_equal(@env_db == 'mssql' ? 22 : 21, t(@arthur, @created_at.week).to_i)
|
592
|
-
assert_equal
|
593
|
-
#
|
395
|
+
assert_equal 8, User.where(@created_at.month.eq("05")).count
|
396
|
+
#Day
|
594
397
|
assert_equal 23, t(@laure, @created_at.day).to_i
|
595
|
-
assert_equal 0, User.where(@created_at.day.eq(
|
398
|
+
assert_equal 0, User.where(@created_at.day.eq("05")).count
|
596
399
|
|
597
|
-
#
|
598
|
-
#
|
400
|
+
#skip "manage DATE" if @env_db == 'oracle'
|
401
|
+
#Hour
|
599
402
|
assert_equal 0, t(@laure, @created_at.hour).to_i
|
600
403
|
assert_equal 12, t(@lucas, @updated_at.hour).to_i
|
601
|
-
#
|
404
|
+
#Minute
|
602
405
|
assert_equal 0, t(@laure, @created_at.minute).to_i
|
603
406
|
assert_equal 42, t(@lucas, @updated_at.minute).to_i
|
604
|
-
#
|
407
|
+
#Second
|
605
408
|
assert_equal 0, t(@laure, @created_at.second).to_i
|
606
409
|
assert_equal 0, t(@lucas, @updated_at.second).to_i
|
607
410
|
end
|
@@ -609,8 +412,8 @@ module ArelExtensions
|
|
609
412
|
def test_datetime_diff
|
610
413
|
assert_equal 0, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 42)).to_i
|
611
414
|
if @env_db == 'oracle' && Arel::VERSION.to_i > 6 # in rails 5, result is multiplied by 24*60*60 = 86400...
|
612
|
-
assert_equal 42 *
|
613
|
-
assert_equal(-3600 *
|
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)
|
614
417
|
else
|
615
418
|
assert_equal 42, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 12, 41, 18)).to_i
|
616
419
|
assert_equal(-3600, t(@lucas, @updated_at - Time.utc(2014, 3, 3, 13, 42)).to_i)
|
@@ -621,8 +424,7 @@ module ArelExtensions
|
|
621
424
|
end
|
622
425
|
end
|
623
426
|
|
624
|
-
|
625
|
-
skip 'not yet implemented' if $sqlite
|
427
|
+
skip "not yet implemented" if $sqlite
|
626
428
|
|
627
429
|
date1 = Date.new(2016, 5, 23)
|
628
430
|
durPos = 10.years
|
@@ -634,108 +436,106 @@ module ArelExtensions
|
|
634
436
|
|
635
437
|
datetime1 = Time.utc(2014, 3, 3, 12, 42, 0)
|
636
438
|
# Pull Request #5 tests
|
637
|
-
#
|
638
|
-
assert_includes [date2,
|
639
|
-
assert_includes [date3,
|
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))
|
640
442
|
|
641
|
-
#
|
642
|
-
assert_includes [date4,
|
643
|
-
#
|
644
|
-
assert_includes [date5,
|
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))
|
645
447
|
|
646
|
-
assert_includes [datetime1 + 42.seconds,
|
647
|
-
assert_includes [datetime1 - 42.seconds,
|
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))
|
648
450
|
|
649
451
|
# (@updated_at + Arel.duration('s',(@updated_at.hour*60 + @updated_at.minute))).to_sql
|
650
|
-
assert_includes [datetime1 + (12
|
651
|
-
|
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))))
|
652
454
|
|
653
|
-
assert_includes [datetime1 + (12
|
654
|
-
|
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))))
|
655
457
|
|
656
|
-
assert_includes [
|
657
|
-
#
|
658
|
-
assert_includes [
|
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'))
|
659
461
|
|
660
462
|
|
661
463
|
# we test with the ruby object or the string because some adapters don't return an object Date
|
662
|
-
|
464
|
+
# end
|
663
465
|
end
|
664
466
|
|
665
467
|
# TODO; cast types
|
666
468
|
def test_cast_types
|
667
|
-
assert_equal
|
668
|
-
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))
|
669
470
|
if @env_db == 'mysql' || @env_db == 'postgresql' || @env_db == 'oracle' || @env_db == 'mssql'
|
670
|
-
assert_equal 1, t(@laure,
|
671
|
-
assert_equal 1, t(@laure,
|
672
|
-
assert_equal
|
673
|
-
assert_equal
|
674
|
-
assert_equal 21.16, t(@laure
|
675
|
-
assert_equal 21, t(@laure
|
676
|
-
|
677
|
-
assert_equal String, t(@lucas
|
678
|
-
|
679
|
-
assert_equal
|
680
|
-
assert_equal Time, t(@lucas
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
assert_equal
|
685
|
-
assert_equal
|
686
|
-
assert_equal Date.parse(
|
687
|
-
assert_equal
|
688
|
-
assert_equal Date.parse('2014-03-03'), t(@lucas, @updated_at.cast(:date))
|
689
|
-
assert_equal '12:42:00', t(@lucas, @updated_at.cast(:time).cast(:string)).split('.').first unless @env_db == 'oracle' # DateTime
|
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
|
690
489
|
end
|
691
490
|
end
|
692
491
|
|
693
492
|
def test_is_null
|
694
|
-
#
|
695
|
-
#
|
696
|
-
#
|
697
|
-
#
|
698
|
-
#
|
699
|
-
assert_equal
|
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
|
700
499
|
end
|
701
500
|
|
702
501
|
def test_math_plus
|
703
502
|
d = Date.new(1997, 6, 15)
|
704
|
-
#
|
705
|
-
assert_equal
|
706
|
-
assert_equal
|
707
|
-
assert_equal
|
708
|
-
assert_equal
|
709
|
-
assert_equal
|
710
|
-
assert_equal
|
711
|
-
assert_equal
|
712
|
-
assert_equal
|
713
|
-
#
|
714
|
-
assert_equal
|
715
|
-
#
|
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
|
716
515
|
assert_equal 1, User.where((@age + 10).eq(33)).count
|
717
|
-
assert_equal 1, User.where((@age +
|
516
|
+
assert_equal 1, User.where((@age + "1").eq(6)).count
|
718
517
|
assert_equal 1, User.where((@age + @age).eq(10)).count
|
719
|
-
#
|
720
|
-
#
|
721
|
-
assert_equal
|
722
|
-
assert_equal
|
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
|
723
522
|
end
|
724
523
|
|
524
|
+
|
725
525
|
def test_math_minus
|
726
526
|
d = Date.new(2016, 5, 20)
|
727
|
-
#
|
728
|
-
assert_equal
|
729
|
-
assert_equal 3, @laure.select((@created_at - d).as(
|
730
|
-
#
|
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
|
731
531
|
assert_equal 0, User.where((@age - 10).eq(50)).count
|
732
|
-
assert_equal 0, User.where((@age -
|
532
|
+
assert_equal 0, User.where((@age - "10").eq(50)).count
|
733
533
|
# assert_equal 0, User.where((@age - 9.5).eq(50.5)).count # should work: TODO
|
734
|
-
assert_equal 0, User.where((@age -
|
534
|
+
assert_equal 0, User.where((@age - "9.5").eq(50.5)).count
|
735
535
|
end
|
736
536
|
|
737
537
|
def test_wday
|
738
|
-
#
|
538
|
+
#d = Date.new(2016, 6, 26)
|
739
539
|
assert_equal(@env_db == 'oracle' || @env_db == 'mssql' ? 2 : 1, t(@myung, @created_at.wday).to_i) # monday
|
740
540
|
end
|
741
541
|
|
@@ -754,8 +554,8 @@ module ArelExtensions
|
|
754
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
|
755
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
|
756
556
|
|
757
|
-
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
|
758
|
-
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
|
759
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
|
760
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
|
761
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
|
@@ -766,118 +566,116 @@ module ArelExtensions
|
|
766
566
|
# Case clause
|
767
567
|
def test_case
|
768
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)
|
769
|
-
assert_equal
|
770
|
-
assert_equal
|
771
|
-
assert_equal
|
772
|
-
assert_equal
|
773
|
-
assert_equal
|
774
|
-
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
|
775
574
|
end
|
776
575
|
|
777
576
|
def test_format_numbers
|
778
|
-
#
|
779
|
-
skip
|
780
|
-
|
781
|
-
assert_equal
|
782
|
-
assert_equal
|
783
|
-
assert_equal
|
784
|
-
assert_equal
|
785
|
-
assert_equal
|
786
|
-
assert_equal
|
787
|
-
assert_equal
|
788
|
-
assert_equal
|
789
|
-
assert_equal
|
790
|
-
assert_equal
|
791
|
-
assert_equal
|
792
|
-
assert_equal
|
793
|
-
assert_includes [
|
794
|
-
assert_includes [
|
795
|
-
assert_includes [
|
796
|
-
assert_equal
|
797
|
-
assert_equal
|
798
|
-
assert_equal
|
799
|
-
assert_equal
|
800
|
-
assert_equal
|
801
|
-
assert_equal
|
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"))
|
802
601
|
end
|
803
602
|
|
804
603
|
def test_accent_insensitive
|
805
|
-
skip
|
806
|
-
skip
|
604
|
+
skip "SQLite is natively Case Insensitive and Accent Sensitive" if $sqlite
|
605
|
+
skip "Not finished" if @env_db == 'mysql'
|
807
606
|
# actual comments value: "arrêté"
|
808
|
-
#
|
607
|
+
#AI & CI
|
809
608
|
if !['postgresql'].include?(@env_db) # Extension unaccent required on PG
|
810
|
-
assert_equal
|
811
|
-
assert_equal
|
812
|
-
assert_equal
|
813
|
-
assert_equal
|
814
|
-
assert_equal
|
815
|
-
assert_equal
|
816
|
-
#
|
817
|
-
assert_equal
|
818
|
-
assert_equal
|
819
|
-
assert_equal
|
820
|
-
assert_equal
|
821
|
-
if
|
822
|
-
assert_equal
|
823
|
-
assert_equal
|
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"))
|
824
623
|
end
|
825
624
|
end
|
826
|
-
#
|
827
|
-
assert_equal
|
625
|
+
#AS & CI
|
626
|
+
assert_equal "1", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arrêté")).then("1").else("0"))
|
828
627
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
829
|
-
assert_equal
|
830
|
-
assert_equal
|
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"))
|
831
630
|
end
|
832
|
-
assert_equal
|
631
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("arretez")).then("1").else("0"))
|
833
632
|
if !['mysql'].include?(@env_db) # CI => AI in utf8 (AI not possible in latin1)
|
834
|
-
assert_equal
|
633
|
+
assert_equal "0", t(@arthur,ArelExtensions::Nodes::Case.new.when(@comments.imatches("Arrete")).then("1").else("0"))
|
835
634
|
end
|
836
|
-
assert_equal
|
837
|
-
#
|
838
|
-
assert_equal
|
839
|
-
assert_equal
|
840
|
-
assert_equal
|
841
|
-
assert_equal
|
842
|
-
assert_equal
|
843
|
-
assert_equal
|
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"))
|
844
643
|
end
|
845
644
|
|
846
645
|
def test_subquery_with_order
|
847
|
-
|
848
|
-
assert_equal
|
849
|
-
|
850
|
-
|
851
|
-
assert_equal
|
852
|
-
# 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
|
853
651
|
end
|
854
652
|
end
|
855
653
|
|
856
654
|
def test_in_with_nil
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
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?
|
875
673
|
end
|
876
674
|
|
877
675
|
def test_scope_with_in_plus_new
|
878
676
|
begin
|
879
|
-
@test.where(@age.in([1,
|
880
|
-
@test.where(@age.not_in([1,
|
677
|
+
@test.where(@age.in([1,2])).new
|
678
|
+
@test.where(@age.not_in([1,2])).new
|
881
679
|
assert true
|
882
680
|
rescue
|
883
681
|
assert false
|
@@ -885,129 +683,114 @@ module ArelExtensions
|
|
885
683
|
end
|
886
684
|
|
887
685
|
def test_is_not_null
|
888
|
-
|
889
|
-
|
686
|
+
assert_equal false , @myung.where(@age.is_not_null).blank?
|
687
|
+
assert_equal true , @test.where(@age.is_not_null).blank?
|
890
688
|
end
|
891
689
|
|
892
690
|
def test_not_in_with_nil
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
#
|
908
|
-
#
|
909
|
-
#
|
910
|
-
#
|
911
|
-
#
|
912
|
-
#
|
913
|
-
#
|
914
|
-
#
|
915
|
-
end
|
916
|
-
|
917
|
-
def test_in_on_grouping
|
918
|
-
skip 'We should modify the visitor of IN to make it work' if $sqlite || @env_db == 'mssql'
|
919
|
-
assert_equal 2, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23), Arel.tuple('Arthur', 21))).count
|
920
|
-
assert_equal 1, User.where(Arel.tuple(@name, @age).in(Arel.tuple('Myung', 23))).count
|
921
|
-
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?
|
922
713
|
end
|
923
714
|
|
924
715
|
def test_alias_shortened
|
925
|
-
if
|
716
|
+
if ['postgresql','oracle'].include?(@env_db)
|
926
717
|
new_alias = Arel.shorten('azerty' * 15)
|
927
718
|
at = User.arel_table.alias('azerty' * 15)
|
928
719
|
assert_equal "\"user_tests\" \"#{new_alias}\"".downcase, User.arel_table.alias('azerty' * 15).to_sql.downcase
|
929
720
|
assert_equal '"user_tests" "u"'.downcase, User.arel_table.alias('u').to_sql.downcase
|
930
|
-
assert_equal
|
931
|
-
|
721
|
+
assert_equal %Q[SELECT "#{new_alias}"."id" FROM "user_tests" "#{new_alias}"].downcase,
|
722
|
+
User.select(at[:id]).from(at).to_sql.downcase
|
932
723
|
end
|
933
724
|
end
|
934
725
|
|
935
726
|
def test_stat_functions
|
936
727
|
skip "SQLite doesn't work for most on this functions" if $sqlite
|
937
|
-
#
|
938
|
-
#
|
939
|
-
#
|
940
|
-
#
|
941
|
-
#
|
942
|
-
|
943
|
-
assert ( 15.
|
944
|
-
assert (
|
945
|
-
assert (
|
946
|
-
assert (
|
947
|
-
assert (
|
948
|
-
skip 'Not Yet Implemented' # if !['postgresql'].include?(@env_db)
|
949
|
-
assert_equal 2, User.select(@score.std(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
950
|
-
assert_equal 2, User.select(@score.variance(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
951
|
-
assert_equal 2, User.select(@score.sum(group: Arel.when(@name > 'M').then(0).else(1)).as('res')).map{|e| e['res']}.uniq.length
|
952
|
-
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
|
953
739
|
end
|
954
740
|
|
955
741
|
def test_levenshtein_distance
|
956
|
-
skip
|
957
|
-
assert_equal 0, t(@arthur
|
958
|
-
assert_equal 2, t(@arthur
|
959
|
-
assert_equal 1, t(@arthur
|
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"))
|
960
746
|
end
|
961
747
|
|
962
748
|
def test_json
|
963
749
|
skip "Can't be tested on travis"
|
964
|
-
#
|
965
|
-
assert_equal 'Arthur', t(@arthur,
|
966
|
-
assert_equal
|
967
|
-
assert_equal ({
|
968
|
-
assert_equal ({
|
969
|
-
assert_equal [{
|
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}])))
|
970
756
|
|
971
757
|
# aggregate
|
972
|
-
assert_equal ({
|
973
|
-
|
974
|
-
assert_equal ({
|
975
|
-
|
976
|
-
assert_equal [{
|
977
|
-
|
978
|
-
|
979
|
-
#
|
980
|
-
#
|
981
|
-
|
982
|
-
skip
|
983
|
-
#
|
984
|
-
h1 = Arel.json({@name => @name
|
985
|
-
assert_equal
|
986
|
-
h2 = Arel.json([{age: @age},
|
987
|
-
assert_equal ({
|
988
|
-
assert_equal 21, parse_json(t(@arthur,
|
989
|
-
assert_nil t(@arthur,
|
990
|
-
#
|
991
|
-
assert_equal ({
|
992
|
-
assert_equal ({
|
993
|
-
assert_equal ({
|
994
|
-
assert_equal ({
|
995
|
-
#
|
996
|
-
assert_equal ({
|
997
|
-
assert_equal ({
|
998
|
-
assert_equal ({
|
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({})))
|
999
785
|
end
|
1000
786
|
|
1001
787
|
def test_as_on_everything
|
1002
788
|
name = @arthur.select(@name.as('NaMe')).first.attributes
|
1003
|
-
assert_equal 'Arthur', name[
|
1004
|
-
assert_equal 'Arthur', @arthur.select(@name.as('Na Me')).first.attributes[
|
1005
|
-
assert_equal 'ArthurArthur', @arthur.select((@name
|
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"]
|
1006
792
|
end
|
1007
793
|
|
1008
|
-
def test_exists_in_subquery
|
1009
|
-
assert User.where(User.where(nil).arel.exists).first
|
1010
|
-
end
|
1011
794
|
end
|
1012
795
|
end
|
1013
796
|
end
|