aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/quoting_test.rb
blob: 80ee74e41ec2a2fdb8b4b962cdead2c96c3f7fda (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require "cases/helper"

module ActiveRecord
  module ConnectionAdapters
    class QuotingTest < ActiveRecord::TestCase
      class FakeColumn < ActiveRecord::ConnectionAdapters::Column
        attr_accessor :type

        def initialize type
          @type = type
        end
      end

      def setup
        @quoter = Class.new { include Quoting }.new
      end

      def test_quoted_true
        assert_equal "'t'", @quoter.quoted_true
      end

      def test_quoted_false
        assert_equal "'f'", @quoter.quoted_false
      end

      def test_quote_column_name
        assert_equal "foo", @quoter.quote_column_name('foo')
      end

      def test_quote_table_name
        assert_equal "foo", @quoter.quote_table_name('foo')
      end

      def test_quote_table_name_calls_quote_column_name
        @quoter.extend(Module.new {
          def quote_column_name(string)
            'lol'
          end
        })
        assert_equal 'lol', @quoter.quote_table_name('foo')
      end

      def test_quote_string
        assert_equal "''", @quoter.quote_string("'")
        assert_equal "\\\\", @quoter.quote_string("\\")
        assert_equal "hi''i", @quoter.quote_string("hi'i")
        assert_equal "hi\\\\i", @quoter.quote_string("hi\\i")
      end

      def test_quoted_date
        t = Date.today
        assert_equal t.to_s(:db), @quoter.quoted_date(t)
      end

      def test_quoted_time_utc
        before = ActiveRecord::Base.default_timezone
        ActiveRecord::Base.default_timezone = :utc
        t = Time.now
        assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t)
      ensure
        ActiveRecord::Base.default_timezone = before
      end

      def test_quoted_time_local
        before = ActiveRecord::Base.default_timezone
        ActiveRecord::Base.default_timezone = :local
        t = Time.now
        assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t)
      ensure
        ActiveRecord::Base.default_timezone = before
      end

      def test_quoted_time_crazy
        before = ActiveRecord::Base.default_timezone
        ActiveRecord::Base.default_timezone = :asdfasdf
        t = Time.now
        assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t)
      ensure
        ActiveRecord::Base.default_timezone = before
      end

      def test_quoted_datetime_utc
        before = ActiveRecord::Base.default_timezone
        ActiveRecord::Base.default_timezone = :utc
        t = DateTime.now
        assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t)
      ensure
        ActiveRecord::Base.default_timezone = before
      end

      ###
      # DateTime doesn't define getlocal, so make sure it does nothing
      def test_quoted_datetime_local
        before = ActiveRecord::Base.default_timezone
        ActiveRecord::Base.default_timezone = :local
        t = DateTime.now
        assert_equal t.to_s(:db), @quoter.quoted_date(t)
      ensure
        ActiveRecord::Base.default_timezone = before
      end

      def test_quote_with_quoted_id
        assert_equal 1, @quoter.quote(Struct.new(:quoted_id).new(1), nil)
        assert_equal 1, @quoter.quote(Struct.new(:quoted_id).new(1), 'foo')
      end

      def test_quote_nil
        assert_equal 'NULL', @quoter.quote(nil, nil)
        assert_equal 'NULL', @quoter.quote(nil, 'foo')
      end

      def test_quote_true
        assert_equal @quoter.quoted_true, @quoter.quote(true, nil)
        assert_equal '1', @quoter.quote(true, Struct.new(:type).new(:integer))
      end

      def test_quote_false
        assert_equal @quoter.quoted_false, @quoter.quote(false, nil)
        assert_equal '0', @quoter.quote(false, Struct.new(:type).new(:integer))
      end

      def test_quote_float
        float = 1.2
        assert_equal float.to_s, @quoter.quote(float, nil)
        assert_equal float.to_s, @quoter.quote(float, Object.new)
      end

      def test_quote_fixnum
        fixnum = 1
        assert_equal fixnum.to_s, @quoter.quote(fixnum, nil)
        assert_equal fixnum.to_s, @quoter.quote(fixnum, Object.new)
      end

      def test_quote_bignum
        bignum = 1 << 100
        assert_equal bignum.to_s, @quoter.quote(bignum, nil)
        assert_equal bignum.to_s, @quoter.quote(bignum, Object.new)
      end

      def test_quote_bigdecimal
        bigdec = BigDecimal.new((1 << 100).to_s)
        assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, nil)
        assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, Object.new)
      end

      def test_dates_and_times
        @quoter.extend(Module.new { def quoted_date(value) 'lol' end })
        assert_equal "'lol'", @quoter.quote(Date.today, nil)
        assert_equal "'lol'", @quoter.quote(Date.today, Object.new)
        assert_equal "'lol'", @quoter.quote(Time.now, nil)
        assert_equal "'lol'", @quoter.quote(Time.now, Object.new)
        assert_equal "'lol'", @quoter.quote(DateTime.now, nil)
        assert_equal "'lol'", @quoter.quote(DateTime.now, Object.new)
      end

      def test_crazy_object
        crazy = Class.new.new
        expected = "'#{YAML.dump(crazy)}'"
        assert_equal expected, @quoter.quote(crazy, nil)
        assert_equal expected, @quoter.quote(crazy, Object.new)
      end

      def test_crazy_object_calls_quote_string
        crazy = Class.new { def initialize; @lol = 'lo\l' end }.new
        assert_match "lo\\\\l", @quoter.quote(crazy, nil)
        assert_match "lo\\\\l", @quoter.quote(crazy, Object.new)
      end

      def test_quote_string_no_column
        assert_equal "'lo\\\\l'", @quoter.quote('lo\l', nil)
      end

      def test_quote_as_mb_chars_no_column
        string = ActiveSupport::Multibyte::Chars.new('lo\l')
        assert_equal "'lo\\\\l'", @quoter.quote(string, nil)
      end

      def test_quote_string_int_column
        assert_equal "1", @quoter.quote('1', FakeColumn.new(:integer))
        assert_equal "1", @quoter.quote('1.2', FakeColumn.new(:integer))
      end

      def test_quote_string_float_column
        assert_equal "1.0", @quoter.quote('1', FakeColumn.new(:float))
        assert_equal "1.2", @quoter.quote('1.2', FakeColumn.new(:float))
      end

      def test_quote_as_mb_chars_binary_column
        string = ActiveSupport::Multibyte::Chars.new('lo\l')
        assert_equal "'lo\\\\l'", @quoter.quote(string, FakeColumn.new(:binary))
      end

      def test_quote_binary_without_string_to_binary
        assert_equal "'lo\\\\l'", @quoter.quote('lo\l', FakeColumn.new(:binary))
      end

      def test_quote_binary_with_string_to_binary
        col = Class.new(FakeColumn) {
          def string_to_binary(value)
            'foo'
          end
        }.new(:binary)
        assert_equal "'foo'", @quoter.quote('lo\l', col)
      end

      def test_quote_as_mb_chars_binary_column_with_string_to_binary
        col = Class.new(FakeColumn) {
          def string_to_binary(value)
            'foo'
          end
        }.new(:binary)
        string = ActiveSupport::Multibyte::Chars.new('lo\l')
        assert_equal "'foo'", @quoter.quote(string, col)
      end

      def test_string_with_crazy_column
        assert_equal "'lo\\\\l'", @quoter.quote('lo\l', FakeColumn.new(:foo))
      end
    end
  end
end