2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-06 10:29:52 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
describe GroupDescendant do
|
2017-09-06 10:29:52 -04:00
|
|
|
let(:parent) { create(:group) }
|
|
|
|
let(:subgroup) { create(:group, parent: parent) }
|
|
|
|
let(:subsub_group) { create(:group, parent: subgroup) }
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
def all_preloaded_groups(*groups)
|
|
|
|
groups + [parent, subgroup, subsub_group]
|
|
|
|
end
|
|
|
|
|
2017-09-06 10:29:52 -04:00
|
|
|
context 'for a group' do
|
|
|
|
describe '#hierarchy' do
|
2017-10-02 12:21:18 -04:00
|
|
|
it 'only queries once for the ancestors' do
|
|
|
|
# make sure the subsub_group does not have anything cached
|
|
|
|
test_group = create(:group, parent: subsub_group).reload
|
|
|
|
|
|
|
|
query_count = ActiveRecord::QueryRecorder.new { test_group.hierarchy }.count
|
|
|
|
|
|
|
|
expect(query_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only queries once for the ancestors when a top is given' do
|
|
|
|
test_group = create(:group, parent: subsub_group).reload
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
recorder = ActiveRecord::QueryRecorder.new { test_group.hierarchy(subgroup) }
|
|
|
|
expect(recorder.count).to eq(1)
|
2017-10-02 12:21:18 -04:00
|
|
|
end
|
|
|
|
|
2017-09-06 10:29:52 -04:00
|
|
|
it 'builds a hierarchy for a group' do
|
|
|
|
expected_hierarchy = { parent => { subgroup => subsub_group } }
|
|
|
|
|
|
|
|
expect(subsub_group.hierarchy).to eq(expected_hierarchy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'builds a hierarchy upto a specified parent' do
|
|
|
|
expected_hierarchy = { subgroup => subsub_group }
|
|
|
|
|
|
|
|
expect(subsub_group.hierarchy(parent)).to eq(expected_hierarchy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if specifying a base that is not part of the tree' do
|
2017-10-04 10:57:33 -04:00
|
|
|
expect { subsub_group.hierarchy(build_stubbed(:group)) }
|
|
|
|
.to raise_error('specified top is not part of the tree')
|
2017-09-06 10:29:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
describe '.build_hierarchy' do
|
2017-09-07 13:08:56 -04:00
|
|
|
it 'combines hierarchies until the top' do
|
|
|
|
other_subgroup = create(:group, parent: parent)
|
|
|
|
other_subsub_group = create(:group, parent: subgroup)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
groups = all_preloaded_groups(other_subgroup, subsub_group, other_subsub_group)
|
2017-09-07 13:08:56 -04:00
|
|
|
|
|
|
|
expected_hierarchy = { parent => [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }] }
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
expect(described_class.build_hierarchy(groups)).to eq(expected_hierarchy)
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'combines upto a given parent' do
|
|
|
|
other_subgroup = create(:group, parent: parent)
|
|
|
|
other_subsub_group = create(:group, parent: subgroup)
|
|
|
|
|
|
|
|
groups = [other_subgroup, subsub_group, other_subsub_group]
|
2017-10-04 10:57:33 -04:00
|
|
|
groups << subgroup # Add the parent as if it was preloaded
|
2017-09-07 13:08:56 -04:00
|
|
|
|
|
|
|
expected_hierarchy = [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }]
|
2017-09-22 11:36:39 -04:00
|
|
|
expect(described_class.build_hierarchy(groups, parent)).to eq(expected_hierarchy)
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
2017-09-12 13:03:12 -04:00
|
|
|
|
|
|
|
it 'handles building a tree out of order' do
|
|
|
|
other_subgroup = create(:group, parent: parent)
|
|
|
|
other_subgroup2 = create(:group, parent: parent)
|
|
|
|
other_subsub_group = create(:group, parent: other_subgroup)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
groups = all_preloaded_groups(subsub_group, other_subgroup2, other_subsub_group, other_subgroup)
|
2017-09-12 13:03:12 -04:00
|
|
|
expected_hierarchy = { parent => [{ subgroup => subsub_group }, other_subgroup2, { other_subgroup => other_subsub_group }] }
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
expect(described_class.build_hierarchy(groups)).to eq(expected_hierarchy)
|
2017-09-12 13:03:12 -04:00
|
|
|
end
|
2017-10-04 10:57:33 -04:00
|
|
|
|
2018-04-13 12:52:28 -04:00
|
|
|
it 'tracks the exception when a parent was not preloaded' do
|
|
|
|
expect(Gitlab::Sentry).to receive(:track_exception).and_call_original
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
expect { described_class.build_hierarchy([subsub_group]) }.to raise_error(ArgumentError)
|
2018-04-13 12:52:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'recovers if a parent was not reloaded by querying for the parent' do
|
|
|
|
expected_hierarchy = { parent => { subgroup => subsub_group } }
|
|
|
|
|
|
|
|
# this does not raise in production, so stubbing it here.
|
|
|
|
allow(Gitlab::Sentry).to receive(:track_exception)
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
expect(described_class.build_hierarchy([subsub_group])).to eq(expected_hierarchy)
|
2018-04-13 12:52:28 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
it 'raises an error if not all elements were preloaded' do
|
|
|
|
expect { described_class.build_hierarchy([subsub_group]) }
|
2018-04-13 12:52:28 -04:00
|
|
|
.to raise_error(/was not preloaded/)
|
2017-10-04 10:57:33 -04:00
|
|
|
end
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
2017-09-06 10:29:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a project' do
|
|
|
|
let(:project) { create(:project, namespace: subsub_group) }
|
|
|
|
|
|
|
|
describe '#hierarchy' do
|
2017-10-04 10:57:33 -04:00
|
|
|
it 'builds a hierarchy for a project' do
|
2017-09-06 10:29:52 -04:00
|
|
|
expected_hierarchy = { parent => { subgroup => { subsub_group => project } } }
|
|
|
|
|
|
|
|
expect(project.hierarchy).to eq(expected_hierarchy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'builds a hierarchy upto a specified parent' do
|
|
|
|
expected_hierarchy = { subsub_group => project }
|
|
|
|
|
|
|
|
expect(project.hierarchy(subgroup)).to eq(expected_hierarchy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
describe '.build_hierarchy' do
|
2017-09-07 13:08:56 -04:00
|
|
|
it 'combines hierarchies until the top' do
|
|
|
|
other_project = create(:project, namespace: parent)
|
|
|
|
other_subgroup_project = create(:project, namespace: subgroup)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
elements = all_preloaded_groups(other_project, subsub_group, other_subgroup_project)
|
2017-09-07 13:08:56 -04:00
|
|
|
|
|
|
|
expected_hierarchy = { parent => [other_project, { subgroup => [subsub_group, other_subgroup_project] }] }
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
expect(described_class.build_hierarchy(elements)).to eq(expected_hierarchy)
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'combines upto a given parent' do
|
|
|
|
other_project = create(:project, namespace: parent)
|
|
|
|
other_subgroup_project = create(:project, namespace: subgroup)
|
|
|
|
|
|
|
|
elements = [other_project, subsub_group, other_subgroup_project]
|
2017-10-04 10:57:33 -04:00
|
|
|
elements << subgroup # Added as if it was preloaded
|
2017-09-07 13:08:56 -04:00
|
|
|
|
|
|
|
expected_hierarchy = [other_project, { subgroup => [subsub_group, other_subgroup_project] }]
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
expect(described_class.build_hierarchy(elements, parent)).to eq(expected_hierarchy)
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
2017-09-10 11:20:27 -04:00
|
|
|
|
|
|
|
it 'merges to elements in the same hierarchy' do
|
|
|
|
expected_hierarchy = { parent => subgroup }
|
|
|
|
|
2017-09-22 11:36:39 -04:00
|
|
|
expect(described_class.build_hierarchy([parent, subgroup])).to eq(expected_hierarchy)
|
2017-09-10 11:20:27 -04:00
|
|
|
end
|
2017-09-19 07:11:09 -04:00
|
|
|
|
|
|
|
it 'merges complex hierarchies' do
|
|
|
|
project = create(:project, namespace: parent)
|
|
|
|
sub_project = create(:project, namespace: subgroup)
|
|
|
|
subsubsub_group = create(:group, parent: subsub_group)
|
|
|
|
subsub_project = create(:project, namespace: subsub_group)
|
|
|
|
subsubsub_project = create(:project, namespace: subsubsub_group)
|
|
|
|
other_subgroup = create(:group, parent: parent)
|
|
|
|
other_subproject = create(:project, namespace: other_subgroup)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
elements = [project, subsubsub_project, sub_project, other_subproject, subsub_project]
|
|
|
|
# Add parent groups as if they were preloaded
|
|
|
|
elements += [other_subgroup, subsubsub_group, subsub_group, subgroup]
|
2017-09-19 07:11:09 -04:00
|
|
|
|
|
|
|
expected_hierarchy = [
|
|
|
|
project,
|
|
|
|
{
|
|
|
|
subgroup => [
|
|
|
|
{ subsub_group => [{ subsubsub_group => subsubsub_project }, subsub_project] },
|
|
|
|
sub_project
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{ other_subgroup => other_subproject }
|
|
|
|
]
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
actual_hierarchy = described_class.build_hierarchy(elements, parent)
|
2017-09-19 07:11:09 -04:00
|
|
|
|
|
|
|
expect(actual_hierarchy).to eq(expected_hierarchy)
|
|
|
|
end
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
2017-09-06 10:29:52 -04:00
|
|
|
end
|
|
|
|
end
|