1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
# frozen_string_literal: true
RSpec.shared_examples 'namespace traversal' do
shared_examples 'recursive version' do |method|
let(:recursive_method) { "recursive_#{method}" }
it "is equivalent to ##{method}" do
groups.each do |group|
expect(group.public_send(method)).to match_array group.public_send(recursive_method)
end
end
it "makes a recursive query" do
groups.each do |group|
expect { group.public_send(recursive_method).try(:load) }.to make_queries_matching(/WITH RECURSIVE/)
end
end
end
let_it_be(:group) { create(:group) }
let_it_be(:nested_group) { create(:group, parent: group) }
let_it_be(:deep_nested_group) { create(:group, parent: nested_group) }
let_it_be(:very_deep_nested_group) { create(:group, parent: deep_nested_group) }
let_it_be(:groups) { [group, nested_group, deep_nested_group, very_deep_nested_group] }
let_it_be(:project) { create(:project, group: nested_group) }
let_it_be(:project_namespace) { project.project_namespace }
describe '#root_ancestor' do
it 'returns the correct root ancestor' do
expect(group.root_ancestor).to eq(group)
expect(nested_group.root_ancestor).to eq(group)
expect(deep_nested_group.root_ancestor).to eq(group)
end
describe '#recursive_root_ancestor' do
it "is equivalent to #recursive_root_ancestor" do
groups.each do |group|
expect(group.root_ancestor).to eq(group.recursive_root_ancestor)
end
end
end
end
describe '#self_and_hierarchy' do
let!(:another_group) { create(:group) }
let!(:another_group_nested) { create(:group, parent: another_group) }
it 'returns the correct tree' do
expect(group.self_and_hierarchy).to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group)
expect(nested_group.self_and_hierarchy).to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group)
expect(very_deep_nested_group.self_and_hierarchy).to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group)
end
describe '#recursive_self_and_hierarchy' do
it_behaves_like 'recursive version', :self_and_hierarchy
end
end
describe '#ancestors' do
it 'returns the correct ancestors' do
expect(very_deep_nested_group.ancestors).to contain_exactly(group, nested_group, deep_nested_group)
expect(deep_nested_group.ancestors).to contain_exactly(group, nested_group)
expect(nested_group.ancestors).to contain_exactly(group)
expect(group.ancestors).to eq([])
expect(project_namespace.ancestors).to be_empty
end
context 'with asc hierarchy_order' do
it 'returns the correct ancestors' do
expect(very_deep_nested_group.ancestors(hierarchy_order: :asc)).to eq [deep_nested_group, nested_group, group]
expect(deep_nested_group.ancestors(hierarchy_order: :asc)).to eq [nested_group, group]
expect(nested_group.ancestors(hierarchy_order: :asc)).to eq [group]
expect(group.ancestors(hierarchy_order: :asc)).to eq([])
expect(project_namespace.ancestors(hierarchy_order: :asc)).to be_empty
end
end
context 'with desc hierarchy_order' do
it 'returns the correct ancestors' do
expect(very_deep_nested_group.ancestors(hierarchy_order: :desc)).to eq [group, nested_group, deep_nested_group]
expect(deep_nested_group.ancestors(hierarchy_order: :desc)).to eq [group, nested_group]
expect(nested_group.ancestors(hierarchy_order: :desc)).to eq [group]
expect(group.ancestors(hierarchy_order: :desc)).to eq([])
expect(project_namespace.ancestors(hierarchy_order: :desc)).to be_empty
end
end
describe '#recursive_ancestors' do
let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] }
it_behaves_like 'recursive version', :ancestors
end
end
describe '#ancestor_ids' do
it 'returns the correct ancestor ids' do
expect(very_deep_nested_group.ancestor_ids).to contain_exactly(group.id, nested_group.id, deep_nested_group.id)
expect(deep_nested_group.ancestor_ids).to contain_exactly(group.id, nested_group.id)
expect(nested_group.ancestor_ids).to contain_exactly(group.id)
expect(group.ancestor_ids).to be_empty
expect(project_namespace.ancestor_ids).to be_empty
end
context 'with asc hierarchy_order' do
it 'returns the correct ancestor ids' do
expect(very_deep_nested_group.ancestor_ids(hierarchy_order: :asc)).to eq [deep_nested_group.id, nested_group.id, group.id]
expect(deep_nested_group.ancestor_ids(hierarchy_order: :asc)).to eq [nested_group.id, group.id]
expect(nested_group.ancestor_ids(hierarchy_order: :asc)).to eq [group.id]
expect(group.ancestor_ids(hierarchy_order: :asc)).to eq([])
expect(project_namespace.ancestor_ids(hierarchy_order: :asc)).to eq([])
end
end
context 'with desc hierarchy_order' do
it 'returns the correct ancestor ids' do
expect(very_deep_nested_group.ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id, deep_nested_group.id]
expect(deep_nested_group.ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id]
expect(nested_group.ancestor_ids(hierarchy_order: :desc)).to eq [group.id]
expect(group.ancestor_ids(hierarchy_order: :desc)).to eq([])
expect(project_namespace.ancestor_ids(hierarchy_order: :desc)).to eq([])
end
end
describe '#recursive_ancestor_ids' do
let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] }
it_behaves_like 'recursive version', :ancestor_ids
end
end
describe '#self_and_ancestors' do
it 'returns the correct ancestors' do
expect(very_deep_nested_group.self_and_ancestors).to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group)
expect(deep_nested_group.self_and_ancestors).to contain_exactly(group, nested_group, deep_nested_group)
expect(nested_group.self_and_ancestors).to contain_exactly(group, nested_group)
expect(group.self_and_ancestors).to contain_exactly(group)
expect(project_namespace.self_and_ancestors).to contain_exactly(project_namespace)
end
context 'with asc hierarchy_order' do
it 'returns the correct ancestors' do
expect(very_deep_nested_group.self_and_ancestors(hierarchy_order: :asc)).to eq [very_deep_nested_group, deep_nested_group, nested_group, group]
expect(deep_nested_group.self_and_ancestors(hierarchy_order: :asc)).to eq [deep_nested_group, nested_group, group]
expect(nested_group.self_and_ancestors(hierarchy_order: :asc)).to eq [nested_group, group]
expect(group.self_and_ancestors(hierarchy_order: :asc)).to eq([group])
expect(project_namespace.self_and_ancestors(hierarchy_order: :asc)).to eq([project_namespace])
end
end
context 'with desc hierarchy_order' do
it 'returns the correct ancestors' do
expect(very_deep_nested_group.self_and_ancestors(hierarchy_order: :desc)).to eq [group, nested_group, deep_nested_group, very_deep_nested_group]
expect(deep_nested_group.self_and_ancestors(hierarchy_order: :desc)).to eq [group, nested_group, deep_nested_group]
expect(nested_group.self_and_ancestors(hierarchy_order: :desc)).to eq [group, nested_group]
expect(group.self_and_ancestors(hierarchy_order: :desc)).to eq([group])
expect(project_namespace.self_and_ancestors(hierarchy_order: :desc)).to eq([project_namespace])
end
end
describe '#recursive_self_and_ancestors' do
let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] }
it_behaves_like 'recursive version', :self_and_ancestors
end
end
describe '#self_and_ancestor_ids' do
it 'returns the correct ancestor ids' do
expect(very_deep_nested_group.self_and_ancestor_ids).to contain_exactly(group.id, nested_group.id, deep_nested_group.id, very_deep_nested_group.id)
expect(deep_nested_group.self_and_ancestor_ids).to contain_exactly(group.id, nested_group.id, deep_nested_group.id)
expect(nested_group.self_and_ancestor_ids).to contain_exactly(group.id, nested_group.id)
expect(group.self_and_ancestor_ids).to contain_exactly(group.id)
expect(project_namespace.self_and_ancestor_ids).to contain_exactly(project_namespace.id)
end
context 'with asc hierarchy_order' do
it 'returns the correct ancestor ids' do
expect(very_deep_nested_group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq [very_deep_nested_group.id, deep_nested_group.id, nested_group.id, group.id]
expect(deep_nested_group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq [deep_nested_group.id, nested_group.id, group.id]
expect(nested_group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq [nested_group.id, group.id]
expect(group.self_and_ancestor_ids(hierarchy_order: :asc)).to eq([group.id])
expect(project_namespace.self_and_ancestor_ids(hierarchy_order: :asc)).to eq([project_namespace.id])
end
end
context 'with desc hierarchy_order' do
it 'returns the correct ancestor ids' do
expect(very_deep_nested_group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id, deep_nested_group.id, very_deep_nested_group.id]
expect(deep_nested_group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id, deep_nested_group.id]
expect(nested_group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq [group.id, nested_group.id]
expect(group.self_and_ancestor_ids(hierarchy_order: :desc)).to eq([group.id])
expect(project_namespace.self_and_ancestor_ids(hierarchy_order: :desc)).to eq([project_namespace.id])
end
end
describe '#recursive_self_and_ancestor_ids' do
let_it_be(:groups) { [nested_group, deep_nested_group, very_deep_nested_group] }
it_behaves_like 'recursive version', :self_and_ancestor_ids
end
end
shared_examples '#ancestors_upto' do
let(:parent) { create(:group) }
let(:child) { create(:group, parent: parent) }
let(:child2) { create(:group, parent: child) }
it 'returns all ancestors when no namespace is given' do
expect(child2.ancestors_upto).to contain_exactly(child, parent)
end
it 'includes ancestors upto but excluding the given ancestor' do
expect(child2.ancestors_upto(parent)).to contain_exactly(child)
end
context 'with asc hierarchy_order' do
it 'returns the correct ancestor ids' do
expect(child2.ancestors_upto(hierarchy_order: :asc)).to eq([child, parent])
end
end
context 'with desc hierarchy_order' do
it 'returns the correct ancestor ids' do
expect(child2.ancestors_upto(hierarchy_order: :desc)).to eq([parent, child])
end
end
describe '#recursive_self_and_ancestor_ids' do
it 'is equivalent to ancestors_upto' do
recursive_result = child2.recursive_ancestors_upto(parent)
linear_result = child2.ancestors_upto(parent)
expect(linear_result).to match_array recursive_result
end
it 'makes a recursive query' do
expect { child2.recursive_ancestors_upto.try(:load) }.to make_queries_matching(/WITH RECURSIVE/)
end
end
end
describe '#ancestors_upto' do
include_examples '#ancestors_upto'
end
describe '#descendants' do
let!(:another_group) { create(:group) }
let!(:another_group_nested) { create(:group, parent: another_group) }
it 'returns the correct descendants' do
expect(very_deep_nested_group.descendants.to_a).to eq([])
expect(deep_nested_group.descendants.to_a).to include(very_deep_nested_group)
expect(nested_group.descendants.to_a).to include(deep_nested_group, very_deep_nested_group)
expect(group.descendants.to_a).to include(nested_group, deep_nested_group, very_deep_nested_group)
end
describe '#recursive_descendants' do
it_behaves_like 'recursive version', :descendants
end
it 'does not include project namespaces' do
expect(group.descendants.to_a).not_to include(project_namespace)
end
end
describe '#self_and_descendants' do
let!(:another_group) { create(:group) }
let!(:another_group_nested) { create(:group, parent: another_group) }
it 'returns the correct descendants' do
expect(very_deep_nested_group.self_and_descendants).to contain_exactly(very_deep_nested_group)
expect(deep_nested_group.self_and_descendants).to contain_exactly(deep_nested_group, very_deep_nested_group)
expect(nested_group.self_and_descendants).to contain_exactly(nested_group, deep_nested_group, very_deep_nested_group)
expect(group.self_and_descendants).to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group)
end
it 'includes project namespaces when scope is Namespace' do
expect(very_deep_nested_group.self_and_descendants(skope: Namespace))
.to contain_exactly(very_deep_nested_group)
expect(deep_nested_group.self_and_descendants(skope: Namespace))
.to contain_exactly(deep_nested_group, very_deep_nested_group)
expect(nested_group.self_and_descendants(skope: Namespace))
.to contain_exactly(nested_group, deep_nested_group, very_deep_nested_group, project_namespace)
expect(group.self_and_descendants(skope: Namespace))
.to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group, project_namespace)
end
describe '#recursive_self_and_descendants' do
let_it_be(:groups) { [group, nested_group, deep_nested_group] }
it_behaves_like 'recursive version', :self_and_descendants
it 'returns the correct descendants' do
expect(very_deep_nested_group.recursive_self_and_descendants)
.to contain_exactly(very_deep_nested_group)
expect(deep_nested_group.recursive_self_and_descendants)
.to contain_exactly(deep_nested_group, very_deep_nested_group)
expect(nested_group.recursive_self_and_descendants)
.to contain_exactly(nested_group, deep_nested_group, very_deep_nested_group)
expect(group.recursive_self_and_descendants)
.to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group)
end
it 'includes project namespaces when scope is Namespace' do
expect(very_deep_nested_group.recursive_self_and_descendants(skope: Namespace))
.to contain_exactly(very_deep_nested_group)
expect(deep_nested_group.recursive_self_and_descendants(skope: Namespace))
.to contain_exactly(deep_nested_group, very_deep_nested_group)
expect(nested_group.recursive_self_and_descendants(skope: Namespace))
.to contain_exactly(nested_group, deep_nested_group, very_deep_nested_group, project_namespace)
expect(group.recursive_self_and_descendants(skope: Namespace))
.to contain_exactly(group, nested_group, deep_nested_group, very_deep_nested_group, project_namespace)
end
end
end
describe 'all_project_ids' do
it 'is a AR relation' do
expect(group.all_project_ids).to be_kind_of(ActiveRecord::Relation)
end
it_behaves_like 'recursive version', :all_project_ids
end
describe '#self_and_descendant_ids' do
subject { group.self_and_descendant_ids.pluck(:id) }
it { is_expected.to contain_exactly(group.id, nested_group.id, deep_nested_group.id, very_deep_nested_group.id) }
it 'includes project namespaces when when scope is Namespace' do
expect(group.self_and_descendant_ids(skope: Namespace).pluck(:id))
.to contain_exactly(
group.id, nested_group.id, deep_nested_group.id, very_deep_nested_group.id, project_namespace.id
)
end
describe '#recursive_self_and_descendant_ids' do
it_behaves_like 'recursive version', :self_and_descendant_ids
it 'includes project namespaces when when scope is Namespace' do
expect(group.recursive_self_and_descendant_ids(skope: Namespace).pluck(:id))
.to contain_exactly(
group.id, nested_group.id, deep_nested_group.id, very_deep_nested_group.id, project_namespace.id
)
end
end
end
end
|