2010-10-18 19:54:50 -04:00
|
|
|
require 'helper'
|
2010-08-13 21:55:15 -04:00
|
|
|
|
|
|
|
module Arel
|
|
|
|
module Visitors
|
|
|
|
describe 'the to_sql visitor' do
|
|
|
|
before do
|
|
|
|
@visitor = ToSql.new Table.engine
|
|
|
|
@attr = Table.new(:users)[:id]
|
|
|
|
end
|
|
|
|
|
2010-09-10 16:26:30 -04:00
|
|
|
describe 'equality' do
|
|
|
|
it 'should handle false' do
|
|
|
|
sql = @visitor.accept Nodes::Equality.new(false, false)
|
2010-10-18 18:41:21 -04:00
|
|
|
sql.must_be_like %{ 'f' = 'f' }
|
2010-09-10 16:26:30 -04:00
|
|
|
end
|
2010-09-27 14:32:04 -04:00
|
|
|
|
|
|
|
it 'should use the column to quote' do
|
|
|
|
table = Table.new(:users)
|
|
|
|
sql = @visitor.accept Nodes::Equality.new(table[:id], '1-fooo')
|
2010-10-18 18:41:21 -04:00
|
|
|
sql.must_be_like %{ "users"."id" = 1 }
|
2010-09-27 14:32:04 -04:00
|
|
|
end
|
2010-09-10 16:26:30 -04:00
|
|
|
end
|
|
|
|
|
2010-11-16 11:15:58 -05:00
|
|
|
it "should visit_Class" do
|
|
|
|
@visitor.accept(DateTime).must_equal "'DateTime'"
|
|
|
|
end
|
|
|
|
|
2010-09-10 12:51:03 -04:00
|
|
|
it "should visit_DateTime" do
|
|
|
|
@visitor.accept DateTime.now
|
|
|
|
end
|
|
|
|
|
2010-09-08 19:29:08 -04:00
|
|
|
it "should visit_Float" do
|
|
|
|
@visitor.accept 2.14
|
|
|
|
end
|
|
|
|
|
2010-11-23 19:24:41 -05:00
|
|
|
it "should visit_Not" do
|
|
|
|
sql = @visitor.accept Nodes::Not.new(Arel.sql("foo"))
|
|
|
|
sql.must_be_like "NOT foo"
|
|
|
|
end
|
|
|
|
|
2010-11-23 21:22:42 -05:00
|
|
|
it "should visit_As" do
|
|
|
|
as = Nodes::As.new(Arel.sql("foo"), Arel.sql("bar"))
|
|
|
|
sql = @visitor.accept as
|
|
|
|
sql.must_be_like "foo AS bar"
|
|
|
|
end
|
|
|
|
|
2010-10-26 02:35:31 -04:00
|
|
|
it "should visit_Bignum" do
|
|
|
|
@visitor.accept 8787878092
|
|
|
|
end
|
|
|
|
|
2010-09-11 00:48:32 -04:00
|
|
|
it "should visit_Hash" do
|
|
|
|
@visitor.accept({:a => 1})
|
|
|
|
end
|
|
|
|
|
2010-09-08 17:45:38 -04:00
|
|
|
it "should visit_BigDecimal" do
|
|
|
|
@visitor.accept BigDecimal.new('2.14')
|
|
|
|
end
|
|
|
|
|
2010-08-24 21:42:47 -04:00
|
|
|
it "should visit_Date" do
|
|
|
|
@visitor.accept Date.today
|
|
|
|
end
|
|
|
|
|
2010-11-03 08:32:23 -04:00
|
|
|
it "should visit_NilClass" do
|
|
|
|
@visitor.accept(nil).must_be_like "NULL"
|
|
|
|
end
|
|
|
|
|
2010-08-23 17:44:10 -04:00
|
|
|
it "should visit_Arel_Nodes_And" do
|
|
|
|
node = Nodes::And.new @attr.eq(10), @attr.eq(11)
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-08-23 17:44:10 -04:00
|
|
|
"users"."id" = 10 AND "users"."id" = 11
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-16 20:26:12 -04:00
|
|
|
it "should visit_Arel_Nodes_Or" do
|
|
|
|
node = Nodes::Or.new @attr.eq(10), @attr.eq(11)
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-08-16 20:26:12 -04:00
|
|
|
"users"."id" = 10 OR "users"."id" = 11
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-08-13 21:55:15 -04:00
|
|
|
it "should visit visit_Arel_Attributes_Time" do
|
|
|
|
attr = Attributes::Time.new(@attr.relation, @attr.name, @attr.column)
|
|
|
|
@visitor.accept attr
|
|
|
|
end
|
2010-08-16 17:09:48 -04:00
|
|
|
|
2010-08-16 18:46:53 -04:00
|
|
|
it "should visit_TrueClass" do
|
2010-11-05 17:05:10 -04:00
|
|
|
test = Table.new(:users)[:bool].eq(true)
|
|
|
|
@visitor.accept(test).must_be_like %{ "users"."bool" = 't' }
|
2010-08-16 18:46:53 -04:00
|
|
|
end
|
|
|
|
|
2010-09-29 20:08:57 -04:00
|
|
|
describe "Nodes::Ordering" do
|
|
|
|
it "should know how to visit" do
|
|
|
|
node = @attr.desc
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-09-29 20:08:57 -04:00
|
|
|
"users"."id" DESC
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-16 17:43:18 -04:00
|
|
|
describe "Nodes::In" do
|
|
|
|
it "should know how to visit" do
|
|
|
|
node = @attr.in [1, 2, 3]
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-08-16 17:43:18 -04:00
|
|
|
"users"."id" IN (1, 2, 3)
|
|
|
|
}
|
|
|
|
end
|
2010-09-20 19:03:44 -04:00
|
|
|
|
|
|
|
it "should turn empty right to NULL" do
|
|
|
|
node = @attr.in []
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-09-20 19:03:44 -04:00
|
|
|
"users"."id" IN (NULL)
|
|
|
|
}
|
|
|
|
end
|
2010-09-20 19:23:23 -04:00
|
|
|
|
|
|
|
it 'can handle two dot ranges' do
|
|
|
|
node = @attr.in 1..3
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-09-20 19:23:23 -04:00
|
|
|
"users"."id" BETWEEN 1 AND 3
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can handle three dot ranges' do
|
|
|
|
node = @attr.in 1...3
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(node).must_be_like %{
|
2010-09-20 19:23:23 -04:00
|
|
|
"users"."id" >= 1 AND "users"."id" < 3
|
|
|
|
}
|
|
|
|
end
|
2010-09-29 17:18:18 -04:00
|
|
|
|
|
|
|
it 'uses the same column for escaping values' do
|
2010-11-05 17:05:10 -04:00
|
|
|
@attr = Table.new(:users)[:name]
|
2010-09-29 17:18:18 -04:00
|
|
|
visitor = Class.new(ToSql) do
|
|
|
|
attr_accessor :expected
|
|
|
|
|
|
|
|
def quote value, column = nil
|
|
|
|
raise unless column == expected
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
in_node = Nodes::In.new @attr, %w{ a b c }
|
|
|
|
visitor = visitor.new(Table.engine)
|
|
|
|
visitor.expected = @attr.column
|
2010-11-05 17:05:10 -04:00
|
|
|
visitor.accept(in_node).must_equal %("users"."name" IN ('a', 'b', 'c'))
|
2010-09-29 17:18:18 -04:00
|
|
|
end
|
2010-08-16 17:43:18 -04:00
|
|
|
end
|
|
|
|
|
2010-11-17 09:18:25 -05:00
|
|
|
describe "Nodes::NotIn" do
|
|
|
|
it "should know how to visit" do
|
|
|
|
node = @attr.not_in [1, 2, 3]
|
|
|
|
@visitor.accept(node).must_be_like %{
|
|
|
|
"users"."id" NOT IN (1, 2, 3)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should turn empty right to NULL" do
|
|
|
|
node = @attr.not_in []
|
|
|
|
@visitor.accept(node).must_be_like %{
|
|
|
|
"users"."id" NOT IN (NULL)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can handle two dot ranges' do
|
|
|
|
node = @attr.not_in 1..3
|
|
|
|
@visitor.accept(node).must_be_like %{
|
|
|
|
"users"."id" < 1 OR "users"."id" > 3
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can handle three dot ranges' do
|
|
|
|
node = @attr.not_in 1...3
|
|
|
|
@visitor.accept(node).must_be_like %{
|
|
|
|
"users"."id" < 1 OR "users"."id" >= 3
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the same column for escaping values' do
|
|
|
|
@attr = Table.new(:users)[:name]
|
|
|
|
visitor = Class.new(ToSql) do
|
|
|
|
attr_accessor :expected
|
|
|
|
|
|
|
|
def quote value, column = nil
|
|
|
|
raise unless column == expected
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
in_node = Nodes::NotIn.new @attr, %w{ a b c }
|
|
|
|
visitor = visitor.new(Table.engine)
|
|
|
|
visitor.expected = @attr.column
|
|
|
|
visitor.accept(in_node).must_equal %("users"."name" NOT IN ('a', 'b', 'c'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-16 17:09:48 -04:00
|
|
|
describe 'Equality' do
|
|
|
|
it "should escape strings" do
|
2010-11-05 17:05:10 -04:00
|
|
|
test = Table.new(:users)[:name].eq 'Aaron Patterson'
|
2010-10-18 18:41:21 -04:00
|
|
|
@visitor.accept(test).must_be_like %{
|
2010-11-05 17:05:10 -04:00
|
|
|
"users"."name" = 'Aaron Patterson'
|
2010-08-16 17:09:48 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2010-08-13 21:55:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|