aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb
blob: bc823fd07201c52b676c569ac54415a445de76d6 (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
# frozen_string_literal: true

require "cases/helper"
require "support/connection_helper"

if current_adapter?(:Mysql2Adapter)
  module ActiveRecord
    module ConnectionAdapters
      class MysqlTypeLookupTest < ActiveRecord::TestCase
        include ConnectionHelper

        setup do
          @connection = ActiveRecord::Base.connection
        end

        def teardown
          reset_connection
        end

        def test_boolean_types
          emulate_booleans(true) do
            assert_lookup_type :boolean, "tinyint(1)"
            assert_lookup_type :boolean, "TINYINT(1)"
          end
        end

        def test_string_types
          assert_lookup_type :string, "enum('one', 'two', 'three')"
          assert_lookup_type :string, "ENUM('one', 'two', 'three')"
          assert_lookup_type :string, "enum ('one', 'two', 'three')"
          assert_lookup_type :string, "ENUM ('one', 'two', 'three')"
          assert_lookup_type :string, "set('one', 'two', 'three')"
          assert_lookup_type :string, "SET('one', 'two', 'three')"
          assert_lookup_type :string, "set ('one', 'two', 'three')"
          assert_lookup_type :string, "SET ('one', 'two', 'three')"
        end

        def test_set_type_with_value_matching_other_type
          assert_lookup_type :string, "SET('unicode', '8bit', 'none', 'time')"
        end

        def test_enum_type_with_value_matching_other_type
          assert_lookup_type :string, "ENUM('unicode', '8bit', 'none')"
        end

        def test_binary_types
          assert_lookup_type :binary, "bit"
          assert_lookup_type :binary, "BIT"
        end

        def test_integer_types
          emulate_booleans(false) do
            assert_lookup_type :integer, "tinyint(1)"
            assert_lookup_type :integer, "TINYINT(1)"
            assert_lookup_type :integer, "year"
            assert_lookup_type :integer, "YEAR"
          end
        end

        private
          def assert_lookup_type(type, lookup)
            cast_type = @connection.send(:type_map).lookup(lookup)
            assert_equal type, cast_type.type
          end

          def emulate_booleans(value)
            old_emulate_booleans = @connection.emulate_booleans
            change_emulate_booleans(value)
            yield
          ensure
            change_emulate_booleans(old_emulate_booleans)
          end

          def change_emulate_booleans(value)
            @connection.emulate_booleans = value
            @connection.clear_cache!
          end
      end
    end
  end
end