1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# frozen_string_literal: true
RSpec.shared_examples 'namespace traversal scopes' do
# Hierarchy 1
let_it_be(:group_1) { create(:group) }
let_it_be(:nested_group_1) { create(:group, parent: group_1) }
let_it_be(:deep_nested_group_1) { create(:group, parent: nested_group_1) }
# Hierarchy 2
let_it_be(:group_2) { create(:group) }
let_it_be(:nested_group_2) { create(:group, parent: group_2) }
let_it_be(:deep_nested_group_2) { create(:group, parent: nested_group_2) }
# All groups
let_it_be(:groups) do
[
group_1, nested_group_1, deep_nested_group_1,
group_2, nested_group_2, deep_nested_group_2
]
end
describe '.as_ids' do
subject { described_class.where(id: [group_1, group_2]).as_ids.pluck(:id) }
it { is_expected.to contain_exactly(group_1.id, group_2.id) }
end
describe '.without_sti_condition' do
subject { described_class.without_sti_condition }
it { expect(subject.where_values_hash).not_to have_key(:type) }
end
describe '.order_by_depth' do
subject { described_class.where(id: [group_1, nested_group_1, deep_nested_group_1]).order_by_depth(direction) }
context 'ascending' do
let(:direction) { :asc }
it { is_expected.to eq [deep_nested_group_1, nested_group_1, group_1] }
end
context 'descending' do
let(:direction) { :desc }
it { is_expected.to eq [group_1, nested_group_1, deep_nested_group_1] }
end
end
describe '.normal_select' do
let(:query_result) { described_class.where(id: group_1).normal_select }
subject { query_result.column_names }
it { is_expected.to eq described_class.column_names }
end
shared_examples '.self_and_ancestors' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_ancestors }
it { is_expected.to contain_exactly(group_1, nested_group_1, group_2, nested_group_2) }
context 'when include_self is false' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_ancestors(include_self: false) }
it { is_expected.to contain_exactly(group_1, group_2) }
end
context 'when hierarchy_order is ascending' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_ancestors(hierarchy_order: :asc) }
# Recursive order per level is not defined.
it { is_expected.to contain_exactly(nested_group_1, nested_group_2, group_1, group_2) }
it { expect(subject[0, 2]).to contain_exactly(nested_group_1, nested_group_2) }
it { expect(subject[2, 2]).to contain_exactly(group_1, group_2) }
end
context 'when hierarchy_order is descending' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_ancestors(hierarchy_order: :desc) }
# Recursive order per level is not defined.
it { is_expected.to contain_exactly(nested_group_1, nested_group_2, group_1, group_2) }
it { expect(subject[0, 2]).to contain_exactly(group_1, group_2) }
it { expect(subject[2, 2]).to contain_exactly(nested_group_1, nested_group_2) }
end
end
describe '.self_and_ancestors' do
context "use_traversal_ids_ancestor_scopes feature flag is true" do
before do
stub_feature_flags(use_traversal_ids: true)
stub_feature_flags(use_traversal_ids_for_ancestor_scopes: true)
end
it_behaves_like '.self_and_ancestors'
it 'not make recursive queries' do
expect { described_class.where(id: [nested_group_1]).self_and_ancestors.load }.not_to make_queries_matching(/WITH RECURSIVE/)
end
end
context "use_traversal_ids_ancestor_scopes feature flag is false" do
before do
stub_feature_flags(use_traversal_ids_for_ancestor_scopes: false)
end
it_behaves_like '.self_and_ancestors'
it 'make recursive queries' do
expect { described_class.where(id: [nested_group_1]).self_and_ancestors.load }.to make_queries_matching(/WITH RECURSIVE/)
end
end
end
shared_examples '.self_and_ancestor_ids' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_ancestor_ids.pluck(:id) }
it { is_expected.to contain_exactly(group_1.id, nested_group_1.id, group_2.id, nested_group_2.id) }
context 'when include_self is false' do
subject do
described_class
.where(id: [nested_group_1, nested_group_2])
.self_and_ancestor_ids(include_self: false)
.pluck(:id)
end
it { is_expected.to contain_exactly(group_1.id, group_2.id) }
end
end
describe '.self_and_ancestor_ids' do
context "use_traversal_ids_ancestor_scopes feature flag is true" do
before do
stub_feature_flags(use_traversal_ids: true)
stub_feature_flags(use_traversal_ids_for_ancestor_scopes: true)
end
it_behaves_like '.self_and_ancestor_ids'
it 'make recursive queries' do
expect { described_class.where(id: [nested_group_1]).self_and_ancestor_ids.load }.not_to make_queries_matching(/WITH RECURSIVE/)
end
end
context "use_traversal_ids_ancestor_scopes feature flag is false" do
before do
stub_feature_flags(use_traversal_ids_for_ancestor_scopes: false)
end
it_behaves_like '.self_and_ancestor_ids'
it 'make recursive queries' do
expect { described_class.where(id: [nested_group_1]).self_and_ancestor_ids.load }.to make_queries_matching(/WITH RECURSIVE/)
end
end
end
describe '.self_and_descendants' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_descendants }
it { is_expected.to contain_exactly(nested_group_1, deep_nested_group_1, nested_group_2, deep_nested_group_2) }
context 'with duplicate descendants' do
subject { described_class.where(id: [group_1, group_2, nested_group_1]).self_and_descendants }
it { is_expected.to match_array(groups) }
end
context 'when include_self is false' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_descendants(include_self: false) }
it { is_expected.to contain_exactly(deep_nested_group_1, deep_nested_group_2) }
end
end
describe '.self_and_descendant_ids' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_descendant_ids.pluck(:id) }
it { is_expected.to contain_exactly(nested_group_1.id, deep_nested_group_1.id, nested_group_2.id, deep_nested_group_2.id) }
context 'when include_self is false' do
subject do
described_class
.where(id: [nested_group_1, nested_group_2])
.self_and_descendant_ids(include_self: false)
.pluck(:id)
end
it { is_expected.to contain_exactly(deep_nested_group_1.id, deep_nested_group_2.id) }
end
end
end
|