diff options
Diffstat (limited to 'test/visitors')
-rw-r--r-- | test/visitors/test_bind_visitor.rb | 22 | ||||
-rw-r--r-- | test/visitors/test_mysql.rb | 18 | ||||
-rw-r--r-- | test/visitors/test_oracle.rb | 42 | ||||
-rw-r--r-- | test/visitors/test_postgres.rb | 30 | ||||
-rw-r--r-- | test/visitors/test_sqlite.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 162 |
6 files changed, 152 insertions, 126 deletions
diff --git a/test/visitors/test_bind_visitor.rb b/test/visitors/test_bind_visitor.rb index 2bfd03c861..4563fddfd1 100644 --- a/test/visitors/test_bind_visitor.rb +++ b/test/visitors/test_bind_visitor.rb @@ -4,8 +4,14 @@ require 'support/fake_record' module Arel module Visitors - class TestBindVisitor < Minitest::Test - + class TestBindVisitor < Arel::Test + attr_reader :collector + + def setup + @collector = Collectors::SQLString.new + super + end + ## # Tests visit_Arel_Nodes_Assignment correctly # substitutes binds with values from block @@ -19,8 +25,12 @@ module Arel }.new Table.engine.connection assignment = um.ast.values[0] - actual = visitor.accept(assignment) { "replace" } - actual.must_be_like "\"name\" = replace" + actual = visitor.accept(assignment, collector) { |collector| + collector << "replace" + } + assert actual + value = actual.value + assert_like "\"name\" = replace", value end def test_visitor_yields_on_binds @@ -33,7 +43,7 @@ module Arel bp = Nodes::BindParam.new 'omg' called = false - visitor.accept(bp) { called = true } + visitor.accept(bp, collector) { |collector| called = true } assert called end @@ -49,7 +59,7 @@ module Arel called = false assert_raises(TypeError) { - visitor.accept(bp) { called = true } + visitor.accept(bp, collector) { called = true } } refute called end diff --git a/test/visitors/test_mysql.rb b/test/visitors/test_mysql.rb index 6b62ec133a..8e4b9e6861 100644 --- a/test/visitors/test_mysql.rb +++ b/test/visitors/test_mysql.rb @@ -7,14 +7,18 @@ module Arel @visitor = MySQL.new Table.engine.connection end + def compile node + @visitor.accept(node, Collectors::SQLString.new).value + end + it 'squashes parenthesis on multiple unions' do subnode = Nodes::Union.new Arel.sql('left'), Arel.sql('right') node = Nodes::Union.new subnode, Arel.sql('topright') - assert_equal 1, @visitor.accept(node).scan('(').length + assert_equal 1, compile(node).scan('(').length subnode = Nodes::Union.new Arel.sql('left'), Arel.sql('right') node = Nodes::Union.new Arel.sql('topleft'), subnode - assert_equal 1, @visitor.accept(node).scan('(').length + assert_equal 1, compile(node).scan('(').length end ### @@ -23,7 +27,7 @@ module Arel it 'defaults limit to 18446744073709551615' do stmt = Nodes::SelectStatement.new stmt.offset = Nodes::Offset.new(1) - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like "SELECT FROM DUAL LIMIT 18446744073709551615 OFFSET 1" end @@ -31,24 +35,24 @@ module Arel sc = Arel::Nodes::UpdateStatement.new sc.relation = Table.new(:users) sc.limit = Nodes::Limit.new(Nodes.build_quoted("omg")) - assert_equal("UPDATE \"users\" LIMIT 'omg'", @visitor.accept(sc)) + assert_equal("UPDATE \"users\" LIMIT 'omg'", compile(sc)) end it 'uses DUAL for empty from' do stmt = Nodes::SelectStatement.new - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like "SELECT FROM DUAL" end describe 'locking' do it 'defaults to FOR UPDATE when locking' do node = Nodes::Lock.new(Arel.sql('FOR UPDATE')) - @visitor.accept(node).must_be_like "FOR UPDATE" + compile(node).must_be_like "FOR UPDATE" end it 'allows a custom string to be used as a lock' do node = Nodes::Lock.new(Arel.sql('LOCK IN SHARE MODE')) - @visitor.accept(node).must_be_like "LOCK IN SHARE MODE" + compile(node).must_be_like "LOCK IN SHARE MODE" end end end diff --git a/test/visitors/test_oracle.rb b/test/visitors/test_oracle.rb index bd22822bca..bb0fe404e8 100644 --- a/test/visitors/test_oracle.rb +++ b/test/visitors/test_oracle.rb @@ -7,13 +7,17 @@ module Arel @visitor = Oracle.new Table.engine.connection_pool end + def compile node + @visitor.accept(node, Collectors::SQLString.new).value + 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 = compile(stmt) sql.must_be_like %{ SELECT #{select} ORDER BY alias_0__ } @@ -26,8 +30,8 @@ module Arel stmt.cores.first.projections << Nodes::SqlLiteral.new(select) stmt.orders << Nodes::SqlLiteral.new('foo') - sql = @visitor.accept(stmt) - sql2 = @visitor.accept(stmt) + sql = compile(stmt) + sql2 = compile(stmt) sql.must_equal sql2 end @@ -37,7 +41,7 @@ module Arel stmt = Nodes::SelectStatement.new stmt.cores.first.projections << Nodes::SqlLiteral.new(select) stmt.orders << Nodes::SqlLiteral.new('foo, bar') - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like %{ SELECT #{select} ORDER BY alias_0__, alias_1__ } @@ -49,7 +53,7 @@ module Arel stmt = Nodes::SelectStatement.new stmt.cores.first.projections << Nodes::SqlLiteral.new(select) stmt.orders << Nodes::SqlLiteral.new('NVL(LOWER(bar, foo), foo) DESC, UPPER(baz)') - sql = @visitor.accept(stmt) + sql = compile(stmt) sql.must_be_like %{ SELECT #{select} ORDER BY alias_0__ DESC, alias_1__ } @@ -60,7 +64,7 @@ module Arel it 'adds a rownum clause' do stmt = Nodes::SelectStatement.new stmt.limit = Nodes::Limit.new(10) - sql = @visitor.accept stmt + sql = compile stmt sql.must_be_like %{ SELECT WHERE ROWNUM <= 10 } end @@ -68,8 +72,8 @@ module Arel stmt = Nodes::SelectStatement.new stmt.orders << Nodes::SqlLiteral.new('foo') stmt.limit = Nodes::Limit.new(10) - sql = @visitor.accept stmt - sql2 = @visitor.accept stmt + sql = compile stmt + sql2 = compile stmt sql.must_equal sql2 end @@ -77,9 +81,9 @@ module Arel stmt = Nodes::SelectStatement.new stmt.orders << Nodes::SqlLiteral.new('foo') stmt.limit = Nodes::Limit.new(10) - sql = @visitor.accept stmt + sql = compile stmt sql.must_be_like %{ - SELECT * FROM (SELECT ORDER BY foo) WHERE ROWNUM <= 10 + SELECT * FROM (SELECT ORDER BY foo ) WHERE ROWNUM <= 10 } end @@ -88,9 +92,9 @@ module Arel stmt.cores.first.set_quantifier = Arel::Nodes::Distinct.new stmt.cores.first.projections << Nodes::SqlLiteral.new('id') stmt.limit = Arel::Nodes::Limit.new(10) - sql = @visitor.accept stmt + sql = compile stmt sql.must_be_like %{ - SELECT * FROM (SELECT DISTINCT id) WHERE ROWNUM <= 10 + SELECT * FROM (SELECT DISTINCT id ) WHERE ROWNUM <= 10 } end @@ -98,11 +102,11 @@ module Arel stmt = Nodes::SelectStatement.new stmt.limit = Nodes::Limit.new(10) stmt.offset = Nodes::Offset.new(10) - sql = @visitor.accept stmt + sql = compile stmt sql.must_be_like %{ SELECT * FROM ( SELECT raw_sql_.*, rownum raw_rnum_ - FROM (SELECT) raw_sql_ + FROM (SELECT ) raw_sql_ WHERE rownum <= 20 ) WHERE raw_rnum_ > 10 @@ -113,8 +117,8 @@ module Arel stmt = Nodes::SelectStatement.new stmt.limit = Nodes::Limit.new(10) stmt.offset = Nodes::Offset.new(10) - sql = @visitor.accept stmt - sql2 = @visitor.accept stmt + sql = compile stmt + sql2 = compile stmt sql.must_equal sql2 end end @@ -123,7 +127,7 @@ module Arel it 'creates a select from subquery with rownum condition' do stmt = Nodes::SelectStatement.new stmt.offset = Nodes::Offset.new(10) - sql = @visitor.accept stmt + sql = compile stmt sql.must_be_like %{ SELECT * FROM ( SELECT raw_sql_.*, rownum raw_rnum_ @@ -139,7 +143,7 @@ module Arel it 'modified except to be minus' do left = Nodes::SqlLiteral.new("SELECT * FROM users WHERE age > 10") right = Nodes::SqlLiteral.new("SELECT * FROM users WHERE age > 20") - sql = @visitor.accept Nodes::Except.new(left, right) + sql = compile Nodes::Except.new(left, right) sql.must_be_like %{ ( SELECT * FROM users WHERE age > 10 MINUS SELECT * FROM users WHERE age > 20 ) } @@ -148,7 +152,7 @@ module Arel describe 'locking' do it 'defaults to FOR UPDATE when locking' do node = Nodes::Lock.new(Arel.sql('FOR UPDATE')) - @visitor.accept(node).must_be_like "FOR UPDATE" + compile(node).must_be_like "FOR UPDATE" end end end diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb index 995e9bf515..3d646a7324 100644 --- a/test/visitors/test_postgres.rb +++ b/test/visitors/test_postgres.rb @@ -9,16 +9,20 @@ module Arel @attr = @table[:id] end + def compile node + @visitor.accept(node, Collectors::SQLString.new).value + end + describe 'locking' do it 'defaults to FOR UPDATE' do - @visitor.accept(Nodes::Lock.new(Arel.sql('FOR UPDATE'))).must_be_like %{ + compile(Nodes::Lock.new(Arel.sql('FOR UPDATE'))).must_be_like %{ FOR UPDATE } end it 'allows a custom string to be used as a lock' do node = Nodes::Lock.new(Arel.sql('FOR SHARE')) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ FOR SHARE } end @@ -29,7 +33,7 @@ module Arel sc.limit = Nodes::Limit.new(Nodes.build_quoted("omg")) sc.cores.first.projections << Arel.sql('DISTINCT ON') sc.orders << Arel.sql("xyz") - sql = @visitor.accept(sc) + sql = compile(sc) assert_match(/LIMIT 'omg'/, sql) assert_equal 1, sql.scan(/LIMIT/).length, 'should have one limit' end @@ -37,19 +41,19 @@ module Arel it 'should support DISTINCT ON' do core = Arel::Nodes::SelectCore.new core.set_quantifier = Arel::Nodes::DistinctOn.new(Arel.sql('aaron')) - assert_match 'DISTINCT ON ( aaron )', @visitor.accept(core) + assert_match 'DISTINCT ON ( aaron )', compile(core) end it 'should support DISTINCT' do core = Arel::Nodes::SelectCore.new core.set_quantifier = Arel::Nodes::Distinct.new - assert_equal 'SELECT DISTINCT', @visitor.accept(core) + assert_equal 'SELECT DISTINCT', compile(core) 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" ILIKE 'foo%' } end @@ -57,7 +61,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" ILIKE 'foo%') } end @@ -66,7 +70,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 ILIKE 'foo%' } end @@ -74,7 +78,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 ILIKE 'foo%') } end @@ -83,7 +87,7 @@ module Arel describe "Nodes::Regexp" do it "should know how to visit" do node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."name" ~ 'foo%' } end @@ -91,7 +95,7 @@ module Arel it 'can handle subqueries' do subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('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" ~ 'foo%') } end @@ -100,7 +104,7 @@ module Arel describe "Nodes::NotRegexp" do it "should know how to visit" do node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')) - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."name" !~ 'foo%' } end @@ -108,7 +112,7 @@ module Arel it 'can handle subqueries' do subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('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" !~ 'foo%') } end diff --git a/test/visitors/test_sqlite.rb b/test/visitors/test_sqlite.rb index c06f554ea4..8fb8e76095 100644 --- a/test/visitors/test_sqlite.rb +++ b/test/visitors/test_sqlite.rb @@ -10,13 +10,13 @@ module Arel it 'defaults limit to -1' do stmt = Nodes::SelectStatement.new stmt.offset = Nodes::Offset.new(1) - sql = @visitor.accept(stmt) + sql = @visitor.accept(stmt, Collectors::SQLString.new).value sql.must_be_like "SELECT LIMIT -1 OFFSET 1" end it 'does not support locking' do node = Nodes::Lock.new(Arel.sql('FOR UPDATE')) - @visitor.accept(node).must_be_nil + assert_equal '', @visitor.accept(node, Collectors::SQLString.new).value end end end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 644951d71c..eb102c1905 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -10,16 +10,20 @@ module Arel @attr = @table[:id] end + def compile node + @visitor.accept(node, Collectors::SQLString.new).value + end + it 'works with BindParams' do node = Nodes::BindParam.new 'omg' - sql = @visitor.accept node + sql = compile node sql.must_be_like 'omg' end it 'can define a dispatch method' do visited = false - viz = Class.new(Arel::Visitors::Visitor) { - define_method(:hello) do |node| + viz = Class.new(Arel::Visitors::Reduce) { + define_method(:hello) do |node, c| visited = true end @@ -28,75 +32,75 @@ module Arel end }.new - viz.accept(@table) + viz.accept(@table, Collectors::SQLString.new) assert visited, 'hello method was called' end it 'should not quote sql literals' do node = @table[Arel.star] - sql = @visitor.accept node + sql = compile node sql.must_be_like '"users".*' end 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 @@ -104,19 +108,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 @@ -124,13 +128,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 @@ -141,19 +145,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 @@ -168,7 +172,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")}'} @@ -176,37 +180,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 @@ -221,31 +225,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 @@ -256,25 +260,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 @@ -282,7 +286,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 @@ -291,7 +295,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 @@ -299,7 +303,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 @@ -308,7 +312,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 @@ -317,52 +321,52 @@ 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 it 'can handle three dot ranges' do node = @attr.in 1...3 - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" >= 1 AND "users"."id" < 3 } end 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 @@ -383,29 +387,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 @@ -414,59 +418,59 @@ 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 describe "Nodes::NotIn" do it "should know how to visit" do node = @attr.not_in [1, 2, 3] - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" NOT IN (1, 2, 3) } end it "should return 1=1 when empty right which is always true" do node = @attr.not_in [] - @visitor.accept(node).must_equal '1=1' + compile(node).must_equal '1=1' end 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 it 'can handle three 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 it 'can handle ranges bounded by infinity' do node = @attr.not_in 1..Float::INFINITY - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "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 table = Table.new(:users) subquery = table.project(:id).where(table[:name].eq('Aaron')) node = @attr.not_in subquery - @visitor.accept(node).must_be_like %{ + compile(node).must_be_like %{ "users"."id" NOT IN (SELECT id FROM "users" WHERE "users"."name" = 'Aaron') } end @@ -487,21 +491,21 @@ 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 describe 'Constants' do it "should handle true" do test = Table.new(:users).create_true - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ TRUE } end it "should handle false" do test = Table.new(:users).create_false - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ FALSE } end @@ -510,7 +514,7 @@ module Arel describe 'TableAlias' do it "should use the underlying table for checking columns" do test = Table.new(:users).alias('zomgusers')[:id].eq '3' - @visitor.accept(test).must_be_like %{ + compile(test).must_be_like %{ "zomgusers"."id" = 3 } end @@ -522,7 +526,7 @@ module Arel core.set_quantifier = Arel::Nodes::DistinctOn.new(Arel.sql('aaron')) assert_raises(NotImplementedError) do - @visitor.accept(core) + compile(core) end end end @@ -532,7 +536,7 @@ module Arel node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo%')) assert_raises(NotImplementedError) do - @visitor.accept(node) + compile(node) end end end @@ -542,7 +546,7 @@ module Arel node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo%')) assert_raises(NotImplementedError) do - @visitor.accept(node) + compile(node) end end end |