aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel/collectors/composite_test.rb
blob: 545637496fa3c6355e35f245cf0c7874c6d9bf78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

require_relative "../helper"

require "arel/collectors/bind"
require "arel/collectors/composite"

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