Skip to content
Browse files

decouple unit tests from the application

  • Loading branch information...
1 parent 347002e commit 68aa5a11f03764033e654e98ac997c161e5a2337 @mislav mislav committed
Showing with 31 additions and 60 deletions.
  1. +31 −60 test/connection_proxy_test.rb
View
91 test/connection_proxy_test.rb
@@ -1,33 +1,43 @@
-require File.dirname(__FILE__) + '/../../../../config/environment'
require 'test/unit'
-require 'fileutils'
-require 'pp'
+require 'active_record'
+require 'active_reload/connection_proxy'
-module ActiveReload
- class ConnectionProxyTest < Test::Unit::TestCase
+RAILS_ENV = 'test'
- MASTER = 'db/masochism_master.sqlite3'
- SLAVE = 'db/masochism_slave.sqlite3'
+module Rails
+ def self.env() RAILS_ENV end
+end
+module ActiveReload
+ class ConnectionProxyTest < Test::Unit::TestCase
+
+ def setup
+ ActiveRecord::Base.configurations = {
+ Rails.env => {'adapter' => 'sqlite3', 'database' => ':memory:'}
+ }
+ ActiveRecord::Base.establish_connection
+ end
+
+ def config
+ ActiveRecord::Base.configurations
+ end
+
def teardown
- ActiveRecord::Base.remove_connection
- FileUtils.rm_f(Rails.root + '/' + MASTER)
- FileUtils.rm_f(Rails.root + '/' + SLAVE)
+ [ActiveRecord::Base, ActiveReload::MasterDatabase, ActiveReload::SlaveDatabase].each do |klass|
+ klass.remove_connection
+ end
end
def test_slave_defined_returns_false_when_slave_not_defined
- ActiveRecord::Base.configurations = default_config
- assert_nil ActiveReload::ConnectionProxy.slave_defined?, 'Slave should not be defined'
+ assert !ActiveReload::ConnectionProxy.slave_defined?, 'Slave should not be defined'
end
def test_slave_defined_returns_true_when_slave_defined
- ActiveRecord::Base.configurations = slave_inside_config
- assert_not_nil ActiveReload::ConnectionProxy.slave_defined?, 'Slave should be defined'
+ config.update('slave_database' => {})
+ assert ActiveReload::ConnectionProxy.slave_defined?, 'Slave should be defined'
end
def test_default
- ActiveRecord::Base.configurations = default_config
- reload
ActiveReload::ConnectionProxy.setup!
ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
@@ -36,8 +46,7 @@ def test_default
end
def test_master_database_outside_environment
- ActiveRecord::Base.configurations = master_outside_config
- reload
+ config.update('master_database' => config[Rails.env].dup)
ActiveReload::ConnectionProxy.setup!
ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
@@ -46,8 +55,7 @@ def test_master_database_outside_environment
end
def test_master_database_within_environment
- ActiveRecord::Base.configurations = master_inside_config
- reload
+ config[Rails.env].update('master_database' => config[Rails.env].dup)
ActiveReload::ConnectionProxy.setup!
ActiveRecord::Base.connection.master.execute('CREATE TABLE foo (id int)')
@@ -56,49 +64,12 @@ def test_master_database_within_environment
end
def test_slave_database_within_environment
- ActiveRecord::Base.configurations = slave_inside_config
- reload
+ config[Rails.env].update('slave_database' => config[Rails.env].dup)
ActiveReload::ConnectionProxy.setup!
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'
end
-
- private
-
- def reload
- # force establish_connection calls to be re-executed
- load File.dirname(__FILE__)+'/../lib/active_reload/connection_proxy.rb'
- end
-
- def default_config
- {Rails.env => {'adapter' => 'sqlite3', 'database' => MASTER}}
- end
-
- def master_outside_config
- {
- Rails.env => {'adapter' => 'sqlite3', 'database' => SLAVE},
- 'master_database' => {'adapter' => 'sqlite3', 'database' => MASTER}
- }
- end
-
- def master_inside_config
- {
- Rails.env => {'adapter' => 'sqlite3', 'database' => SLAVE,
- 'master_database' => {'adapter' => 'sqlite3', 'database' => MASTER}
- }
- }
- end
-
- def slave_inside_config
- {
- Rails.env => {'adapter' => 'sqlite3', 'database' => MASTER,
- 'slave_database' => {'adapter' => 'sqlite3', 'database' => SLAVE}
- }
- }
- end
-
-
- end # class ConnectionProxyTest
-end # module ActiveReload
+ end
+end

0 comments on commit 68aa5a1

Please sign in to comment.
Something went wrong with that request. Please try again.