aboutsummaryrefslogtreecommitdiffstats
path: root/test/collectors/test_composite.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-07-21 09:41:07 -0400
committerSean Griffin <sean@seantheprogrammer.com>2017-07-21 09:41:07 -0400
commitecec50da1d709f9b442c88c2f53a8065e2ed6548 (patch)
treea17e078fd51f7d83c96b2243452e1d261ffd2b63 /test/collectors/test_composite.rb
parent7ca5c37f484edebd169e4136331500d4b5c31cbe (diff)
downloadrails-ecec50da1d709f9b442c88c2f53a8065e2ed6548.tar.gz
rails-ecec50da1d709f9b442c88c2f53a8065e2ed6548.tar.bz2
rails-ecec50da1d709f9b442c88c2f53a8065e2ed6548.zip
Add a collector to grab the bind values off the AST
Now that the bind values are being stored on the actual AST, we need a way to pull them off into the array that we were previously maintaining separately. This requires a full walk of the AST. This is an expensive operation, so I've also added a visitor for delegating to more than one visitor in a single pass.
Diffstat (limited to 'test/collectors/test_composite.rb')
-rw-r--r--test/collectors/test_composite.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/collectors/test_composite.rb b/test/collectors/test_composite.rb
new file mode 100644
index 0000000000..b47c37db73
--- /dev/null
+++ b/test/collectors/test_composite.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+require 'helper'
+
+require 'arel/collectors/bind'
+require 'arel/collectors/composite'
+require 'arel/collectors/sql_string'
+
+module Arel
+ module Collectors
+ class TestComposite < Arel::Test
+ def setup
+ @conn = FakeRecord::Base.new
+ @visitor = Visitors::ToSql.new @conn.connection
+ super
+ end
+
+ def collect node
+ sql_collector = Collectors::SQLString.new
+ bind_collector = Collectors::Bind.new
+ collector = Collectors::Composite.new(sql_collector, bind_collector)
+ @visitor.accept(node, collector)
+ end
+
+ def compile node
+ collect(node).value
+ end
+
+ def ast_with_binds bvs
+ table = Table.new(:users)
+ manager = Arel::SelectManager.new table
+ manager.where(table[:age].eq(Nodes::BindParam.new(bvs.shift)))
+ manager.where(table[:name].eq(Nodes::BindParam.new(bvs.shift)))
+ manager.ast
+ end
+
+ def test_composite_collector_performs_multiple_collections_at_once
+ sql, binds = compile(ast_with_binds(["hello", "world"]))
+ assert_equal 'SELECT FROM "users" WHERE "users"."age" = ? AND "users"."name" = ?', sql
+ assert_equal ["hello", "world"], binds
+
+ sql, binds = compile(ast_with_binds(["hello2", "world3"]))
+ assert_equal 'SELECT FROM "users" WHERE "users"."age" = ? AND "users"."name" = ?', sql
+ assert_equal ["hello2", "world3"], binds
+ end
+ end
+ end
+end