|
3 | 3 | require 'spec_helper'
|
4 | 4 |
|
5 | 5 | 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 |
8 | 7 |
|
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) } |
17 | 9 |
|
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 } |
22 | 13 |
|
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 |
28 | 18 | end
|
29 | 19 | end
|
30 | 20 |
|
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 |
35 | 32 | end
|
36 | 33 | end
|
37 | 34 | end
|
38 | 35 |
|
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 } |
42 | 39 |
|
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 |
45 | 44 | end
|
46 | 45 |
|
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 |
48 | 49 | end
|
49 | 50 |
|
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) |
54 | 66 | end
|
55 |
| - expect(ancestors).not_to include(@root) |
56 | 67 | end
|
57 | 68 | end
|
58 | 69 | end
|
0 commit comments