From b76ae638462ab0f673e5915986070518dd3f9ad3 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 19 Aug 2021 09:08:42 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-2-stable-ee --- .../namespaces/linear_traversal_examples.rb | 23 ------ .../namespaces/traversal_examples.rb | 86 ++++++++++++++++++++-- .../namespaces/traversal_scope_examples.rb | 68 +++++++++++++++++ 3 files changed, 149 insertions(+), 28 deletions(-) delete mode 100644 spec/support/shared_examples/namespaces/linear_traversal_examples.rb create mode 100644 spec/support/shared_examples/namespaces/traversal_scope_examples.rb (limited to 'spec/support/shared_examples/namespaces') diff --git a/spec/support/shared_examples/namespaces/linear_traversal_examples.rb b/spec/support/shared_examples/namespaces/linear_traversal_examples.rb deleted file mode 100644 index 2fd90c36953..00000000000 --- a/spec/support/shared_examples/namespaces/linear_traversal_examples.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -# Traversal examples common to linear and recursive methods are in -# spec/support/shared_examples/namespaces/traversal_examples.rb - -RSpec.shared_examples 'linear namespace traversal' do - context 'when use_traversal_ids feature flag is enabled' do - before do - stub_feature_flags(use_traversal_ids: true) - end - - context 'scopes' do - describe '.as_ids' do - let_it_be(:namespace1) { create(:group) } - let_it_be(:namespace2) { create(:group) } - - subject { Namespace.where(id: [namespace1, namespace2]).as_ids.pluck(:id) } - - it { is_expected.to contain_exactly(namespace1.id, namespace2.id) } - end - end - end -end diff --git a/spec/support/shared_examples/namespaces/traversal_examples.rb b/spec/support/shared_examples/namespaces/traversal_examples.rb index f09634556c3..d126b242fb0 100644 --- a/spec/support/shared_examples/namespaces/traversal_examples.rb +++ b/spec/support/shared_examples/namespaces/traversal_examples.rb @@ -55,12 +55,34 @@ RSpec.shared_examples 'namespace traversal' do end describe '#ancestors' do - it 'returns the correct ancestors' do + before do # #reload is called to make sure traversal_ids are reloaded - expect(very_deep_nested_group.reload.ancestors).to contain_exactly(group, nested_group, deep_nested_group) - expect(deep_nested_group.reload.ancestors).to contain_exactly(group, nested_group) - expect(nested_group.reload.ancestors).to contain_exactly(group) - expect(group.reload.ancestors).to eq([]) + reload_models(group, nested_group, deep_nested_group, very_deep_nested_group) + end + + it 'returns the correct ancestors' do + expect(very_deep_nested_group.ancestors).to contain_exactly(group, nested_group, deep_nested_group) + expect(deep_nested_group.ancestors).to contain_exactly(group, nested_group) + expect(nested_group.ancestors).to contain_exactly(group) + expect(group.ancestors).to eq([]) + end + + context 'with asc hierarchy_order' do + it 'returns the correct ancestors' do + expect(very_deep_nested_group.ancestors(hierarchy_order: :asc)).to eq [deep_nested_group, nested_group, group] + expect(deep_nested_group.ancestors(hierarchy_order: :asc)).to eq [nested_group, group] + expect(nested_group.ancestors(hierarchy_order: :asc)).to eq [group] + expect(group.ancestors(hierarchy_order: :asc)).to eq([]) + end + end + + context 'with desc hierarchy_order' do + it 'returns the correct ancestors' do + expect(very_deep_nested_group.ancestors(hierarchy_order: :desc)).to eq [group, nested_group, deep_nested_group] + expect(deep_nested_group.ancestors(hierarchy_order: :desc)).to eq [group, nested_group] + expect(nested_group.ancestors(hierarchy_order: :desc)).to eq [group] + expect(group.ancestors(hierarchy_order: :desc)).to eq([]) + end end describe '#recursive_ancestors' do @@ -78,6 +100,24 @@ RSpec.shared_examples 'namespace traversal' do expect(group.ancestor_ids).to be_empty end + context 'with asc hierarchy_order' do + it 'returns the correct ancestor ids' do + expect(very_deep_nested_group.ancestor_ids(hierarchy_order: :asc)).to eq [deep_nested_group.id, nested_group.id, group.id] + expect(deep_nested_group.ancestor_ids(hierarchy_order: :asc)).to eq [nested_group.id, group.id] + expect(nested_group.ancestor_ids(hierarchy_order: :asc)).to eq [group.id] + expect(group.ancestor_ids(hierarchy_order: :asc)).to eq([]) + end + end + + context 'with desc hierarchy_order' do + it 'returns the correct ancestor ids' do + expect(very_deep_nested_group.ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id, deep_nested_group.id] + expect(deep_nested_group.ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id] + expect(nested_group.ancestor_ids(hierarchy_order: :desc)).to eq [group.id] + expect(group.ancestor_ids(hierarchy_order: :desc)).to eq([]) + end + end + describe '#recursive_ancestor_ids' do let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] } @@ -93,6 +133,24 @@ RSpec.shared_examples 'namespace traversal' do expect(group.self_and_ancestors).to contain_exactly(group) end + context 'with asc hierarchy_order' do + it 'returns the correct ancestors' do + expect(very_deep_nested_group.self_and_ancestors(hierarchy_order: :asc)).to eq [very_deep_nested_group, deep_nested_group, nested_group, group] + expect(deep_nested_group.self_and_ancestors(hierarchy_order: :asc)).to eq [deep_nested_group, nested_group, group] + expect(nested_group.self_and_ancestors(hierarchy_order: :asc)).to eq [nested_group, group] + expect(group.self_and_ancestors(hierarchy_order: :asc)).to eq([group]) + end + end + + context 'with desc hierarchy_order' do + it 'returns the correct ancestors' do + expect(very_deep_nested_group.self_and_ancestors(hierarchy_order: :desc)).to eq [group, nested_group, deep_nested_group, very_deep_nested_group] + expect(deep_nested_group.self_and_ancestors(hierarchy_order: :desc)).to eq [group, nested_group, deep_nested_group] + expect(nested_group.self_and_ancestors(hierarchy_order: :desc)).to eq [group, nested_group] + expect(group.self_and_ancestors(hierarchy_order: :desc)).to eq([group]) + end + end + describe '#recursive_self_and_ancestors' do let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] } @@ -108,6 +166,24 @@ RSpec.shared_examples 'namespace traversal' do expect(group.self_and_ancestor_ids).to contain_exactly(group.id) end + context 'with asc hierarchy_order' do + it 'returns the correct ancestor ids' do + expect(very_deep_nested_group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq [very_deep_nested_group.id, deep_nested_group.id, nested_group.id, group.id] + expect(deep_nested_group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq [deep_nested_group.id, nested_group.id, group.id] + expect(nested_group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq [nested_group.id, group.id] + expect(group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq([group.id]) + end + end + + context 'with desc hierarchy_order' do + it 'returns the correct ancestor ids' do + expect(very_deep_nested_group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id, deep_nested_group.id, very_deep_nested_group.id] + expect(deep_nested_group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id, deep_nested_group.id] + expect(nested_group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id] + expect(group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq([group.id]) + end + end + describe '#recursive_self_and_ancestor_ids' do let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] } diff --git a/spec/support/shared_examples/namespaces/traversal_scope_examples.rb b/spec/support/shared_examples/namespaces/traversal_scope_examples.rb new file mode 100644 index 00000000000..4d328c03641 --- /dev/null +++ b/spec/support/shared_examples/namespaces/traversal_scope_examples.rb @@ -0,0 +1,68 @@ +# 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 '.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 -- cgit v1.2.3