diff --git a/.gitignore b/.gitignore index 5143ba4..15db876 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ cached-models-*.gem cached-models-*.tar.gz cached-models-*.tar.bz2 +test/*.log \ No newline at end of file diff --git a/CHANGELOG b/CHANGELOG index fe22d82..6c3cb14 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Make test suite work with Rails 2.3.0 + * has_and_belongs_to_many association support * Fix memory leak issue in cached_associations diff --git a/tasks/cached_models_tasks.rake b/tasks/cached_models_tasks.rake index 6b906b7..bc270b4 100644 --- a/tasks/cached_models_tasks.rake +++ b/tasks/cached_models_tasks.rake @@ -1,108 +1,16 @@ # RAILS_ENV = "test" - -require 'rubygems' -require 'active_record' -require 'active_record/fixtures' - -path_to_fixtures = File.dirname(__FILE__) + '/../test/fixtures' -fixtures = %w( addresses authors blogs posts categories categories_posts comments tags ) - desc 'Run default task (test)' task :cached_models => 'cached_models:test' namespace :cached_models do - desc 'Reset the CachedModels data' - task :reset => [ :teardown, :setup ] - desc 'Create CachedModels test database tables and load fixtures' - task :setup => [ :create_tables, :load_fixtures ] - - desc 'Remove all CachedModels data' - task :teardown => :drop_tables - - desc 'Create CachedModels test database tables' - task :create_tables => :environment do - ActiveRecord::Schema.define do - create_table :addresses, :force => true do |t| - t.integer :author_id - t.string :street - t.string :zip - t.string :city - t.string :state - t.string :country - - t.timestamps - end - - create_table :authors, :force => true do |t| - t.integer :blog_id - t.string :first_name - t.string :last_name - t.integer :age - - t.timestamps - end - - create_table :blogs, :force => true do |t| - t.string :title - - t.timestamps - end - - create_table :posts, :force => true do |t| - t.integer :author_id - t.integer :blog_id - t.string :title - t.text :text - t.datetime :published_at - t.integer :rating, :default => 0 - - t.timestamps - end - - create_table :categories, :force => true do |t| - t.string :name - - t.timestamps - end - - create_table :categories_posts, :force => true, :id => false do |t| - t.integer :category_id - t.integer :post_id - end - - create_table :comments, :force => true do |t| - t.integer :post_id - t.string :email - t.text :text - - t.timestamps - end - - create_table :tags, :force => true do |t| - t.integer :taggable_id - t.string :taggable_type - t.string :name - - t.timestamps - end - end - end - - desc 'Drops CachedModels test database tables' - task :drop_tables => :environment do - ActiveRecord::Base.connection.drop_table :addresses - ActiveRecord::Base.connection.drop_table :authors - ActiveRecord::Base.connection.drop_table :posts - ActiveRecord::Base.connection.drop_table :comments - ActiveRecord::Base.connection.drop_table :tags - ActiveRecord::Base.connection.drop_table :categories - ActiveRecord::Base.connection.drop_table :categories_posts - end + task :setup => [ ] - desc 'Load fixtures' - task :load_fixtures => :environment do - fixtures.each { |f| Fixtures.create_fixtures(path_to_fixtures, f) } + desc 'Prepare the environment' + task :environment do + ActiveRecord::Base.configurations['test'] = { :adapter => 'sqlite3', :file => ':memory:' } + ENV['SCHEMA'] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'test', 'schema.rb')) + ENV['FIXTURES_PATH'] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'test', 'fixtures')) end desc 'Test CachedModels' diff --git a/test/active_record/associations/has_and_belongs_to_many_association_test.rb b/test/active_record/associations/has_and_belongs_to_many_association_test.rb index c762205..8243543 100644 --- a/test/active_record/associations/has_and_belongs_to_many_association_test.rb +++ b/test/active_record/associations/has_and_belongs_to_many_association_test.rb @@ -1,9 +1,10 @@ require File.dirname(__FILE__) + '/../../test_helper' -class HasAndBelongsToManyAssociationTest < Test::Unit::TestCase +class HasAndBelongsToManyAssociationTest < ActiveSupport::TestCase include ActiveRecord::Associations def setup + load_schema cache.clear rescue nil end diff --git a/test/active_record/associations/has_many_association_test.rb b/test/active_record/associations/has_many_association_test.rb index 62558cc..91ea3ad 100644 --- a/test/active_record/associations/has_many_association_test.rb +++ b/test/active_record/associations/has_many_association_test.rb @@ -1,10 +1,11 @@ require File.dirname(__FILE__) + '/../../test_helper' require 'active_record/associations/has_many_association' -class HasManyAssociationTest < Test::Unit::TestCase +class HasManyAssociationTest < ActiveSupport::TestCase include ActiveRecord::Associations def setup + load_schema cache.clear rescue nil end diff --git a/test/active_record/associations/has_one_association_test.rb b/test/active_record/associations/has_one_association_test.rb index ca24deb..0e33936 100644 --- a/test/active_record/associations/has_one_association_test.rb +++ b/test/active_record/associations/has_one_association_test.rb @@ -1,6 +1,6 @@ require File.dirname(__FILE__) + '/../../test_helper' -class HasOneAssociationTest < Test::Unit::TestCase +class HasOneAssociationTest < ActiveSupport::TestCase include ActiveRecord::Associations def test_should_not_raise_exception_when_use_has_one diff --git a/test/active_record/base_test.rb b/test/active_record/base_test.rb index 3853b6f..0148333 100644 --- a/test/active_record/base_test.rb +++ b/test/active_record/base_test.rb @@ -1,6 +1,6 @@ require File.dirname(__FILE__) + '/../test_helper' -class BaseTest < Test::Unit::TestCase +class BaseTest < ActiveSupport::TestCase fixtures :authors def test_should_have_cache diff --git a/test/schema.rb b/test/schema.rb new file mode 100644 index 0000000..780f83c --- /dev/null +++ b/test/schema.rb @@ -0,0 +1,65 @@ +ActiveRecord::Schema.define do + create_table :addresses, :force => true do |t| + t.integer :author_id + t.string :street + t.string :zip + t.string :city + t.string :state + t.string :country + + t.timestamps + end + + create_table :authors, :force => true do |t| + t.integer :blog_id + t.string :first_name + t.string :last_name + t.integer :age + + t.timestamps + end + + create_table :blogs, :force => true do |t| + t.string :title + + t.timestamps + end + + create_table :posts, :force => true do |t| + t.integer :author_id + t.integer :blog_id + t.string :title + t.text :text + t.datetime :published_at + t.integer :rating, :default => 0 + + t.timestamps + end + + create_table :categories, :force => true do |t| + t.string :name + + t.timestamps + end + + create_table :categories_posts, :force => true, :id => false do |t| + t.integer :category_id + t.integer :post_id + end + + create_table :comments, :force => true do |t| + t.integer :post_id + t.string :email + t.text :text + + t.timestamps + end + + create_table :tags, :force => true do |t| + t.integer :taggable_id + t.string :taggable_type + t.string :name + + t.timestamps + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index a216fca..2e39588 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,29 +1,30 @@ -RAILS_ENV = "test" unless defined? RAILS_ENV +ENV['RAILS_ENV'] = 'test' +ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..' require 'test/unit' -require 'rubygems' - -# FIXME load path -require File.dirname(__FILE__) + '/../../../../config/environment' - -require 'active_support' -require 'action_controller' -require 'active_support/test_case' +require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb')) require 'active_record/fixtures' -require 'action_controller/integration' $:.unshift File.dirname(__FILE__) + '/models' require 'author' require 'post' -Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures" -ActionController::IntegrationTest.fixture_path = Test::Unit::TestCase.fixture_path silence_warnings do cache = ActiveSupport::Cache.lookup_store :mem_cache_store Object.const_set "RAILS_CACHE", cache ActiveRecord::Base.rails_cache = cache end +def load_schema + ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") + ActiveRecord::Schema.verbose = false + + ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :dbfile => ':memory:' + load(File.dirname(__FILE__) + "/schema.rb") + fixtures = %w( addresses authors blogs posts categories categories_posts comments tags ) + fixtures.each { |f| Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, f) } +end + begin require 'memcache' MemCache.new('localhost').stats @@ -53,11 +54,18 @@ class AssociationCollection < AssociationProxy #:nodoc: end end -class Test::Unit::TestCase +class ActiveSupport::TestCase + include ActiveRecord::TestFixtures + + self.fixture_path = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures')) self.use_transactional_fixtures = true self.use_instantiated_fixtures = false fixtures :all + def setup + load_schema + end + # Assert the given condition is false def assert_false(condition, message = nil) assert !condition, message @@ -80,9 +88,11 @@ def post_options(options = {}) :title => "Cached models review", :text => "Cached models review..", :published_at => 1.week.ago }.merge(options) - end + end end +ActionController::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path + def uses_mocha(description) require 'rubygems' require 'mocha'