arel_extensions 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.travis/oracle/download.js +116 -0
  3. data/.travis/oracle/download.sh +16 -0
  4. data/.travis/oracle/install.sh +32 -0
  5. data/.travis.yml +72 -69
  6. data/Rakefile +29 -4
  7. data/arel_extensions.gemspec +1 -1
  8. data/functions.html +21 -12
  9. data/gemfiles/rails4.gemfile +11 -3
  10. data/gemfiles/rails5.gemfile +8 -4
  11. data/lib/arel_extensions/math.rb +2 -2
  12. data/lib/arel_extensions/math_functions.rb +1 -1
  13. data/lib/arel_extensions/nodes/concat.rb +16 -0
  14. data/lib/arel_extensions/nodes/date_diff.rb +37 -25
  15. data/lib/arel_extensions/nodes/function.rb +30 -0
  16. data/lib/arel_extensions/nodes/is_null.rb +6 -0
  17. data/lib/arel_extensions/nodes/replace.rb +8 -26
  18. data/lib/arel_extensions/nodes/round.rb +6 -6
  19. data/lib/arel_extensions/nodes.rb +1 -1
  20. data/lib/arel_extensions/null_functions.rb +2 -2
  21. data/lib/arel_extensions/string_functions.rb +5 -1
  22. data/lib/arel_extensions/version.rb +1 -1
  23. data/lib/arel_extensions/visitors/mysql.rb +89 -113
  24. data/lib/arel_extensions/visitors/oracle.rb +28 -65
  25. data/lib/arel_extensions/visitors/postgresql.rb +82 -121
  26. data/lib/arel_extensions/visitors/sqlite.rb +60 -55
  27. data/lib/arel_extensions/visitors/to_sql.rb +30 -5
  28. data/test/database.yml +15 -3
  29. data/test/real_db_test.rb +0 -1
  30. data/test/visitors/test_bulk_insert_sqlite.rb +2 -1
  31. data/test/visitors/test_oracle.rb +2 -2
  32. data/test/visitors/test_to_sql.rb +3 -4
  33. data/test/with_ar/all_agnostic_test.rb +294 -0
  34. data/test/with_ar/insert_agnostic_test.rb +52 -0
  35. data/test/with_ar/test_bulk_sqlite.rb +3 -2
  36. metadata +10 -5
  37. data/lib/arel_extensions/nodes/isnull.rb +0 -30
  38. data/test/with_ar/test_string_postgresql.rb +0 -81
@@ -0,0 +1,294 @@
1
+ require 'helper'
2
+ require 'date'
3
+
4
+ module ArelExtensions
5
+ module WthAr
6
+
7
+ class ListTest < Minitest::Test
8
+ def setup_db
9
+ ActiveRecord::Base.configurations = YAML.load_file('test/database.yml')
10
+ ActiveRecord::Base.establish_connection(ENV['DB'].try(:to_sym) || (RUBY_PLATFORM == 'java' ? :"jdbc-sqlite" : :sqlite))
11
+ ActiveRecord::Base.default_timezone = :utc
12
+ @cnx = ActiveRecord::Base.connection
13
+ $sqlite ||= false
14
+ if ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
15
+ $sqlite = true
16
+ db = @cnx.raw_connection
17
+ $load_extension_disabled ||= false
18
+ if !$load_extension_disabled
19
+ begin
20
+ db.create_function("find_in_set", 1) do |func, value1, value2|
21
+ func.result = value1.index(value2)
22
+ end
23
+ db.enable_load_extension(1)
24
+ db.load_extension("/usr/lib/sqlite3/pcre.so")
25
+ db.load_extension("/usr/lib/sqlite3/extension-functions.so")
26
+ db.enable_load_extension(0)
27
+ #function find_in_set
28
+ rescue => e
29
+ $load_extension_disabled = true
30
+ puts "can not load extensions #{e.inspect}"
31
+ end
32
+ end
33
+ end
34
+ if File.exist?("init/#{ENV['DB']}.sql")
35
+ sql = File.read("init/#{ENV['DB']}.sql")
36
+ @cnx.execute(sql) unless sql.blank?
37
+ end
38
+ @cnx.drop_table(:users) rescue nil
39
+ @cnx.create_table :users do |t|
40
+ t.column :age, :integer
41
+ t.column :name, :string
42
+ t.column :comments, :text
43
+ t.column :created_at, :date
44
+ t.column :updated_at, :datetime
45
+ t.column :score, :decimal, :precision => 20, :scale => 10
46
+ end
47
+ @cnx.drop_table(:products) rescue nil
48
+ @cnx.create_table :products do |t|
49
+ t.column :price, :decimal, :precision => 20, :scale => 10
50
+ end
51
+ end
52
+
53
+ def teardown_db
54
+ @cnx.drop_table(:users)
55
+ @cnx.drop_table(:products)
56
+ end
57
+
58
+ class User < ActiveRecord::Base
59
+ end
60
+ class Product < ActiveRecord::Base
61
+ end
62
+
63
+
64
+ def setup
65
+ d = Date.new(2016,05,23)
66
+ setup_db
67
+ u = User.create :age => 5, :name => "Lucas", :created_at => d, :score => 20.16
68
+ @lucas = User.where(:id => u.id)
69
+ u = User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
70
+ @sophie = User.where(:id => u.id)
71
+ u = User.create :age => 20, :name => "Camille", :created_at => d, :score => 20.16
72
+ @camille = User.where(:id => u.id)
73
+ u = User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62
74
+ @arthur = User.where(:id => u.id)
75
+ u = User.create :age => 23, :name => "Myung", :created_at => d, :score => 20.16
76
+ @myung = User.where(:id => u.id)
77
+ u = User.create :age => 25, :name => "Laure", :created_at => d, :score => 20.16
78
+ @laure = User.where(:id => u.id)
79
+ u = User.create :age => nil, :name => "Test", :created_at => d, :score => 1.62
80
+ @test = User.where(:id => u.id)
81
+ u = User.create :age => -42, :name => "Negatif", :comments => '1,22,3,42,2', :created_at => d, :updated_at => d.to_time, :score => 0.17
82
+ @neg = User.where(:id => u.id)
83
+
84
+ @age = User.arel_table[:age]
85
+ @name = User.arel_table[:name]
86
+ @score = User.arel_table[:score]
87
+ @created_at = User.arel_table[:created_at]
88
+ @comments = User.arel_table[:comments]
89
+ @price = Product.arel_table[:price]
90
+ end
91
+
92
+ def teardown
93
+ teardown_db
94
+ end
95
+
96
+ def t(scope, node)
97
+ scope.select(node.as('res')).first.res
98
+ end
99
+
100
+ # Math Functions
101
+ def test_classical_arel
102
+ assert_in_epsilon 42.16, t(@laure, @score + 22), 0.01
103
+ end
104
+
105
+ def test_abs
106
+ assert_equal 42, t(@neg, @age.abs)
107
+ assert_equal 14, t(@laure, (@age - 39).abs)
108
+ assert_equal 28, t(@laure, (@age - 39).abs + (@age - 39).abs)
109
+ end
110
+
111
+ def test_ceil
112
+ if !$sqlite || !$load_extension_disabled
113
+ assert_equal 1, t(@neg, @score.ceil)
114
+ assert_equal 63, t(@arthur, @age.ceil + 42)
115
+ end
116
+ end
117
+
118
+ def test_floor
119
+ if !$sqlite || !$load_extension_disabled
120
+ assert_equal 0, t(@neg, @score.floor)
121
+ assert_equal 42, t(@arthur, @score.floor - 23)
122
+ end
123
+ end
124
+
125
+ def test_rand
126
+ assert 42 != User.select(Arel.rand.as('res')).first.res
127
+ assert 0 <= User.select(Arel.rand.abs.as('res')).first.res
128
+ assert_equal 8, User.order(Arel.rand).limit(50).count
129
+ end
130
+
131
+ def test_round
132
+ assert_equal 1, User.where(@age.round(0).eq(5.0)).count
133
+ assert_equal 0, User.where(@age.round(-1).eq(6.0)).count
134
+ assert_equal 66, t(@arthur, @score.round)
135
+ assert_in_epsilon 67.6, t(@arthur, @score.round(1) + 2), 0.01
136
+ end
137
+
138
+ def test_sum
139
+ assert_equal 68, User.select((@age.sum + 1).as("res")).take(50).first.res
140
+ assert_equal 134, User.select((@age.sum + @age.sum).as("res")).take(50).first.res
141
+ assert_equal 201, User.select(((@age * 3).sum).as("res")).take(50).first.res
142
+ assert_equal 4009, User.select(((@age * @age).sum).as("res")).take(50).first.res
143
+ end
144
+
145
+ # String Functions
146
+ def test_concat
147
+ assert_equal 'Camille Camille', t(@camille, @name + ' ' + @name)
148
+ assert_equal 'Laure 2', t(@laure, @name + ' ' + 2)
149
+ if ENV['DB'] == 'postgresql'
150
+ assert_equal "Lucas Sophie", t(User.reorder(nil).from(User.select(:name).where(:name => ['Lucas', 'Sophie']).reorder(:name).as('users')), @name.group_concat(' '))
151
+ else
152
+ assert_equal "Lucas Sophie", t(User.where(:name => ['Lucas', 'Sophie']).reorder(:name), @name.group_concat(' '))
153
+ end
154
+ end
155
+
156
+ def test_length
157
+ assert_equal 7, t(@camille, @name.length)
158
+ assert_equal 7, t(@camille, @name.length.round.abs)
159
+ assert_equal 42, t(@laure, @name.length + 37)
160
+ end
161
+
162
+ def test_locate
163
+ if !$sqlite || !$load_extension_disabled
164
+ assert_equal 1, t(@camille, @name.locate("C"))
165
+ assert_equal 0, t(@lucas, @name.locate("z"))
166
+ assert_equal 5, t(@lucas, @name.locate("s"))
167
+ end
168
+ end
169
+
170
+ def test_find_in_set
171
+ if !$sqlite || !$load_extension_disabled
172
+ assert 4, t(@neg, @comments & 2)
173
+ assert 2, t(@neg, @comments & 6)
174
+ end
175
+ end
176
+
177
+ def test_string_comparators
178
+ assert 1, t(@neg, @name >= 'test')
179
+ assert 1, t(@neg, @name <= @comments)
180
+ end
181
+
182
+ def test_regexp_not_regex
183
+ if !$sqlite || !$load_extension_disabled
184
+ assert_equal 1, User.where(@name =~ '^M').count
185
+ assert_equal 6, User.where(@name !~ '^L').count
186
+ assert_equal 1, User.where(@name =~ /^M/).count
187
+ assert_equal 6, User.where(@name !~ /^L/).count
188
+ end
189
+ end
190
+
191
+ def test_imatches
192
+ assert_equal 1, User.where(@name.imatches('m%')).count
193
+ assert_equal 4, User.where(@name.imatches_any(['L%', '%e'])).count
194
+ assert_equal 6, User.where(@name.idoes_not_match('L%')).count
195
+ end
196
+
197
+ def test_replace
198
+ assert_equal "LucaX", t(@lucas, @name.replace("s","X"))
199
+ assert_equal "replace", t(@lucas, @name.replace(@name,"replace"))
200
+ end
201
+
202
+ def test_soundex
203
+ if (!$sqlite || !$load_extension_disabled) && (ENV['DB'] != 'postgresql')
204
+ assert_equal "C540", t(@camille, @name.soundex)
205
+ assert_equal 8, User.where(@name.soundex.eq(@name.soundex)).count
206
+ end
207
+ end
208
+
209
+ def test_trim
210
+ assert_equal "Myun", t(@myung, @name.rtrim("g"))
211
+ assert_equal "yung", t(@myung, @name.ltrim("M"))
212
+ assert_equal "yung", t(@myung, (@name + "M").trim("M"))
213
+ assert_equal "", t(@myung, @name.rtrim(@name))
214
+ end
215
+
216
+ def test_coalesce
217
+ if ENV['DB'] == 'postgresql'
218
+ assert_equal 100, t(@test, @age.coalesce(100))
219
+ assert_equal "Camille", t(@camille, @name.coalesce(nil, "default"))
220
+ assert_equal 20, t(@test, @age.coalesce(nil, 20))
221
+ else
222
+ assert_equal "Camille", t(@camille, @name.coalesce(nil, '20'))
223
+ assert_equal 20, t(@test, @age.coalesce(nil, 20))
224
+ end
225
+ end
226
+
227
+ def test_comparator
228
+ assert_equal 2, User.where(@age < 6).count
229
+ assert_equal 2, User.where(@age <= 10).count
230
+ assert_equal 3, User.where(@age > 20).count
231
+ assert_equal 4, User.where(@age >= 20).count
232
+ assert_equal 1, User.where(@age > 5).where(@age < 20).count
233
+ end
234
+
235
+ def test_date_duration
236
+ #Year
237
+ assert_equal 2016, @lucas.select((User.arel_table[:created_at].year).as("res")).first.res.to_i
238
+ assert_equal 0, User.where(@created_at.year.eq("2012")).count
239
+ #Month
240
+ assert_equal 5, @camille.select((User.arel_table[:created_at].month).as("res")).first.res.to_i
241
+ assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
242
+ #Week
243
+ assert_equal 21,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:created_at].week).as("res")).first.res.to_i
244
+ assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
245
+ #Day
246
+ assert_equal 23,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at].day).as("res")).first.res.to_i
247
+ assert_equal 0,User.where(User.arel_table[:created_at].day.eq("05")).count
248
+ end
249
+
250
+
251
+ def test_is_null
252
+ assert_equal "Test", User.where(@age.is_null).select(@name).first.name
253
+ end
254
+
255
+
256
+ def test_math_plus
257
+ d = Date.new(1997, 6, 15)
258
+ #Concat String
259
+ assert_equal "SophiePhan", t(@sophie, @name + "Phan")
260
+ assert_equal "Sophie2", t(@sophie, @name + 2)
261
+ assert_equal "Sophie1997-06-15", t(@sophie, @name + d)
262
+ assert_equal "Sophie15", t(@sophie, @name + @age)
263
+ assert_equal "SophieSophie", t(@sophie, @name + @name)
264
+ assert_equal "Sophie2016-05-23", t(@sophie, @name + @created_at)
265
+ #concat Integer
266
+ assert_equal 1, User.where((@age + 10).eq(33)).count
267
+ assert_equal 1, User.where((@age + "1").eq(6)).count
268
+ assert_equal 1, User.where((@age + @age).eq(10)).count
269
+ #concat Date
270
+ # puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
271
+ assert_equal "2016-05-24", t(@myung, @created_at + 1).to_date.to_s
272
+ assert_equal "2016-05-25", t(@myung, @created_at + 2.day).to_date.to_s
273
+ end
274
+
275
+
276
+ def test_math_moins
277
+ d = Date.new(2016,05,20)
278
+ #Datediff
279
+ assert_equal 8, User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
280
+ assert_equal 3, User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at] - d).as("res")).first.res.abs.to_i
281
+ #Substraction
282
+ assert_equal 0, User.where((@age - 10).eq(50)).count
283
+ assert_equal 0, User.where((@age - "10").eq(50)).count
284
+ end
285
+
286
+ def test_wday
287
+ d = Date.new(2016, 6, 26)
288
+ assert_equal 1, t(@myung, @created_at.wday).to_i
289
+ assert_equal 0, User.select(d.wday).as("res").first.to_i
290
+ end
291
+
292
+ end
293
+ end
294
+ end
@@ -0,0 +1,52 @@
1
+ require 'helper'
2
+ require 'date'
3
+
4
+ module ArelExtensions
5
+ module WthAr
6
+
7
+ class InsertManagerTest < Minitest::Test
8
+ def setup_db
9
+ ActiveRecord::Base.configurations = YAML.load_file('test/database.yml')
10
+ ActiveRecord::Base.establish_connection(ENV['DB'].try(:to_sym) || (RUBY_PLATFORM == 'java' ? :"jdbc-sqlite" : :sqlite))
11
+ ActiveRecord::Base.default_timezone = :utc
12
+ @cnx = ActiveRecord::Base.connection
13
+ Arel::Table.engine = ActiveRecord::Base
14
+ if File.exist?("init/#{ENV['DB']}.sql")
15
+ sql = File.read("init/#{ENV['DB']}.sql")
16
+ @cnx.execute(sql) unless sql.blank?
17
+ end
18
+ @cnx.drop_table(:users) rescue nil
19
+ @cnx.create_table :users do |t|
20
+ t.column :age, :integer
21
+ t.column :name, :string
22
+ t.column :comments, :text
23
+ t.column :created_at, :date
24
+ t.column :updated_at, :datetime
25
+ t.column :score, :decimal, :precision => 20, :scale => 10
26
+ end
27
+ end
28
+
29
+ def setup
30
+ setup_db
31
+ @table = Arel::Table.new(:users)
32
+ @cols = ['id', 'name', 'comments', 'created_at']
33
+ @data = [
34
+ [23, 'nom1', "sdfdsfdsfsdfsd fdsf dsf dsf sdf afdg fsdg sg sd gsdfg e 54435 344", '2016-01-01'],
35
+ [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
36
+ ]
37
+ end
38
+
39
+ def teardown
40
+ @cnx.drop_table(:users)
41
+ end
42
+
43
+ # Math Functions
44
+ def test_bulk_insert
45
+ insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(Arel::Table.engine).into(@table)
46
+ insert_manager.bulk_insert(@cols, @data)
47
+ @cnx.execute(insert_manager.to_sql)
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -11,7 +11,8 @@ module ArelExtensions
11
11
  ActiveRecord::Base.default_timezone = :utc
12
12
  @cnx = ActiveRecord::Base.connection
13
13
  Arel::Table.engine = ActiveRecord::Base
14
- @cnx.drop_table(:users) rescue nil
14
+ @cnx.drop_table(:users) rescue nil
15
+ @cnx.drop_table(:products) rescue nil
15
16
  @cnx.create_table :users do |t|
16
17
  t.column :age, :integer
17
18
  t.column :name, :string
@@ -38,7 +39,7 @@ module ArelExtensions
38
39
  insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(ActiveRecord::Base).into(@table)
39
40
  insert_manager.bulk_insert(@cols, @data)
40
41
  sql = insert_manager.to_sql
41
- sql.must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") VALUES (23, 'nom1', 'sdfdsfdsfsdf', '2016-01-01'), (25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01')]
42
+ sql.must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") SELECT 23 AS 'id', 'nom1' AS 'name', 'sdfdsfdsfsdf' AS 'comments', '2016-01-01' AS 'created_at' UNION ALL SELECT 25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
42
43
  end
43
44
 
44
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arel_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yann Azoury
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-08-30 00:00:00.000000000 Z
13
+ date: 2016-08-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: arel
@@ -81,6 +81,9 @@ extra_rdoc_files:
81
81
  files:
82
82
  - ".gitignore"
83
83
  - ".travis.yml"
84
+ - ".travis/oracle/download.js"
85
+ - ".travis/oracle/download.sh"
86
+ - ".travis/oracle/install.sh"
84
87
  - Gemfile
85
88
  - MIT-LICENSE.txt
86
89
  - README.md
@@ -112,7 +115,7 @@ files:
112
115
  - lib/arel_extensions/nodes/find_in_set.rb
113
116
  - lib/arel_extensions/nodes/floor.rb
114
117
  - lib/arel_extensions/nodes/function.rb
115
- - lib/arel_extensions/nodes/isnull.rb
118
+ - lib/arel_extensions/nodes/is_null.rb
116
119
  - lib/arel_extensions/nodes/length.rb
117
120
  - lib/arel_extensions/nodes/locate.rb
118
121
  - lib/arel_extensions/nodes/ltrim.rb
@@ -146,10 +149,11 @@ files:
146
149
  - test/visitors/test_bulk_insert_to_sql.rb
147
150
  - test/visitors/test_oracle.rb
148
151
  - test/visitors/test_to_sql.rb
152
+ - test/with_ar/all_agnostic_test.rb
153
+ - test/with_ar/insert_agnostic_test.rb
149
154
  - test/with_ar/test_bulk_sqlite.rb
150
155
  - test/with_ar/test_math_sqlite.rb
151
156
  - test/with_ar/test_string_mysql.rb
152
- - test/with_ar/test_string_postgresql.rb
153
157
  - test/with_ar/test_string_sqlite.rb
154
158
  homepage: https://github.com/Faveod/arel-extensions
155
159
  licenses:
@@ -188,8 +192,9 @@ test_files:
188
192
  - test/visitors/test_bulk_insert_to_sql.rb
189
193
  - test/visitors/test_oracle.rb
190
194
  - test/visitors/test_to_sql.rb
195
+ - test/with_ar/all_agnostic_test.rb
196
+ - test/with_ar/insert_agnostic_test.rb
191
197
  - test/with_ar/test_bulk_sqlite.rb
192
198
  - test/with_ar/test_math_sqlite.rb
193
199
  - test/with_ar/test_string_mysql.rb
194
- - test/with_ar/test_string_postgresql.rb
195
200
  - test/with_ar/test_string_sqlite.rb
@@ -1,30 +0,0 @@
1
- module ArelExtensions
2
- module Nodes
3
- class Isnull < Arel::Nodes::Function
4
-
5
-
6
- def initialize left, right, aliaz = nil
7
- tab = Array.new
8
- tab << left
9
- tab << right
10
- super(tab, aliaz)
11
- end
12
-
13
-
14
- def left
15
- @expressions.first
16
- end
17
-
18
-
19
- def right
20
- @expressions[1]
21
- end
22
-
23
-
24
- def as other
25
- Arel::Nodes::As.new self, Arel::Nodes::SqlLiteral.new(other)
26
- end
27
-
28
- end
29
- end
30
- end
@@ -1,81 +0,0 @@
1
- require 'helper'
2
- require 'date'
3
-
4
- module ArelExtensions
5
- module WthAr
6
-
7
- describe 'the postgresql visitor can do string operations' do
8
-
9
- before do
10
- ActiveRecord::Base.configurations = YAML.load_file('test/database.yml')
11
- ActiveRecord::Base.establish_connection(ENV['DB'] || (RUBY_PLATFORM == 'java' ? :"jdbc-postgres" : :postgres))
12
- ActiveRecord::Base.default_timezone = :utc
13
- begin
14
- @cnx = ActiveRecord::Base.connection
15
- rescue => e
16
- puts "\n#{e.inspect}"
17
- ActiveRecord::Base.establish_connection(ENV['DB'] || :sqlite)
18
- @cnx = ActiveRecord::Base.connection
19
- end
20
- Arel::Table.engine = ActiveRecord::Base
21
- @cnx.drop_table(:users) rescue nil
22
- @cnx.drop_table(:products) rescue nil
23
- @cnx.create_table :users do |t|
24
- t.column :age, :integer
25
- t.column :name, :string
26
- t.column :comments, :text
27
- t.column :created_at, :date
28
- t.column :updated_at, :date
29
- t.column :score, :decimal
30
- end
31
- @cnx.create_table :products do |t|
32
- t.column :price, :decimal
33
- end
34
- class User < ActiveRecord::Base
35
- end
36
- d = Date.new(2016, 5,23)
37
- lucas = User.create :age => 5, :name => "Lucas", :created_at => d, :score => 20.16
38
- @lucas = User.where(:id => lucas.id)
39
- sophie = User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
40
- @sophie = User.where(:id => sophie.id)
41
- User.create :age => 20, :name => "Camille", :created_at => d, :score => 20.16
42
- User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62
43
- User.create :age => 23, :name => "Myung", :created_at => d, :score => 20.16
44
- @laure = User.create :age => 25, :name => "Laure", :created_at => d, :score =>20.16
45
- User.create :age => nil, :name => "Test", :created_at => d, :score => 1.62
46
- @neg = User.create :age => -20, :name => "Negatif", :created_at => d, :score => 0.17
47
- @table = Arel::Table.new(:users)
48
- @name = @table[:name]
49
- @age = @table[:age]
50
- end
51
- after do
52
- @cnx.drop_table(:users)
53
- end
54
-
55
- it "should do string operations" do
56
- # concat
57
- d = Date.new(1997, 6, 15)
58
- assert_equal "SophiePhan", @sophie.select((@name + "Phan").as("res")).first.res
59
- assert_equal "Sophie2", @sophie.select((@name + 2).as("res")).first.res
60
- assert_equal "Sophie1997-06-15", @sophie.select((@name + d).as("res")).first.res
61
- assert_equal "Sophie15", @sophie.select((User.arel_table[:name] + User.arel_table[:age]).as("res")).first.res
62
- assert_equal "SophieSophie", @sophie.select((User.arel_table[:name] + User.arel_table[:name]).as("res")).first.res
63
- assert_equal "Sophie2016-05-23", @sophie.select((User.arel_table[:name] + User.arel_table[:created_at]).as("res")).first.res
64
- #concat Integer
65
- assert_equal 1, User.where((@age + 10).eq(33)).count
66
- assert_equal 6, @lucas.select((@age + "1").as('res')).first.res.to_i
67
- assert_equal 1, @sophie.where(@age>14).where(@age<16).count
68
-
69
-
70
-
71
- # Replace
72
- assert_equal "LucaX", User.where(:id => @lucas).select(@name.replace("s","X").as("res")).first.res
73
- assert_equal "replace", User.where(:id => @lucas).select(@name.replace(@name,"replace").as("res")).first.res
74
-
75
- #
76
- end
77
-
78
- end
79
-
80
- end
81
- end