diff --git a/spec/closure_tree/hierarchy_maintenance_spec.rb b/spec/closure_tree/hierarchy_maintenance_spec.rb deleted file mode 100644 index a4ad09f..0000000 --- a/spec/closure_tree/hierarchy_maintenance_spec.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'spec_helper' - -RSpec.describe ClosureTree::HierarchyMaintenance do - describe '.rebuild!' do - it 'rebuild tree' do - 20.times do |counter| - Metal.create(:value => "Nitro-#{counter}", parent: Metal.all.sample) - end - hierarchy_count = MetalHierarchy.count - expect(hierarchy_count).to be > (20*2)-1 # shallowest-possible case, where all children use the first root - MetalHierarchy.delete_all - Metal.rebuild! - expect(MetalHierarchy.count).to eq(hierarchy_count) - end - end - - describe '.cleanup!' do - let!(:parent) { Metal.create(:value => "parent metal") } - let!(:child) { Metal.create(:value => "child metal", parent: parent) } - - before do - MetalHierarchy.delete_all - Metal.rebuild! - end - - context 'when an element is deleted' do - it 'should delete the child hierarchies' do - child.delete - - Metal.cleanup! - - expect(MetalHierarchy.where(descendant_id: child.id)).to be_empty - expect(MetalHierarchy.where(ancestor_id: child.id)).to be_empty - end - - it 'should not delete the parent hierarchies' do - child.delete - Metal.cleanup! - expect(MetalHierarchy.where(ancestor_id: parent.id).size).to eq 1 - end - - it 'should not delete other hierarchies' do - other_parent = Metal.create(:value => "other parent metal") - other_child = Metal.create(:value => "other child metal", parent: other_parent) - Metal.rebuild! - - child.delete - Metal.cleanup! - - expect(MetalHierarchy.where(ancestor_id: other_parent.id).size).to eq 2 - expect(MetalHierarchy.where(descendant_id: other_child.id).size).to eq 2 - end - end - end -end diff --git a/spec/closure_tree/cuisine_type_spec.rb b/test/closure_tree/cuisine_type_test.rb similarity index 63% rename from spec/closure_tree/cuisine_type_spec.rb rename to test/closure_tree/cuisine_type_test.rb index b59e72e..60fc537 100644 --- a/spec/closure_tree/cuisine_type_spec.rb +++ b/test/closure_tree/cuisine_type_test.rb @@ -1,15 +1,15 @@ -require 'spec_helper' +require 'test_helper' def assert_lineage(e, m) - expect(m.parent).to eq(e) - expect(m.self_and_ancestors).to eq([m, e]) + assert_equal e, m.parent + assert_equal [m, e], m.self_and_ancestors # make sure reloading doesn't affect the self_and_ancestors: m.reload - expect(m.self_and_ancestors).to eq([m, e]) + assert_equal [m, e], m.self_and_ancestors end -RSpec.describe CuisineType do +describe CuisineType do it "finds self and parents when children << is used" do e = CuisineType.new(:name => "e") m = CuisineType.new(:name => "m") @@ -25,14 +25,14 @@ def assert_lineage(e, m) end it "sets the table_name of the hierarchy class properly" do - expect(CuisineTypeHierarchy.table_name).to eq(ActiveRecord::Base.table_name_prefix + "cuisine_type_hierarchies" + ActiveRecord::Base.table_name_suffix) + assert_equal(ActiveRecord::Base.table_name_prefix + "cuisine_type_hierarchies" + ActiveRecord::Base.table_name_suffix, CuisineTypeHierarchy.table_name) end it 'fixes self_and_ancestors properly on reparenting' do a = CuisineType.create! :name => 'a' b = CuisineType.create! :name => 'b' - expect(b.self_and_ancestors.to_a).to eq([b]) + assert_equal([b], b.self_and_ancestors.to_a) a.children << b - expect(b.self_and_ancestors.to_a).to eq([b, a]) + assert_equal([b, a], b.self_and_ancestors.to_a) end end diff --git a/test/closure_tree/hierarchy_maintenance_test.rb b/test/closure_tree/hierarchy_maintenance_test.rb new file mode 100644 index 0000000..60cf1ed --- /dev/null +++ b/test/closure_tree/hierarchy_maintenance_test.rb @@ -0,0 +1,54 @@ +require 'test_helper' + +describe ClosureTree::HierarchyMaintenance do + describe '.rebuild!' do + it 'rebuild tree' do + 20.times do |counter| + Metal.create(:value => "Nitro-#{counter}", parent: Metal.all.sample) + end + hierarchy_count = MetalHierarchy.count + assert_operator hierarchy_count, :>, (20*2)-1 # shallowest-possible case, where all children use the first root + MetalHierarchy.delete_all + Metal.rebuild! + assert_equal MetalHierarchy.count, hierarchy_count + end + end + + describe '.cleanup!' do + before do + @parent = Metal.create(:value => "parent metal") + @child = Metal.create(:value => "child metal", parent: @parent) + MetalHierarchy.delete_all + Metal.rebuild! + end + + describe 'when an element is deleted' do + it 'should delete the child hierarchies' do + @child.delete + + Metal.cleanup! + + assert_empty MetalHierarchy.where(descendant_id: @child.id) + assert_empty MetalHierarchy.where(ancestor_id: @child.id) + end + + it 'should not delete the parent hierarchies' do + @child.delete + Metal.cleanup! + assert_equal 1, MetalHierarchy.where(ancestor_id: @parent.id).size + end + + it 'should not delete other hierarchies' do + other_parent = Metal.create(:value => "other parent metal") + other_child = Metal.create(:value => "other child metal", parent: other_parent) + Metal.rebuild! + + @child.delete + Metal.cleanup! + + assert_equal 2, MetalHierarchy.where(ancestor_id: other_parent.id).size + assert_equal 2, MetalHierarchy.where(descendant_id: other_child.id).size + end + end + end +end diff --git a/spec/closure_tree/metal_spec.rb b/test/closure_tree/metal_test.rb similarity index 70% rename from spec/closure_tree/metal_spec.rb rename to test/closure_tree/metal_test.rb index f29f0af..624c597 100644 --- a/spec/closure_tree/metal_spec.rb +++ b/test/closure_tree/metal_test.rb @@ -1,18 +1,18 @@ -require 'spec_helper' +require 'test_helper' -RSpec.describe Metal do +describe Metal do describe '#find_or_create_by_path' do def assert_correctness(grandchild) - expect(grandchild).to be_a(Metal) - expect(grandchild.description).to eq('slag') + assert(Metal, grandchild) + assert_equal "slag", grandchild.description child = grandchild.parent - expect(child).to be_a(Unobtanium) - expect(child.description).to eq('frames') - expect(child.value).to eq('child') + assert(Unobtanium, child) + assert_equal "frames", child.description + assert_equal "child", child.value parent = child.parent - expect(parent).to be_a(Adamantium) - expect(parent.description).to eq('claws') - expect(parent.value).to eq('parent') + assert(Adamantium, parent) + assert_equal "claws", parent.description + assert_equal "parent", parent.value end let(:attr_path) do @@ -45,10 +45,10 @@ def assert_correctness(grandchild) it 'maintains the current STI subclass if attributes are not specified' do leaf = Unobtanium.find_or_create_by_path(%w(a b c d)) - expect(leaf).to be_a(Unobtanium) - expect(leaf.ancestors.map(&:value)).to eq(%w(c b a)) + assert(Unobtanium, leaf) + assert_equal %w(c b a), leaf.ancestors.map(&:value) leaf.ancestors.each do |anc| - expect(anc).to be_a(Unobtanium) + assert(Unobtanium, anc) end end end