connection_test.rb 6.6 KB
Newer Older
1
require "cases/helper"
2
require 'support/connection_helper'
G
Guo Xiang Tan 已提交
3
require 'support/ddl_helper'
4

5
class MysqlConnectionTest < ActiveRecord::MysqlTestCase
6
  include ConnectionHelper
G
Guo Xiang Tan 已提交
7 8
  include DdlHelper

9 10 11
  class Klass < ActiveRecord::Base
  end

12
  def setup
13
    super
J
Jon Leighton 已提交
14
    @connection = ActiveRecord::Base.connection
15 16
  end

17 18
  def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
    run_without_connection do |orig_connection|
J
Jon Leighton 已提交
19 20
      ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true}))
      assert ActiveRecord::Base.connection.raw_connection.reconnect
21 22 23
    end
  end

24 25 26 27
  unless ARTest.connection_config['arunit']['socket']
    def test_connect_with_url
      run_without_connection do
        ar_config = ARTest.connection_config['arunit']
28

29 30 31 32
        url = "mysql://#{ar_config["username"]}@localhost/#{ar_config["database"]}"
        Klass.establish_connection(url)
        assert_equal ar_config['database'], Klass.connection.current_database
      end
A
Aaron Patterson 已提交
33 34 35
    end
  end

36 37
  def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
    run_without_connection do |orig_connection|
J
Jon Leighton 已提交
38 39
      ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false}))
      assert !ActiveRecord::Base.connection.raw_connection.reconnect
40 41 42
    end
  end

43 44 45 46 47
  def test_no_automatic_reconnection_after_timeout
    assert @connection.active?
    @connection.update('set @@wait_timeout=1')
    sleep 2
    assert !@connection.active?
48 49

    # Repair all fixture connections so other tests won't break.
50
    @fixture_connections.each(&:verify!)
51 52 53 54 55 56 57 58 59 60 61 62 63 64
  end

  def test_successful_reconnection_after_timeout_with_manual_reconnect
    assert @connection.active?
    @connection.update('set @@wait_timeout=1')
    sleep 2
    @connection.reconnect!
    assert @connection.active?
  end

  def test_successful_reconnection_after_timeout_with_verify
    assert @connection.active?
    @connection.update('set @@wait_timeout=1')
    sleep 2
65
    @connection.verify!
66
    assert @connection.active?
M
Mark Turner 已提交
67
  end
68

A
Aaron Patterson 已提交
69
  def test_bind_value_substitute
70
    bind_param = @connection.substitute_at('foo')
S
Sean Griffin 已提交
71
    assert_equal Arel.sql('?'), bind_param.to_sql
A
Aaron Patterson 已提交
72 73 74
  end

  def test_exec_no_binds
G
Guo Xiang Tan 已提交
75 76 77 78 79
    with_example_table do
      result = @connection.exec_query('SELECT id, data FROM ex')
      assert_equal 0, result.rows.length
      assert_equal 2, result.columns.length
      assert_equal %w{ id data }, result.columns
A
Aaron Patterson 已提交
80

G
Guo Xiang Tan 已提交
81
      @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
82

G
Guo Xiang Tan 已提交
83 84 85 86 87
      # if there are no bind parameters, it will return a string (due to
      # the libmysql api)
      result = @connection.exec_query('SELECT id, data FROM ex')
      assert_equal 1, result.rows.length
      assert_equal 2, result.columns.length
A
Aaron Patterson 已提交
88

G
Guo Xiang Tan 已提交
89 90
      assert_equal [['1', 'foo']], result.rows
    end
A
Aaron Patterson 已提交
91 92 93
  end

  def test_exec_with_binds
G
Guo Xiang Tan 已提交
94 95 96
    with_example_table do
      @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
      result = @connection.exec_query(
S
Sean Griffin 已提交
97
        'SELECT id, data FROM ex WHERE id = ?', nil, [ActiveRecord::Relation::QueryAttribute.new("id", 1, ActiveRecord::Type::Value.new)])
A
Aaron Patterson 已提交
98

G
Guo Xiang Tan 已提交
99 100
      assert_equal 1, result.rows.length
      assert_equal 2, result.columns.length
A
Aaron Patterson 已提交
101

G
Guo Xiang Tan 已提交
102 103
      assert_equal [[1, 'foo']], result.rows
    end
A
Aaron Patterson 已提交
104 105 106
  end

  def test_exec_typecasts_bind_vals
G
Guo Xiang Tan 已提交
107 108
    with_example_table do
      @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
S
Sean Griffin 已提交
109
      bind = ActiveRecord::Relation::QueryAttribute.new("id", "1-fuu", ActiveRecord::Type::Integer.new)
A
Aaron Patterson 已提交
110

G
Guo Xiang Tan 已提交
111
      result = @connection.exec_query(
S
Sean Griffin 已提交
112
        'SELECT id, data FROM ex WHERE id = ?', nil, [bind])
A
Aaron Patterson 已提交
113

G
Guo Xiang Tan 已提交
114 115
      assert_equal 1, result.rows.length
      assert_equal 2, result.columns.length
A
Aaron Patterson 已提交
116

G
Guo Xiang Tan 已提交
117 118
      assert_equal [[1, 'foo']], result.rows
    end
A
Aaron Patterson 已提交
119 120
  end

121
  # Test that MySQL allows multiple results for stored procedures
M
Marcin Raczkowski 已提交
122
  if defined?(Mysql) && Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
123
    def test_multi_results
J
Jon Leighton 已提交
124
      rows = ActiveRecord::Base.connection.select_rows('CALL ten();')
125
      assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}"
126
      assert @connection.active?, "Bad connection use by 'MysqlAdapter.select_rows'"
127
    end
128
  end
129

130 131 132 133 134
  def test_mysql_connection_collation_is_configured
    assert_equal 'utf8_unicode_ci', @connection.show_variable('collation_connection')
    assert_equal 'utf8_general_ci', ARUnit2Model.connection.show_variable('collation_connection')
  end

135 136 137 138 139
  def test_mysql_default_in_strict_mode
    result = @connection.exec_query "SELECT @@SESSION.sql_mode"
    assert_equal [["STRICT_ALL_TABLES"]], result.rows
  end

140
  def test_mysql_strict_mode_disabled
141
    run_without_connection do |orig_connection|
J
Jon Leighton 已提交
142
      ActiveRecord::Base.establish_connection(orig_connection.merge({:strict => false}))
143 144
      result = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.sql_mode"
      assert_equal [['']], result.rows
145 146 147
    end
  end

148 149 150 151 152 153 154 155 156
  def test_mysql_strict_mode_specified_default
    run_without_connection do |orig_connection|
      ActiveRecord::Base.establish_connection(orig_connection.merge({strict: :default}))
      global_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@GLOBAL.sql_mode"
      session_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.sql_mode"
      assert_equal global_sql_mode.rows, session_sql_mode.rows
    end
  end

157 158 159 160 161 162 163 164
  def test_mysql_set_session_variable
    run_without_connection do |orig_connection|
      ActiveRecord::Base.establish_connection(orig_connection.deep_merge({:variables => {:default_week_format => 3}}))
      session_mode = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.DEFAULT_WEEK_FORMAT"
      assert_equal 3, session_mode.rows.first.first.to_i
    end
  end

165
  def test_mysql_sql_mode_variable_overrides_strict_mode
166 167 168 169 170 171 172
    run_without_connection do |orig_connection|
      ActiveRecord::Base.establish_connection(orig_connection.deep_merge(variables: { 'sql_mode' => 'ansi' }))
      result = ActiveRecord::Base.connection.exec_query 'SELECT @@SESSION.sql_mode'
      assert_not_equal [['STRICT_ALL_TABLES']], result.rows
    end
  end

173 174 175 176 177 178 179 180 181
  def test_mysql_set_session_variable_to_default
    run_without_connection do |orig_connection|
      ActiveRecord::Base.establish_connection(orig_connection.deep_merge({:variables => {:default_week_format => :default}}))
      global_mode = ActiveRecord::Base.connection.exec_query "SELECT @@GLOBAL.DEFAULT_WEEK_FORMAT"
      session_mode = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.DEFAULT_WEEK_FORMAT"
      assert_equal global_mode.rows, session_mode.rows
    end
  end

182 183
  private

G
Guo Xiang Tan 已提交
184 185
  def with_example_table(&block)
    definition ||= <<-SQL
186
      `id` int auto_increment PRIMARY KEY,
G
Guo Xiang Tan 已提交
187 188 189 190
      `data` varchar(255)
    SQL
    super(@connection, 'ex', definition, &block)
  end
191
end