aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/sqlite3/quoting_test.rb
blob: 40b58e86bf1cef70c475f99860916458802f36d3 (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
# frozen_string_literal: true

require "cases/helper"
require "bigdecimal"
require "securerandom"

class SQLite3QuotingTest < ActiveRecord::SQLite3TestCase
  def setup
    @conn = ActiveRecord::Base.connection
    @initial_represent_boolean_as_integer = ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
  end

  def teardown
    ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = @initial_represent_boolean_as_integer
  end

  def test_type_cast_binary_encoding_without_logger
    @conn.extend(Module.new { def logger; end })
    binary = SecureRandom.hex
    expected = binary.dup.encode!(Encoding::UTF_8)
    assert_equal expected, @conn.type_cast(binary)
  end

  def test_type_cast_true
    ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = false
    assert_equal "t", @conn.type_cast(true)

    ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
    assert_equal 1, @conn.type_cast(true)
  end

  def test_type_cast_false
    ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = false
    assert_equal "f", @conn.type_cast(false)

    ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
    assert_equal 0, @conn.type_cast(false)
  end

  def test_type_cast_bigdecimal
    bd = BigDecimal "10.0"
    assert_equal bd.to_f, @conn.type_cast(bd)
  end

  def test_quoting_binary_strings
    value = "hello".encode("ascii-8bit")
    type = ActiveRecord::Type::String.new

    assert_equal "'hello'", @conn.quote(type.serialize(value))
  end

  def test_quoted_time_returns_date_qualified_time
    value = ::Time.utc(2000, 1, 1, 12, 30, 0, 999999)
    type = ActiveRecord::Type::Time.new

    assert_equal "'2000-01-01 12:30:00.999999'", @conn.quote(type.serialize(value))
  end

  def test_quoted_time_normalizes_date_qualified_time
    value = ::Time.utc(2018, 3, 11, 12, 30, 0, 999999)
    type = ActiveRecord::Type::Time.new

    assert_equal "'2000-01-01 12:30:00.999999'", @conn.quote(type.serialize(value))
  end

  def test_quoted_time_dst_utc
    with_env_tz "America/New_York" do
      with_timezone_config default: :utc do
        t = Time.new(2000, 7, 1, 0, 0, 0, "+04:30")

        expected = t.change(year: 2000, month: 1, day: 1)
        expected = expected.getutc.to_s(:db).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ")

        assert_equal expected, @conn.quoted_time(t)
      end
    end
  end

  def test_quoted_time_dst_local
    with_env_tz "America/New_York" do
      with_timezone_config default: :local do
        t = Time.new(2000, 7, 1, 0, 0, 0, "+04:30")

        expected = t.change(year: 2000, month: 1, day: 1)
        expected = expected.getlocal.to_s(:db).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ")

        assert_equal expected, @conn.quoted_time(t)
      end
    end
  end
end