Skip to content

Commit

Permalink
Make tests run independently of a Rails app
Browse files Browse the repository at this point in the history
  • Loading branch information
chipiga committed Sep 16, 2010
1 parent 1b0be8d commit b024682
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 72 deletions.
17 changes: 0 additions & 17 deletions test/database.yml

This file was deleted.

12 changes: 8 additions & 4 deletions test/has_ancestry_test.rb
Expand Up @@ -3,15 +3,12 @@
# Setup the required models for all test cases

class TestNode < ActiveRecord::Base
has_ancestry :cache_depth => true, :depth_cache_column => :depth_cache
end

class AlternativeTestNode < ActiveRecord::Base
has_ancestry :ancestry_column => :alternative_ancestry, :orphan_strategy => :rootify
end

class ActsAsTreeTestNode < ActiveRecord::Base
acts_as_tree
end

class ParentIdTestNode < ActiveRecord::Base
Expand All @@ -24,7 +21,14 @@ class TestNodeSub2 < TestNode
end

class ActsAsTreeTest < ActiveSupport::TestCase
load_schema
def setup
setup_db
setup_ancestry
end

def teardown
teardown_db
end

def setup_test_nodes model, level, quantity
model.delete_all
Expand Down
24 changes: 0 additions & 24 deletions test/schema.rb

This file was deleted.

95 changes: 68 additions & 27 deletions test/test_helper.rb
@@ -1,31 +1,72 @@
ENV['RAILS_ENV'] = 'test'
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'

require 'test/unit'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
require 'test_help'

def load_schema
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
db_adapter = ENV['DB']
# no db passed, try one of these fine config-free DBs before bombing.
db_adapter ||= begin
require 'rubygems'
require 'sqlite'
'sqlite'
rescue MissingSourceFile
begin
require 'sqlite3'
'sqlite3'
rescue MissingSourceFile
require 'rubygems'
require 'active_record'
require 'ancestry'

ActiveRecord::Base.establish_connection :adapter => "sqlite3",
:database => ":memory:"

def setup_db
# AR keeps printing annoying schema statements
$stdout_orig = $stdout
$stdout = StringIO.new

ActiveRecord::Base.logger
ActiveRecord::Schema.define(:version => 0) do
create_table :test_nodes do |t|
t.string :ancestry
t.integer :depth_cache
t.string :type
end

create_table :alternative_test_nodes do |t|
t.string :alternative_ancestry
end

create_table :other_test_nodes do |t|
t.string :ancestry
end

create_table :parent_id_test_nodes do |t|
t.string :ancestry
t.integer :parent_id
end

create_table :acts_as_tree_test_nodes do |t|
t.string :ancestry
end
end

$stdout = $stdout_orig
end

def teardown_db
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.drop_table(table)
end

if db_adapter.nil?
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
end

# In order for the `has_ancestry` scopes to make use of the Rails
# 3.1-compatible `where` method rather than use the old finder options, the
# scopes must be initialized *after* the database has been created.
#
# If `has_ancestry` is declared directly in the classes above before the test
# database has been created, ActiveRecord will complain and all tests will
# fail. Hence the reason for the `setup_ancestry` method which can be run
# after the database columns have been initialized.
#
def setup_ancestry
TestNode.instance_eval do
has_ancestry :cache_depth => true,
:depth_cache_column => :depth_cache
end

AlternativeTestNode.instance_eval do
has_ancestry :ancestry_column => :alternative_ancestry,
:orphan_strategy => :rootify
end

ActsAsTreeTestNode.instance_eval do
acts_as_tree
end
ActiveRecord::Base.establish_connection(config[db_adapter])
load(File.dirname(__FILE__) + "/schema.rb")
require File.dirname(__FILE__) + '/../init.rb'
end
end

0 comments on commit b024682

Please sign in to comment.