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

4
class MysqlConnectionTest < ActiveRecord::TestCase
G
Guo Xiang Tan 已提交
5 6
  include DdlHelper

7 8 9
  class Klass < ActiveRecord::Base
  end

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

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

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

27 28 29 30
        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 已提交
31 32 33
    end
  end

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

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

    # Repair all fixture connections so other tests won't break.
    @fixture_connections.each do |c|
      c.verify!
    end
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', 0)
A
Aaron Patterson 已提交
71 72 73 74
    assert_equal Arel.sql('?'), bind_param
  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 97
    with_example_table do
      @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
      result = @connection.exec_query(
        'SELECT id, data FROM ex WHERE id = ?', nil, [[nil, 1]])
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 109
    with_example_table do
      @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
      column = @connection.columns('ex').find { |col| col.name == 'id' }
A
Aaron Patterson 已提交
110

G
Guo Xiang Tan 已提交
111 112
      result = @connection.exec_query(
        'SELECT id, data FROM ex WHERE id = ?', nil, [[column, '1-fuu']])
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_default_in_strict_mode
    result = @connection.exec_query "SELECT @@SESSION.sql_mode"
    assert_equal [["STRICT_ALL_TABLES"]], result.rows
  end

135
  def test_mysql_strict_mode_disabled_dont_override_global_sql_mode
136
    run_without_connection do |orig_connection|
J
Jon Leighton 已提交
137 138 139
      ActiveRecord::Base.establish_connection(orig_connection.merge({:strict => false}))
      global_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@GLOBAL.sql_mode"
      session_sql_mode = ActiveRecord::Base.connection.exec_query "SELECT @@SESSION.sql_mode"
140
      assert_equal global_sql_mode.rows, session_sql_mode.rows
141 142 143
    end
  end

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  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

  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

161 162 163
  private

  def run_without_connection
J
Jon Leighton 已提交
164
    original_connection = ActiveRecord::Base.remove_connection
165 166 167
    begin
      yield original_connection
    ensure
J
Jon Leighton 已提交
168
      ActiveRecord::Base.establish_connection(original_connection)
169 170
    end
  end
G
Guo Xiang Tan 已提交
171 172 173 174 175 176 177 178

  def with_example_table(&block)
    definition ||= <<-SQL
      `id` int(11) auto_increment PRIMARY KEY,
      `data` varchar(255)
    SQL
    super(@connection, 'ex', definition, &block)
  end
179
end