aboutsummaryrefslogtreecommitdiffstats
path: root/test/visitors/test_to_sql.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/visitors/test_to_sql.rb')
-rw-r--r--test/visitors/test_to_sql.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb
index 7ae5d5b3af..ea58039529 100644
--- a/test/visitors/test_to_sql.rb
+++ b/test/visitors/test_to_sql.rb
@@ -607,6 +607,66 @@ module Arel
end
end
end
+
+ describe 'Nodes::Case' do
+ it 'supports simple case expressions' do
+ node = Arel::Nodes::Case.new(@table[:name])
+ .when('foo').then(1)
+ .else(0)
+
+ compile(node).must_be_like %{
+ CASE "users"."name" WHEN 'foo' THEN 1 ELSE 0 END
+ }
+ end
+
+ it 'supports extended case expressions' do
+ node = Arel::Nodes::Case.new
+ .when(@table[:name].in(%w(foo bar))).then(1)
+ .else(0)
+
+ compile(node).must_be_like %{
+ CASE WHEN "users"."name" IN ('foo', 'bar') THEN 1 ELSE 0 END
+ }
+ end
+
+ it 'works without default branch' do
+ node = Arel::Nodes::Case.new(@table[:name])
+ .when('foo').then(1)
+
+ compile(node).must_be_like %{
+ CASE "users"."name" WHEN 'foo' THEN 1 END
+ }
+ end
+
+ it 'allows chaining multiple conditions' do
+ node = Arel::Nodes::Case.new(@table[:name])
+ .when('foo').then(1)
+ .when('bar').then(2)
+ .else(0)
+
+ compile(node).must_be_like %{
+ CASE "users"."name" WHEN 'foo' THEN 1 WHEN 'bar' THEN 2 ELSE 0 END
+ }
+ end
+
+ it 'supports #when with two arguments and no #then' do
+ node = Arel::Nodes::Case.new @table[:name]
+
+ { foo: 1, bar: 0 }.reduce(node) { |node, pair| node.when *pair }
+
+ compile(node).must_be_like %{
+ CASE "users"."name" WHEN 'foo' THEN 1 WHEN 'bar' THEN 0 END
+ }
+ end
+
+ it 'can be chained as a predicate' do
+ node = @table[:name].when('foo').then('bar').else('baz')
+
+ compile(node).must_be_like %{
+ CASE "users"."name" WHEN 'foo' THEN 'bar' ELSE 'baz' END
+ }
+ end
+ end
end
end
end