aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/sqlite3/quoting_test.rb
blob: 51f01c262dd5c5a6b3f516ca4aeaff33e199a848 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require File.expand_path('../../../helper', __FILE__)
require 'bigdecimal'
require 'yaml'

module ActiveRecord
  module ConnectionAdapters
    class SQLiteAdapter
      class QuotingTest < ActiveRecord::TestCase
        def setup
          @conn = Base.sqlite3_connection :database => ':memory:',
            :adapter => 'sqlite3',
            :timeout => 100
        end

        def test_type_cast_symbol
          assert_equal 'foo', @conn.type_cast(:foo, nil)
        end

        def test_type_cast_date
          date = Date.today
          expected = @conn.quoted_date(date)
          assert_equal expected, @conn.type_cast(date, nil)
        end

        def test_type_cast_time
          time = Time.now
          expected = @conn.quoted_date(time)
          assert_equal expected, @conn.type_cast(time, nil)
        end

        def test_type_cast_numeric
          assert_equal 10, @conn.type_cast(10, nil)
          assert_equal 2.2, @conn.type_cast(2.2, nil)
        end

        def test_type_cast_nil
          assert_equal nil, @conn.type_cast(nil, nil)
        end

        def test_type_cast_true
          c = Column.new(nil, 1, 'int')
          assert_equal 't', @conn.type_cast(true, nil)
          assert_equal 1, @conn.type_cast(true, c)
        end

        def test_type_cast_false
          c = Column.new(nil, 1, 'int')
          assert_equal 'f', @conn.type_cast(false, nil)
          assert_equal 0, @conn.type_cast(false, c)
        end

        def test_type_cast_string
          assert_equal '10', @conn.type_cast('10', nil)

          c = Column.new(nil, 1, 'int')
          assert_equal 10, @conn.type_cast('10', c)

          c = Column.new(nil, 1, 'float')
          assert_equal 10.1, @conn.type_cast('10.1', c)

          c = Column.new(nil, 1, 'binary')
          assert_equal '10.1', @conn.type_cast('10.1', c)

          c = Column.new(nil, 1, 'date')
          assert_equal '10.1', @conn.type_cast('10.1', c)
        end

        def test_type_cast_bigdecimal
          bd = BigDecimal.new '10.0'
          assert_equal bd.to_s('F'), @conn.type_cast(bd, nil)
        end

        def test_type_cast_unknown
          obj = Class.new.new
          assert_equal YAML.dump(obj), @conn.type_cast(obj, nil)
        end

        def test_quoted_id
          quoted_id_obj = Class.new {
            def quoted_id
              "'zomg'"
            end

            def id
              10
            end
          }.new
          assert_equal 10, @conn.type_cast(quoted_id_obj, nil)
        end
      end
    end
  end
end