diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-08 15:22:27 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-08 15:22:27 -0700 |
commit | 4e2e1cc63a81bf549712c4cacfe1f3dc32e632a2 (patch) | |
tree | e7168b9f2abb928f922a91be5983baa2d9718498 /test | |
parent | 1f9333e23b0e6c5634eaac9635f90a4235a16de8 (diff) | |
download | rails-4e2e1cc63a81bf549712c4cacfe1f3dc32e632a2.tar.gz rails-4e2e1cc63a81bf549712c4cacfe1f3dc32e632a2.tar.bz2 rails-4e2e1cc63a81bf549712c4cacfe1f3dc32e632a2.zip |
to_sql is passing
Diffstat (limited to 'test')
-rw-r--r-- | test/visitors/test_to_sql.rb | 126 |
1 files changed, 63 insertions, 63 deletions
diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index bcf2010d97..33c7bb9e93 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -12,7 +12,7 @@ module Arel end def compile node - @visitor.accept(node, @collector).value + @visitor.accept(node, Collectors::SQLString.new).value end it 'works with BindParams' do @@ -45,63 +45,63 @@ module Arel it 'should visit named functions' do function = Nodes::NamedFunction.new('omg', [Arel.star]) - assert_equal 'omg(*)', @visitor.accept(function) + assert_equal 'omg(*)', compile(function) end it 'should chain predications on named functions' do function = Nodes::NamedFunction.new('omg', [Arel.star]) - sql = @visitor.accept(function.eq(2)) + sql = compile(function.eq(2)) sql.must_be_like %{ omg(*) = 2 } end it 'should visit built-in functions' do function = Nodes::Count.new([Arel.star]) - assert_equal 'COUNT(*)', @visitor.accept(function) + assert_equal 'COUNT(*)', compile(function) function = Nodes::Sum.new([Arel.star]) - assert_equal 'SUM(*)', @visitor.accept(function) + assert_equal 'SUM(*)', compile(function) function = Nodes::Max.new([Arel.star]) - assert_equal 'MAX(*)', @visitor.accept(function) + assert_equal 'MAX(*)', compile(function) function = Nodes::Min.new([Arel.star]) - assert_equal 'MIN(*)', @visitor.accept(function) + assert_equal 'MIN(*)', compile(function) function = Nodes::Avg.new([Arel.star]) - assert_equal 'AVG(*)', @visitor.accept(function) + assert_equal 'AVG(*)', compile(function) end it 'should visit built-in functions operating on distinct values' do function = Nodes::Count.new([Arel.star]) function.distinct = true - assert_equal 'COUNT(DISTINCT *)', @visitor.accept(function) + assert_equal 'COUNT(DISTINCT *)', compile(function) function = Nodes::Sum.new([Arel.star]) function.distinct = true - assert_equal 'SUM(DISTINCT *)', @visitor.accept(function) + assert_equal 'SUM(DISTINCT *)', compile(function) function = Nodes::Max.new([Arel.star]) function.distinct = true - assert_equal 'MAX(DISTINCT *)', @visitor.accept(function) + assert_equal 'MAX(DISTINCT *)', compile(function) function = Nodes::Min.new([Arel.star]) function.distinct = true - assert_equal 'MIN(DISTINCT *)', @visitor.accept(function) + assert_equal 'MIN(DISTINCT *)', compile(function) function = Nodes::Avg.new([Arel.star]) function.distinct = true - assert_equal 'AVG(DISTINCT *)', @visitor.accept(function) + assert_equal 'AVG(DISTINCT *)', compile(function) end it 'works with lists' do function = Nodes::NamedFunction.new('omg', [Arel.star, Arel.star]) - assert_equal 'omg(*, *)', @visitor.accept(function) + assert_equal 'omg(*, *)', compile(function) end describe 'Nodes::Equality' do it "should escape strings" do test = Table.new(:users)[:name].eq 'Aaron Patterson' - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ "users"."name" = 'Aaron Patterson' } end @@ -109,19 +109,19 @@ module Arel it 'should handle false' do table = Table.new(:users) val = Nodes.build_quoted(false, table[:active]) - sql = @visitor.accept Nodes::Equality.new(val, val) + sql = compile Nodes::Equality.new(val, val) sql.must_be_like %{ 'f' = 'f' } end it 'should use the column to quote' do table = Table.new(:users) val = Nodes.build_quoted('1-fooo', table[:id]) - sql = @visitor.accept Nodes::Equality.new(table[:id], val) + sql = compile Nodes::Equality.new(table[:id], val) sql.must_be_like %{ "users"."id" = 1 } end it 'should handle nil' do - sql = @visitor.accept Nodes::Equality.new(@table[:name], nil) + sql = compile Nodes::Equality.new(@table[:name], nil) sql.must_be_like %{ "users"."name" IS NULL } end end @@ -129,13 +129,13 @@ module Arel describe 'Nodes::NotEqual' do it 'should handle false' do val = Nodes.build_quoted(false, @table[:active]) - sql = @visitor.accept Nodes::NotEqual.new(@table[:active], val) + sql = compile Nodes::NotEqual.new(@table[:active], val) sql.must_be_like %{ "users"."active" != 'f' } end it 'should handle nil' do val = Nodes.build_quoted(nil, @table[:active]) - sql = @visitor.accept Nodes::NotEqual.new(@table[:name], val) + sql = compile Nodes::NotEqual.new(@table[:name], val) sql.must_be_like %{ "users"."name" IS NOT NULL } end end @@ -146,19 +146,19 @@ module Arel Class.new(Class.new(String)).new(":'("), ].each do |obj| val = Nodes.build_quoted(obj, @table[:active]) - sql = @visitor.accept Nodes::NotEqual.new(@table[:name], val) + sql = compile Nodes::NotEqual.new(@table[:name], val) sql.must_be_like %{ "users"."name" != ':\\'(' } end end it "should visit_Class" do - @visitor.accept(Nodes.build_quoted(DateTime)).must_equal "'DateTime'" + compile(Nodes.build_quoted(DateTime)).must_equal "'DateTime'" end it "should escape LIMIT" do sc = Arel::Nodes::SelectStatement.new sc.limit = Arel::Nodes::Limit.new(Nodes.build_quoted("omg")) - assert_match(/LIMIT 'omg'/, @visitor.accept(sc)) + assert_match(/LIMIT 'omg'/, compile(sc)) end it "should visit_DateTime" do @@ -173,7 +173,7 @@ module Arel dt = DateTime.now table = Table.new(:users) test = table[:created_at].eq dt - sql = @visitor.accept test + sql = compile test assert_equal "created_at", called_with.name sql.must_be_like %{"users"."created_at" = '#{dt.strftime("%Y-%m-%d %H:%M:%S")}'} @@ -181,37 +181,37 @@ module Arel it "should visit_Float" do test = Table.new(:users)[:name].eq 2.14 - sql = @visitor.accept test + sql = compile test sql.must_be_like %{"users"."name" = 2.14} end it "should visit_Not" do - sql = @visitor.accept Nodes::Not.new(Arel.sql("foo")) + sql = compile Nodes::Not.new(Arel.sql("foo")) sql.must_be_like "NOT (foo)" end it "should apply Not to the whole expression" do node = Nodes::And.new [@attr.eq(10), @attr.eq(11)] - sql = @visitor.accept Nodes::Not.new(node) + sql = compile Nodes::Not.new(node) sql.must_be_like %{NOT ("users"."id" = 10 AND "users"."id" = 11)} end it "should visit_As" do as = Nodes::As.new(Arel.sql("foo"), Arel.sql("bar")) - sql = @visitor.accept as + sql = compile as sql.must_be_like "foo AS bar" end it "should visit_Bignum" do - @visitor.accept 8787878092 + compile 8787878092 end it "should visit_Hash" do - @visitor.accept(Nodes.build_quoted({:a => 1})) + compile(Nodes.build_quoted({:a => 1})) end it "should visit_BigDecimal" do - @visitor.accept Nodes.build_quoted(BigDecimal.new('2.14')) + compile Nodes.build_quoted(BigDecimal.new('2.14')) end it "should visit_Date" do @@ -226,31 +226,31 @@ module Arel dt = Date.today table = Table.new(:users) test = table[:created_at].eq dt - sql = @visitor.accept test + sql = compile test assert_equal "created_at", called_with.name sql.must_be_like %{"users"."created_at" = '#{dt.strftime("%Y-%m-%d")}'} end it "should visit_NilClass" do - @visitor.accept(Nodes.build_quoted(nil)).must_be_like "NULL" + compile(Nodes.build_quoted(nil)).must_be_like "NULL" end it "should visit_Arel_SelectManager, which is a subquery" do mgr = Table.new(:foo).project(:bar) - @visitor.accept(mgr).must_be_like '(SELECT bar FROM "foo")' + compile(mgr).must_be_like '(SELECT bar FROM "foo")' end it "should visit_Arel_Nodes_And" do node = Nodes::And.new [@attr.eq(10), @attr.eq(11)] - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" = 10 AND "users"."id" = 11 } end it "should visit_Arel_Nodes_Or" do node = Nodes::Or.new @attr.eq(10), @attr.eq(11) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" = 10 OR "users"."id" = 11 } end @@ -261,25 +261,25 @@ module Arel Nodes::UnqualifiedColumn.new(column), Nodes::UnqualifiedColumn.new(column) ) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "id" = "id" } end it "should visit visit_Arel_Attributes_Time" do attr = Attributes::Time.new(@attr.relation, @attr.name) - @visitor.accept attr + compile attr end it "should visit_TrueClass" do test = Table.new(:users)[:bool].eq(true) - @visitor.accept(test).must_be_like %{ "users"."bool" = 't' } + compile(test).must_be_like %{ "users"."bool" = 't' } end describe "Nodes::Matches" do it "should know how to visit" do node = @table[:name].matches('foo%') - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."name" LIKE 'foo%' } end @@ -287,7 +287,7 @@ module Arel it 'can handle subqueries' do subquery = @table.project(:id).where(@table[:name].matches('foo%')) node = @attr.in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" LIKE 'foo%') } end @@ -296,7 +296,7 @@ module Arel describe "Nodes::DoesNotMatch" do it "should know how to visit" do node = @table[:name].does_not_match('foo%') - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."name" NOT LIKE 'foo%' } end @@ -304,7 +304,7 @@ module Arel it 'can handle subqueries' do subquery = @table.project(:id).where(@table[:name].does_not_match('foo%')) node = @attr.in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" NOT LIKE 'foo%') } end @@ -313,7 +313,7 @@ module Arel describe "Nodes::Ordering" do it "should know how to visit" do node = @attr.desc - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" DESC } end @@ -322,19 +322,19 @@ module Arel describe "Nodes::In" do it "should know how to visit" do node = @attr.in [1, 2, 3] - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (1, 2, 3) } end it "should return 1=0 when empty right which is always false" do node = @attr.in [] - @visitor.accept(node).must_equal '1=0' + compile(node).must_equal '1=0' end it 'can handle two dot ranges' do node = @attr.in 1..3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" BETWEEN 1 AND 3 } end @@ -348,26 +348,26 @@ module Arel it 'can handle ranges bounded by infinity' do node = @attr.in 1..Float::INFINITY - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" >= 1 } node = @attr.in(-Float::INFINITY..3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" <= 3 } node = @attr.in(-Float::INFINITY...3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" < 3 } node = @attr.in(-Float::INFINITY..Float::INFINITY) - @visitor.accept(node).must_be_like %{1=1} + compile(node).must_be_like %{1=1} end it 'can handle subqueries' do table = Table.new(:users) subquery = table.project(:id).where(table[:name].eq('Aaron')) node = @attr.in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron') } end @@ -388,29 +388,29 @@ module Arel visitor.expected = Table.engine.connection.columns(:users).find { |x| x.name == 'name' } - visitor.accept(in_node).must_equal %("users"."name" IN ('a', 'b', 'c')) + visitor.accept(in_node, Collectors::SQLString.new).value.must_equal %("users"."name" IN ('a', 'b', 'c')) end end describe "Nodes::InfixOperation" do it "should handle Multiplication" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) * Arel::Attributes::Decimal.new(Table.new(:currency_rates), :rate) - @visitor.accept(node).must_equal %("products"."price" * "currency_rates"."rate") + compile(node).must_equal %("products"."price" * "currency_rates"."rate") end it "should handle Division" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) / 5 - @visitor.accept(node).must_equal %("products"."price" / 5) + compile(node).must_equal %("products"."price" / 5) end it "should handle Addition" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) + 6 - @visitor.accept(node).must_equal %(("products"."price" + 6)) + compile(node).must_equal %(("products"."price" + 6)) end it "should handle Subtraction" do node = Arel::Attributes::Decimal.new(Table.new(:products), :price) - 7 - @visitor.accept(node).must_equal %(("products"."price" - 7)) + compile(node).must_equal %(("products"."price" - 7)) end it "should handle arbitrary operators" do @@ -419,7 +419,7 @@ module Arel Arel::Attributes::String.new(Table.new(:products), :name), Arel::Attributes::String.new(Table.new(:products), :name) ) - @visitor.accept(node).must_equal %("products"."name" || "products"."name") + compile(node).must_equal %("products"."name" || "products"."name") end end @@ -438,7 +438,7 @@ module Arel it 'can handle two dot ranges' do node = @attr.not_in 1..3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" < 1 OR "users"."id" > 3 } end @@ -456,15 +456,15 @@ module Arel "users"."id" < 1 } node = @attr.not_in(-Float::INFINITY..3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" > 3 } node = @attr.not_in(-Float::INFINITY...3) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" >= 3 } node = @attr.not_in(-Float::INFINITY..Float::INFINITY) - @visitor.accept(node).must_be_like %{1=0} + compile(node).must_be_like %{1=0} end it 'can handle subqueries' do @@ -492,7 +492,7 @@ module Arel visitor.expected = Table.engine.connection.columns(:users).find { |x| x.name == 'name' } - visitor.accept(in_node).must_equal %("users"."name" NOT IN ('a', 'b', 'c')) + compile(in_node).must_equal %("users"."name" NOT IN ('a', 'b', 'c')) end end |