fiveruns / data_fabric

Sharding support for ActiveRecord 2.x

This URL has Read+Write access

data_fabric / test / database_test.rb
100644 63 lines (48 sloc) 1.724 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
require File.join(File.dirname(__FILE__), 'test_helper')
require 'flexmock/test_unit'
require 'erb'
 
class TheWholeBurrito < ActiveRecord::Base
  data_fabric :prefix => 'fiveruns', :replicated => true, :shard_by => :city
end
 
class DatabaseTest < Test::Unit::TestCase
  
  def setup
    ActiveRecord::Base.configurations = load_database_yml
    if ar22?
      DataFabric::ConnectionProxy.shard_pools.clear
    end
  end
 
  def test_ar22_features
    return unless ar22?
 
    DataFabric.activate_shard :city => :dallas do
      assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name
 
      assert_raises RuntimeError do
        TheWholeBurrito.connection_pool
      end
 
      assert !TheWholeBurrito.connected?
 
      # Should use the slave
      burrito = TheWholeBurrito.find(1)
      assert_match 'vr_dallas_slave', burrito.name
 
      assert TheWholeBurrito.connected?
    end
  end
 
  def test_live_burrito
    DataFabric.activate_shard :city => :dallas do
      assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name
 
      # Should use the slave
      burrito = TheWholeBurrito.find(1)
      assert_match 'vr_dallas_slave', burrito.name
 
      # Should use the master
      burrito.reload
      assert_match 'vr_dallas_master', burrito.name
 
      # ...but immediately set it back to default to the slave
      assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name
 
      # Should use the master
      TheWholeBurrito.transaction do
        burrito = TheWholeBurrito.find(1)
        assert_match 'vr_dallas_master', burrito.name
        burrito.name = 'foo'
        burrito.save!
      end
    end
  end
end