aboutsummaryrefslogtreecommitdiffstats
path: root/test/collectors/test_sql_string.rb
blob: 0185f2ab1738a29d8aa1bab0d413f13f9b50cdcf (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
# frozen_string_literal: true
require 'helper'

module Arel
  module Collectors
    class TestSqlString < Arel::Test
      def setup
        @conn = FakeRecord::Base.new
        @visitor = Visitors::ToSql.new @conn.connection
        super
      end

      def collect node
        @visitor.accept(node, Collectors::SQLString.new)
      end

      def compile node
        collect(node).value
      end

      def ast_with_binds bv
        table = Table.new(:users)
        manager = Arel::SelectManager.new table
        manager.where(table[:age].eq(bv))
        manager.where(table[:name].eq(bv))
        manager.ast
      end

      def test_compile
        bv = Nodes::BindParam.new(1)
        collector = collect ast_with_binds bv

        sql = collector.compile ["hello", "world"]
        assert_equal 'SELECT FROM "users" WHERE "users"."age" = ? AND "users"."name" = ?', sql
      end

      def test_returned_sql_uses_utf8_encoding
        bv = Nodes::BindParam.new(1)
        collector = collect ast_with_binds bv

        sql = collector.compile ["hello", "world"]
        assert_equal sql.encoding, Encoding::UTF_8
      end
    end
  end
end