Permalink
Browse files

extract reusable test methods to MasochismTestCase

  • Loading branch information...
1 parent 67bf9b0 commit 3e2af08496094975e1f218ba8a5cda9fe159e94b @mislav committed Apr 12, 2009
Showing with 46 additions and 25 deletions.
  1. +46 −25 test/connection_proxy_test.rb
@@ -1,4 +1,6 @@
require 'test/unit'
+require 'active_support'
+require 'active_support/test_case'
require 'active_record'
require 'active_reload/connection_proxy'
@@ -8,25 +10,48 @@ module Rails
def self.env() RAILS_ENV end
end
-class ConnectionProxyTest < Test::Unit::TestCase
-
- def setup
- ActiveRecord::Base.configurations = {
- Rails.env => {'adapter' => 'sqlite3', 'database' => ':memory:'}
- }
+class MasochismTestCase < ActiveSupport::TestCase
+ setup do
+ ActiveRecord::Base.configurations = default_configuration
ActiveRecord::Base.establish_connection
end
+ def self.default_configuration
+ { Rails.env => {'adapter' => 'sqlite3', 'database' => ':memory:'} }
+ end
+
def config
ActiveRecord::Base.configurations
end
- def teardown
+ def enable_masochism
+ ActiveReload::ConnectionProxy.setup!
+ end
+
+ def master
+ ActiveRecord::Base.connection.master
+ end
+
+ def slave
+ ActiveRecord::Base.connection.slave
+ end
+end
+
+class ConnectionProxyTest < MasochismTestCase
+ setup do
+ ActiveRecord::Base.establish_connection
+ end
+
+ teardown do
[ActiveRecord::Base, ActiveReload::MasterDatabase, ActiveReload::SlaveDatabase].each do |klass|
klass.remove_connection
end
end
-
+
+ def create_table
+ master.create_table(:foo) {|t|}
+ end
+
def test_slave_defined_returns_false_when_slave_not_defined
assert !ActiveReload::ConnectionProxy.slave_defined?, 'Slave should not be defined'
end
@@ -37,37 +62,33 @@ def test_slave_defined_returns_true_when_slave_defined
end
def test_default
- ActiveReload::ConnectionProxy.setup!
-
- ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
- assert_equal ['foo'], ActiveRecord::Base.connection.tables, 'Master and Slave should be the same database'
- assert_equal ['foo'], ActiveRecord::Base.connection.slave.tables, 'Master and Slave should be the same database'
+ enable_masochism
+ create_table
+
+ assert_equal ['foo'], slave.tables, 'Master and Slave should be the same database'
end
def test_master_database_outside_environment
config.update('master_database' => config[Rails.env].dup)
- ActiveReload::ConnectionProxy.setup!
+ enable_masochism
+ create_table
- ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
- assert_equal [], ActiveRecord::Base.connection.tables, 'Master and Slave should be different databases'
- assert_equal [], ActiveRecord::Base.connection.slave.tables, 'Master and Slave should be different databases'
+ assert_equal [], slave.tables, 'Master and Slave should be different databases'
end
def test_master_database_within_environment
config[Rails.env].update('master_database' => config[Rails.env].dup)
- ActiveReload::ConnectionProxy.setup!
+ enable_masochism
+ create_table
- ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
- assert_equal [], ActiveRecord::Base.connection.tables, 'Master and Slave should be different databases'
- assert_equal [], ActiveRecord::Base.connection.slave.tables, 'Master and Slave should be different databases'
+ assert_equal [], slave.tables, 'Master and Slave should be different databases'
end
def test_slave_database_within_environment
config[Rails.env].update('slave_database' => config[Rails.env].dup)
- ActiveReload::ConnectionProxy.setup!
+ enable_masochism
+ create_table
- ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
- assert_equal [], ActiveRecord::Base.connection.tables, 'Master and Slave should be different databases'
- assert_equal [], ActiveRecord::Base.connection.slave.tables, 'Master and Slave should be different databases'
+ assert_equal [], slave.tables, 'Master and Slave should be different databases'
end
end

0 comments on commit 3e2af08

Please sign in to comment.