arel_extensions 1.4.0 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) 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 +28 -2
  13. data/README.md +90 -239
  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 -15
  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 +191 -422
  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 +62 -83
  90. data/lib/arel_extensions.rb +31 -224
  91. data/test/database.yml +7 -15
  92. data/test/helper.rb +18 -0
  93. data/test/real_db_test.rb +117 -120
  94. data/test/support/fake_record.rb +3 -9
  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 +366 -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 -43
  108. data/.github/workflows/publish.yml +0 -29
  109. data/.github/workflows/release.yml +0 -30
  110. data/.github/workflows/ruby.yml +0 -399
  111. data/CONTRIBUTING.md +0 -102
  112. data/NEWS.md +0 -78
  113. data/bin/build +0 -15
  114. data/bin/compose +0 -6
  115. data/bin/publish +0 -8
  116. data/dev/arelx.dockerfile +0 -44
  117. data/dev/compose.yaml +0 -71
  118. data/dev/postgres.dockerfile +0 -5
  119. data/dev/rbenv +0 -189
  120. data/gemfiles/rails4_2.gemfile +0 -38
  121. data/gemfiles/rails5.gemfile +0 -29
  122. data/gemfiles/rails6.gemfile +0 -33
  123. data/gemfiles/rails6_1.gemfile +0 -33
  124. data/gemfiles/rails7.gemfile +0 -33
  125. data/gemfiles/rails7_1.gemfile +0 -33
  126. data/gemfiles/rails7_2.gemfile +0 -33
  127. data/gemspecs/arel_extensions-v1.gemspec +0 -27
  128. data/gemspecs/arel_extensions-v2.gemspec +0 -27
  129. data/generate_gems.sh +0 -15
  130. data/lib/arel_extensions/aliases.rb +0 -14
  131. data/lib/arel_extensions/helpers.rb +0 -62
  132. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  133. data/lib/arel_extensions/nodes/formatted_date.rb +0 -42
  134. data/lib/arel_extensions/nodes/rollup.rb +0 -36
  135. data/lib/arel_extensions/nodes/select.rb +0 -10
  136. data/lib/arel_extensions/nodes/sum.rb +0 -7
  137. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  138. data/test/arelx_test_helper.rb +0 -74
  139. data/version_v1.rb +0 -3
  140. data/version_v2.rb +0 -3
data/test/real_db_test.rb CHANGED
@@ -8,39 +8,34 @@ require 'arel_extensions'
8
8
  def setup_db
9
9
  ActiveRecord::Base.configurations = YAML.load_file('test/database.yml')
10
10
  ActiveRecord::Base.establish_connection(ENV['DB'].try(:to_sym) || (RUBY_PLATFORM == 'java' ? :"jdbc-sqlite" : :sqlite))
11
- if ActiveRecord::VERSION::MAJOR >= 7
12
- ActiveRecord.default_timezone = :utc
13
- else
14
- ActiveRecord::Base.default_timezone = :utc
15
- end
11
+ ActiveRecord::Base.default_timezone = :utc
16
12
  @cnx = ActiveRecord::Base.connection
17
- if /sqlite/i.match?(ActiveRecord::Base.connection.adapter_name)
13
+ if ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
18
14
  $sqlite = true
19
15
  db = @cnx.raw_connection
20
16
  if !$load_extension_disabled
21
17
  begin
22
18
  db.enable_load_extension(1)
23
- db.load_extension('/usr/lib/sqlite3/pcre.so')
24
- db.load_extension('/usr/lib/sqlite3/extension-functions.so')
19
+ db.load_extension("/usr/lib/sqlite3/pcre.so")
20
+ db.load_extension("/usr/lib/sqlite3/extension-functions.so")
25
21
  db.enable_load_extension(0)
26
22
  rescue => e
27
23
  $load_extension_disabled = true
28
- $stderr << "cannot load extensions #{e.inspect}\n"
24
+ $stderr << "can not load extensions #{e.inspect}\n"
29
25
  end
30
26
  end
31
- # function find_in_set
32
- db.create_function('find_in_set', 1) do |func, value1, value2|
27
+ #function find_in_set
28
+ db.create_function("find_in_set", 1) do |func, value1, value2|
33
29
  func.result = value1.index(value2)
34
30
  end
35
31
  end
36
- @cnx.drop_table(:users) rescue nil
32
+ @cnx.drop_table(:users) rescue nil
37
33
  @cnx.create_table :users do |t|
38
- t.column :age, :integer
34
+ t.column :age, :integer
39
35
  t.column :name, :string
40
36
  t.column :created_at, :date
41
37
  t.column :updated_at, :date
42
38
  t.column :score, :decimal
43
- t.column :updated_at, :datetime
44
39
  end
45
40
  end
46
41
 
@@ -49,22 +44,23 @@ def teardown_db
49
44
  end
50
45
 
51
46
  class User < ActiveRecord::Base
47
+
52
48
  end
53
49
 
54
50
  class ListTest < Minitest::Test
51
+
55
52
  def setup
56
- d = Date.new(2016, 05, 23)
57
- dt = Time.new(2016, 05, 23, 12, 34, 56)
53
+ d = Date.new(2016,05,23)
58
54
  setup_db
59
- User.create age: 5, name: 'Lucas', created_at: d, score: 20.16
60
- User.create age: 15, name: 'Sophie', created_at: d, score: 20.16
61
- User.create age: 20, name: 'Camille', created_at: d, score: 20.16
62
- User.create age: 21, name: 'Arthur', created_at: d, score: 65.62, updated_at: dt
63
- u = User.create age: 23, name: 'Myung', created_at: d, score: 20.16
64
- @myung = User.where(id: u.id)
65
- User.create age: 25, name: 'Laure', created_at: d, score: 20.16
66
- User.create age: nil, name: 'Test', created_at: d, score: 1.62
67
- User.create age: -0, name: 'Negatif', created_at: d, score: 0.17
55
+ User.create :age => 5, :name => "Lucas", :created_at => d , :score => 20.16
56
+ User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
57
+ User.create :age => 20, :name => "Camille", :created_at => d, :score => 20.16
58
+ User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62
59
+ u = User.create :age => 23, :name => "Myung", :created_at => d, :score => 20.16
60
+ @myung = User.where(:id => u.id)
61
+ User.create :age => 25, :name => "Laure", :created_at => d, :score =>20.16
62
+ User.create :age =>nil, :name => "Test", :created_at => d, :score => 1.62
63
+ User.create :age =>-0, :name => "Negatif", :created_at => d, :score => 0.17
68
64
  end
69
65
 
70
66
  def teardown
@@ -72,136 +68,133 @@ class ListTest < Minitest::Test
72
68
  end
73
69
 
74
70
  def test_abs
75
- assert_equal 0, User.where(User.arel_table[:name].eq('Negatif')).select((User.arel_table[:age].abs).as('res')).first.res
76
- assert_equal 14, User.where(User.arel_table[:name].eq('Laure')).select(((User.arel_table[:age] - 39).abs).as('res')).first.res
71
+ assert_equal 0,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:age].abs).as("res")).first.res
72
+ assert_equal 14,User.where(User.arel_table[:name].eq("Laure")).select(((User.arel_table[:age] - 39).abs).as("res")).first.res
77
73
  end
78
74
 
79
75
  def test_ceil
80
76
  if !$sqlite || !$load_extension_disabled
81
- assert_equal 1, User.where(User.arel_table[:name].eq('Negatif')).select((User.arel_table[:score].ceil).as('res')).first.res
82
- assert_equal 66, User.where(User.arel_table[:name].eq('Arthur')).select((User.arel_table[:score].ceil).as('res')).first.res
77
+ assert_equal 1,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:score].ceil).as("res")).first.res
78
+ assert_equal 66,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:score].ceil).as("res")).first.res
83
79
  end
84
80
  end
85
81
 
86
82
  def test_coalesce
87
- if /pgsql/i.match?(@cnx.adapter_name)
88
- assert_equal 100, User.where(User.arel_table[:name].eq('Test')).select((User.arel_table[:age].coalesce(100)).as('res')).first.res
89
- assert_equal 'Camille', User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].coalesce('Null', 'default')).as('res')).first.res
83
+ if @cnx.adapter_name =~ /pgsql/i
84
+ assert_equal 100,User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].coalesce(100)).as("res")).first.res
85
+ assert_equal "Camille",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].coalesce("Null","default")).as("res")).first.res
90
86
  else
91
- assert_equal 'Camille', User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].coalesce('Null', 20)).as('res')).first.res
92
- assert_equal 20, User.where(User.arel_table[:name].eq('Test')).select((User.arel_table[:age].coalesce(nil, 20)).as('res')).first.res
87
+ assert_equal "Camille",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].coalesce("Null",20)).as("res")).first.res
88
+ assert_equal 20,User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].coalesce(nil,20)).as("res")).first.res
93
89
  end
94
90
  end
95
91
 
96
- def test_comparator
97
- assert_equal 2, User.where(User.arel_table[:age] < 6).count
98
- assert_equal 2, User.where(User.arel_table[:age] <= 10).count
99
- assert_equal 3, User.where(User.arel_table[:age] > 20).count
100
- assert_equal 4, User.where(User.arel_table[:age] >= 20).count
101
- assert_equal 1, User.where(User.arel_table[:age] > 5).where(User.arel_table[:age] < 20).count
102
- end
103
-
104
- def test_date_date_comparator
105
- d = Date.new(2016, 05, 24) #after created_at in db
106
- assert_equal 8, User.where(User.arel_table[:age] < d).count
107
- assert_equal 0, User.where(User.arel_table[:age] > d).count
108
- assert_equal 0, User.where(User.arel_table[:age] == d).count
109
- d = Date.new(2016, 05, 23)
110
- assert_equal 8, User.where(User.arel_table[:age] == d).count
111
- dt = Time.new(2016, 05, 23, 12, 35, 00) #after updated_at in db
112
- assert_equal 1, User.where(User.arel_table[:update_at] < dt).count
113
- assert_equal 0, User.where(User.arel_table[:update_at] > dt).count
92
+ def test_Comparator
93
+ assert_equal 2,User.where(User.arel_table[:age] < 6 ).count
94
+ assert_equal 2,User.where(User.arel_table[:age] <=10 ).count
95
+ assert_equal 3,User.where(User.arel_table[:age] > 20 ).count
96
+ assert_equal 4,User.where(User.arel_table[:age] >=20 ).count
97
+ assert_equal 1,User.where(User.arel_table[:age] > 5 ).where(User.arel_table[:age] < 20 ).count
114
98
  end
115
99
 
116
100
  def test_date_duration
117
- # Year
118
- assert_equal 2016, User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:created_at].year).as('res')).first.res.to_i
119
- assert_equal 0, User.where(User.arel_table[:created_at].year.eq('2012')).count
120
- # Month
121
- assert_equal 5, User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:created_at].month).as('res')).first.res.to_i
122
- assert_equal 8, User.where(User.arel_table[:created_at].month.eq('05')).count
123
- # Week
124
- assert_equal 21, User.where(User.arel_table[:name].eq('Arthur')).select((User.arel_table[:created_at].week).as('res')).first.res.to_i
125
- assert_equal 8, User.where(User.arel_table[:created_at].month.eq('05')).count
126
- # Day
127
- assert_equal 23, User.where(User.arel_table[:name].eq('Laure')).select((User.arel_table[:created_at].day).as('res')).first.res.to_i
128
- assert_equal 0, User.where(User.arel_table[:created_at].day.eq('05')).count
101
+ #Year
102
+ assert_equal 2016,User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:created_at].year).as("res")).first.res.to_i
103
+ assert_equal 0,User.where(User.arel_table[:created_at].year.eq("2012")).count
104
+ #Month
105
+ assert_equal 5,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:created_at].month).as("res")).first.res.to_i
106
+ assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
107
+ #Week
108
+ assert_equal 21,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:created_at].week).as("res")).first.res.to_i
109
+ assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
110
+ #Day
111
+ assert_equal 23,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at].day).as("res")).first.res.to_i
112
+ assert_equal 0,User.where(User.arel_table[:created_at].day.eq("05")).count
129
113
  end
130
114
 
115
+
116
+
131
117
  def test_length
132
- assert_equal 7, User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].length).as('res')).first.res
133
- assert_equal 5, User.where(User.arel_table[:name].eq('Laure')).select((User.arel_table[:name].length).as('res')).first.res
118
+ assert_equal 7,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].length).as("res")).first.res
119
+ assert_equal 5,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:name].length).as("res")).first.res
134
120
  end
135
121
 
136
122
  def test_locate
137
123
  if !$sqlite || !$load_extension_disabled
138
- assert_equal 1, User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].locate('C')).as('res')).first.res
139
- assert_equal 0, User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:name].locate('z')).as('res')).first.res
140
- assert_equal 5, User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:name].locate('s')).as('res')).first.res
124
+ assert_equal 1, User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].locate("C")).as("res")).first.res
125
+ assert_equal 0, User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:name].locate("z")).as("res")).first.res
126
+ assert_equal 5, User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:name].locate("s")).as("res")).first.res
141
127
  end
142
128
  end
143
129
 
144
130
  def test_isnull
145
- if /pgsql/i.match?(ActiveRecord::Base.connection.adapter_name)
146
- assert_equal 100, User.where(User.arel_table[:name].eq('Test')).select((User.arel_table[:age].isnull(100)).as('res')).first.res
131
+ if ActiveRecord::Base.connection.adapter_name =~ /pgsql/i
132
+ assert_equal 100,User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].isnull(100)).as("res")).first.res
147
133
  else
148
- assert_equal 'default', User.where(User.arel_table[:name].eq('Test')).select((User.arel_table[:age].isnull('default')).as('res')).first.res
149
- assert_equal 'Test', User.where((User.arel_table[:age].isnull('default')).eq('default')).select(User.arel_table[:name]).first.name.to_s
134
+ assert_equal "default",User.where(User.arel_table[:name].eq("Test")).select((User.arel_table[:age].isnull('default')).as("res")).first.res
135
+ assert_equal "Test",User.where((User.arel_table[:age].isnull('default')).eq('default')).select(User.arel_table[:name]).first.name.to_s
150
136
  end
151
137
  end
152
138
 
139
+
153
140
  def test_floor
154
141
  if !$sqlite || !$load_extension_disabled
155
- assert_equal 0, User.where(User.arel_table[:name].eq('Negatif')).select((User.arel_table[:score].floor).as('res')).first.res
156
- assert_equal 65, User.where(User.arel_table[:name].eq('Arthur')).select((User.arel_table[:score].floor).as('res')).first.res
142
+ assert_equal 0,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:score].floor).as("res")).first.res
143
+ assert_equal 65,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:score].floor).as("res")).first.res
157
144
  end
158
145
  end
159
146
 
147
+
160
148
  def test_findinset
161
149
  db = ActiveRecord::Base.connection.raw_connection
162
- assert_equal 3, db.get_first_value("select find_in_set(name,'i') from users where name = 'Camille'")
163
- assert_equal '', db.get_first_value("select find_in_set(name,'p') from users where name = 'Camille'").to_s
164
- # number
165
- # assert_equal 1,User.select(User.arel_table[:name] & ("l")).count
166
- # assert_equal 3,(User.select(User.arel_table[:age] & [5,15,20]))
167
- # string
150
+ assert_equal 3,db.get_first_value( "select find_in_set(name,'i') from users where name = 'Camille'" )
151
+ assert_equal "",db.get_first_value( "select find_in_set(name,'p') from users where name = 'Camille'" ).to_s
152
+ #number
153
+ #assert_equal 1,User.select(User.arel_table[:name] & ("l")).count
154
+ #assert_equal 3,(User.select(User.arel_table[:age] & [5,15,20]))
155
+ #string
168
156
  end
169
157
 
158
+
170
159
  def test_math_plus
171
- d = Date.new(1997, 06, 15)
172
- # Concat String
173
- assert_equal 'SophiePhan', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + 'Phan').as('res')).first.res
174
- assert_equal 'Sophie2', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + 2).as('res')).first.res
175
- assert_equal 'Sophie1997-06-15', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + d).as('res')).first.res
176
- assert_equal 'Sophie15', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + User.arel_table[:age]).as('res')).first.res
177
- assert_equal 'SophieSophie', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + User.arel_table[:name]).as('res')).first.res
178
- assert_equal 'Sophie2016-05-23', User.where(User.arel_table[:name].eq('Sophie')).select((User.arel_table[:name] + User.arel_table[:created_at]).as('res')).first.res
179
- # concat Integer
160
+ d = Date.new(1997,06,15)
161
+ #Concat String
162
+ assert_equal "SophiePhan",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + "Phan").as("res")).first.res
163
+ assert_equal "Sophie2",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + 2 ).as("res")).first.res
164
+ assert_equal "Sophie1997-06-15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + d).as("res")).first.res
165
+ assert_equal "Sophie15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:age]).as("res")).first.res
166
+ assert_equal "SophieSophie",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:name]).as("res")).first.res
167
+ assert_equal "Sophie2016-05-23",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:created_at]).as("res")).first.res
168
+ #concat Integer
180
169
  assert_equal 1, User.where((User.arel_table[:age] + 10).eq(33)).count
181
- assert_equal 1, User.where((User.arel_table[:age] + '1').eq(6)).count
170
+ assert_equal 1, User.where((User.arel_table[:age] + "1").eq(6)).count
182
171
  assert_equal 1, User.where((User.arel_table[:age] + User.arel_table[:age]).eq(10)).count
183
- # concat Date
184
- # puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
185
- assert_equal '2016-05-24', @myung.select((User.arel_table[:created_at] + 1).as('res')).first.res.to_date.to_s
186
- assert_equal '2016-05-25', @myung.select((User.arel_table[:created_at] + 2.day).as('res')).first.res.to_date.to_s
172
+ #concat Date
173
+ # puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
174
+ assert_equal "2016-05-24", @myung.select((User.arel_table[:created_at] + 1).as("res")).first.res.to_date.to_s
175
+ assert_equal "2016-05-25", @myung.select((User.arel_table[:created_at] + 2.day).as("res")).first.res.to_date.to_s
187
176
  end
188
177
 
178
+
189
179
  def test_math_moins
190
- d = Date.new(2016, 05, 20)
191
- # Datediff
192
- assert_equal 8, User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
193
- 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
194
- # Substraction
180
+ d = Date.new(2016,05,20)
181
+ #Datediff
182
+ assert_equal 8,User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
183
+ 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
184
+ #Substraction
195
185
  assert_equal 0, User.where((User.arel_table[:age] - 10).eq(50)).count
196
- assert_equal 0, User.where((User.arel_table[:age] - '10').eq(50)).count
186
+ assert_equal 0, User.where((User.arel_table[:age] - "10").eq(50)).count
197
187
  end
198
188
 
189
+
190
+
199
191
  def test_rand
200
- assert_equal 5, User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
201
- # test_alias :random :rand
202
- assert_equal 8, User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
192
+ assert_equal 5,User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
193
+ #test_alias :random :rand
194
+ assert_equal 8,User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
203
195
  end
204
196
 
197
+
205
198
  def test_regexp_not_regex
206
199
  if !$sqlite || !$load_extension_disabled
207
200
  assert_equal 1, User.where(User.arel_table[:name] =~ '^M').count
@@ -210,41 +203,45 @@ class ListTest < Minitest::Test
210
203
  end
211
204
 
212
205
  def test_replace
213
- assert_equal 'LucaX', User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:name].replace('s', 'X')).as('res')).first.res
214
- assert_equal 'replace', User.where(User.arel_table[:name].eq('Lucas')).select((User.arel_table[:name].replace(User.arel_table[:name], 'replace')).as('res')).first.res
206
+ assert_equal "LucaX",User.where(User.arel_table[:name].eq("Lucas")).select(((User.arel_table[:name]).replace("s","X")).as("res")).first.res
207
+ assert_equal "replace",User.where(User.arel_table[:name].eq("Lucas")).select(((User.arel_table[:name]).replace(User.arel_table[:name],"replace")).as("res")).first.res
215
208
  end
216
209
 
210
+
217
211
  def test_round
218
212
  assert_equal 1, User.where(((User.arel_table[:age]).round(0)).eq(5.0)).count
219
213
  assert_equal 0, User.where(((User.arel_table[:age]).round(-1)).eq(6.0)).count
220
214
  end
221
215
 
216
+
222
217
  def test_Soundex
223
218
  if !$sqlite || !$load_extension_disabled
224
- assert_equal 'C540', User.where(User.arel_table[:name].eq('Camille')).select((User.arel_table[:name].soundex).as('res')).first.res.to_s
225
- assert_equal 8, User.where((User.arel_table[:name].soundex).eq(User.arel_table[:name].soundex)).count
219
+ assert_equal "C540",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].soundex).as("res")).first.res.to_s
220
+ assert_equal 8,User.where((User.arel_table[:name].soundex).eq(User.arel_table[:name].soundex)).count
226
221
  end
227
222
  end
228
223
 
229
224
  def test_Sum
230
- # .take(50) because of limit by ORDER BY
231
- assert_equal 110, User.select((User.arel_table[:age].sum + 1).as('res')).take(50).first.res
232
- assert_equal 218, User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as('res')).take(50).first.res
233
- assert_equal 327, User.select(((User.arel_table[:age] * 3).sum).as('res')).take(50).first.res
234
- assert_equal 2245, User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as('res')).take(50).first.res
225
+ #.take(50) because of limit by ORDER BY
226
+ assert_equal 110,User.select((User.arel_table[:age].sum + 1).as("res")).take(50).first.res
227
+ assert_equal 218,User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as("res")).take(50).first.res
228
+ assert_equal 327,User.select(((User.arel_table[:age] * 3).sum).as("res")).take(50).first.res
229
+ assert_equal 2245,User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as("res")).take(50).first.res
235
230
  end
236
231
 
232
+
237
233
  def test_trim
238
- assert_equal 'Myun', User.where(User.arel_table[:name].eq('Myung')).select(User.arel_table[:name].rtrim('g').as('res')).first.res
239
- assert_equal 'yung', User.where(User.arel_table[:name].eq('Myung')).select(User.arel_table[:name].ltrim('M').as('res')).first.res
240
- assert_equal 'yung', User.where(User.arel_table[:name].eq('Myung')).select((User.arel_table[:name] + 'M').trim('M').as('res')).first.res
241
- assert_equal '', User.where(User.arel_table[:name].eq('Myung')).select(User.arel_table[:name].rtrim(User.arel_table[:name]).as('res')).first.res
234
+ assert_equal "Myun",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].rtrim("g").as("res")).first.res
235
+ assert_equal "yung",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].ltrim("M").as("res")).first.res
236
+ assert_equal "yung",User.where(User.arel_table[:name].eq("Myung")).select((User.arel_table[:name] + "M").trim("M").as("res")).first.res
237
+ assert_equal "",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].rtrim(User.arel_table[:name]).as("res")).first.res
242
238
 
243
239
  end
244
240
 
245
241
  def test_wday
246
- d = Date.new(2016, 06, 26)
247
- assert_equal 1, User.where(User.arel_table[:name].eq('Myung')).select((User.arel_table[:created_at].wday).as('res')).first.res.to_i
248
- assert_equal 0, User.select(d.wday).as('res').first.to_i
242
+ d = Date.new(2016,06,26)
243
+ assert_equal 1,User.where(User.arel_table[:name].eq("Myung")).select((User.arel_table[:created_at].wday).as("res")).first.res.to_i
244
+ assert_equal 0,User.select(d.wday).as("res").first.to_i
249
245
  end
246
+
250
247
  end
@@ -7,7 +7,7 @@ module FakeRecord
7
7
  attr_accessor :visitor
8
8
 
9
9
  def initialize(visitor = nil)
10
- @tables = %w{users photos developers products}
10
+ @tables = %w{ users photos developers products}
11
11
  @columns = {
12
12
  'users' => [
13
13
  Column.new('id', :integer),
@@ -66,7 +66,7 @@ module FakeRecord
66
66
  end
67
67
 
68
68
  def in_clause_length
69
- 10_000
69
+ 6
70
70
  end
71
71
 
72
72
  def quote thing, column = nil
@@ -105,7 +105,7 @@ module FakeRecord
105
105
  attr_reader :spec, :connection
106
106
 
107
107
  def initialize
108
- @spec = Spec.new({adapter: 'america'})
108
+ @spec = Spec.new(:adapter => 'america')
109
109
  @connection = Connection.new
110
110
  @connection.visitor = Arel::Visitors::ToSql.new(connection)
111
111
  end
@@ -145,11 +145,5 @@ module FakeRecord
145
145
  def connection
146
146
  connection_pool.connection
147
147
  end
148
-
149
- if ActiveRecord.version >= Gem::Version.create('7.2')
150
- def with_connection(*args, **kwargs, &block)
151
- connection_pool.with_connection(*args, **kwargs, &block)
152
- end
153
- end
154
148
  end
155
149
  end
@@ -1,8 +1,10 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module Nodes
5
+
5
6
  describe ArelExtensions::Comparators do
7
+
6
8
  before do
7
9
  @conn = FakeRecord::Base.new
8
10
  Arel::Table.engine = @conn
@@ -17,26 +19,27 @@ module ArelExtensions
17
19
  @visitor.accept(node, Arel::Collectors::SQLString.new).value
18
20
  end
19
21
 
20
- it '< is equal lt' do
21
- _(compile(@table[:id] < 10)).must_be_like('"users"."id" < 10')
22
+ it "< is equal lt" do
23
+ compile(@table[:id] < 10).must_be_like('"users"."id" < 10')
22
24
  end
23
-
24
- it '<= is equal lteq' do
25
- _(compile(@table[:id] <= 10)).must_be_like('"users"."id" <= 10')
25
+
26
+ it "<= is equal lteq" do
27
+ compile(@table[:id] <= 10).must_be_like('"users"."id" <= 10')
26
28
  end
27
29
 
28
- it '> is equal gt' do
29
- _(compile(@table[:id] > 10)).must_be_like('"users"."id" > 10')
30
+ it "> is equal gt" do
31
+ compile(@table[:id] > 10).must_be_like('"users"."id" > 10')
30
32
  end
31
33
 
32
- it '< is equal gteq' do
33
- _(compile(@table[:id] >= 10)).must_be_like('"users"."id" >= 10')
34
+ it "< is equal gteq" do
35
+ compile(@table[:id] >= 10).must_be_like('"users"."id" >= 10')
34
36
  end
35
37
 
36
- it 'should compare with dates' do
37
- _(compile(@table[:created_at] >= Date.new(2016, 3, 31)))
38
- .must_be_like %{"users"."created_at" >= '2016-03-31'}
38
+ it "should compare with dates" do
39
+ compile(@table[:created_at] >= Date.new(2016, 3, 31)).must_be_like %{"users"."created_at" >= '2016-03-31'}
39
40
  end
41
+
40
42
  end
43
+
41
44
  end
42
- end
45
+ end
@@ -1,4 +1,4 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module BulkInsertOracle
@@ -7,10 +7,10 @@ module ArelExtensions
7
7
  @conn = FakeRecord::Base.new
8
8
  @visitor = Arel::Visitors::Oracle.new @conn.connection
9
9
  @table = Arel::Table.new(:users)
10
- @cols = %w[name comments created_at]
10
+ @cols = ['name', 'comments', 'created_at']
11
11
  @data = [
12
- %w[nom1 sdfdsfdsfsdf 2016-01-01],
13
- %w[nom2 sdfdsfdsfsdf 2016-01-01]
12
+ ['nom1', "sdfdsfdsfsdf", '2016-01-01'],
13
+ ['nom2', "sdfdsfdsfsdf", '2016-01-01']
14
14
  ]
15
15
  end
16
16
 
@@ -22,13 +22,13 @@ module ArelExtensions
22
22
  end
23
23
  end
24
24
 
25
- it 'should import large set of data in Oracle' do
26
- insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new.into(@table) : Arel::InsertManager.new(@conn).into(@table)
27
- insert_manager.bulk_insert(@cols, @data)
28
- _(compile(insert_manager.ast))
29
- .must_be_like %Q[INSERT INTO "users" ("name", "comments", "created_at")
30
- ((SELECT 'nom1', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL) UNION ALL (SELECT 'nom2', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL))]
25
+ it "should import large set of data in Oracle" do
26
+ insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
27
+ insert_manager.bulk_insert(@cols, @data)
28
+ sql = compile(insert_manager.ast)
29
+ sql.must_be_like %Q[INSERT INTO "users" ("name", "comments", "created_at") ((SELECT 'nom1', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL) UNION ALL (SELECT 'nom2', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL))]
31
30
  end
32
- end
31
+
32
+ end
33
33
  end
34
34
  end
@@ -1,20 +1,21 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module BulkInsertSQLlite
5
+
5
6
  describe 'the sqlite visitor can bulk insert' do
6
7
  before do
7
8
  @conn = FakeRecord::Base.new
8
9
  @visitor = Arel::Visitors::SQLite.new @conn.connection
9
10
  @table = Arel::Table.new(:users)
10
11
  Arel::Table.engine = @conn
11
- @cols = %w[id name comments created_at]
12
+ @cols = ['id', 'name', 'comments', 'created_at']
12
13
  @data = [
13
- [23, 'nom1', 'sdfdsfdsfsdf', '2016-01-01'],
14
- [25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
14
+ [23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
15
+ [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
15
16
  ]
16
17
  end
17
-
18
+
18
19
  def compile node
19
20
  if Arel::VERSION.to_i > 5
20
21
  @visitor.accept(node, Arel::Collectors::SQLString.new).value
@@ -23,13 +24,13 @@ module ArelExtensions
23
24
  end
24
25
  end
25
26
 
26
- it 'should import large set of data' do
27
- insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new.into(@table) : Arel::InsertManager.new(@conn).into(@table)
27
+ it "should import large set of data" do
28
+ insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
28
29
  insert_manager.bulk_insert(@cols, @data)
29
- _(compile(insert_manager.ast))
30
- .must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at")
31
- 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']
30
+ sql = compile(insert_manager.ast)
31
+ 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']
32
32
  end
33
- end
33
+
34
+ end
34
35
  end
35
- end
36
+ end
@@ -1,4 +1,4 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module BulkInsertToSql
@@ -8,10 +8,10 @@ module ArelExtensions
8
8
  Arel::Table.engine = @conn
9
9
  @visitor = Arel::Visitors::ToSql.new @conn.connection
10
10
  @table = Arel::Table.new(:users)
11
- @cols = %w[id name comments created_at]
11
+ @cols = ['id', 'name', 'comments', 'created_at']
12
12
  @data = [
13
- [23, 'nom1', 'sdfdsfdsfsdf', '2016-01-01'],
14
- [25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
13
+ [23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
14
+ [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
15
15
  ]
16
16
  end
17
17
 
@@ -24,12 +24,14 @@ module ArelExtensions
24
24
  end
25
25
 
26
26
 
27
- it 'should import large set of data using ToSql' do
28
- insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new.into(@table) : Arel::InsertManager.new(@conn).into(@table)
29
- insert_manager.bulk_insert(@cols, @data)
30
- _(compile(insert_manager.ast))
31
- .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')]
27
+ it "should import large set of data using ToSql" do
28
+ insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
29
+ insert_manager.bulk_insert(@cols, @data)
30
+ sql = compile(insert_manager.ast)
31
+ 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')]
32
32
  end
33
- end
33
+
34
+
35
+ end
34
36
  end
35
- end
37
+ end