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

3
class MysqlConnectionTest < ActiveRecord::TestCase
4
  def setup
5
    super
J
Fix CI  
Jon Leighton 已提交
6
    @connection = ActiveRecord::Model.connection
7 8
  end

9 10
  def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
    run_without_connection do |orig_connection|
J
Fix CI  
Jon Leighton 已提交
11 12
      ActiveRecord::Model.establish_connection(orig_connection.merge({:reconnect => true}))
      assert ActiveRecord::Model.connection.raw_connection.reconnect
13 14 15
    end
  end

A
Aaron Patterson 已提交
16 17 18 19 20 21 22 23 24 25
  def test_connect_with_url
    run_without_connection do |orig|
      ar_config = ARTest.connection_config['arunit']
      url = "mysql://#{ar_config["username"]}@localhost/#{ar_config["database"]}"
      klass = Class.new(ActiveRecord::Base)
      klass.establish_connection(url)
      assert_equal ar_config['database'], klass.connection.current_database
    end
  end

26 27
  def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
    run_without_connection do |orig_connection|
J
Fix CI  
Jon Leighton 已提交
28 29
      ActiveRecord::Model.establish_connection(orig_connection.merge({:reconnect => false}))
      assert !ActiveRecord::Model.connection.raw_connection.reconnect
30 31 32
    end
  end

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  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
52
    @connection.verify!
53
    assert @connection.active?
M
Mark Turner 已提交
54
  end
55

A
Aaron Patterson 已提交
56
  def test_bind_value_substitute
57
    bind_param = @connection.substitute_at('foo', 0)
A
Aaron Patterson 已提交
58 59 60 61
    assert_equal Arel.sql('?'), bind_param
  end

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

72 73
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
    result = @connection.exec_query('SELECT id, data FROM ex')
A
Aaron Patterson 已提交
74 75 76 77 78 79 80
    assert_equal 1, result.rows.length
    assert_equal 2, result.columns.length

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

  def test_exec_with_binds
81 82
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
83 84 85
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
86 87
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
    result = @connection.exec_query(
A
Aaron Patterson 已提交
88 89 90 91 92 93 94 95 96
      '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
97 98
    @connection.exec_query('drop table if exists ex')
    @connection.exec_query(<<-eosql)
A
Aaron Patterson 已提交
99 100 101
      CREATE TABLE `ex` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
        `data` varchar(255))
    eosql
102
    @connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
A
Aaron Patterson 已提交
103 104
    column = @connection.columns('ex').find { |col| col.name == 'id' }

105
    result = @connection.exec_query(
A
Aaron Patterson 已提交
106 107 108 109 110 111 112 113
      '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

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

  private

  def run_without_connection
J
Fix CI  
Jon Leighton 已提交
126
    original_connection = ActiveRecord::Model.remove_connection
127 128 129
    begin
      yield original_connection
    ensure
J
Fix CI  
Jon Leighton 已提交
130
      ActiveRecord::Model.establish_connection(original_connection)
131 132
    end
  end
133
end