aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb
blob: 774380d7e06e6676015ca6b5be8e0a7a102e88ec (plain) (tree)
1
2
3
4
5
6
7
8
9

                             
                      
                                   
 
                                   


                                                        

                                


                                                     
 



                          




                                                     
           
 


                                                                   

                                                                    

                                                                  

                                                                   
           
 


                                                                              
 
                                                         
                                                                               
           
 



                                           
 






                                                     
           
 
               
                                              
                                                                  

                                             
 






                                                               
 



                                                



         
# 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', 'time')"
        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