public
Rubygem
Description: An ActiveRecord to the DBSlayer lightweight proxy/pooling layer (http://code.nytimes.com/projects/dbslayer)
Clone URL: git://github.com/harrisj/activerecord-dbslayer-adapter.git
activerecord-dbslayer-adapter / test / test_dbslayer_adapter.rb
100644 89 lines (68 sloc) 2.985 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require File.dirname(__FILE__) + '/helper.rb'
 
class Test_ActiveRecord_ConnectionAdapters_DbslayerAdapter < Test::Unit::TestCase
  def setup
    @config = { :host => 'localhost', :port => 9090 }
    @adapter = ActiveRecord::Base.dbslayer_connection(@config)
  end
  
  def test_adapter_name
    assert_equal 'DBSlayer (MySQL)', @adapter.adapter_name
  end
  
  def test_active?
    @adapter.raw_connection.expects(:mysql_stats).returns("STAT_REPLY")
    assert @adapter.active?
  end
  
  def test_active_fail?
    @adapter.raw_connection.expects(:mysql_stats).raises(RuntimeError)
    assert_equal false, @adapter.active?
  end
  
  def test_select_rows
    select_query = "select * from cities limit 10"
    select_name = 'foo'
    
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(select_query)).returns(CITY_RESULTS)
    rows = @adapter.select_rows(select_query, select_name)
    
    assert_equal CITY_ROWS, rows
  end
  
  def test_insert_returns_id
    insert_sql = "insert into cities(name) values(\"Seattle\")"
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(insert_sql)).returns(INSERT_ID_RESULT)
    
    id = @adapter.insert_sql(insert_sql)
    assert_equal 1, id
  end
  
  def test_update_affected_rows
    update_sql = "update cities set urban=1"
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash(update_sql)).returns(UPDATE_RESULT)
    
    affected_rows = @adapter.send :update_sql, update_sql
    assert_equal 42, affected_rows
  end
  
  
  def test_tables
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash("SHOW TABLES")).returns(SHOW_TABLES_REPLY)
    tables = @adapter.tables
    
    assert_equal ["table1", "table2"], tables
  end
  
  def test_columns
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash("SHOW FIELDS FROM `places`")).returns(SHOW_COLUMNS_REPLY)
    columns = @adapter.columns('places')
    
    assert_equal 4, columns.size
    assert_equal %w(id zipcode terms md5hash), columns.map {|c| c.name }
    columns.each do |c|
      assert_kind_of ActiveRecord::ConnectionAdapters::DbslayerColumn, c
    end
  end
  
  def test_indexes
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash("SHOW KEYS FROM `places`")).returns(SHOW_KEYS_REPLY)
    indexes = @adapter.indexes('places')
    
    assert_equal 2, indexes.size
    indexes.each do |i|
      assert_kind_of ActiveRecord::ConnectionAdapters::IndexDefinition, i
    end
  end
  
  def test_pk_and_sequence_for
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash("describe `places`")).returns(DESCRIBE_REPLY)
    assert_equal ['id', nil], @adapter.pk_and_sequence_for('places')
  end
  
  def test_show_variable
    @adapter.raw_connection.expects(:cmd_execute).with(:db, sql_hash("SHOW VARIABLES LIKE 'character_set_database'")).returns(VARIABLE_REPLY)
    variable = @adapter.show_variable('character_set_database')
    assert_equal 'utf8', variable
  end
end