diff options
author | Ryan Davis <ryand-ruby@zenspider.com> | 2010-10-18 15:41:21 -0700 |
---|---|---|
committer | Ryan Davis <ryand-ruby@zenspider.com> | 2010-10-18 15:41:21 -0700 |
commit | e1ebe6e949ef3674434bfa90d271a7b74c2ac153 (patch) | |
tree | b0c340817d75bddf9840c97a946a7217ff12e8ab /test/visitors | |
parent | 7959e55ead84be096247d9622404d1c60ca21c88 (diff) | |
download | rails-e1ebe6e949ef3674434bfa90d271a7b74c2ac153.tar.gz rails-e1ebe6e949ef3674434bfa90d271a7b74c2ac153.tar.bz2 rails-e1ebe6e949ef3674434bfa90d271a7b74c2ac153.zip |
Fisting arel specs -- still needs tree_manager and cleanup
Diffstat (limited to 'test/visitors')
-rw-r--r-- | test/visitors/test_join_sql.rb | 35 | ||||
-rw-r--r-- | test/visitors/test_oracle.rb | 111 | ||||
-rw-r--r-- | test/visitors/test_postgres.rb | 17 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 134 |
4 files changed, 297 insertions, 0 deletions
diff --git a/test/visitors/test_join_sql.rb b/test/visitors/test_join_sql.rb new file mode 100644 index 0000000000..3dc70d7dd6 --- /dev/null +++ b/test/visitors/test_join_sql.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +module Arel + module Visitors + describe 'the join_sql visitor' do + before do + @visitor = JoinSql.new Table.engine + end + + describe 'inner join' do + it 'should visit left if left is a join' do + t = Table.new :users + join = Nodes::InnerJoin.new t, t, Nodes::On.new(t[:id]) + j2 = Nodes::InnerJoin.new join, t, Nodes::On.new(t[:id]) + @visitor.accept(j2).must_be_like %{ + INNER JOIN "users" ON "users"."id" + INNER JOIN "users" ON "users"."id" + } + end + end + + describe 'outer join' do + it 'should visit left if left is a join' do + t = Table.new :users + join = Nodes::OuterJoin.new t, t, Nodes::On.new(t[:id]) + j2 = Nodes::OuterJoin.new join, t, Nodes::On.new(t[:id]) + @visitor.accept(j2).must_be_like %{ + LEFT OUTER JOIN "users" ON "users"."id" + LEFT OUTER JOIN "users" ON "users"."id" + } + end + end + end + end +end diff --git a/test/visitors/test_oracle.rb b/test/visitors/test_oracle.rb new file mode 100644 index 0000000000..8b5732f287 --- /dev/null +++ b/test/visitors/test_oracle.rb @@ -0,0 +1,111 @@ +require 'spec_helper' + +module Arel + module Visitors + describe 'the oracle visitor' do + before do + @visitor = Oracle.new Table.engine + end + + it 'modifies order when there is distinct and first value' do + # *sigh* + select = "DISTINCT foo.id, FIRST_VALUE(projects.name) OVER (foo) AS alias_0__" + stmt = Nodes::SelectStatement.new + stmt.cores.first.projections << Nodes::SqlLiteral.new(select) + stmt.orders << Nodes::SqlLiteral.new('foo') + sql = @visitor.accept(stmt) + sql.must_be_like %{ + SELECT #{select} ORDER BY alias_0__ + } + end + + it 'is idempotent with crazy query' do + # *sigh* + select = "DISTINCT foo.id, FIRST_VALUE(projects.name) OVER (foo) AS alias_0__" + stmt = Nodes::SelectStatement.new + stmt.cores.first.projections << Nodes::SqlLiteral.new(select) + stmt.orders << Nodes::SqlLiteral.new('foo') + + sql = @visitor.accept(stmt) + sql2 = @visitor.accept(stmt) + check sql.must_equal sql2 + end + + it 'splits orders with commas' do + # *sigh* + select = "DISTINCT foo.id, FIRST_VALUE(projects.name) OVER (foo) AS alias_0__" + stmt = Nodes::SelectStatement.new + stmt.cores.first.projections << Nodes::SqlLiteral.new(select) + stmt.orders << Nodes::SqlLiteral.new('foo, bar') + sql = @visitor.accept(stmt) + sql.must_be_like %{ + SELECT #{select} ORDER BY alias_0__, alias_1__ + } + end + + describe 'Nodes::SelectStatement' do + describe 'limit' do + it 'adds a rownum clause' do + stmt = Nodes::SelectStatement.new + stmt.limit = 10 + sql = @visitor.accept stmt + sql.must_be_like %{ SELECT WHERE ROWNUM <= 10 } + end + + it 'is idempotent' do + stmt = Nodes::SelectStatement.new + stmt.orders << Nodes::SqlLiteral.new('foo') + stmt.limit = 10 + sql = @visitor.accept stmt + sql2 = @visitor.accept stmt + check sql.must_equal sql2 + end + + it 'creates a subquery when there is order_by' do + stmt = Nodes::SelectStatement.new + stmt.orders << Nodes::SqlLiteral.new('foo') + stmt.limit = 10 + sql = @visitor.accept stmt + sql.must_be_like %{ + SELECT * FROM (SELECT ORDER BY foo) WHERE ROWNUM <= 10 + } + end + + it 'creates a subquery when there is DISTINCT' do + stmt = Nodes::SelectStatement.new + stmt.cores.first.projections << Nodes::SqlLiteral.new('DISTINCT id') + stmt.limit = 10 + sql = @visitor.accept stmt + sql.must_be_like %{ + SELECT * FROM (SELECT DISTINCT id) WHERE ROWNUM <= 10 + } + end + + it 'creates a different subquery when there is an offset' do + stmt = Nodes::SelectStatement.new + stmt.limit = 10 + stmt.offset = Nodes::Offset.new(10) + sql = @visitor.accept stmt + sql.must_be_like %{ + SELECT * FROM ( + SELECT raw_sql_.*, rownum raw_rnum_ + FROM (SELECT ) raw_sql_ + WHERE rownum <= 20 + ) + WHERE raw_rnum_ > 10 + } + end + + it 'is idempotent with different subquery' do + stmt = Nodes::SelectStatement.new + stmt.limit = 10 + stmt.offset = Nodes::Offset.new(10) + sql = @visitor.accept stmt + sql2 = @visitor.accept stmt + check sql.must_equal sql2 + end + end + end + end + end +end diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb new file mode 100644 index 0000000000..70b935c185 --- /dev/null +++ b/test/visitors/test_postgres.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +module Arel + module Visitors + describe 'the postgres visitor' do + before do + @visitor = PostgreSQL.new Table.engine + end + + it 'should produce a lock value' do + @visitor.accept(Nodes::Lock.new).must_be_like %{ + FOR UPDATE + } + end + end + end +end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb new file mode 100644 index 0000000000..f7455d7fa3 --- /dev/null +++ b/test/visitors/test_to_sql.rb @@ -0,0 +1,134 @@ +require 'spec_helper' + +module Arel + module Visitors + describe 'the to_sql visitor' do + before do + @visitor = ToSql.new Table.engine + @attr = Table.new(:users)[:id] + end + + describe 'equality' do + it 'should handle false' do + sql = @visitor.accept Nodes::Equality.new(false, false) + sql.must_be_like %{ 'f' = 'f' } + end + + it 'should use the column to quote' do + table = Table.new(:users) + sql = @visitor.accept Nodes::Equality.new(table[:id], '1-fooo') + sql.must_be_like %{ "users"."id" = 1 } + end + end + + it "should visit_DateTime" do + @visitor.accept DateTime.now + end + + it "should visit_Float" do + @visitor.accept 2.14 + end + + it "should visit_Hash" do + @visitor.accept({:a => 1}) + end + + it "should visit_BigDecimal" do + @visitor.accept BigDecimal.new('2.14') + end + + it "should visit_Date" do + @visitor.accept Date.today + end + + it "should visit_Arel_Nodes_And" do + node = Nodes::And.new @attr.eq(10), @attr.eq(11) + @visitor.accept(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 %{ + "users"."id" = 10 OR "users"."id" = 11 + } + end + + it "should visit visit_Arel_Attributes_Time" do + attr = Attributes::Time.new(@attr.relation, @attr.name, @attr.column) + @visitor.accept attr + end + + it "should visit_TrueClass" do + test = @attr.eq(true) + test.left.column.type = :boolean + @visitor.accept(test).must_be_like %{ "users"."id" = 't' } + end + + describe "Nodes::Ordering" do + it "should know how to visit" do + node = @attr.desc + @visitor.accept(node).must_be_like %{ + "users"."id" DESC + } + end + end + + describe "Nodes::In" do + it "should know how to visit" do + node = @attr.in [1, 2, 3] + @visitor.accept(node).must_be_like %{ + "users"."id" IN (1, 2, 3) + } + end + + it "should turn empty right to NULL" do + node = @attr.in [] + @visitor.accept(node).must_be_like %{ + "users"."id" IN (NULL) + } + end + + it 'can handle two dot ranges' do + node = @attr.in 1..3 + @visitor.accept(node).must_be_like %{ + "users"."id" BETWEEN 1 AND 3 + } + end + + it 'can handle three dot ranges' do + node = @attr.in 1...3 + @visitor.accept(node).must_be_like %{ + "users"."id" >= 1 AND "users"."id" < 3 + } + end + + it 'uses the same column for escaping values' do + 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 + visitor.accept(in_node).must_equal %("users"."id" IN ('a', 'b', 'c')) + end + end + + describe 'Equality' do + it "should escape strings" do + test = @attr.eq 'Aaron Patterson' + test.left.column.type = :string + @visitor.accept(test).must_be_like %{ + "users"."id" = 'Aaron Patterson' + } + end + end + end + end +end |