Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test database middleware loading from the initializer.

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit ea5d288606cac0b6055dd4e088cfb848f3b9b78f 1 parent 809409d
@Manfred Manfred authored josh committed
Showing with 50 additions and 10 deletions.
  1. +2 −1  railties/test/abstract_unit.rb
  2. +48 −9 railties/test/initializer_test.rb
View
3  railties/test/abstract_unit.rb
@@ -1,4 +1,5 @@
$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
+$:.unshift File.dirname(__FILE__) + "/../../activerecord/lib"
$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
$:.unshift File.dirname(__FILE__) + "/../lib"
$:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
@@ -25,4 +26,4 @@ def uses_gem(gem_name, test_name, version = '> 0')
yield
rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
-end
+end
View
57 railties/test/initializer_test.rb
@@ -1,6 +1,9 @@
require 'abstract_unit'
require 'initializer'
+require 'action_view'
+require 'active_record'
+
# Mocks out the configuration
module Rails
def self.configuration
@@ -26,7 +29,6 @@ def test_load_environment_with_constant
ensure
$initialize_test_set_from_env = nil
end
-
end
class Initializer_eager_loading_Test < Test::Unit::TestCase
@@ -268,7 +270,6 @@ def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib'))
end
-
private
def load_plugins!
@@ -288,7 +289,7 @@ def test_config_locales_dir_present_should_be_added_to_load_path
Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
assert_equal [ "my/test/locale.yml" ], Rails::Configuration.new.i18n.load_path
end
-
+
def test_config_defaults_should_be_added_with_config_settings
File.stubs(:exist?).returns(true)
Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
@@ -298,7 +299,7 @@ def test_config_defaults_should_be_added_with_config_settings
assert_equal [ "my/test/locale.yml", "my/other/locale.yml" ], config.i18n.load_path
end
-
+
def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
File.stubs(:exist?).returns(true)
Dir.stubs(:[]).returns([ "my/test/locale.yml" ])
@@ -306,17 +307,15 @@ def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
config = Rails::Configuration.new
config.i18n.load_path << "my/other/locale.yml"
- # To bring in AV's i18n load path.
- require 'action_view'
-
Rails::Initializer.run(:initialize_i18n, config)
assert_equal [
File.expand_path(File.dirname(__FILE__) + "/../../activesupport/lib/active_support/locale/en.yml"),
File.expand_path(File.dirname(__FILE__) + "/../../actionpack/lib/action_view/locale/en.yml"),
+ File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"),
"my/test/locale.yml",
"my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path }
end
-
+
def test_setting_another_default_locale
config = Rails::Configuration.new
config.i18n.default_locale = :de
@@ -325,6 +324,46 @@ def test_setting_another_default_locale
end
end
+class InitializerDatabaseMiddlewareTest < Test::Unit::TestCase
+ def setup
+ @config = Rails::Configuration.new
+ @config.frameworks = [:active_record, :action_controller, :action_view]
+ end
+
+ def test_database_middleware_doesnt_perform_anything_when_active_record_in_frameworks
+ @config.frameworks.clear
+ @config.expects(:middleware).never
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ end
+
+ def test_database_middleware_initializes_when_session_store_is_active_record
+ store = ActionController::Base.session_store
+ ActionController::Base.session_store = ActiveRecord::SessionStore
+
+ @config.middleware.expects(:insert_before).with(:"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement)
+ @config.middleware.expects(:insert_before).with(:"ActiveRecord::SessionStore", ActiveRecord::QueryCache)
+
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ ensure
+ ActionController::Base.session_store = store
+ end
+
+ def test_database_middleware_doesnt_initialize_when_session_store_is_not_active_record
+ store = ActionController::Base.session_store
+ ActionController::Base.session_store = ActionController::Session::CookieStore
+
+ # Define the class, so we don't have to actually make it load
+ eval("class ActiveRecord::ConnectionAdapters::ConnectionManagement; end")
+
+ @config.middleware.expects(:use).with(ActiveRecord::ConnectionAdapters::ConnectionManagement)
+ @config.middleware.expects(:use).with(ActiveRecord::QueryCache)
+
+ Rails::Initializer.run(:initialize_database_middleware, @config)
+ ensure
+ ActionController::Base.session_store = store
+ end
+end
+
class RailsRootTest < Test::Unit::TestCase
def test_rails_dot_root_equals_rails_root
assert_equal RAILS_ROOT, Rails.root.to_s
@@ -333,4 +372,4 @@ def test_rails_dot_root_equals_rails_root
def test_rails_dot_root_should_be_a_pathname
assert_equal File.join(RAILS_ROOT, 'app', 'controllers'), Rails.root.join('app', 'controllers').to_s
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.