connection_test.rb 6.0 KB
Newer Older
1
require "cases/helper"
2

3
class MysqlConnectionTest < ActiveRecord::TestCase
4 5 6
  class Klass < ActiveRecord::Base
  end

7
  def setup
8
    super
J
Jon Leighton 已提交
9
    @connection = ActiveRecord::Base.connection
10 11
  end

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

A
Aaron Patterson 已提交
19
  def test_connect_with_url
A
Anupam Choudhury 已提交
20
    run_without_connection do
A
Aaron Patterson 已提交
21
      ar_config = ARTest.connection_config['arunit']
22 23 24

      skip "This test doesn't work with custom socket location" if ar_config['socket']

A
Aaron Patterson 已提交
25
      url = "mysql://#{ar_config["username"]}@localhost/#{ar_config["database"]}"
26 27
      Klass.establish_connection(url)
      assert_equal ar_config['database'], Klass.connection.current_database
A
Aaron Patterson 已提交
28 29 30
    end
  end

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

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  def test_no_automatic_reconnection_after_timeout
    assert @connection.active?
    @connection.update('set @@wait_timeout=1')
    sleep 2
    assert !@connection.active?
  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
57
    @connection.verify!
58
    assert @connection.active?
M
Mark Turner 已提交
59
  end
60

A
Aaron Patterson 已提交
61
  def test_bind_value_substitute
62
    bind_param = @connection.substitute_at('foo', 0)
A
Aaron Patterson 已提交
63 64 65 66
    assert_equal Arel.sql('?'), bind_param
  end

  def test_exec_no_binds
67 68
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
69 70 71
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
72
    result = @connection.exec_query('SELECT id, data FROM ex')
A
Aaron Patterson 已提交
73 74 75 76
    assert_equal 0, result.rows.length
    assert_equal 2, result.columns.length
    assert_equal %w{ id data }, result.columns

77
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
78 79 80

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

85
    assert_equal [['1', 'foo']], result.rows
A
Aaron Patterson 已提交
86 87 88
  end

  def test_exec_with_binds
89 90
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
91 92 93
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
94 95
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
    result = @connection.exec_query(
A
Aaron Patterson 已提交
96 97 98 99 100 101 102 103 104
      'SELECT id, data FROM ex WHERE id = ?', nil, [[nil, 1]])

    assert_equal 1, result.rows.length
    assert_equal 2, result.columns.length

    assert_equal [[1, 'foo']], result.rows
  end

  def test_exec_typecasts_bind_vals
105 106
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
107 108 109
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
110
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
A
Aaron Patterson 已提交
111 112
    column = @connection.columns('ex').find { |col| col.name == 'id' }

113
    result = @connection.exec_query(
A
Aaron Patterson 已提交
114 115 116 117 118 119 120 121
      'SELECT id, data FROM ex WHERE id = ?', nil, [[column, '1-fuu']])

    assert_equal 1, result.rows.length
    assert_equal 2, result.columns.length

    assert_equal [[1, 'foo']], result.rows
  end

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

131 132 133 134 135
  def test_mysql_default_in_strict_mode
    result = @connection.exec_query "SELECT @@SESSION.sql_mode"
    assert_equal [["STRICT_ALL_TABLES"]], result.rows
  end

136
  def test_mysql_strict_mode_disabled_dont_override_global_sql_mode
137
    run_without_connection do |orig_connection|
J
Jon Leighton 已提交
138 139 140
      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"
141
      assert_equal global_sql_mode.rows, session_sql_mode.rows
142 143 144
    end
  end

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

162 163 164
  private

  def run_without_connection
J
Jon Leighton 已提交
165
    original_connection = ActiveRecord::Base.remove_connection
166 167 168
    begin
      yield original_connection
    ensure
J
Jon Leighton 已提交
169
      ActiveRecord::Base.establish_connection(original_connection)
170 171
    end
  end
172
end