2010-06-19 11:51:29 -04:00
|
|
|
require 'isolation/abstract_unit'
|
|
|
|
|
|
|
|
class LoadingTest < Test::Unit::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
@app ||= Rails.application
|
|
|
|
end
|
|
|
|
|
2010-06-29 13:47:04 -04:00
|
|
|
def test_constants_in_app_are_autoloaded
|
2010-06-19 11:51:29 -04:00
|
|
|
app_file "app/models/post.rb", <<-MODEL
|
|
|
|
class Post < ActiveRecord::Base
|
|
|
|
validates_acceptance_of :title, :accept => "omg"
|
|
|
|
end
|
|
|
|
MODEL
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
setup_ar!
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
p = Post.create(:title => 'omg')
|
|
|
|
assert_equal 1, Post.count
|
|
|
|
assert_equal 'omg', p.title
|
|
|
|
p = Post.first
|
|
|
|
assert_equal 'omg', p.title
|
|
|
|
end
|
|
|
|
|
2010-06-29 13:47:04 -04:00
|
|
|
def test_models_without_table_do_not_panic_on_scope_definitions_when_loaded
|
|
|
|
app_file "app/models/user.rb", <<-MODEL
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
default_scope where(:published => true)
|
|
|
|
end
|
|
|
|
MODEL
|
|
|
|
|
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
setup_ar!
|
|
|
|
|
|
|
|
User
|
|
|
|
end
|
|
|
|
|
2010-07-18 17:23:54 -04:00
|
|
|
test "load config/environments/environment before Bootstrap initializers" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
AppTemplate::Application.configure do
|
|
|
|
config.development_environment_loaded = true
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.before_initialize do
|
|
|
|
config.loaded = config.development_environment_loaded
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert ::AppTemplate::Application.config.loaded
|
|
|
|
end
|
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
def test_descendants_are_cleaned_on_each_request_without_cache_classes
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/models/post.rb", <<-MODEL
|
|
|
|
class Post < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
MODEL
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2010-08-05 16:47:01 -04:00
|
|
|
AppTemplate::Application.routes.draw do
|
2010-06-19 11:51:29 -04:00
|
|
|
match '/load', :to => lambda { |env| [200, {}, Post.all] }
|
|
|
|
match '/unload', :to => lambda { |env| [200, {}, []] }
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
|
|
|
|
require "#{rails_root}/config/environment"
|
|
|
|
setup_ar!
|
|
|
|
|
|
|
|
assert_equal [], ActiveRecord::Base.descendants
|
|
|
|
get "/load"
|
|
|
|
assert_equal [Post], ActiveRecord::Base.descendants
|
|
|
|
get "/unload"
|
|
|
|
assert_equal [], ActiveRecord::Base.descendants
|
|
|
|
end
|
|
|
|
|
2010-07-22 06:07:45 -04:00
|
|
|
test "initialize_cant_be_called_twice" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_raise(RuntimeError) { ::AppTemplate::Application.initialize! }
|
|
|
|
end
|
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
protected
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-19 11:51:29 -04:00
|
|
|
def setup_ar!
|
|
|
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
|
|
|
ActiveRecord::Migration.verbose = false
|
|
|
|
ActiveRecord::Schema.define(:version => 1) do
|
|
|
|
create_table :posts do |t|
|
|
|
|
t.string :title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|