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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
require 'spec_helper'
describe "SeamlessDatabasePool::ControllerFilter" do
module SeamlessDatabasePool
class TestApplicationController
attr_reader :session
def initialize(session)
@session = session
end
def process(action, *args)
send action
end
def redirect_to (options = {}, response_status = {})
options
end
def base_action
::SeamlessDatabasePool.read_only_connection_type
end
end
class TestBaseController < TestApplicationController
include ::SeamlessDatabasePool::ControllerFilter
use_database_pool :read => :persistent
def read
::SeamlessDatabasePool.read_only_connection_type
end
def other
::SeamlessDatabasePool.read_only_connection_type
end
end
class TestOtherController < TestBaseController
use_database_pool :all => :random, [:edit, :save, :redirect_master_action] => :master
def edit
::SeamlessDatabasePool.read_only_connection_type
end
def save
::SeamlessDatabasePool.read_only_connection_type
end
def redirect_master_action
redirect_to(:action => :read)
end
def redirect_read_action
redirect_to(:action => :read)
end
end
class TestRails2ApplicationController < TestApplicationController
attr_reader :action_name
def process(action, *args)
@action_name = action
perform_action
end
private
def perform_action
send action_name
end
end
class TestRails2BaseController < TestRails2ApplicationController
include ::SeamlessDatabasePool::ControllerFilter
use_database_pool :read => :persistent
def read
::SeamlessDatabasePool.read_only_connection_type
end
end
end
let(:session){Hash.new}
let(:controller){SeamlessDatabasePool::TestOtherController.new(session)}
it "should work with nothing set" do
controller = SeamlessDatabasePool::TestApplicationController.new(session)
controller.process('base_action').should == :master
end
it "should allow setting a connection type for a single action" do
controller = SeamlessDatabasePool::TestBaseController.new(session)
controller.process('read').should == :persistent
end
it "should allow setting a connection type for actions" do
controller.process('edit').should == :master
controller.process('save').should == :master
end
it "should allow setting a connection type for all actions" do
controller.process('other').should == :random
end
it "should inherit the superclass' options" do
controller.process('read').should == :persistent
end
it "should be able to force using the master connection on the next request" do
# First request
controller.process('read').should == :persistent
controller.use_master_db_connection_on_next_request
# Second request
controller.process('read').should == :master
# Third request
controller.process('read').should == :persistent
end
it "should not break trying to force the master connection if sessions are not enabled" do
controller.process('read').should == :persistent
controller.use_master_db_connection_on_next_request
# Second request
session.clear
controller.process('read').should == :persistent
end
it "should force the master connection on the next request for a redirect in master connection block" do
controller = SeamlessDatabasePool::TestOtherController.new(session)
controller.process('redirect_master_action').should == {:action => :read}
controller.process('read').should == :master
end
it "should not force the master connection on the next request for a redirect not in master connection block" do
controller.process('redirect_read_action').should == {:action => :read}
controller.process('read').should == :persistent
end
it "should work with a Rails 2 controller" do
controller = SeamlessDatabasePool::TestRails2BaseController.new(session)
controller.process('read').should == :persistent
end
end
|