diff options
Diffstat (limited to 'spec/lib/gitlab/object_hierarchy_spec.rb')
-rw-r--r-- | spec/lib/gitlab/object_hierarchy_spec.rb | 303 |
1 files changed, 108 insertions, 195 deletions
diff --git a/spec/lib/gitlab/object_hierarchy_spec.rb b/spec/lib/gitlab/object_hierarchy_spec.rb index 7615b37521a..64161fbafdd 100644 --- a/spec/lib/gitlab/object_hierarchy_spec.rb +++ b/spec/lib/gitlab/object_hierarchy_spec.rb @@ -9,265 +9,178 @@ RSpec.describe Gitlab::ObjectHierarchy do let(:options) { {} } - shared_context 'Gitlab::ObjectHierarchy test cases' do - describe '#base_and_ancestors' do - let(:relation) do - described_class.new(Group.where(id: child2.id), options: options).base_and_ancestors - end - - it 'includes the base rows' do - expect(relation).to include(child2) - end - - it 'includes all of the ancestors' do - expect(relation).to include(parent, child1) - end - - it 'can find ancestors upto a certain level' do - relation = described_class.new(Group.where(id: child2), options: options).base_and_ancestors(upto: child1) - - expect(relation).to contain_exactly(child2) - end - - it 'uses ancestors_base #initialize argument' do - relation = described_class.new(Group.where(id: child2.id), Group.none, options: options).base_and_ancestors + describe '#base_and_ancestors' do + let(:relation) do + described_class.new(Group.where(id: child2.id), options: options).base_and_ancestors + end - expect(relation).to include(parent, child1, child2) - end + it 'includes the base rows' do + expect(relation).to include(child2) + end - it 'does not allow the use of #update_all' do - expect { relation.update_all(share_with_group_lock: false) } - .to raise_error(ActiveRecord::ReadOnlyRecord) - end + it 'includes all of the ancestors' do + expect(relation).to include(parent, child1) + end - describe 'hierarchy_order option' do - let(:relation) do - described_class.new(Group.where(id: child2.id), options: options).base_and_ancestors(hierarchy_order: hierarchy_order) - end + it 'can find ancestors upto a certain level' do + relation = described_class.new(Group.where(id: child2), options: options).base_and_ancestors(upto: child1) - context ':asc' do - let(:hierarchy_order) { :asc } + expect(relation).to contain_exactly(child2) + end - it 'orders by child to parent' do - expect(relation).to eq([child2, child1, parent]) - end - end + it 'uses ancestors_base #initialize argument' do + relation = described_class.new(Group.where(id: child2.id), Group.none, options: options).base_and_ancestors - context ':desc' do - let(:hierarchy_order) { :desc } + expect(relation).to include(parent, child1, child2) + end - it 'orders by parent to child' do - expect(relation).to eq([parent, child1, child2]) - end - end - end + it 'does not allow the use of #update_all' do + expect { relation.update_all(share_with_group_lock: false) } + .to raise_error(ActiveRecord::ReadOnlyRecord) end - describe '#base_and_descendants' do + describe 'hierarchy_order option' do let(:relation) do - described_class.new(Group.where(id: parent.id), options: options).base_and_descendants - end - - it 'includes the base rows' do - expect(relation).to include(parent) - end - - it 'includes all the descendants' do - expect(relation).to include(child1, child2) + described_class.new(Group.where(id: child2.id), options: options).base_and_ancestors(hierarchy_order: hierarchy_order) end - it 'uses descendants_base #initialize argument' do - relation = described_class.new(Group.none, Group.where(id: parent.id), options: options).base_and_descendants + context ':asc' do + let(:hierarchy_order) { :asc } - expect(relation).to include(parent, child1, child2) - end - - it 'does not allow the use of #update_all' do - expect { relation.update_all(share_with_group_lock: false) } - .to raise_error(ActiveRecord::ReadOnlyRecord) - end - - context 'when with_depth is true' do - let(:relation) do - described_class.new(Group.where(id: parent.id), options: options).base_and_descendants(with_depth: true) + it 'orders by child to parent' do + expect(relation).to eq([child2, child1, parent]) end + end - it 'includes depth in the results' do - object_depths = { - parent.id => 1, - child1.id => 2, - child2.id => 3 - } + context ':desc' do + let(:hierarchy_order) { :desc } - relation.each do |object| - expect(object.depth).to eq(object_depths[object.id]) - end + it 'orders by parent to child' do + expect(relation).to eq([parent, child1, child2]) end end end + end - describe '#descendants' do - it 'includes only the descendants' do - relation = described_class.new(Group.where(id: parent), options: options).descendants - - expect(relation).to contain_exactly(child1, child2) - end + describe '#base_and_descendants' do + let(:relation) do + described_class.new(Group.where(id: parent.id), options: options).base_and_descendants end - describe '#max_descendants_depth' do - subject { described_class.new(base_relation, options: options).max_descendants_depth } - - context 'when base relation is empty' do - let(:base_relation) { Group.where(id: nil) } - - it { expect(subject).to be_nil } - end - - context 'when base has no children' do - let(:base_relation) { Group.where(id: child2) } - - it { expect(subject).to eq(1) } - end - - context 'when base has grandchildren' do - let(:base_relation) { Group.where(id: parent) } - - it { expect(subject).to eq(3) } - end + it 'includes the base rows' do + expect(relation).to include(parent) end - describe '#ancestors' do - it 'includes only the ancestors' do - relation = described_class.new(Group.where(id: child2), options: options).ancestors + it 'includes all the descendants' do + expect(relation).to include(child1, child2) + end - expect(relation).to contain_exactly(child1, parent) - end + it 'uses descendants_base #initialize argument' do + relation = described_class.new(Group.none, Group.where(id: parent.id), options: options).base_and_descendants - it 'can find ancestors upto a certain level' do - relation = described_class.new(Group.where(id: child2), options: options).ancestors(upto: child1) + expect(relation).to include(parent, child1, child2) + end - expect(relation).to be_empty - end + it 'does not allow the use of #update_all' do + expect { relation.update_all(share_with_group_lock: false) } + .to raise_error(ActiveRecord::ReadOnlyRecord) end - describe '#all_objects' do + context 'when with_depth is true' do let(:relation) do - described_class.new(Group.where(id: child1.id), options: options).all_objects + described_class.new(Group.where(id: parent.id), options: options).base_and_descendants(with_depth: true) end - it 'includes the base rows' do - expect(relation).to include(child1) - end - - it 'includes the ancestors' do - expect(relation).to include(parent) - end + it 'includes depth in the results' do + object_depths = { + parent.id => 1, + child1.id => 2, + child2.id => 3 + } - it 'includes the descendants' do - expect(relation).to include(child2) - end - - it 'uses ancestors_base #initialize argument for ancestors' do - relation = described_class.new(Group.where(id: child1.id), Group.where(id: non_existing_record_id), options: options).all_objects - - expect(relation).to include(parent) + relation.each do |object| + expect(object.depth).to eq(object_depths[object.id]) + end end + end + end - it 'uses descendants_base #initialize argument for descendants' do - relation = described_class.new(Group.where(id: non_existing_record_id), Group.where(id: child1.id), options: options).all_objects - - expect(relation).to include(child2) - end + describe '#descendants' do + it 'includes only the descendants' do + relation = described_class.new(Group.where(id: parent), options: options).descendants - it 'does not allow the use of #update_all' do - expect { relation.update_all(share_with_group_lock: false) } - .to raise_error(ActiveRecord::ReadOnlyRecord) - end + expect(relation).to contain_exactly(child1, child2) end end - context 'when the use_distinct_in_object_hierarchy feature flag is enabled' do - before do - stub_feature_flags(use_distinct_in_object_hierarchy: true) - stub_feature_flags(use_distinct_for_all_object_hierarchy: false) - end + describe '#max_descendants_depth' do + subject { described_class.new(base_relation, options: options).max_descendants_depth } - it_behaves_like 'Gitlab::ObjectHierarchy test cases' + context 'when base relation is empty' do + let(:base_relation) { Group.where(id: nil) } - it 'calls DISTINCT' do - expect(child2.self_and_ancestors.to_sql).to include("DISTINCT") + it { expect(subject).to be_nil } end - context 'when use_traversal_ids feature flag is enabled' do - it 'does not call DISTINCT' do - expect(parent.self_and_descendants.to_sql).not_to include("DISTINCT") - end + context 'when base has no children' do + let(:base_relation) { Group.where(id: child2) } + + it { expect(subject).to eq(1) } end - context 'when use_traversal_ids feature flag is disabled' do - before do - stub_feature_flags(use_traversal_ids: false) - end + context 'when base has grandchildren' do + let(:base_relation) { Group.where(id: parent) } - it 'calls DISTINCT' do - expect(parent.self_and_descendants.to_sql).to include("DISTINCT") - end + it { expect(subject).to eq(3) } end end - context 'when the use_distinct_for_all_object_hierarchy feature flag is enabled' do - before do - stub_feature_flags(use_distinct_in_object_hierarchy: false) - stub_feature_flags(use_distinct_for_all_object_hierarchy: true) + describe '#ancestors' do + it 'includes only the ancestors' do + relation = described_class.new(Group.where(id: child2), options: options).ancestors + + expect(relation).to contain_exactly(child1, parent) end - it_behaves_like 'Gitlab::ObjectHierarchy test cases' + it 'can find ancestors upto a certain level' do + relation = described_class.new(Group.where(id: child2), options: options).ancestors(upto: child1) - it 'calls DISTINCT' do - expect(child2.self_and_ancestors.to_sql).to include("DISTINCT") + expect(relation).to be_empty end + end - context 'when use_traversal_ids feature flag is enabled' do - it 'does not call DISTINCT' do - expect(parent.self_and_descendants.to_sql).not_to include("DISTINCT") - end + describe '#all_objects' do + let(:relation) do + described_class.new(Group.where(id: child1.id), options: options).all_objects end - context 'when use_traversal_ids feature flag is disabled' do - before do - stub_feature_flags(use_traversal_ids: false) - end - - it 'calls DISTINCT' do - expect(parent.self_and_descendants.to_sql).to include("DISTINCT") - end + it 'includes the base rows' do + expect(relation).to include(child1) + end - context 'when the skip_ordering option is set' do - let(:options) { { skip_ordering: true } } + it 'includes the ancestors' do + expect(relation).to include(parent) + end - it_behaves_like 'Gitlab::ObjectHierarchy test cases' + it 'includes the descendants' do + expect(relation).to include(child2) + end - it 'does not include ROW_NUMBER()' do - query = described_class.new(Group.where(id: parent.id), options: options).base_and_descendants.to_sql + it 'uses ancestors_base #initialize argument for ancestors' do + relation = described_class.new(Group.where(id: child1.id), Group.where(id: non_existing_record_id), options: options).all_objects - expect(query).to include("DISTINCT") - expect(query).not_to include("ROW_NUMBER()") - end - end + expect(relation).to include(parent) end - end - context 'when the use_distinct_in_object_hierarchy feature flag is disabled' do - before do - stub_feature_flags(use_distinct_in_object_hierarchy: false) - stub_feature_flags(use_distinct_for_all_object_hierarchy: false) - end + it 'uses descendants_base #initialize argument for descendants' do + relation = described_class.new(Group.where(id: non_existing_record_id), Group.where(id: child1.id), options: options).all_objects - it_behaves_like 'Gitlab::ObjectHierarchy test cases' + expect(relation).to include(child2) + end - it 'does not call DISTINCT' do - expect(parent.self_and_descendants.to_sql).not_to include("DISTINCT") - expect(child2.self_and_ancestors.to_sql).not_to include("DISTINCT") + it 'does not allow the use of #update_all' do + expect { relation.update_all(share_with_group_lock: false) } + .to raise_error(ActiveRecord::ReadOnlyRecord) end end end |