diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-24 15:21:54 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-24 15:21:54 -0700 |
commit | 6b86ae4f9f2fb8cd145f958753fedf16aae3a0d4 (patch) | |
tree | fff66461830df50e3211d8ced131584e6d75f9d4 /spec/visitors | |
parent | 245d797a8ccab3ed2de9a0eedf455d3094a091ce (diff) | |
download | rails-6b86ae4f9f2fb8cd145f958753fedf16aae3a0d4.tar.gz rails-6b86ae4f9f2fb8cd145f958753fedf16aae3a0d4.tar.bz2 rails-6b86ae4f9f2fb8cd145f958753fedf16aae3a0d4.zip |
shuffling around the spec directory
Diffstat (limited to 'spec/visitors')
-rw-r--r-- | spec/visitors/join_sql_spec.rb | 35 | ||||
-rw-r--r-- | spec/visitors/oracle_spec.rb | 101 | ||||
-rw-r--r-- | spec/visitors/to_sql_spec.rb | 101 |
3 files changed, 237 insertions, 0 deletions
diff --git a/spec/visitors/join_sql_spec.rb b/spec/visitors/join_sql_spec.rb new file mode 100644 index 0000000000..9064dae852 --- /dev/null +++ b/spec/visitors/join_sql_spec.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).should 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).should be_like %{ + LEFT OUTER JOIN "users" ON "users"."id" + LEFT OUTER JOIN "users" ON "users"."id" + } + end + end + end + end +end diff --git a/spec/visitors/oracle_spec.rb b/spec/visitors/oracle_spec.rb new file mode 100644 index 0000000000..cdf0e3427f --- /dev/null +++ b/spec/visitors/oracle_spec.rb @@ -0,0 +1,101 @@ +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.should 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.should == 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.should 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.should 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.should == 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.should be_like %{ + SELECT * FROM (SELECT ORDER BY foo) 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.should 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.should == sql2 + end + end + end + end + end +end diff --git a/spec/visitors/to_sql_spec.rb b/spec/visitors/to_sql_spec.rb new file mode 100644 index 0000000000..25642ee947 --- /dev/null +++ b/spec/visitors/to_sql_spec.rb @@ -0,0 +1,101 @@ +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.should be_like %{ 'f' = 'f' } + 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).should 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).should 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 + @visitor.accept(@attr.eq(true)).should be_like %{ "users"."id" = 't' } + end + + describe "Nodes::In" do + it "should know how to visit" do + node = @attr.in [1, 2, 3] + @visitor.accept(node).should be_like %{ + "users"."id" IN (1, 2, 3) + } + end + + it "should turn empty right to NULL" do + node = @attr.in [] + @visitor.accept(node).should be_like %{ + "users"."id" IN (NULL) + } + end + + it 'can handle two dot ranges' do + node = @attr.in 1..3 + @visitor.accept(node).should be_like %{ + "users"."id" BETWEEN 1 AND 3 + } + end + + it 'can handle three dot ranges' do + node = @attr.in 1...3 + @visitor.accept(node).should be_like %{ + "users"."id" >= 1 AND "users"."id" < 3 + } + end + end + + describe 'Equality' do + it "should escape strings" do + test = @attr.eq 'Aaron Patterson' + @visitor.accept(test).should be_like %{ + "users"."id" = 'Aaron Patterson' + } + end + end + end + end +end |