From 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 20 Sep 2021 13:18:24 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-3-stable-ee --- .../namespaces/traversal_scope_examples.rb | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) (limited to 'spec/support/shared_examples/namespaces') diff --git a/spec/support/shared_examples/namespaces/traversal_scope_examples.rb b/spec/support/shared_examples/namespaces/traversal_scope_examples.rb index 4d328c03641..74b1bacc560 100644 --- a/spec/support/shared_examples/namespaces/traversal_scope_examples.rb +++ b/spec/support/shared_examples/namespaces/traversal_scope_examples.rb @@ -31,6 +31,131 @@ RSpec.shared_examples 'namespace traversal scopes' do 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 } -- cgit v1.2.3