Skip to content
Browse files

Tests needn't run within an app. Closes #9534 [lifofifo]

  • Loading branch information...
1 parent f1de7c5 commit 20988cac158bcf7f7535a3c5dd193165797d719a @jeremy jeremy committed Sep 11, 2007
Showing with 159 additions and 206 deletions.
  1. +0 −39 test/abstract_unit.rb
  2. +159 −63 test/acts_as_tree_test.rb
  3. +0 −18 test/database.yml
  4. +0 −15 test/fixtures/mixin.rb
  5. +0 −59 test/fixtures/mixins.yml
  6. +0 −12 test/schema.rb
View
39 test/abstract_unit.rb
@@ -1,39 +0,0 @@
-$:.unshift(File.dirname(__FILE__) + '/../../../rails/activesupport/lib')
-$:.unshift(File.dirname(__FILE__) + '/../../../rails/activerecord/lib')
-
-require 'test/unit'
-require 'active_support'
-require 'active_record'
-require 'active_record/fixtures'
-
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-require "#{File.dirname(__FILE__)}/../init"
-
-config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
-ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
-ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
-
-load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
-
-class Test::Unit::TestCase #:nodoc:
- self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
- self.use_transactional_fixtures = true
- self.use_instantiated_fixtures = false
-
- def create_fixtures(*table_names, &block)
- Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, {}, &block)
- end
-
- def assert_queries(num = 1)
- $query_count = 0
- yield
- ensure
- assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
- end
-
- def assert_no_queries(&block)
- assert_queries(0, &block)
- end
-
-end
View
222 test/acts_as_tree_test.rb
@@ -1,123 +1,219 @@
-require File.join(File.dirname(__FILE__), 'abstract_unit')
-require File.join(File.dirname(__FILE__), 'fixtures/mixin')
+require 'test/unit'
+
+require 'rubygems'
+require 'active_record'
+
+$:.unshift File.dirname(__FILE__) + '/../lib'
+require File.dirname(__FILE__) + '/../init'
+
+class Test::Unit::TestCase
+ def assert_queries(num = 1)
+ $query_count = 0
+ yield
+ ensure
+ assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
+ end
+
+ def assert_no_queries(&block)
+ assert_queries(0, &block)
+ end
+end
+
+ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
+
+# AR keeps printing annoying schema statements
+$stdout = StringIO.new
+
+def setup_db
+ ActiveRecord::Base.logger
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :mixins do |t|
+ t.column :type, :string
+ t.column :parent_id, :integer
+ end
+ end
+end
+
+def teardown_db
+ ActiveRecord::Base.connection.tables.each do |table|
+ ActiveRecord::Base.connection.drop_table(table)
+ end
+end
+
+class Mixin < ActiveRecord::Base
+end
+
+class TreeMixin < Mixin
+ acts_as_tree :foreign_key => "parent_id", :order => "id"
+end
+
+class TreeMixinWithoutOrder < Mixin
+ acts_as_tree :foreign_key => "parent_id"
+end
+
+class RecursivelyCascadedTreeMixin < Mixin
+ acts_as_tree :foreign_key => "parent_id"
+ has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
+end
class TreeTest < Test::Unit::TestCase
- fixtures :mixins
+
+ def setup
+ setup_db
+ @root1 = TreeMixin.create!
+ @root_child1 = TreeMixin.create! :parent_id => @root1.id
+ @child1_child = TreeMixin.create! :parent_id => @root_child1.id
+ @root_child2 = TreeMixin.create! :parent_id => @root1.id
+ @root2 = TreeMixin.create!
+ @root3 = TreeMixin.create!
+ end
+
+ def teardown
+ teardown_db
+ end
def test_children
- assert_equal mixins(:tree_1).children, mixins(:tree_2, :tree_4)
- assert_equal mixins(:tree_2).children, [mixins(:tree_3)]
- assert_equal mixins(:tree_3).children, []
- assert_equal mixins(:tree_4).children, []
+ assert_equal @root1.children, [@root_child1, @root_child2]
+ assert_equal @root_child1.children, [@child1_child]
+ assert_equal @child1_child.children, []
+ assert_equal @root_child2.children, []
end
def test_parent
- assert_equal mixins(:tree_2).parent, mixins(:tree_1)
- assert_equal mixins(:tree_2).parent, mixins(:tree_4).parent
- assert_nil mixins(:tree_1).parent
+ assert_equal @root_child1.parent, @root1
+ assert_equal @root_child1.parent, @root_child2.parent
+ assert_nil @root1.parent
end
def test_delete
assert_equal 6, TreeMixin.count
- mixins(:tree_1).destroy
+ @root1.destroy
assert_equal 2, TreeMixin.count
- mixins(:tree2_1).destroy
- mixins(:tree3_1).destroy
+ @root2.destroy
+ @root3.destroy
assert_equal 0, TreeMixin.count
end
def test_insert
- @extra = mixins(:tree_1).children.create
+ @extra = @root1.children.create
assert @extra
- assert_equal @extra.parent, mixins(:tree_1)
+ assert_equal @extra.parent, @root1
- assert_equal 3, mixins(:tree_1).children.size
- assert mixins(:tree_1).children.include?(@extra)
- assert mixins(:tree_1).children.include?(mixins(:tree_2))
- assert mixins(:tree_1).children.include?(mixins(:tree_4))
+ assert_equal 3, @root1.children.size
+ assert @root1.children.include?(@extra)
+ assert @root1.children.include?(@root_child1)
+ assert @root1.children.include?(@root_child2)
end
def test_ancestors
- assert_equal [], mixins(:tree_1).ancestors
- assert_equal [mixins(:tree_1)], mixins(:tree_2).ancestors
- assert_equal mixins(:tree_2, :tree_1), mixins(:tree_3).ancestors
- assert_equal [mixins(:tree_1)], mixins(:tree_4).ancestors
- assert_equal [], mixins(:tree2_1).ancestors
- assert_equal [], mixins(:tree3_1).ancestors
+ assert_equal [], @root1.ancestors
+ assert_equal [@root1], @root_child1.ancestors
+ assert_equal [@root_child1, @root1], @child1_child.ancestors
+ assert_equal [@root1], @root_child2.ancestors
+ assert_equal [], @root2.ancestors
+ assert_equal [], @root3.ancestors
end
def test_root
- assert_equal mixins(:tree_1), TreeMixin.root
- assert_equal mixins(:tree_1), mixins(:tree_1).root
- assert_equal mixins(:tree_1), mixins(:tree_2).root
- assert_equal mixins(:tree_1), mixins(:tree_3).root
- assert_equal mixins(:tree_1), mixins(:tree_4).root
- assert_equal mixins(:tree2_1), mixins(:tree2_1).root
- assert_equal mixins(:tree3_1), mixins(:tree3_1).root
+ assert_equal @root1, TreeMixin.root
+ assert_equal @root1, @root1.root
+ assert_equal @root1, @root_child1.root
+ assert_equal @root1, @child1_child.root
+ assert_equal @root1, @root_child2.root
+ assert_equal @root2, @root2.root
+ assert_equal @root3, @root3.root
end
def test_roots
- assert_equal mixins(:tree_1, :tree2_1, :tree3_1), TreeMixin.roots
+ assert_equal [@root1, @root2, @root3], TreeMixin.roots
end
def test_siblings
- assert_equal mixins(:tree2_1, :tree3_1), mixins(:tree_1).siblings
- assert_equal [mixins(:tree_4)], mixins(:tree_2).siblings
- assert_equal [], mixins(:tree_3).siblings
- assert_equal [mixins(:tree_2)], mixins(:tree_4).siblings
- assert_equal mixins(:tree_1, :tree3_1), mixins(:tree2_1).siblings
- assert_equal mixins(:tree_1, :tree2_1), mixins(:tree3_1).siblings
+ assert_equal [@root2, @root3], @root1.siblings
+ assert_equal [@root_child2], @root_child1.siblings
+ assert_equal [], @child1_child.siblings
+ assert_equal [@root_child1], @root_child2.siblings
+ assert_equal [@root1, @root3], @root2.siblings
+ assert_equal [@root1, @root2], @root3.siblings
end
def test_self_and_siblings
- assert_equal mixins(:tree_1, :tree2_1, :tree3_1), mixins(:tree_1).self_and_siblings
- assert_equal mixins(:tree_2, :tree_4), mixins(:tree_2).self_and_siblings
- assert_equal [mixins(:tree_3)], mixins(:tree_3).self_and_siblings
- assert_equal mixins(:tree_2, :tree_4), mixins(:tree_4).self_and_siblings
- assert_equal mixins(:tree_1, :tree2_1, :tree3_1), mixins(:tree2_1).self_and_siblings
- assert_equal mixins(:tree_1, :tree2_1, :tree3_1), mixins(:tree3_1).self_and_siblings
+ assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
+ assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
+ assert_equal [@child1_child], @child1_child.self_and_siblings
+ assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
+ assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
+ assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
end
end
class TreeTestWithEagerLoading < Test::Unit::TestCase
- fixtures :mixins
+
+ def setup
+ teardown_db
+ setup_db
+ @root1 = TreeMixin.create!
+ @root_child1 = TreeMixin.create! :parent_id => @root1.id
+ @child1_child = TreeMixin.create! :parent_id => @root_child1.id
+ @root_child2 = TreeMixin.create! :parent_id => @root1.id
+ @root2 = TreeMixin.create!
+ @root3 = TreeMixin.create!
+
+ @rc1 = RecursivelyCascadedTreeMixin.create!
+ @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
+ @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
+ @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
+ end
+
+ def teardown
+ teardown_db
+ end
def test_eager_association_loading
- roots = TreeMixin.find(:all, :include=>"children", :conditions=>"mixins.parent_id IS NULL", :order=>"mixins.id")
- assert_equal mixins(:tree_1, :tree2_1, :tree3_1), roots
+ roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
+ assert_equal [@root1, @root2, @root3], roots
assert_no_queries do
assert_equal 2, roots[0].children.size
assert_equal 0, roots[1].children.size
assert_equal 0, roots[2].children.size
- end
+ end
end
def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
- root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:children=>{:children=>:children}}, :order => 'mixins.id')
- assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.children.first.children.first.children.first }
+ root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
+ assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
end
-
+
def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
- root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:first_child=>{:first_child=>:first_child}}, :order => 'mixins.id')
- assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.first_child.first_child.first_child }
+ root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
+ assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
end
-
+
def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
- leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:parent=>{:parent=>:parent}}, :order => 'mixins.id DESC')
- assert_equal mixins(:recursively_cascaded_tree_1), assert_no_queries { leaf_node.parent.parent.parent }
- end
+ leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
+ assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
+ end
end
class TreeTestWithoutOrder < Test::Unit::TestCase
- fixtures :mixins
+
+ def setup
+ setup_db
+ @root1 = TreeMixinWithoutOrder.create!
+ @root2 = TreeMixinWithoutOrder.create!
+ end
- def test_root
- assert mixins(:tree_without_order_1, :tree_without_order_2).include?(TreeMixinWithoutOrder.root)
+ def teardown
+ teardown_db
end
+ def test_root
+ assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
+ end
+
def test_roots
- assert_equal [], mixins(:tree_without_order_1, :tree_without_order_2) - TreeMixinWithoutOrder.roots
+ assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
end
-end
+end
View
18 test/database.yml
@@ -1,18 +0,0 @@
-sqlite:
- :adapter: sqlite
- :dbfile: acts_as_tree_plugin.sqlite.db
-sqlite3:
- :adapter: sqlite3
- :dbfile: acts_as_tree_plugin.sqlite3.db
-postgresql:
- :adapter: postgresql
- :username: postgres
- :password: postgres
- :database: acts_as_tree_plugin_test
- :min_messages: ERROR
-mysql:
- :adapter: mysql
- :host: localhost
- :username: rails
- :password:
- :database: acts_as_tree_plugin_test
View
15 test/fixtures/mixin.rb
@@ -1,15 +0,0 @@
-class Mixin < ActiveRecord::Base
-end
-
-class TreeMixin < Mixin
- acts_as_tree :foreign_key => "parent_id", :order => "id"
-end
-
-class TreeMixinWithoutOrder < Mixin
- acts_as_tree :foreign_key => "parent_id"
-end
-
-class RecursivelyCascadedTreeMixin < Mixin
- acts_as_tree :foreign_key => "parent_id"
- has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
-end
View
59 test/fixtures/mixins.yml
@@ -1,59 +0,0 @@
-tree_1:
- id: 1001
- type: TreeMixin
- parent_id:
-
-tree_2:
- id: 1002
- type: TreeMixin
- parent_id: 1001
-
-tree_3:
- id: 1003
- type: TreeMixin
- parent_id: 1002
-
-tree_4:
- id: 1004
- type: TreeMixin
- parent_id: 1001
-
-tree2_1:
- id: 1005
- type: TreeMixin
- parent_id:
-
-tree3_1:
- id: 1006
- type: TreeMixin
- parent_id:
-
-tree_without_order_1:
- id: 1101
- type: TreeMixinWithoutOrder
- parent_id:
-
-tree_without_order_2:
- id: 1100
- type: TreeMixinWithoutOrder
- parent_id:
-
-recursively_cascaded_tree_1:
- id: 5005
- type: RecursivelyCascadedTreeMixin
- parent_id:
-
-recursively_cascaded_tree_2:
- id: 5006
- type: RecursivelyCascadedTreeMixin
- parent_id: 5005
-
-recursively_cascaded_tree_3:
- id: 5007
- type: RecursivelyCascadedTreeMixin
- parent_id: 5006
-
-recursively_cascaded_tree_4:
- id: 5008
- type: RecursivelyCascadedTreeMixin
- parent_id: 5007
View
12 test/schema.rb
@@ -1,12 +0,0 @@
-ActiveRecord::Schema.define(:version => 1) do
- create_table :mixins, :force => true do |t|
- t.column :parent_id, :integer
- t.column :pos, :integer
- t.column :lft, :integer
- t.column :rgt, :integer
- t.column :root_id, :integer
- t.column :type, :string
- t.column :created_at, :datetime
- t.column :updated_at, :datetime
- end
-end

1 comment on commit 20988ca

@ggonnella

cloning this specifical repository with http protocol seems not to work:

>git clone http://github.com/rails/acts_as_tree.git acts_as_tree
Initialized empty Git repository in …vendor/plugins/acts_as_tree/.git/
Cannot get remote repository information.
Perhaps git-update-server-info needs to be run there?

while with git protocol works fine (but under corporate firewalls, you know the story…)

>git clone git://github.com/rails/acts_as_tree.git acts_as_tree
Initialized empty Git repository in …vendor/plugins/acts_as_tree/.git/
remote: Counting objects: 37, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 37 (delta 10), reused 0 (delta 0)
Indexing 37 objects…
100% (37/37) done
Resolving 10 deltas…
100% (10/10) done

Please sign in to comment.
Something went wrong with that request. Please try again.