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

3
class MysqlConnectionTest < ActiveRecord::TestCase
4
  def setup
5
    super
6 7 8
    @connection = ActiveRecord::Base.connection
  end

9 10 11 12 13 14 15 16 17 18 19 20 21 22
  def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
    run_without_connection do |orig_connection|
      ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true}))
      assert ActiveRecord::Base.connection.raw_connection.reconnect
    end
  end

  def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
    run_without_connection do |orig_connection|
      ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false}))
      assert !ActiveRecord::Base.connection.raw_connection.reconnect
    end
  end

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  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
42
    @connection.verify!
43
    assert @connection.active?
M
Mark Turner 已提交
44
  end
45

A
Aaron Patterson 已提交
46
  def test_bind_value_substitute
47
    bind_param = @connection.substitute_at('foo', 0)
A
Aaron Patterson 已提交
48 49 50 51
    assert_equal Arel.sql('?'), bind_param
  end

  def test_exec_no_binds
52 53
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
54 55 56
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
57
    result = @connection.exec_query('SELECT id, data FROM ex')
A
Aaron Patterson 已提交
58 59 60 61
    assert_equal 0, result.rows.length
    assert_equal 2, result.columns.length
    assert_equal %w{ id data }, result.columns

62 63
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
    result = @connection.exec_query('SELECT id, data FROM ex')
A
Aaron Patterson 已提交
64 65 66 67 68 69 70
    assert_equal 1, result.rows.length
    assert_equal 2, result.columns.length

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

  def test_exec_with_binds
71 72
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
73 74 75
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
76 77
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
    result = @connection.exec_query(
A
Aaron Patterson 已提交
78 79 80 81 82 83 84 85 86
      '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
87 88
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
89 90 91
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
92
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
A
Aaron Patterson 已提交
93 94
    column = @connection.columns('ex').find { |col| col.name == 'id' }

95
    result = @connection.exec_query(
A
Aaron Patterson 已提交
96 97 98 99 100 101 102 103
      '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

104
  # Test that MySQL allows multiple results for stored procedures
M
Marcin Raczkowski 已提交
105
  if defined?(Mysql) && Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
106 107 108
    def test_multi_results
      rows = ActiveRecord::Base.connection.select_rows('CALL ten();')
      assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}"
109
      assert @connection.active?, "Bad connection use by 'MysqlAdapter.select_rows'"
110
    end
111
  end
112 113 114 115 116 117 118 119 120 121 122

  private

  def run_without_connection
    original_connection = ActiveRecord::Base.remove_connection
    begin
      yield original_connection
    ensure
      ActiveRecord::Base.establish_connection(original_connection)
    end
  end
123
end