From 145de17d2db94d9a39320b8ca834a5ee2c7f82db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Bu=CC=88nemann?= Date: Sun, 14 Aug 2016 01:25:35 +0200 Subject: [PATCH] Cleanup tests * Get rid of teardown / teardown_db * Use an options hash in setup_db --- test/acts_as_tree_test.rb | 58 ++++++--------------------------------- 1 file changed, 8 insertions(+), 50 deletions(-) diff --git a/test/acts_as_tree_test.rb b/test/acts_as_tree_test.rb index 5c2dce9..3f2a8d4 100644 --- a/test/acts_as_tree_test.rb +++ b/test/acts_as_tree_test.rb @@ -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 @@ -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 @@ -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] @@ -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 @@ -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 @@ -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 @@ -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') @@ -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 @@ -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 @@ -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 @@ -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 @@ -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