group_descendant_spec.rb 5.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
require 'spec_helper'

describe GroupDescendant, :nested_groups do
  let(:parent) { create(:group) }
  let(:subgroup) { create(:group, parent: parent) }
  let(:subsub_group) { create(:group, parent: subgroup) }

  context 'for a group' do
    describe '#hierarchy' do
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
      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

        query_count = ActiveRecord::QueryRecorder.new { test_group.hierarchy(subgroup) }.count

        expect(query_count).to eq(1)
      end

27
28
29
30
31
32
33
34
35
36
37
38
39
      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
40
        expect { subsub_group.hierarchy(build_stubbed(:group)) }.to raise_error('specified top is not part of the tree')
41
42
43
      end
    end

44
    describe '.build_hierarchy' do
45
46
47
48
49
50
51
52
      it 'combines hierarchies until the top' do
        other_subgroup = create(:group, parent: parent)
        other_subsub_group = create(:group, parent: subgroup)

        groups = [other_subgroup, subsub_group, other_subsub_group]

        expected_hierarchy = { parent => [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }] }

53
        expect(described_class.build_hierarchy(groups)).to eq(expected_hierarchy)
54
55
56
57
58
59
60
61
62
63
      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]

        expected_hierarchy = [other_subgroup, { subgroup => [subsub_group, other_subsub_group] }]

64
        expect(described_class.build_hierarchy(groups, parent)).to eq(expected_hierarchy)
65
66
67
68
69
70
71
72
73
74
      end

      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)

        groups = [subsub_group, other_subgroup2, other_subsub_group]
        expected_hierarchy = { parent => [{ subgroup => subsub_group }, other_subgroup2, { other_subgroup => other_subsub_group }] }

75
        expect(described_class.build_hierarchy(groups)).to eq(expected_hierarchy)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
      end
    end
  end

  context 'for a project' do
    let(:project) { create(:project, namespace: subsub_group) }

    describe '#hierarchy' do
      it 'builds a hierarchy for a group' do
        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

      it 'raises an error if specifying a base that is not part of the tree' do
97
        expect { project.hierarchy(build_stubbed(:group)) }.to raise_error('specified top is not part of the tree')
98
99
100
      end
    end

101
    describe '.build_hierarchy' do
102
103
104
105
106
107
108
109
      it 'combines hierarchies until the top' do
        other_project = create(:project, namespace: parent)
        other_subgroup_project = create(:project, namespace: subgroup)

        elements = [other_project, subsub_group, other_subgroup_project]

        expected_hierarchy = { parent => [other_project, { subgroup => [subsub_group, other_subgroup_project] }] }

110
        expect(described_class.build_hierarchy(elements)).to eq(expected_hierarchy)
111
112
113
114
115
116
117
118
119
120
      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]

        expected_hierarchy = [other_project, { subgroup => [subsub_group, other_subgroup_project] }]

121
        expect(described_class.build_hierarchy(elements, parent)).to eq(expected_hierarchy)
122
123
124
125
126
      end

      it 'merges to elements in the same hierarchy' do
        expected_hierarchy = { parent => subgroup }

127
        expect(described_class.build_hierarchy([parent, subgroup])).to eq(expected_hierarchy)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
      end

      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)

        projects = [project, subsubsub_project, sub_project, other_subproject, subsub_project]

        expected_hierarchy = [
          project,
          {
            subgroup => [
              { subsub_group => [{ subsubsub_group => subsubsub_project }, subsub_project] },
              sub_project
            ]
          },
          { other_subgroup => other_subproject }
        ]

152
        actual_hierarchy = described_class.build_hierarchy(projects, parent)
153
154
155
156
157
158

        expect(actual_hierarchy).to eq(expected_hierarchy)
      end
    end
  end
end