2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::ObjectHierarchy do
|
2017-09-10 17:25:29 +05:30
|
|
|
let!(:parent) { create(:group) }
|
|
|
|
let!(:child1) { create(:group, parent: parent) }
|
|
|
|
let!(:child2) { create(:group, parent: child1) }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
shared_context 'Gitlab::ObjectHierarchy test cases' do
|
|
|
|
describe '#base_and_ancestors' do
|
|
|
|
let(:relation) do
|
|
|
|
described_class.new(Group.where(id: child2.id)).base_and_ancestors
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'includes the base rows' do
|
|
|
|
expect(relation).to include(child2)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'includes all of the ancestors' do
|
|
|
|
expect(relation).to include(parent, child1)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'can find ancestors upto a certain level' do
|
|
|
|
relation = described_class.new(Group.where(id: child2)).base_and_ancestors(upto: child1)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(relation).to contain_exactly(child2)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'uses ancestors_base #initialize argument' do
|
|
|
|
relation = described_class.new(Group.where(id: child2.id), Group.none).base_and_ancestors
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(relation).to include(parent, child1, child2)
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'does not allow the use of #update_all' do
|
|
|
|
expect { relation.update_all(share_with_group_lock: false) }
|
|
|
|
.to raise_error(ActiveRecord::ReadOnlyRecord)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe 'hierarchy_order option' do
|
|
|
|
let(:relation) do
|
|
|
|
described_class.new(Group.where(id: child2.id)).base_and_ancestors(hierarchy_order: hierarchy_order)
|
|
|
|
end
|
|
|
|
|
|
|
|
context ':asc' do
|
|
|
|
let(:hierarchy_order) { :asc }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'orders by child to parent' do
|
|
|
|
expect(relation).to eq([child2, child1, parent])
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context ':desc' do
|
|
|
|
let(:hierarchy_order) { :desc }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'orders by parent to child' do
|
|
|
|
expect(relation).to eq([parent, child1, child2])
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe '#base_and_descendants' do
|
|
|
|
let(:relation) do
|
|
|
|
described_class.new(Group.where(id: parent.id)).base_and_descendants
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'includes the base rows' do
|
|
|
|
expect(relation).to include(parent)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'includes all the descendants' do
|
|
|
|
expect(relation).to include(child1, child2)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'uses descendants_base #initialize argument' do
|
|
|
|
relation = described_class.new(Group.none, Group.where(id: parent.id)).base_and_descendants
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(relation).to include(parent, child1, child2)
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'does not allow the use of #update_all' do
|
|
|
|
expect { relation.update_all(share_with_group_lock: false) }
|
|
|
|
.to raise_error(ActiveRecord::ReadOnlyRecord)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when with_depth is true' do
|
|
|
|
let(:relation) do
|
|
|
|
described_class.new(Group.where(id: parent.id)).base_and_descendants(with_depth: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes depth in the results' do
|
|
|
|
object_depths = {
|
|
|
|
parent.id => 1,
|
|
|
|
child1.id => 2,
|
|
|
|
child2.id => 3
|
|
|
|
}
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
relation.each do |object|
|
|
|
|
expect(object.depth).to eq(object_depths[object.id])
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe '#descendants' do
|
|
|
|
it 'includes only the descendants' do
|
|
|
|
relation = described_class.new(Group.where(id: parent)).descendants
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(relation).to contain_exactly(child1, child2)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe '#max_descendants_depth' do
|
|
|
|
subject { described_class.new(base_relation).max_descendants_depth }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when base relation is empty' do
|
|
|
|
let(:base_relation) { Group.where(id: nil) }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it { expect(subject).to be_nil }
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when base has no children' do
|
|
|
|
let(:base_relation) { Group.where(id: child2) }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it { expect(subject).to eq(1) }
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when base has grandchildren' do
|
|
|
|
let(:base_relation) { Group.where(id: parent) }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it { expect(subject).to eq(3) }
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe '#ancestors' do
|
|
|
|
it 'includes only the ancestors' do
|
|
|
|
relation = described_class.new(Group.where(id: child2)).ancestors
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(relation).to contain_exactly(child1, parent)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'can find ancestors upto a certain level' do
|
|
|
|
relation = described_class.new(Group.where(id: child2)).ancestors(upto: child1)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(relation).to be_empty
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
describe '#all_objects' do
|
|
|
|
let(:relation) do
|
|
|
|
described_class.new(Group.where(id: child1.id)).all_objects
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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 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)).all_objects
|
|
|
|
|
|
|
|
expect(relation).to include(parent)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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)).all_objects
|
|
|
|
|
|
|
|
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
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when the use_distinct_in_object_hierarchy feature flag is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(use_distinct_in_object_hierarchy: true)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it_behaves_like 'Gitlab::ObjectHierarchy test cases'
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'calls DISTINCT' do
|
|
|
|
expect(parent.self_and_descendants.to_sql).to include("DISTINCT")
|
|
|
|
expect(child2.self_and_ancestors.to_sql).to include("DISTINCT")
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when the use_distinct_in_object_hierarchy feature flag is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(use_distinct_in_object_hierarchy: false)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it_behaves_like 'Gitlab::ObjectHierarchy test cases'
|
|
|
|
|
|
|
|
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")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|