summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/namespaces/traversal_scope_examples.rb
blob: 0c4e5ce51fc64d8a9fdb09872fe0531545a5e32f (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# 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 '.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 '.roots' do
    context 'with only sub-groups' do
      subject { described_class.where(id: [deep_nested_group_1, nested_group_1, deep_nested_group_2]).roots }

      it { is_expected.to contain_exactly(group_1, group_2) }
    end

    context 'with only root groups' do
      subject { described_class.where(id: [group_1, group_2]).roots }

      it { is_expected.to contain_exactly(group_1, group_2) }
    end

    context 'with all groups' do
      subject { described_class.where(id: groups).roots }

      it { is_expected.to contain_exactly(group_1, group_2) }
    end
  end

  describe '.roots' do
    context "use_traversal_ids_roots feature flag is true" do
      before do
        stub_feature_flags(use_traversal_ids: true)
        stub_feature_flags(use_traversal_ids_roots: true)
      end

      it_behaves_like '.roots'

      it 'not make recursive queries' do
        expect { described_class.where(id: [nested_group_1]).roots.load }.not_to make_queries_matching(/WITH RECURSIVE/)
      end
    end

    context "use_traversal_ids_roots feature flag is false" do
      before do
        stub_feature_flags(use_traversal_ids_roots: false)
      end

      it_behaves_like '.roots'

      it 'makes recursive queries' do
        expect { described_class.where(id: [nested_group_1]).roots.load }.to make_queries_matching(/WITH RECURSIVE/)
      end
    end
  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

    context 'with offset and limit' do
      subject { described_class.where(id: [deep_nested_group_1, deep_nested_group_2]).order(:traversal_ids).offset(1).limit(1).self_and_ancestors }

      it { is_expected.to contain_exactly(group_2, nested_group_2, deep_nested_group_2) }
    end

    context 'with upto' do
      subject { described_class.where(id: deep_nested_group_1).self_and_ancestors(upto: nested_group_1.id) }

      it { is_expected.to contain_exactly(deep_nested_group_1) }
    end
  end

  describe '.self_and_ancestors' do
    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

  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

    context 'with offset and limit' do
      subject do
        described_class
          .where(id: [deep_nested_group_1, deep_nested_group_2])
          .order(:traversal_ids)
          .limit(1)
          .offset(1)
          .self_and_ancestor_ids
          .pluck(:id)
      end

      it { is_expected.to contain_exactly(group_2.id, nested_group_2.id, deep_nested_group_2.id) }
    end
  end

  describe '.self_and_ancestor_ids' do
    it_behaves_like '.self_and_ancestor_ids'

    it 'not 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

  shared_examples '.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) }

      context 'with duplicate descendants' do
        subject { described_class.where(id: [group_1, nested_group_1]).self_and_descendants(include_self: false) }

        it { is_expected.to contain_exactly(nested_group_1, deep_nested_group_1) }
      end
    end

    context 'with offset and limit' do
      subject { described_class.where(id: [group_1, group_2]).order(:traversal_ids).offset(1).limit(1).self_and_descendants }

      it { is_expected.to contain_exactly(group_2, nested_group_2, deep_nested_group_2) }
    end

    context 'with nested query groups' do
      let!(:nested_group_1b) { create(:group, parent: group_1) }
      let!(:deep_nested_group_1b) { create(:group, parent: nested_group_1b) }
      let(:group1_hierarchy) { [group_1, nested_group_1, deep_nested_group_1, nested_group_1b, deep_nested_group_1b] }

      subject { described_class.where(id: [group_1, nested_group_1]).self_and_descendants }

      it { is_expected.to match_array group1_hierarchy }
    end
  end

  describe '.self_and_descendants' do
    include_examples '.self_and_descendants'
  end

  shared_examples '.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

    context 'with offset and limit' do
      subject do
        described_class
          .where(id: [group_1, group_2])
          .order(:traversal_ids)
          .limit(1)
          .offset(1)
          .self_and_descendant_ids
          .pluck(:id)
      end

      it { is_expected.to contain_exactly(group_2.id, nested_group_2.id, deep_nested_group_2.id) }
    end
  end

  describe '.self_and_descendant_ids' do
    include_examples '.self_and_descendant_ids'
  end

  shared_examples '.self_and_hierarchy' do
    let(:base_scope) { Group.where(id: base_groups) }

    subject { base_scope.self_and_hierarchy }

    context 'with ancestors only' do
      let(:base_groups) { [group_1, group_2] }

      it { is_expected.to match_array(groups) }
    end

    context 'with descendants only' do
      let(:base_groups) { [deep_nested_group_1, deep_nested_group_2] }

      it { is_expected.to match_array(groups) }
    end

    context 'nodes with both ancestors and descendants' do
      let(:base_groups) { [nested_group_1, nested_group_2] }

      it { is_expected.to match_array(groups) }
    end

    context 'with duplicate base groups' do
      let(:base_groups) { [nested_group_1, nested_group_1] }

      it { is_expected.to contain_exactly(group_1, nested_group_1, deep_nested_group_1) }
    end
  end

  describe '.self_and_hierarchy' do
    it_behaves_like '.self_and_hierarchy'

    context "use_traversal_ids_for_self_and_hierarchy_scopes feature flag is false" do
      before do
        stub_feature_flags(use_traversal_ids_for_self_and_hierarchy_scopes: false)
      end

      it_behaves_like '.self_and_hierarchy'

      it 'makes recursive queries' do
        base_groups = Group.where(id: nested_group_1)
        expect { base_groups.self_and_hierarchy.load }.to make_queries_matching(/WITH RECURSIVE/)
      end
    end
  end
end