Skip to content

Commit 32ba1de

Browse files
authored
Minitest migration part 2 (#406)
* add cuisine_type test * add hierarchy_maintenance test * add metal test
1 parent 9875469 commit 32ba1de

File tree

4 files changed

+75
-76
lines changed

4 files changed

+75
-76
lines changed

spec/closure_tree/hierarchy_maintenance_spec.rb

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
require 'spec_helper'
1+
require 'test_helper'
22

33
def assert_lineage(e, m)
4-
expect(m.parent).to eq(e)
5-
expect(m.self_and_ancestors).to eq([m, e])
4+
assert_equal e, m.parent
5+
assert_equal [m, e], m.self_and_ancestors
66

77
# make sure reloading doesn't affect the self_and_ancestors:
88
m.reload
9-
expect(m.self_and_ancestors).to eq([m, e])
9+
assert_equal [m, e], m.self_and_ancestors
1010
end
1111

12-
RSpec.describe CuisineType do
12+
describe CuisineType do
1313
it "finds self and parents when children << is used" do
1414
e = CuisineType.new(:name => "e")
1515
m = CuisineType.new(:name => "m")
@@ -25,14 +25,14 @@ def assert_lineage(e, m)
2525
end
2626

2727
it "sets the table_name of the hierarchy class properly" do
28-
expect(CuisineTypeHierarchy.table_name).to eq(ActiveRecord::Base.table_name_prefix + "cuisine_type_hierarchies" + ActiveRecord::Base.table_name_suffix)
28+
assert_equal(ActiveRecord::Base.table_name_prefix + "cuisine_type_hierarchies" + ActiveRecord::Base.table_name_suffix, CuisineTypeHierarchy.table_name)
2929
end
3030

3131
it 'fixes self_and_ancestors properly on reparenting' do
3232
a = CuisineType.create! :name => 'a'
3333
b = CuisineType.create! :name => 'b'
34-
expect(b.self_and_ancestors.to_a).to eq([b])
34+
assert_equal([b], b.self_and_ancestors.to_a)
3535
a.children << b
36-
expect(b.self_and_ancestors.to_a).to eq([b, a])
36+
assert_equal([b, a], b.self_and_ancestors.to_a)
3737
end
3838
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'test_helper'
2+
3+
describe ClosureTree::HierarchyMaintenance do
4+
describe '.rebuild!' do
5+
it 'rebuild tree' do
6+
20.times do |counter|
7+
Metal.create(:value => "Nitro-#{counter}", parent: Metal.all.sample)
8+
end
9+
hierarchy_count = MetalHierarchy.count
10+
assert_operator hierarchy_count, :>, (20*2)-1 # shallowest-possible case, where all children use the first root
11+
MetalHierarchy.delete_all
12+
Metal.rebuild!
13+
assert_equal MetalHierarchy.count, hierarchy_count
14+
end
15+
end
16+
17+
describe '.cleanup!' do
18+
before do
19+
@parent = Metal.create(:value => "parent metal")
20+
@child = Metal.create(:value => "child metal", parent: @parent)
21+
MetalHierarchy.delete_all
22+
Metal.rebuild!
23+
end
24+
25+
describe 'when an element is deleted' do
26+
it 'should delete the child hierarchies' do
27+
@child.delete
28+
29+
Metal.cleanup!
30+
31+
assert_empty MetalHierarchy.where(descendant_id: @child.id)
32+
assert_empty MetalHierarchy.where(ancestor_id: @child.id)
33+
end
34+
35+
it 'should not delete the parent hierarchies' do
36+
@child.delete
37+
Metal.cleanup!
38+
assert_equal 1, MetalHierarchy.where(ancestor_id: @parent.id).size
39+
end
40+
41+
it 'should not delete other hierarchies' do
42+
other_parent = Metal.create(:value => "other parent metal")
43+
other_child = Metal.create(:value => "other child metal", parent: other_parent)
44+
Metal.rebuild!
45+
46+
@child.delete
47+
Metal.cleanup!
48+
49+
assert_equal 2, MetalHierarchy.where(ancestor_id: other_parent.id).size
50+
assert_equal 2, MetalHierarchy.where(descendant_id: other_child.id).size
51+
end
52+
end
53+
end
54+
end

spec/closure_tree/metal_spec.rb renamed to test/closure_tree/metal_test.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
require 'spec_helper'
1+
require 'test_helper'
22

3-
RSpec.describe Metal do
3+
describe Metal do
44
describe '#find_or_create_by_path' do
55
def assert_correctness(grandchild)
6-
expect(grandchild).to be_a(Metal)
7-
expect(grandchild.description).to eq('slag')
6+
assert(Metal, grandchild)
7+
assert_equal "slag", grandchild.description
88
child = grandchild.parent
9-
expect(child).to be_a(Unobtanium)
10-
expect(child.description).to eq('frames')
11-
expect(child.value).to eq('child')
9+
assert(Unobtanium, child)
10+
assert_equal "frames", child.description
11+
assert_equal "child", child.value
1212
parent = child.parent
13-
expect(parent).to be_a(Adamantium)
14-
expect(parent.description).to eq('claws')
15-
expect(parent.value).to eq('parent')
13+
assert(Adamantium, parent)
14+
assert_equal "claws", parent.description
15+
assert_equal "parent", parent.value
1616
end
1717

1818
let(:attr_path) do
@@ -45,10 +45,10 @@ def assert_correctness(grandchild)
4545

4646
it 'maintains the current STI subclass if attributes are not specified' do
4747
leaf = Unobtanium.find_or_create_by_path(%w(a b c d))
48-
expect(leaf).to be_a(Unobtanium)
49-
expect(leaf.ancestors.map(&:value)).to eq(%w(c b a))
48+
assert(Unobtanium, leaf)
49+
assert_equal %w(c b a), leaf.ancestors.map(&:value)
5050
leaf.ancestors.each do |anc|
51-
expect(anc).to be_a(Unobtanium)
51+
assert(Unobtanium, anc)
5252
end
5353
end
5454
end

0 commit comments

Comments
 (0)