Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
* Get rid of teardown / teardown_db
* Use an options hash in setup_db
  • Loading branch information
felixbuenemann committed Aug 13, 2016
1 parent 212cd02 commit 145de17
Showing 1 changed file with 8 additions and 50 deletions.
58 changes: 8 additions & 50 deletions test/acts_as_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ def capture_stdout(&block)

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

def setup_db(counter_cache = false, external_ids = false)
def setup_db(options = {})
# AR keeps printing annoying schema statements
capture_stdout do
ActiveRecord::Base.logger
ActiveRecord::Schema.define(version: 1) do
create_table :mixins do |t|
create_table :mixins, force: true do |t|
t.column :type, :string
t.column :parent_id, :integer
t.column :external_id, :integer if external_ids
t.column :external_parent_id, :integer if external_ids
t.column(:children_count, :integer, default: 0) if counter_cache
t.column :external_id, :integer if options[:external_ids]
t.column :external_parent_id, :integer if options[:external_ids]
t.column :children_count, :integer, default: 0 if options[:counter_cache]
t.timestamps null: false
end
end
Expand All @@ -71,15 +71,6 @@ def setup_db(counter_cache = false, external_ids = false)
end
end

def teardown_db
# Silence tables deprecation on rails 5.0
ActiveSupport::Deprecation.silence do
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.drop_table(table)
end
end
end

class Mixin < ActiveRecord::Base
include ActsAsTree
end
Expand Down Expand Up @@ -132,10 +123,6 @@ def setup
@root3 = @tree_mixin.create!
end

def teardown
teardown_db
end

def test_children
assert_equal @root1.children, [@root_child1, @root_child2]
assert_equal @root_child1.children, [@child1_child]
Expand Down Expand Up @@ -343,7 +330,6 @@ def test_tree_walker

class TestDeepDescendantsPerformance < ActsAsTreeTestCase
def setup
teardown_db
setup_db
@root1 = TreeMixin.create!
create_cascade_children @root1, "root1", 10
Expand All @@ -361,10 +347,6 @@ def setup
create_cascade_children @root5, "root5", 50
end

def teardown
teardown_db
end

def self.bench_range
[1, 2, 3, 4, 5]
end
Expand Down Expand Up @@ -394,7 +376,6 @@ def create_cascade_children parent, parent_name, count
class TreeTestWithEagerLoading < ActsAsTreeTestCase

def setup
teardown_db
setup_db
@root1 = TreeMixin.create!
@root_child1 = TreeMixin.create! parent_id: @root1.id
Expand All @@ -409,10 +390,6 @@ def setup
@rc4 = RecursivelyCascadedTreeMixin.create! parent_id: @rc3.id
end

def teardown
teardown_db
end

def test_eager_association_loading
roots = TreeMixin.includes(:children)
.where('mixins.parent_id IS NULL')
Expand Down Expand Up @@ -460,10 +437,6 @@ def setup
@root2 = TreeMixinWithoutOrder.create!
end

def teardown
teardown_db
end

def test_root
assert [@root1, @root2].include? TreeMixinWithoutOrder.root
end
Expand All @@ -480,10 +453,6 @@ def setup
@root_child = @root.children.build
end

def teardown
teardown_db
end

def test_inverse_of
# We want children to be aware of their parent before saving either
assert_equal @root, @root_child.parent
Expand All @@ -493,8 +462,7 @@ def test_inverse_of

class TreeTestWithCounterCache < ActsAsTreeTestCase
def setup
teardown_db
setup_db(true)
setup_db counter_cache: true

@root = TreeMixinWithCounterCache.create!
@child1 = TreeMixinWithCounterCache.create! parent_id: @root.id
Expand All @@ -504,10 +472,6 @@ def setup
[@root, @child1, @child1_child1, @child2].map(&:reload)
end

def teardown
teardown_db
end

def test_counter_cache
assert_equal 2, @root.children_count
assert_equal 1, @child1.children_count
Expand Down Expand Up @@ -535,30 +499,24 @@ def test_counter_cache_being_used

class TreeTestWithTouch < ActsAsTreeTestCase
def setup
teardown_db
setup_db

@root = TreeMixinWithTouch.create!
@child = TreeMixinWithTouch.create! parent_id: @root.id
end

def teardown
teardown_db
end

def test_updated_at
previous_root_updated_at = @root.updated_at
@child.update_attributes(:type => "new_type")
@root.reload

assert @root.updated_at != previous_root_updated_at
end
end

class ExternalTreeTest < TreeTest
def setup
teardown_db
setup_db false, true
setup_db external_ids: true
@tree_mixin = ExternalTreeMixin

@root1 = @tree_mixin.create! external_id: 1101
Expand Down

0 comments on commit 145de17

Please sign in to comment.