arel_extensions 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,22 +25,22 @@ module ArelExtensions
25
25
  # Math Functions
26
26
  it "should not break Arel functions" do
27
27
  compile(@price + 42).must_be_like %{("products"."price" + 42)}
28
- compile(@table[:id] + @table[:pas_en_base])
29
- .must_be_like %{("users"."id" + "users"."pas_en_base")}
30
- compile(@table[:pas_en_base] + @table[:id])
31
- .must_be_like %{("users"."pas_en_base" + "users"."id")}
32
- compile(@table[:id] - @table[:pas_en_base])
33
- .must_be_like %{("users"."id" - "users"."pas_en_base")}
34
- compile(@table[:pas_en_base] - @table[:id])
35
- .must_be_like %{("users"."pas_en_base" - "users"."id")}
36
- compile(@table[:id] * @table[:pas_en_base])
37
- .must_be_like %{"users"."id" * "users"."pas_en_base"}
38
- compile(@table[:pas_en_base] * @table[:id])
39
- .must_be_like %{"users"."pas_en_base" * "users"."id"}
28
+ compile(@table[:id] + @table[:pas_en_base])
29
+ .must_be_like %{("users"."id" + "users"."pas_en_base")}
30
+ compile(@table[:pas_en_base] + @table[:id])
31
+ .must_be_like %{("users"."pas_en_base" + "users"."id")}
32
+ compile(@table[:id] - @table[:pas_en_base])
33
+ .must_be_like %{("users"."id" - "users"."pas_en_base")}
34
+ compile(@table[:pas_en_base] - @table[:id])
35
+ .must_be_like %{("users"."pas_en_base" - "users"."id")}
36
+ compile(@table[:id] * @table[:pas_en_base])
37
+ .must_be_like %{"users"."id" * "users"."pas_en_base"}
38
+ compile(@table[:pas_en_base] * @table[:id])
39
+ .must_be_like %{"users"."pas_en_base" * "users"."id"}
40
40
  end
41
41
 
42
42
  it "should return right calculations on numbers" do
43
- #puts (@price.abs + 42).inspect
43
+ #puts (@price.abs + 42).inspect
44
44
  compile(@price.abs + 42).must_be_like %{(ABS("products"."price") + 42)}
45
45
  compile(@price.ceil + 42).must_be_like %{(CEIL("products"."price") + 42)}
46
46
  compile(@price.floor + 42).must_be_like %{(FLOOR("products"."price") + 42)}
@@ -55,21 +55,24 @@ module ArelExtensions
55
55
  compile(@price.sum + 42).must_be_like %{(SUM("products"."price") + 42)}
56
56
  compile((@price + 42).sum).must_be_like %{SUM(("products"."price" + 42))}
57
57
  compile((@price + 42).average).must_be_like %{AVG(("products"."price" + 42))}
58
- compile((Arel.rand * 9).round + 42).must_be_like %{(ROUND(RAND() * 9) + 42)}
59
- compile((Arel.rand * @price).round(2) + @price).must_be_like %{(ROUND(RAND() * "products"."price", 2) + "products"."price")}
60
-
58
+ compile((Arel.rand * 9).round + 42).must_be_like %{(ROUND(RAND() * 9) + 42)}
59
+ compile((Arel.rand * @price).round(2) + @price).must_be_like %{(ROUND(RAND() * "products"."price", 2) + "products"."price")}
60
+
61
61
  compile(@price.std + 42).must_be_like %{(STD("products"."price") + 42)}
62
62
  compile(@price.variance + 42).must_be_like %{(VARIANCE("products"."price") + 42)}
63
-
63
+
64
64
  compile(@price.coalesce(0) - 42).must_be_like %{(COALESCE("products"."price", 0) - 42)}
65
65
  compile(@price.sum - 42).must_be_like %{(SUM("products"."price") - 42)}
66
66
  compile(@price.std - 42).must_be_like %{(STD("products"."price") - 42)}
67
67
  compile(@price.variance - 42).must_be_like %{(VARIANCE("products"."price") - 42)}
68
-
68
+
69
+ compile(@price * 42.0).must_be_like %{"products"."price" * 42.0}
70
+ compile(@price / 42.0).must_be_like %{"products"."price" / 42.0}
71
+
69
72
  fake_table = Arel::Table.new('fake_tables')
70
-
71
- compile(fake_table[:fake_att] - 42).must_be_like %{("fake_tables"."fake_att" - 42)}
72
- compile(fake_table[:fake_att].coalesce(0) - 42).must_be_like %{(COALESCE("fake_tables"."fake_att", 0) - 42)}
73
+
74
+ compile(fake_table[:fake_att] - 42).must_be_like %{("fake_tables"."fake_att" - 42)}
75
+ compile(fake_table[:fake_att].coalesce(0) - 42).must_be_like %{(COALESCE("fake_tables"."fake_att", 0) - 42)}
73
76
  end
74
77
 
75
78
  # String Functions
@@ -89,17 +92,17 @@ module ArelExtensions
89
92
  compile(c.imatches('%test%')).must_be_like %{"users"."name" ILIKE '%test%'}
90
93
  compile(c.imatches_any(['%test%', 't2'])).must_be_like %{("users"."name" ILIKE '%test%' OR "users"."name" ILIKE 't2')}
91
94
  compile(c.idoes_not_match('%test%')).must_be_like %{"users"."name" NOT ILIKE '%test%'}
92
-
93
- compile(c.substring(1)).must_be_like %{SUBSTRING("users"."name", 1)}
95
+
96
+ compile(c.substring(1)).must_be_like %{SUBSTRING("users"."name", 1)}
94
97
  compile(c + '0').must_be_like %{CONCAT("users"."name", '0')}
95
98
  compile(c.substring(1) + '0').must_be_like %{CONCAT(SUBSTRING("users"."name", 1), '0')}
96
99
  compile(c.substring(1) + c.substring(2)).must_be_like %{CONCAT(SUBSTRING("users"."name", 1), SUBSTRING("users"."name", 2))}
97
100
  compile(c.concat(c).concat(c)).must_be_like %{CONCAT("users"."name", "users"."name", "users"."name")}
98
101
  compile(c + c + c).must_be_like %{CONCAT("users"."name", "users"."name", "users"."name")}
99
-
102
+
100
103
  # some optimization on concat
101
104
  compile(c + 'test' + ' chain').must_be_like %{CONCAT(\"users\".\"name\", 'test chain')}
102
- compile(Arel::Nodes.build_quoted('test') + ' chain').must_be_like %{'test chain'}
105
+ compile(Arel::Nodes.build_quoted('test') + ' chain').must_be_like %{'test chain'}
103
106
  compile(c + '' + c).must_be_like %{CONCAT(\"users\".\"name\", \"users\".\"name\")}
104
107
 
105
108
  compile(c.md5).must_be_like %{MD5(\"users\".\"name\")}
@@ -168,19 +171,19 @@ module ArelExtensions
168
171
  sql = compile(ArelExtensions::Nodes::DateDiff.new([d1, @table[:updated_at]]))
169
172
  sql.must_match %{DATEDIFF('2015-06-02', "users"."updated_at")}
170
173
  end
171
-
174
+
172
175
  it "should diff between date col and duration" do
173
- d1 = 10
174
- d2 = -10
176
+ d1 = 10
177
+ d2 = -10
175
178
  compile(@table[:created_at] - d1).
176
- must_match %{DATE_SUB("users"."created_at", 10)}
179
+ must_match %{DATE_SUB("users"."created_at", 10)}
177
180
  compile(@table[:created_at] - d2).
178
- must_match %{DATE_SUB("users"."created_at", -10)}
181
+ must_match %{DATE_SUB("users"."created_at", -10)}
179
182
  end
180
183
 
181
184
  it "should accept operators on dates with numbers" do
182
185
  c = @table[:created_at]
183
- # u = @table[:updated_at]
186
+ #u = @table[:updated_at]
184
187
  compile(c - 42).must_be_like %{DATE_SUB("users"."created_at", 42)}
185
188
  compile(c - @table[:id]).must_be_like %{DATE_SUB("users"."created_at", "users"."id")}
186
189
  end
@@ -195,7 +198,7 @@ module ArelExtensions
195
198
  compile(c <= @table[:comments]).must_be_like %{"users"."name" <= "users"."comments"}
196
199
  compile(c =~ /\Atest\Z/).must_be_like %{"users"."name" REGEXP '^test$'}
197
200
  compile(c !~ /\Ate\Dst\Z/).must_be_like %{"users"."name" NOT REGEXP '^te[^0-9]st$'}
198
- end
201
+ end
199
202
 
200
203
  it "should manage complex formulas" do
201
204
  c = @table[:name]
@@ -203,176 +206,184 @@ module ArelExtensions
203
206
  (c.length / 42).round(2).floor > (@table[:updated_at] - Date.new(2000, 3, 31)).abs.ceil
204
207
  ).must_be_like %{FLOOR(ROUND(LENGTH("users"."name") / 42, 2)) > CEIL(ABS(TIMEDIFF("users"."updated_at", '2000-03-31 00:00:00 UTC')))}
205
208
  end
206
-
209
+
207
210
  it "should accept aggregator like GROUP CONCAT" do
208
- @table.project(@table[:first_name].group_concat).group(@table[:last_name]).to_sql
209
- .must_be_like %{SELECT GROUP_CONCAT("users"."first_name") FROM "users" GROUP BY "users"."last_name"}
210
- @table.project(@table[:first_name].group_concat('++')).group(@table[:last_name]).to_sql
211
- .must_be_like %{SELECT GROUP_CONCAT("users"."first_name", '++') FROM "users" GROUP BY "users"."last_name"}
211
+ @table.project(@table[:first_name].group_concat).group(@table[:last_name]).to_sql
212
+ .must_be_like %{SELECT GROUP_CONCAT("users"."first_name") FROM "users" GROUP BY "users"."last_name"}
213
+ @table.project(@table[:first_name].group_concat('++')).group(@table[:last_name]).to_sql
214
+ .must_be_like %{SELECT GROUP_CONCAT("users"."first_name", '++') FROM "users" GROUP BY "users"."last_name"}
212
215
  end
213
-
214
- # Unions
216
+
217
+ # Unions
215
218
  it "should accept union operators on queries and union nodes" do
216
- c = @table.project(@table[:name])
217
- compile(c + c)
218
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
219
- (c + c).to_sql
220
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
221
- (c + (c + c)).to_sql
222
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
223
- ((c + c) + c).to_sql
224
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
225
- (c + c + c).to_sql
226
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
227
-
228
- (c + c).as('union_table').to_sql
229
- .must_be_like %{((SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")) union_table}
230
-
231
-
232
- c = @table.project(@table[:name])
233
- compile(c.union_all(c))
234
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
235
- (c.union_all(c)).to_sql
236
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
237
- (c.union_all(c.union_all(c))).to_sql
238
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
239
- ((c.union_all(c)).union_all(c)).to_sql
240
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
241
- (c.union_all(c).union_all(c)).to_sql
242
- .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
243
- (c.union_all(c)).as('union_table').to_sql
244
- .must_be_like %{((SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")) union_table}
245
-
246
- end
247
-
248
- # Case
249
- it "should accept case clause" do
250
- @table[:name].when("smith").then("cool").when("doe").then("fine").else("uncool").to_sql
251
- .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' WHEN 'doe' THEN 'fine' ELSE 'uncool' END}
252
- @table[:name].when("smith").then(1).when("doe").then(2).else(0).to_sql
253
- .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 1 WHEN 'doe' THEN 2 ELSE 0 END}
254
- ArelExtensions::Nodes::Case.new.when(@table[:name] == "smith").then(1).when(@table[:name] == "doe").then(2).else(0).to_sql
255
- .must_be_like %{CASE WHEN "users"."name" = 'smith' THEN 1 WHEN "users"."name" = 'doe' THEN 2 ELSE 0 END}
256
- ArelExtensions::Nodes::Case.new(@table[:name]).when("smith").then(1).when("doe").then(2).else(0).to_sql
257
- .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 1 WHEN 'doe' THEN 2 ELSE 0 END}
258
- @table[:name].when("smith").then(1).when("doe").then(2).else(0).sum.to_sql
259
- .must_be_like %{SUM(CASE "users"."name" WHEN 'smith' THEN 1 WHEN 'doe' THEN 2 ELSE 0 END)}
219
+ c = @table.project(@table[:name])
220
+ compile(c + c)
221
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
222
+ (c + c).to_sql
223
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
224
+ (c + (c + c)).to_sql
225
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
226
+ ((c + c) + c).to_sql
227
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
228
+ (c + c + c).to_sql
229
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")}
230
+ (c + c).as('union_table').to_sql
231
+ .must_be_like %{((SELECT "users"."name" FROM "users") UNION (SELECT "users"."name" FROM "users")) union_table}
232
+ c = @table.project(@table[:name])
233
+ compile(c.union_all(c))
234
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
235
+ (c.union_all(c)).to_sql
236
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
237
+ (c.union_all(c.union_all(c))).to_sql
238
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
239
+ ((c.union_all(c)).union_all(c)).to_sql
240
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
241
+ (c.union_all(c).union_all(c)).to_sql
242
+ .must_be_like %{(SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")}
243
+ (c.union_all(c)).as('union_table').to_sql
244
+ .must_be_like %{((SELECT "users"."name" FROM "users") UNION ALL (SELECT "users"."name" FROM "users")) union_table}
245
+ end
246
+
247
+ # Case
248
+ it "should accept case clause" do
249
+ @table[:name].when("smith").then("cool").when("doe").then("fine").else("uncool").to_sql
250
+ .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' WHEN 'doe' THEN 'fine' ELSE 'uncool' END}
251
+ @table[:name].when("smith").then(1).when("doe").then(2).else(0).to_sql
252
+ .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 1 WHEN 'doe' THEN 2 ELSE 0 END}
253
+ ArelExtensions::Nodes::Case.new.when(@table[:name] == "smith").then(1).when(@table[:name] == "doe").then(2).else(0).to_sql
254
+ .must_be_like %{CASE WHEN "users"."name" = 'smith' THEN 1 WHEN "users"."name" = 'doe' THEN 2 ELSE 0 END}
255
+ ArelExtensions::Nodes::Case.new(@table[:name]).when("smith").then(1).when("doe").then(2).else(0).to_sql
256
+ .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 1 WHEN 'doe' THEN 2 ELSE 0 END}
257
+ @table[:name].when("smith").then(1).when("doe").then(2).else(0).sum.to_sql
258
+ .must_be_like %{SUM(CASE "users"."name" WHEN 'smith' THEN 1 WHEN 'doe' THEN 2 ELSE 0 END)}
260
259
  @table[:name].when("smith").then("cool").else("uncool").matches('value',false).to_sql
261
- .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' ELSE 'uncool' END LIKE 'value'}
260
+ .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' ELSE 'uncool' END LIKE 'value'}
262
261
  @table[:name].when("smith").then("cool").else("uncool").imatches('value',false).to_sql
263
- .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' ELSE 'uncool' END ILIKE 'value'}
264
- end
265
-
266
- it "should be possible to use as on anything" do
267
- compile(@table[:name].as('alias')).must_be_like %{"users"."name" AS alias}
268
- compile(@table[:name].concat(' test').as('alias')).must_be_like %{CONCAT("users"."name", ' test') AS alias}
269
- compile((@table[:name] + ' test').as('alias')).must_be_like %{CONCAT("users"."name", ' test') AS alias}
270
- compile((@table[:age] + 42).as('alias')).must_be_like %{("users"."age" + 42) AS alias}
271
- compile(@table[:name].coalesce('').as('alias')).must_be_like %{COALESCE("users"."name", '') AS alias}
272
- compile(Arel::Nodes.build_quoted('test').as('alias')).must_be_like %{'test' AS alias}
273
- compile(@table.project(@table[:name]).as('alias')).must_be_like %{(SELECT "users"."name" FROM "users") alias}
274
- compile(@table[:name].when("smith").then("cool").else("uncool").as('alias')).
275
- must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' ELSE 'uncool' END AS alias}
276
- end
277
-
278
- it "should accept comparators on functions" do
279
- c = @table[:name]
280
- compile(c.soundex == 'test').must_be_like %{SOUNDEX("users"."name") = 'test'}
281
- compile(c.soundex != 'test').must_be_like %{SOUNDEX("users"."name") != 'test'}
282
- compile(c.length >= 0 ).must_be_like %{LENGTH("users"."name") >= 0}
283
- end
284
-
285
-
286
- it "should accept in on select statement" do
287
- c = @table[:name]
288
- compile(c.in(@table.project(@table[:name])))
289
- .must_be_like %{"users"."name" IN (SELECT "users"."name" FROM "users")}
290
- end
291
-
292
- it "should accept coalesce function properly even on none actual tables and attributes" do
293
- fake_at = Arel::Table.new('fake_table')
294
- compile(fake_at['fake_attribute'].coalesce('other_value'))
295
- .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value')}
296
- compile(fake_at['fake_attribute'].coalesce('other_value1','other_value2'))
297
- .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value1', 'other_value2')}
262
+ .must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' ELSE 'uncool' END ILIKE 'value'}
263
+ end
264
+
265
+ it "should be possible to use as on anything" do
266
+ compile(@table[:name].as('alias')).must_be_like %{"users"."name" AS alias}
267
+ compile(@table[:name].concat(' test').as('alias')).must_be_like %{CONCAT("users"."name", ' test') AS alias}
268
+ compile((@table[:name] + ' test').as('alias')).must_be_like %{CONCAT("users"."name", ' test') AS alias}
269
+ compile((@table[:age] + 42).as('alias')).must_be_like %{("users"."age" + 42) AS alias}
270
+ compile(@table[:name].coalesce('').as('alias')).must_be_like %{COALESCE("users"."name", '') AS alias}
271
+ compile(Arel::Nodes.build_quoted('test').as('alias')).must_be_like %{'test' AS alias}
272
+ compile(@table.project(@table[:name]).as('alias')).must_be_like %{(SELECT "users"."name" FROM "users") alias}
273
+ compile(@table[:name].when("smith").then("cool").else("uncool").as('alias')).
274
+ must_be_like %{CASE "users"."name" WHEN 'smith' THEN 'cool' ELSE 'uncool' END AS alias}
275
+ end
276
+
277
+ it "should accept comparators on functions" do
278
+ c = @table[:name]
279
+ compile(c.soundex == 'test').must_be_like %{SOUNDEX("users"."name") = 'test'}
280
+ compile(c.soundex != 'test').must_be_like %{SOUNDEX("users"."name") != 'test'}
281
+ compile(c.length >= 0 ).must_be_like %{LENGTH("users"."name") >= 0}
282
+ end
283
+
284
+ it "should accept in on select statement" do
285
+ c = @table[:name]
286
+ compile(c.in(@table.project(@table[:name])))
287
+ .must_be_like %{"users"."name" IN (SELECT "users"."name" FROM "users")}
288
+ end
289
+
290
+ it "should accept coalesce function properly even on none actual tables and attributes" do
291
+ fake_at = Arel::Table.new('fake_table')
292
+ compile(fake_at['fake_attribute'].coalesce('other_value'))
293
+ .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value')}
294
+ compile(fake_at['fake_attribute'].coalesce('other_value1','other_value2'))
295
+ .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value1', 'other_value2')}
298
296
  compile(fake_at['fake_attribute'].coalesce('other_value1').coalesce('other_value2'))
299
- .must_be_like %{COALESCE(COALESCE("fake_table"."fake_attribute", 'other_value1'), 'other_value2')}
300
- compile(fake_at['fake_attribute'].coalesce('other_value').matches('truc'))
301
- .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value') LIKE 'truc'}
302
- compile(fake_at['fake_attribute'].coalesce('other_value').imatches('truc'))
303
- .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value') ILIKE 'truc'}
304
- end
305
-
306
- it "should be possible to cast nodes types" do
307
- compile(@table[:id].cast('char'))
308
- .must_be_like %{CAST("users"."id" AS char)}
309
-
310
- compile(@table[:id].coalesce(' ').cast('char'))
311
- .must_be_like %{CAST(COALESCE("users"."id", ' ') AS char)}
312
-
313
- compile(@table[:id].coalesce(' ').cast(:string))
314
- .must_be_like %{CAST(COALESCE("users"."id", ' ') AS char)}
315
-
316
- compile(@table[:id].cast(:string).coalesce(' '))
317
- .must_be_like %{COALESCE(CAST(\"users\".\"id\" AS char), ' ')}
318
-
319
- compile(@table[:id].cast('char') + ' ')
320
- .must_be_like %{CONCAT(CAST("users"."id" AS char), ' ')}
321
-
322
- compile(@table[:id].cast('int') + 2)
323
- .must_be_like %{(CAST("users"."id" AS int) + 2)}
324
- end
325
-
326
- it "should be possible to have nil element in the function NIL" do
327
- compile(@table[:id].in(nil))
328
- .must_be_like %{ISNULL("users"."id")}
329
- compile(@table[:id].in([nil]))
330
- .must_be_like %{ISNULL("users"."id")}
331
- compile(@table[:id].in([nil,1]))
332
- .must_be_like %{(ISNULL("users"."id") OR "users"."id" = 1)}
333
- compile(@table[:id].in([nil,1,2]))
334
- .must_be_like %{(ISNULL("users"."id") OR "users"."id" IN (1, 2))}
335
- compile(@table[:id].in(1))
336
- .must_be_like %{"users"."id" IN (1)}
337
- compile(@table[:id].in([1]))
338
- .must_be_like %{"users"."id" IN (1)}
339
- compile(@table[:id].in([1,2]))
340
- .must_be_like %{"users"."id" IN (1, 2)}
341
-
342
- end
343
-
344
- it "should be possible to add and substract as much as we want" do
345
- c = @table[:name]
346
- compile(c.locate('test')+1)
347
- .must_be_like %{(LOCATE('test', "users"."name") + 1)}
348
- compile(c.locate('test')-1)
349
- .must_be_like %{(LOCATE('test', "users"."name") - 1)}
350
- compile(c.locate('test')+c.locate('test'))
351
- .must_be_like %{(LOCATE('test', "users"."name") + LOCATE('test', "users"."name"))}
352
- compile(c.locate('test')+1+c.locate('test')-1 + 1)
353
- .must_be_like %{((((LOCATE('test', "users"."name") + 1) + LOCATE('test', "users"."name")) - 1) + 1)}
354
- end
355
-
356
- it "should be possible to add and substract on some nodes" do
357
- c = @table[:name]
297
+ .must_be_like %{COALESCE(COALESCE("fake_table"."fake_attribute", 'other_value1'), 'other_value2')}
298
+ compile(fake_at['fake_attribute'].coalesce('other_value').matches('truc'))
299
+ .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value') LIKE 'truc'}
300
+ compile(fake_at['fake_attribute'].coalesce('other_value').imatches('truc'))
301
+ .must_be_like %{COALESCE("fake_table"."fake_attribute", 'other_value') ILIKE 'truc'}
302
+ end
303
+
304
+ it "should be possible to cast nodes types" do
305
+ compile(@table[:id].cast('char'))
306
+ .must_be_like %{CAST("users"."id" AS char)}
307
+ compile(@table[:id].coalesce(' ').cast('char'))
308
+ .must_be_like %{CAST(COALESCE("users"."id", ' ') AS char)}
309
+ compile(@table[:id].coalesce(' ').cast(:string))
310
+ .must_be_like %{CAST(COALESCE("users"."id", ' ') AS char)}
311
+ compile(@table[:id].cast(:string).coalesce(' '))
312
+ .must_be_like %{COALESCE(CAST(\"users\".\"id\" AS char), ' ')}
313
+ compile(@table[:id].cast('char') + ' ')
314
+ .must_be_like %{CONCAT(CAST("users"."id" AS char), ' ')}
315
+ compile(@table[:id].cast('int') + 2)
316
+ .must_be_like %{(CAST("users"."id" AS int) + 2)}
317
+ end
318
+
319
+ it "should be possible to have nil element in the function NIL" do
320
+ compile(@table[:id].in(nil))
321
+ .must_be_like %{ISNULL("users"."id")}
322
+ compile(@table[:id].in([nil]))
323
+ .must_be_like %{ISNULL("users"."id")}
324
+ compile(@table[:id].in([nil,1]))
325
+ .must_be_like %{ISNULL("users"."id") OR "users"."id" = 1}
326
+ compile(@table[:id].in([nil,1,2]))
327
+ .must_be_like %{ISNULL("users"."id") OR "users"."id" IN (1, 2)}
328
+ compile(@table[:id].in(1))
329
+ .must_be_like %{"users"."id" IN (1)}
330
+ compile(@table[:id].in([1]))
331
+ .must_be_like %{"users"."id" IN (1)}
332
+ compile(@table[:id].in([1,2]))
333
+ .must_be_like %{"users"."id" IN (1, 2)}
334
+ end
335
+
336
+ it "should be possible to correctly use a Range on an IN" do
337
+ compile(@table[:id].in(1..4))
338
+ .must_be_like %{"users"."id" BETWEEN 1 AND 4}
339
+ compile(@table[:created_at].in(@date .. Date.new(2017, 3, 31))) # @date = Date.new(2016, 3, 31)
340
+ .must_be_like %{"users"."created_at" BETWEEN '2016-03-31' AND '2017-03-31'}
341
+ end
342
+
343
+ it "should be possible to add and substract as much as we want" do
344
+ c = @table[:name]
345
+ compile(c.locate('test')+1)
346
+ .must_be_like %{(LOCATE('test', "users"."name") + 1)}
347
+ compile(c.locate('test')-1)
348
+ .must_be_like %{(LOCATE('test', "users"."name") - 1)}
349
+ compile(c.locate('test')+c.locate('test'))
350
+ .must_be_like %{(LOCATE('test', "users"."name") + LOCATE('test', "users"."name"))}
351
+ compile(c.locate('test')+1+c.locate('test')-1 + 1)
352
+ .must_be_like %{((((LOCATE('test', "users"."name") + 1) + LOCATE('test', "users"."name")) - 1) + 1)}
353
+ end
354
+
355
+ it "should be possible to add and substract on some nodes" do
356
+ c = @table[:name]
358
357
  compile(c.when(0,0).else(42) + 42).must_be_like %{(CASE "users"."name" WHEN 0 THEN 0 ELSE 42 END + 42)}
359
358
  compile(c.when(0,0).else(42) - 42).must_be_like %{(CASE "users"."name" WHEN 0 THEN 0 ELSE 42 END - 42)}
360
- compile(c.when(0,"0").else("42") + "42").must_be_like %{CONCAT(CASE "users"."name" WHEN 0 THEN '0' ELSE '42' END, '42')}
361
- end
362
-
363
- it "should be possible to desc and asc on functions" do
364
- c = @table[:name]
365
- compile(c.asc)
366
- .must_be_like %{"users"."name" ASC}
367
- compile(c.substring(2).asc)
368
- .must_be_like %{SUBSTRING("users"."name", 2) ASC}
369
- compile(c.substring(2).desc)
370
- .must_be_like %{SUBSTRING("users"."name", 2) DESC}
371
- compile((c.locate('test')+1).asc)
372
- .must_be_like %{(LOCATE('test', "users"."name") + 1) ASC}
373
- end
374
-
375
- puts "AREL VERSION : " + Arel::VERSION.to_s
359
+ compile(c.when(0,"0").else("42") + "42").must_be_like %{CONCAT(CASE "users"."name" WHEN 0 THEN '0' ELSE '42' END, '42')}
360
+ end
361
+
362
+ it "should be possible to desc and asc on functions" do
363
+ c = @table[:name]
364
+ compile(c.asc)
365
+ .must_be_like %{"users"."name" ASC}
366
+ compile(c.substring(2).asc)
367
+ .must_be_like %{SUBSTRING("users"."name", 2) ASC}
368
+ compile(c.substring(2).desc)
369
+ .must_be_like %{SUBSTRING("users"."name", 2) DESC}
370
+ compile((c.locate('test')+1).asc)
371
+ .must_be_like %{(LOCATE('test', "users"."name") + 1) ASC}
372
+ end
373
+
374
+ it "should be possible to have multiple arguments on an OR or an AND node" do
375
+ c = @table[:id]
376
+ compile((c == 1).and(c == 2, c == 3)).
377
+ must_be_like %{("users"."id" = 1) AND ("users"."id" = 2) AND ("users"."id" = 3)}
378
+ compile((c == 1).and([c == 2, c == 3])).
379
+ must_be_like %{("users"."id" = 1) AND ("users"."id" = 2) AND ("users"."id" = 3)}
380
+ compile((c == 1).or(c == 2, c == 3)).
381
+ must_be_like %{(("users"."id" = 1) OR ("users"."id" = 2) OR ("users"."id" = 3))}
382
+ compile((c == 1).or([c == 2, c == 3])).
383
+ must_be_like %{(("users"."id" = 1) OR ("users"."id" = 2) OR ("users"."id" = 3))}
384
+ end
385
+
386
+ puts "AREL VERSION : " + Arel::VERSION.to_s
376
387
  end
377
388
  end
378
- end
389
+ end