debian-mirror-gitlab/spec/lib/gitlab/ci/pipeline_object_hierarchy_spec.rb

161 lines
5.7 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::PipelineObjectHierarchy do
include Ci::SourcePipelineHelpers
let_it_be(:project) { create(:project, :repository) }
let_it_be(:ancestor) { create(:ci_pipeline, project: project) }
let_it_be(:parent) { create(:ci_pipeline, project: project) }
let_it_be(:child) { create(:ci_pipeline, project: project) }
let_it_be(:cousin_parent) { create(:ci_pipeline, project: project) }
let_it_be(:cousin) { create(:ci_pipeline, project: project) }
let_it_be(:triggered_pipeline) { create(:ci_pipeline) }
2021-09-30 23:02:18 +05:30
let_it_be(:triggered_child_pipeline) { create(:ci_pipeline) }
2020-11-24 15:15:51 +05:30
before_all do
create_source_pipeline(ancestor, parent)
create_source_pipeline(ancestor, cousin_parent)
create_source_pipeline(parent, child)
create_source_pipeline(cousin_parent, cousin)
create_source_pipeline(child, triggered_pipeline)
2021-09-30 23:02:18 +05:30
create_source_pipeline(triggered_pipeline, triggered_child_pipeline)
2020-11-24 15:15:51 +05:30
end
describe '#base_and_ancestors' do
it 'includes the base and its ancestors' do
relation = described_class.new(::Ci::Pipeline.where(id: parent.id),
2021-09-30 23:02:18 +05:30
options: { project_condition: :same }).base_and_ancestors
2020-11-24 15:15:51 +05:30
expect(relation).to contain_exactly(ancestor, parent)
end
it 'can find ancestors upto a certain level' do
relation = described_class.new(::Ci::Pipeline.where(id: child.id),
2021-09-30 23:02:18 +05:30
options: { project_condition: :same }).base_and_ancestors(upto: ancestor.id)
2020-11-24 15:15:51 +05:30
expect(relation).to contain_exactly(parent, child)
end
describe 'hierarchy_order option' do
let(:relation) do
described_class.new(::Ci::Pipeline.where(id: child.id),
2021-09-30 23:02:18 +05:30
options: { project_condition: :same }).base_and_ancestors(hierarchy_order: hierarchy_order)
2020-11-24 15:15:51 +05:30
end
context ':asc' do
let(:hierarchy_order) { :asc }
it 'orders by child to ancestor' do
expect(relation).to eq([child, parent, ancestor])
end
end
context ':desc' do
let(:hierarchy_order) { :desc }
it 'orders by ancestor to child' do
expect(relation).to eq([ancestor, parent, child])
end
end
end
end
describe '#base_and_descendants' do
it 'includes the base and its descendants' do
relation = described_class.new(::Ci::Pipeline.where(id: parent.id),
2021-09-30 23:02:18 +05:30
options: { project_condition: :same }).base_and_descendants
2020-11-24 15:15:51 +05:30
expect(relation).to contain_exactly(parent, child)
end
2021-09-30 23:02:18 +05:30
context 'when project_condition: :different' do
it "includes the base and other project pipelines" do
relation = described_class.new(::Ci::Pipeline.where(id: child.id),
options: { project_condition: :different }).base_and_descendants
expect(relation).to contain_exactly(child, triggered_pipeline, triggered_child_pipeline)
end
end
context 'when project_condition: nil' do
it "includes the base and its descendants with other project pipeline" do
relation = described_class.new(::Ci::Pipeline.where(id: parent.id)).base_and_descendants
expect(relation).to contain_exactly(parent, child, triggered_pipeline, triggered_child_pipeline)
end
end
2020-11-24 15:15:51 +05:30
context 'when with_depth is true' do
let(:relation) do
described_class.new(::Ci::Pipeline.where(id: ancestor.id),
2021-09-30 23:02:18 +05:30
options: { project_condition: :same }).base_and_descendants(with_depth: true)
2020-11-24 15:15:51 +05:30
end
it 'includes depth in the results' do
object_depths = {
ancestor.id => 1,
parent.id => 2,
cousin_parent.id => 2,
child.id => 3,
cousin.id => 3
}
relation.each do |object|
expect(object.depth).to eq(object_depths[object.id])
end
end
end
end
describe '#all_objects' do
2021-09-30 23:02:18 +05:30
context 'when passing ancestors_base' do
let(:options) { { project_condition: project_condition } }
let(:ancestors_base) { ::Ci::Pipeline.where(id: child.id) }
subject(:relation) { described_class.new(ancestors_base, options: options).all_objects }
2020-11-24 15:15:51 +05:30
2021-09-30 23:02:18 +05:30
context 'when project_condition: :same' do
let(:project_condition) { :same }
it "includes its ancestors and descendants" do
expect(relation).to contain_exactly(ancestor, parent, child)
end
end
context 'when project_condition: :different' do
let(:project_condition) { :different }
it "includes the base and other project pipelines" do
expect(relation).to contain_exactly(child, triggered_pipeline, triggered_child_pipeline)
end
end
2020-11-24 15:15:51 +05:30
end
2021-09-30 23:02:18 +05:30
context 'when passing ancestors_base and descendants_base' do
let(:options) { { project_condition: project_condition } }
let(:ancestors_base) { ::Ci::Pipeline.where(id: child.id) }
let(:descendants_base) { described_class.new(::Ci::Pipeline.where(id: child.id), options: options).base_and_ancestors }
subject(:relation) { described_class.new(ancestors_base, descendants_base, options: options).all_objects }
context 'when project_condition: :same' do
let(:project_condition) { :same }
2020-11-24 15:15:51 +05:30
2021-09-30 23:02:18 +05:30
it 'returns all family tree' do
expect(relation).to contain_exactly(ancestor, parent, cousin_parent, child, cousin)
end
end
context 'when project_condition: :different' do
let(:project_condition) { :different }
it "includes the base and other project pipelines" do
expect(relation).to contain_exactly(child, triggered_pipeline, triggered_child_pipeline)
end
end
2020-11-24 15:15:51 +05:30
end
end
end