Skip to content

Minitest migration part 2 #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions spec/closure_tree/hierarchy_maintenance_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
54 changes: 54 additions & 0 deletions test/closure_tree/hierarchy_maintenance_test.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 13 additions & 13 deletions spec/closure_tree/metal_spec.rb → test/closure_tree/metal_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down