2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2010-10-11 20:08:40 -04:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
module ConnectionAdapters
|
|
|
|
class QuotingTest < ActiveRecord::TestCase
|
|
|
|
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
|
|
|
|
|
2010-10-11 20:40:36 -04:00
|
|
|
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
|
|
|
|
|
2010-10-11 20:08:40 -04:00
|
|
|
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
|
2010-10-11 20:40:36 -04:00
|
|
|
|
|
|
|
def test_quoted_date
|
|
|
|
t = Date.today
|
|
|
|
assert_equal t.to_s(:db), @quoter.quoted_date(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_quoted_time_utc
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config default: :utc do
|
2013-07-28 23:37:55 -04:00
|
|
|
t = Time.now
|
|
|
|
assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t)
|
|
|
|
end
|
2010-10-11 20:40:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quoted_time_local
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config default: :local do
|
2013-07-28 23:37:55 -04:00
|
|
|
t = Time.now
|
|
|
|
assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t)
|
|
|
|
end
|
2010-10-11 20:40:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quoted_time_crazy
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config default: :asdfasdf do
|
2013-07-28 23:37:55 -04:00
|
|
|
t = Time.now
|
|
|
|
assert_equal t.getlocal.to_s(:db), @quoter.quoted_date(t)
|
|
|
|
end
|
2010-10-11 20:40:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quoted_datetime_utc
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config default: :utc do
|
2013-07-28 23:37:55 -04:00
|
|
|
t = DateTime.now
|
|
|
|
assert_equal t.getutc.to_s(:db), @quoter.quoted_date(t)
|
|
|
|
end
|
2010-10-11 20:40:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# DateTime doesn't define getlocal, so make sure it does nothing
|
|
|
|
def test_quoted_datetime_local
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config default: :local do
|
2013-07-28 23:37:55 -04:00
|
|
|
t = DateTime.now
|
|
|
|
assert_equal t.to_s(:db), @quoter.quoted_date(t)
|
|
|
|
end
|
2010-10-11 20:40:36 -04:00
|
|
|
end
|
2010-10-11 20:51:48 -04:00
|
|
|
|
|
|
|
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)
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal '1', @quoter.quote(true, Type::Integer.new)
|
2010-10-11 20:51:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quote_false
|
|
|
|
assert_equal @quoter.quoted_false, @quoter.quote(false, nil)
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal '0', @quoter.quote(false, Type::Integer.new)
|
2010-10-11 20:51:48 -04:00
|
|
|
end
|
2010-10-11 20:57:47 -04:00
|
|
|
|
|
|
|
def test_quote_float
|
|
|
|
float = 1.2
|
|
|
|
assert_equal float.to_s, @quoter.quote(float, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal float.to_s, @quoter.quote(float, Object.new)
|
2010-10-11 20:57:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quote_fixnum
|
|
|
|
fixnum = 1
|
|
|
|
assert_equal fixnum.to_s, @quoter.quote(fixnum, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal fixnum.to_s, @quoter.quote(fixnum, Object.new)
|
2010-10-11 20:57:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quote_bignum
|
|
|
|
bignum = 1 << 100
|
|
|
|
assert_equal bignum.to_s, @quoter.quote(bignum, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal bignum.to_s, @quoter.quote(bignum, Object.new)
|
2010-10-11 20:57:47 -04:00
|
|
|
end
|
2010-10-12 14:08:10 -04:00
|
|
|
|
|
|
|
def test_quote_bigdecimal
|
|
|
|
bigdec = BigDecimal.new((1 << 100).to_s)
|
|
|
|
assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal bigdec.to_s('F'), @quoter.quote(bigdec, Object.new)
|
2010-10-12 14:08:10 -04:00
|
|
|
end
|
2010-10-12 14:21:35 -04:00
|
|
|
|
|
|
|
def test_dates_and_times
|
|
|
|
@quoter.extend(Module.new { def quoted_date(value) 'lol' end })
|
|
|
|
assert_equal "'lol'", @quoter.quote(Date.today, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal "'lol'", @quoter.quote(Date.today, Object.new)
|
2010-10-12 14:21:35 -04:00
|
|
|
assert_equal "'lol'", @quoter.quote(Time.now, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal "'lol'", @quoter.quote(Time.now, Object.new)
|
2010-10-12 14:21:35 -04:00
|
|
|
assert_equal "'lol'", @quoter.quote(DateTime.now, nil)
|
2013-02-27 11:46:40 -05:00
|
|
|
assert_equal "'lol'", @quoter.quote(DateTime.now, Object.new)
|
2010-10-12 14:21:35 -04:00
|
|
|
end
|
2010-10-12 14:25:20 -04:00
|
|
|
|
|
|
|
def test_crazy_object
|
2011-04-13 14:41:34 -04:00
|
|
|
crazy = Class.new.new
|
|
|
|
expected = "'#{YAML.dump(crazy)}'"
|
|
|
|
assert_equal expected, @quoter.quote(crazy, nil)
|
|
|
|
assert_equal expected, @quoter.quote(crazy, Object.new)
|
2010-10-12 14:25:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_crazy_object_calls_quote_string
|
2011-04-13 14:41:34 -04:00
|
|
|
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)
|
2010-10-12 14:25:20 -04:00
|
|
|
end
|
2010-10-12 14:29:28 -04:00
|
|
|
|
|
|
|
def test_quote_string_no_column
|
|
|
|
assert_equal "'lo\\\\l'", @quoter.quote('lo\l', nil)
|
|
|
|
end
|
|
|
|
|
2010-10-12 14:38:07 -04:00
|
|
|
def test_quote_as_mb_chars_no_column
|
|
|
|
string = ActiveSupport::Multibyte::Chars.new('lo\l')
|
|
|
|
assert_equal "'lo\\\\l'", @quoter.quote(string, nil)
|
|
|
|
end
|
|
|
|
|
2010-10-12 14:29:28 -04:00
|
|
|
def test_quote_string_int_column
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal "1", @quoter.quote('1', Type::Integer.new)
|
|
|
|
assert_equal "1", @quoter.quote('1.2', Type::Integer.new)
|
2010-10-12 14:29:28 -04:00
|
|
|
end
|
2010-10-12 14:30:34 -04:00
|
|
|
|
|
|
|
def test_quote_string_float_column
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal "1.0", @quoter.quote('1', Type::Float.new)
|
|
|
|
assert_equal "1.2", @quoter.quote('1.2', Type::Float.new)
|
2010-10-12 14:30:34 -04:00
|
|
|
end
|
2010-10-12 14:34:21 -04:00
|
|
|
|
2010-10-12 14:38:07 -04:00
|
|
|
def test_quote_as_mb_chars_binary_column
|
|
|
|
string = ActiveSupport::Multibyte::Chars.new('lo\l')
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal "'lo\\\\l'", @quoter.quote(string, Type::Binary.new)
|
2010-10-12 14:38:07 -04:00
|
|
|
end
|
|
|
|
|
2010-10-12 14:34:21 -04:00
|
|
|
def test_quote_binary_without_string_to_binary
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal "'lo\\\\l'", @quoter.quote('lo\l', Type::Binary.new)
|
2010-10-12 14:34:21 -04:00
|
|
|
end
|
|
|
|
|
2010-10-12 14:43:50 -04:00
|
|
|
def test_string_with_crazy_column
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal "'lo\\\\l'", @quoter.quote('lo\l')
|
2010-10-12 14:43:50 -04:00
|
|
|
end
|
2012-07-04 23:39:16 -04:00
|
|
|
|
|
|
|
def test_quote_duration
|
|
|
|
assert_equal "1800", @quoter.quote(30.minutes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_quote_duration_int_column
|
2014-05-25 12:30:13 -04:00
|
|
|
assert_equal "7200", @quoter.quote(2.hours, Type::Integer.new)
|
2012-07-04 23:39:16 -04:00
|
|
|
end
|
2010-10-11 20:08:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|