Skip to content

Commit

Permalink
Fix test issue with Rails 3.1 due to new Arel visitor impl
Browse files Browse the repository at this point in the history
Rollback Gemfile/bundler integration.  I need to support multiple
Rails versions and bundler does not make that easy.
  • Loading branch information
mperham committed Oct 29, 2011
1 parent 727938d commit 47e8cfe
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 91 deletions.
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

1 change: 0 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ DataFabric changelog

HEAD

- Added .rvmrc and Gemfile to ease development (Paul Gross and Dan Manges)
- Added ConnectionProxy#respond_to? which delegates to underlying connection
(Paul Gross and Dan Manges)
- Remove specific handling of nested transactions and let the underlying
Expand Down
5 changes: 0 additions & 5 deletions Gemfile

This file was deleted.

34 changes: 0 additions & 34 deletions Gemfile.lock

This file was deleted.

19 changes: 9 additions & 10 deletions lib/data_fabric/connection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def to_s
@proc.call
end
end

class PoolProxy
def initialize(proxy)
@proxy = proxy
Expand Down Expand Up @@ -47,7 +47,7 @@ def connected?
end
end

%w(columns columns_hash table_exists? primary_keys).each do |name|
%w(columns column_defaults columns_hash table_exists? primary_keys).each do |name|
define_method(name.to_sym) do |*args|
@proxy.current_pool.send(name.to_sym, *args)
end
Expand All @@ -61,7 +61,7 @@ def method_missing(name, *args)

class ConnectionProxy
cattr_accessor :shard_pools

def initialize(model_class, options)
@model_class = model_class
@replicated = options[:replicated]
Expand Down Expand Up @@ -105,7 +105,7 @@ def with_master
ensure
set_role(old_role)
end

def connected?
current_pool.connected?
end
Expand All @@ -122,17 +122,16 @@ def current_pool
ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config))
end
end

private

def spec_for(config)
# XXX This looks pretty fragile. Will break if AR changes how it initializes connections and adapters.
config = config.symbolize_keys
adapter_method = "#{config[:adapter]}_connection"
initialize_adapter(config[:adapter])
ActiveRecord::Base::ConnectionSpecification.new(config, adapter_method)
end

def initialize_adapter(adapter)
begin
require 'rubygems'
Expand All @@ -145,7 +144,7 @@ def initialize_adapter(adapter)
raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
end
end
end
end

def connection_name_builder
@connection_name_builder ||= begin
Expand All @@ -158,11 +157,11 @@ def connection_name_builder
clauses
end
end

def set_role(role)
Thread.current[:data_fabric_role] = role
end

def current_role
Thread.current[:data_fabric_role] || 'slave'
end
Expand Down
73 changes: 39 additions & 34 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'test_helper'
require 'flexmock/test_unit'

require 'active_record/connection_adapters/mysql_adapter'

class PrefixModel < ActiveRecord::Base
data_fabric :prefix => 'prefix'
end
Expand All @@ -16,33 +18,37 @@ class TheWholeEnchilada < ActiveRecord::Base
class AdapterMock < ActiveRecord::ConnectionAdapters::AbstractAdapter
# Minimum required to perform a find with no results.
# Works on 2.3.10, 3.0.0 and 3.0.3.
def columns(table_name, name=nil)
[ActiveRecord::ConnectionAdapters::Column.new('id', 0, :integer, false)]
end
def primary_key(name)
:id
end
def adapter_name
'mysql'
end
def select(sql, name=nil)
[]
end
def execute(sql, name=nil)
[]
end
def tables
["enchiladas", "the_whole_burritos"]
end
def table_exists?(name)
true
end
def last_inserted_id(result)
1
end
def method_missing(name, *args)
raise ArgumentError, "#{self.class.name} missing '#{name}': #{args.inspect}"
end
def columns(table_name, name=nil)
[ActiveRecord::ConnectionAdapters::Column.new('id', 0, :integer, false)]
end
def primary_key(name)
:id
end
def adapter_name
'mysql'
end
def select(sql, name=nil, bindings=nil)
[]
end
def execute(sql, name=nil)
[]
end
def tables
["enchiladas", "the_whole_burritos"]
end
def table_exists?(name)
true
end
def last_inserted_id(result)
1
end
def method_missing(name, *args)
raise ArgumentError, "#{self.class.name} missing '#{name}': #{args.inspect}"
end

def self.visitor_for(pool)
Arel::Visitors::MySQL.new(pool)
end
end

class RawConnection
Expand All @@ -58,12 +64,12 @@ def test_should_install_into_arbase
meth = PrefixModel.methods.first.is_a?(Symbol) ? :data_fabric : 'data_fabric'
assert PrefixModel.methods.include?(meth)
end

def test_prefix_connection_name
setup_configuration_for PrefixModel, 'prefix_test'
assert_equal 'prefix_test', PrefixModel.connection.connection_name
end

def test_shard_connection_name
setup_configuration_for ShardModel, 'city_austin_test'
# ensure unset means error
Expand Down Expand Up @@ -106,7 +112,7 @@ def test_enchilada
assert_raises ActiveRecord::RecordNotFound do
TheWholeEnchilada.find(1)
end

# Should use the master
mmmm = TheWholeEnchilada.new
mmmm.instance_variable_set(:@attributes, { 'id' => 1 })
Expand All @@ -115,7 +121,7 @@ def test_enchilada
end
# ...but immediately set it back to default to the slave
assert_equal 'fiveruns_city_dallas_test_slave', TheWholeEnchilada.connection.connection_name

# Should use the master
TheWholeEnchilada.transaction do
mmmm.save!
Expand All @@ -131,10 +137,9 @@ def test_enchilada
end

private

def setup_configuration_for(clazz, name)
flexmock(ActiveRecord::ConnectionAdapters::ConnectionPool).new_instances.should_receive(
:new_connection).and_return(AdapterMock.new(RawConnection.new))
flexmock(ActiveRecord::Base).should_receive(:mysql_connection).and_return(AdapterMock.new(RawConnection.new))
ActiveRecord::Base.configurations ||= HashWithIndifferentAccess.new
ActiveRecord::Base.configurations[name] = HashWithIndifferentAccess.new({ :adapter => 'mysql', :database => name, :host => 'localhost'})
end
Expand Down
12 changes: 6 additions & 6 deletions test/thread_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'erb'

class ThreadTest < Test::Unit::TestCase

MUTEX = Mutex.new

def test_class_and_instance_connections
Expand All @@ -18,7 +18,7 @@ class ThreadedEnchilada < ActiveRecord::Base
iconn = ThreadedEnchilada.new.connection
assert_equal cconn, iconn
end

def xtest_threaded_access
clear_databases

Expand Down Expand Up @@ -48,7 +48,7 @@ def xtest_threaded_access
end
end
threads.each { |thread| thread.join }

counts.each_pair do |city, count|
DataFabric.activate_shard(:city => city) do
# slave should be empty
Expand All @@ -62,9 +62,9 @@ def xtest_threaded_access
end
end
end

private

def clear_databases
ActiveRecord::Base.configurations = { 'test' => { :adapter => 'mysql', :host => 'localhost', :database => 'mysql' } }
ActiveRecord::Base.establish_connection 'test'
Expand All @@ -77,7 +77,7 @@ def clear_databases
end
ActiveRecord::Base.clear_active_connections!
end

def using_connection(&block)
ActiveRecord::Base.connection.instance_eval(&block)
end
Expand Down

0 comments on commit 47e8cfe

Please sign in to comment.