debian-mirror-gitlab/spec/lib/gitlab/object_hierarchy_spec.rb

187 lines
5.3 KiB
Ruby
Raw Permalink Normal View History

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
2021-06-08 01:23:25 +05:30
let_it_be(:parent, reload: true) { create(:group) }
2021-04-29 21:17:54 +05:30
let_it_be(:child1) { create(:group, parent: parent) }
let_it_be(:child2) { create(:group, parent: child1) }
let(:options) { {} }
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
describe '#base_and_ancestors' do
let(:relation) do
described_class.new(Group.where(id: child2.id), options: options).base_and_ancestors
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
it 'includes the base rows' do
expect(relation).to include(child2)
end
2019-02-15 15:39:39 +05:30
2021-09-30 23:02:18 +05:30
it 'includes all of the ancestors' do
expect(relation).to include(parent, child1)
end
2019-02-15 15:39:39 +05:30
2021-09-30 23:02:18 +05:30
it 'can find ancestors upto a certain level' do
2021-10-27 15:23:28 +05:30
relation = described_class.new(Group.where(id: child2), options: options).base_and_ancestors(upto: child1.id)
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to contain_exactly(child2)
end
2019-02-15 15:39:39 +05:30
2021-09-30 23:02:18 +05:30
it 'uses ancestors_base #initialize argument' do
relation = described_class.new(Group.where(id: child2.id), Group.none, options: options).base_and_ancestors
2019-02-15 15:39:39 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to include(parent, child1, child2)
end
2019-02-15 15:39:39 +05:30
2021-09-30 23:02:18 +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
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
describe 'hierarchy_order option' do
2021-04-17 20:07:23 +05:30
let(:relation) do
2021-09-30 23:02:18 +05:30
described_class.new(Group.where(id: child2.id), options: options).base_and_ancestors(hierarchy_order: hierarchy_order)
2021-04-17 20:07:23 +05:30
end
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
context ':asc' do
let(:hierarchy_order) { :asc }
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
it 'orders by child to parent' do
expect(relation).to eq([child2, child1, parent])
2021-04-17 20:07:23 +05:30
end
2021-09-30 23:02:18 +05:30
end
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
context ':desc' do
let(:hierarchy_order) { :desc }
2019-07-07 11:18:12 +05:30
2021-09-30 23:02:18 +05:30
it 'orders by parent to child' do
expect(relation).to eq([parent, child1, child2])
2019-07-07 11:18:12 +05:30
end
end
end
2021-09-30 23:02:18 +05:30
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
describe '#base_and_descendants' do
let(:relation) do
described_class.new(Group.where(id: parent.id), options: options).base_and_descendants
2018-03-17 18:26:18 +05:30
end
2021-09-30 23:02:18 +05:30
it 'includes the base rows' do
expect(relation).to include(parent)
2019-07-07 11:18:12 +05:30
end
2021-09-30 23:02:18 +05:30
it 'includes all the descendants' do
expect(relation).to include(child1, child2)
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
it 'uses descendants_base #initialize argument' do
relation = described_class.new(Group.none, Group.where(id: parent.id), options: options).base_and_descendants
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to include(parent, child1, child2)
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +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)
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
context 'when with_depth is true' do
2021-04-17 20:07:23 +05:30
let(:relation) do
2021-09-30 23:02:18 +05:30
described_class.new(Group.where(id: parent.id), options: options).base_and_descendants(with_depth: true)
2021-04-17 20:07:23 +05:30
end
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
it 'includes depth in the results' do
object_depths = {
parent.id => 1,
child1.id => 2,
child2.id => 3
}
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
relation.each do |object|
expect(object.depth).to eq(object_depths[object.id])
end
2021-04-17 20:07:23 +05:30
end
2021-09-30 23:02:18 +05:30
end
end
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
describe '#descendants' do
it 'includes only the descendants' do
relation = described_class.new(Group.where(id: parent), options: options).descendants
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to contain_exactly(child1, child2)
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-09-30 23:02:18 +05:30
describe '#max_descendants_depth' do
subject { described_class.new(base_relation, options: options).max_descendants_depth }
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
context 'when base relation is empty' do
let(:base_relation) { Group.where(id: nil) }
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
it { expect(subject).to be_nil }
2021-04-29 21:17:54 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when base has no children' do
let(:base_relation) { Group.where(id: child2) }
it { expect(subject).to eq(1) }
2021-04-29 21:17:54 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when base has grandchildren' do
let(:base_relation) { Group.where(id: parent) }
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
it { expect(subject).to eq(3) }
2021-04-29 21:17:54 +05:30
end
end
2021-09-30 23:02:18 +05:30
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)
2017-09-10 17:25:29 +05:30
end
2021-09-30 23:02:18 +05:30
it 'can find ancestors upto a certain level' do
2021-10-27 15:23:28 +05:30
relation = described_class.new(Group.where(id: child2), options: options).ancestors(upto: child1.id)
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to be_empty
2017-09-10 17:25:29 +05:30
end
2021-09-30 23:02:18 +05:30
end
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
describe '#all_objects' do
let(:relation) do
described_class.new(Group.where(id: child1.id), options: options).all_objects
2021-04-29 21:17:54 +05:30
end
2021-09-30 23:02:18 +05:30
it 'includes the base rows' do
expect(relation).to include(child1)
end
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
it 'includes the ancestors' do
expect(relation).to include(parent)
end
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
it 'includes the descendants' do
expect(relation).to include(child2)
end
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
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
2021-04-29 21:17:54 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to include(parent)
2021-04-29 21:17:54 +05:30
end
2017-09-10 17:25:29 +05:30
2021-09-30 23:02:18 +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), options: options).all_objects
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
expect(relation).to include(child2)
end
2021-04-17 20:07:23 +05:30
2021-09-30 23:02:18 +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)
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
end
end