Navigation Menu

Skip to content

Commit

Permalink
Make test suite work with Rails 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Guidi committed Feb 16, 2009
1 parent d6281f4 commit 939ad71
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
cached-models-*.gem
cached-models-*.tar.gz
cached-models-*.tar.bz2
test/*.log
2 changes: 2 additions & 0 deletions 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
Expand Down
104 changes: 6 additions & 98 deletions 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'
Expand Down
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion 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

Expand Down
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
65 changes: 65 additions & 0 deletions 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
38 changes: 24 additions & 14 deletions 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
Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down

0 comments on commit 939ad71

Please sign in to comment.