Skip to content

Commit 5da6e04

Browse files
committed
More Test cleanup
1 parent a128467 commit 5da6e04

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

spec/model/relation_spec.rb

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,67 @@
33
require 'spec_helper'
44

55
RSpec.describe 'Relation' do
6-
def create_tree(max_level, current_level: 0, node: nil, stop_at: nil)
7-
node = Node.create!(name: 'root') if node.nil?
6+
include TreeMethods
87

9-
1.upto(max_level - current_level) do |index|
10-
child = node.children.create!(name: "child #{index} - level #{current_level}", active: stop_at > current_level)
11-
child.create_node_info(status: stop_at > current_level ? 'foo' : 'bar')
12-
create_tree(max_level, current_level: current_level + 1, node: child, stop_at: stop_at)
13-
end
14-
15-
node
16-
end
8+
let(:root) { create_tree(4, stop_at: 2) }
179

18-
before do
19-
@root = create_tree(4, stop_at: 2)
20-
@child = @root.children.first
21-
end
10+
describe '#descendants' do
11+
context 'with simple relation' do
12+
let(:descendants) { root.descendants { |opts| opts.condition = Node.where(active: true) }.to_a }
2213

23-
context 'descendants' do
24-
it 'works with simple relation' do
25-
desc = @root.descendants { |opts| opts.condition = Node.where(active: true) }
26-
desc.all.each do |node|
27-
expect(node.active).to be_truthy
14+
it 'returns only active nodes' do
15+
descendants.each do |node|
16+
expect(node.active).to be_truthy
17+
end
2818
end
2919
end
3020

31-
it 'works with joins relation' do
32-
desc = @root.descendants { |opts| opts.condition = Node.joins(:node_info).where.not(node_infos: { status: 'bar' }) }
33-
desc.all.each do |node|
34-
expect(node.node_info.status).to eql('foo')
21+
context 'with condition on joined association' do
22+
let(:descendants) do
23+
root.descendants do |opts|
24+
opts.condition = Node.joins(:node_info).where.not(node_infos: { status: 'bar' })
25+
end
26+
end
27+
28+
it 'returns only node with condition fulfilled' do
29+
descendants.each do |node|
30+
expect(node.node_info.status).to eql('foo')
31+
end
3532
end
3633
end
3734
end
3835

39-
context 'ancestors' do
40-
it 'works with simple relation' do
41-
ancestors = @root.leaves.first.ancestors { |opts| opts.condition = Node.where(active: false) }.to_a
36+
describe '#ancestors' do
37+
context 'with simple_relation' do
38+
let(:ancestors) { root.leaves.first.ancestors { |opts| opts.condition = Node.where(active: false) }.to_a }
4239

43-
ancestors.each do |node|
44-
expect(node.active).to be_falsey
40+
it 'return only active nodes' do
41+
ancestors.each do |node|
42+
expect(node.active).to be_falsey
43+
end
4544
end
4645

47-
expect(ancestors).not_to include(@root)
46+
it 'does not return the root node' do
47+
expect(ancestors).not_to include(root)
48+
end
4849
end
4950

50-
it 'works with joins relation' do
51-
ancestors = @root.leaves.first.ancestors { |opts| opts.condition = Node.joins(:node_info).where.not(node_infos: { status: 'foo' }) }
52-
ancestors.all.each do |node|
53-
expect(node.node_info.status).to eql('bar')
51+
context 'with condition on joined association' do
52+
let(:ancestors) do
53+
root.leaves.first.ancestors do |opts|
54+
opts.condition = Node.joins(:node_info).where.not(node_infos: { status: 'foo' })
55+
end
56+
end
57+
58+
it 'return only nodes for the matching condition' do
59+
ancestors.each do |node|
60+
expect(node.node_info.status).to eql('bar')
61+
end
62+
end
63+
64+
it 'does not return the root node' do
65+
expect(ancestors).not_to include(root)
5466
end
55-
expect(ancestors).not_to include(@root)
5667
end
5768
end
5869
end

spec/support/tree_methods.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22

33
# Helper methods for simple tree creation
44
module TreeMethods
5-
def create_tree(max_level, current_level = 0, node = nil, create_node_info: false)
5+
def create_tree(max_level, current_level: 0, node: nil, create_node_info: false, stop_at: -1)
66
node = Node.create!(name: 'root') if node.nil?
77

88
1.upto(max_level - current_level) do |index|
9-
child = node.children.create!(name: "child #{index} - level #{current_level}")
10-
child.create_node_info if create_node_info
11-
create_tree(max_level, current_level + 1, child)
9+
child = node.children.create!(
10+
name: "child #{index} - level #{current_level}",
11+
active: stop_at > current_level
12+
)
13+
14+
child.create_node_info(status: stop_at > current_level ? 'foo' : 'bar') if create_node_info
15+
16+
create_tree(
17+
max_level,
18+
current_level: current_level + 1,
19+
node: child,
20+
create_node_info: create_node_info,
21+
stop_at: stop_at
22+
)
1223
end
1324

1425
node

0 commit comments

Comments
 (0)