Commit 1d78d130 authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'refactor-pipeline-presenter-spec' into 'master'

Update several MergeRequest and Pipeline specs

See merge request gitlab-org/gitlab!28998
parents 78e842de 3464eb65
...@@ -7,7 +7,7 @@ describe Ci::PipelinePresenter do ...@@ -7,7 +7,7 @@ describe Ci::PipelinePresenter do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:current_user) { user } let(:current_user) { user }
let(:project) { create(:project) } let(:project) { create(:project, :test_repo) }
let(:pipeline) { create(:ci_pipeline, project: project) } let(:pipeline) { create(:ci_pipeline, project: project) }
subject(:presenter) do subject(:presenter) do
...@@ -87,34 +87,32 @@ describe Ci::PipelinePresenter do ...@@ -87,34 +87,32 @@ describe Ci::PipelinePresenter do
end end
describe '#name' do describe '#name' do
before do
allow(pipeline).to receive(:merge_request_event_type) { event_type }
end
subject { presenter.name } subject { presenter.name }
context 'when pipeline is detached merge request pipeline' do context 'for a detached merge request pipeline' do
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) } let(:event_type) { :detached }
let(:pipeline) { merge_request.all_pipelines.last }
it { is_expected.to eq('Detached merge request pipeline') } it { is_expected.to eq('Detached merge request pipeline') }
end end
context 'when pipeline is merge request pipeline' do context 'for a merged result pipeline' do
let(:merge_request) { create(:merge_request, :with_merge_request_pipeline) } let(:event_type) { :merged_result }
let(:pipeline) { merge_request.all_pipelines.last }
it { is_expected.to eq('Merged result pipeline') } it { is_expected.to eq('Merged result pipeline') }
end end
context 'when pipeline is merge train pipeline' do context 'for a merge train pipeline' do
let(:pipeline) { create(:ci_pipeline, project: project) } let(:event_type) { :merge_train }
before do
allow(pipeline).to receive(:merge_request_event_type) { :merge_train }
end
it { is_expected.to eq('Merge train pipeline') } it { is_expected.to eq('Merge train pipeline') }
end end
context 'when pipeline is branch pipeline' do context 'when pipeline is branch pipeline' do
let(:pipeline) { create(:ci_pipeline, project: project) } let(:event_type) { nil }
it { is_expected.to eq('Pipeline') } it { is_expected.to eq('Pipeline') }
end end
...@@ -145,8 +143,6 @@ describe Ci::PipelinePresenter do ...@@ -145,8 +143,6 @@ describe Ci::PipelinePresenter do
end end
context 'when pipeline is branch pipeline' do context 'when pipeline is branch pipeline' do
let(:pipeline) { create(:ci_pipeline, project: project) }
context 'when ref exists in the repository' do context 'when ref exists in the repository' do
before do before do
allow(pipeline).to receive(:ref_exists?) { true } allow(pipeline).to receive(:ref_exists?) { true }
...@@ -165,7 +161,7 @@ describe Ci::PipelinePresenter do ...@@ -165,7 +161,7 @@ describe Ci::PipelinePresenter do
end end
end end
context 'when ref exists in the repository' do context 'when ref does not exist in the repository' do
before do before do
allow(pipeline).to receive(:ref_exists?) { false } allow(pipeline).to receive(:ref_exists?) { false }
end end
...@@ -188,12 +184,17 @@ describe Ci::PipelinePresenter do ...@@ -188,12 +184,17 @@ describe Ci::PipelinePresenter do
describe '#all_related_merge_request_text' do describe '#all_related_merge_request_text' do
subject { presenter.all_related_merge_request_text } subject { presenter.all_related_merge_request_text }
let(:mr_1) { create(:merge_request) }
let(:mr_2) { create(:merge_request) }
context 'with zero related merge requests (branch pipeline)' do context 'with zero related merge requests (branch pipeline)' do
it { is_expected.to eq('No related merge requests found.') } it { is_expected.to eq('No related merge requests found.') }
end end
context 'with one related merge request' do context 'with one related merge request' do
let!(:mr_1) { create(:merge_request, project: project, source_project: project) } before do
allow(pipeline).to receive(:all_merge_requests).and_return(MergeRequest.where(id: mr_1.id))
end
it { it {
is_expected.to eq("1 related merge request: " \ is_expected.to eq("1 related merge request: " \
...@@ -202,8 +203,9 @@ describe Ci::PipelinePresenter do ...@@ -202,8 +203,9 @@ describe Ci::PipelinePresenter do
end end
context 'with two related merge requests' do context 'with two related merge requests' do
let!(:mr_1) { create(:merge_request, project: project, source_project: project, target_branch: 'staging') } before do
let!(:mr_2) { create(:merge_request, project: project, source_project: project, target_branch: 'feature') } allow(pipeline).to receive(:all_merge_requests).and_return(MergeRequest.where(id: [mr_1.id, mr_2.id]))
end
it { it {
is_expected.to eq("2 related merge requests: " \ is_expected.to eq("2 related merge requests: " \
...@@ -223,22 +225,25 @@ describe Ci::PipelinePresenter do ...@@ -223,22 +225,25 @@ describe Ci::PipelinePresenter do
end end
describe '#all_related_merge_requests' do describe '#all_related_merge_requests' do
subject(:all_related_merge_requests) do
presenter.send(:all_related_merge_requests)
end
it 'memoizes the returned relation' do it 'memoizes the returned relation' do
query_count = ActiveRecord::QueryRecorder.new do expect(pipeline).to receive(:all_merge_requests_by_recency).exactly(1).time.and_call_original
3.times { presenter.send(:all_related_merge_requests).count } 2.times { presenter.send(:all_related_merge_requests).count }
end.count end
context 'for a branch pipeline with two open MRs' do
let!(:one) { create(:merge_request, source_project: project, source_branch: pipeline.ref) }
let!(:two) { create(:merge_request, source_project: project, source_branch: pipeline.ref, target_branch: 'wip') }
expect(query_count).to eq(2) it { is_expected.to contain_exactly(one, two) }
end end
context 'permissions' do context 'permissions' do
let!(:merge_request) do let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
create(:merge_request, project: project, source_project: project) let(:pipeline) { merge_request.all_pipelines.take }
end
subject(:all_related_merge_requests) do
presenter.send(:all_related_merge_requests)
end
shared_examples 'private merge requests' do shared_examples 'private merge requests' do
context 'when not logged in' do context 'when not logged in' do
...@@ -315,61 +320,51 @@ describe Ci::PipelinePresenter do ...@@ -315,61 +320,51 @@ describe Ci::PipelinePresenter do
describe '#link_to_merge_request' do describe '#link_to_merge_request' do
subject { presenter.link_to_merge_request } subject { presenter.link_to_merge_request }
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) } context 'with a related merge request' do
let(:pipeline) { merge_request.all_pipelines.last } let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
let(:pipeline) { merge_request.all_pipelines.take }
it 'returns a correct link' do it 'returns a correct link' do
is_expected is_expected.to include(project_merge_request_path(project, merge_request))
.to include(project_merge_request_path(merge_request.project, merge_request)) end
end end
context 'when pipeline is branch pipeline' do context 'when pipeline is branch pipeline' do
let(:pipeline) { create(:ci_pipeline, project: project) } it { is_expected.to be_nil }
it 'returns nothing' do
is_expected.to be_nil
end
end end
end end
describe '#link_to_merge_request_source_branch' do describe '#link_to_merge_request_source_branch' do
subject { presenter.link_to_merge_request_source_branch } subject { presenter.link_to_merge_request_source_branch }
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) } context 'with a related merge request' do
let(:pipeline) { merge_request.all_pipelines.last } let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
let(:pipeline) { merge_request.all_pipelines.take }
it 'returns a correct link' do it 'returns a correct link' do
is_expected is_expected.to include(project_commits_path(project, merge_request.source_branch))
.to include(project_commits_path(merge_request.source_project, end
merge_request.source_branch))
end end
context 'when pipeline is branch pipeline' do context 'when pipeline is branch pipeline' do
let(:pipeline) { create(:ci_pipeline, project: project) } it { is_expected.to be_nil }
it 'returns nothing' do
is_expected.to be_nil
end
end end
end end
describe '#link_to_merge_request_target_branch' do describe '#link_to_merge_request_target_branch' do
subject { presenter.link_to_merge_request_target_branch } subject { presenter.link_to_merge_request_target_branch }
let(:merge_request) { create(:merge_request, :with_merge_request_pipeline) } context 'with a related merge request' do
let(:pipeline) { merge_request.all_pipelines.last } let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline, source_project: project) }
let(:pipeline) { merge_request.all_pipelines.take }
it 'returns a correct link' do it 'returns a correct link' do
is_expected is_expected.to include(project_commits_path(project, merge_request.target_branch))
.to include(project_commits_path(merge_request.target_project, merge_request.target_branch)) end
end end
context 'when pipeline is branch pipeline' do context 'when pipeline is branch pipeline' do
let(:pipeline) { create(:ci_pipeline, project: project) } it { is_expected.to be_nil }
it 'returns nothing' do
is_expected.to be_nil
end
end end
end end
end end
...@@ -22,19 +22,19 @@ describe Ci::ExpirePipelineCacheService do ...@@ -22,19 +22,19 @@ describe Ci::ExpirePipelineCacheService do
end end
it 'invalidates Etag caching for merge request pipelines if pipeline runs on any commit of that source branch' do it 'invalidates Etag caching for merge request pipelines if pipeline runs on any commit of that source branch' do
pipeline = create(:ci_empty_pipeline, status: 'created', project: project, ref: 'master') merge_request = create(:merge_request, :with_detached_merge_request_pipeline)
merge_request = create(:merge_request, source_project: project, source_branch: pipeline.ref) project = merge_request.target_project
merge_request_pipelines_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/pipelines.json" merge_request_pipelines_path = "/#{project.full_path}/-/merge_requests/#{merge_request.iid}/pipelines.json"
allow_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch) allow_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch)
expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(merge_request_pipelines_path) expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(merge_request_pipelines_path)
subject.execute(pipeline) subject.execute(merge_request.all_pipelines.last)
end end
it 'updates the cached status for a project' do it 'updates the cached status for a project' do
expect(Gitlab::Cache::Ci::ProjectPipelineStatus).to receive(:update_for_pipeline) expect(Gitlab::Cache::Ci::ProjectPipelineStatus).to receive(:update_for_pipeline).with(pipeline)
.with(pipeline)
subject.execute(pipeline) subject.execute(pipeline)
end end
......
...@@ -8,10 +8,6 @@ describe MergeRequests::AddTodoWhenBuildFailsService do ...@@ -8,10 +8,6 @@ describe MergeRequests::AddTodoWhenBuildFailsService do
let(:sha) { '1234567890abcdef1234567890abcdef12345678' } let(:sha) { '1234567890abcdef1234567890abcdef12345678' }
let(:ref) { merge_request.source_branch } let(:ref) { merge_request.source_branch }
let(:pipeline) do
create(:ci_pipeline, ref: ref, project: project, sha: sha)
end
let(:service) do let(:service) do
described_class.new(project, user, commit_message: 'Awesome message') described_class.new(project, user, commit_message: 'Awesome message')
end end
...@@ -19,12 +15,11 @@ describe MergeRequests::AddTodoWhenBuildFailsService do ...@@ -19,12 +15,11 @@ describe MergeRequests::AddTodoWhenBuildFailsService do
let(:todo_service) { spy('todo service') } let(:todo_service) { spy('todo service') }
let(:merge_request) do let(:merge_request) do
create(:merge_request, merge_user: user, create(:merge_request, :with_detached_merge_request_pipeline, :opened, merge_user: user)
source_branch: 'master', end
target_branch: 'feature',
source_project: project, let(:pipeline) do
target_project: project, merge_request.all_pipelines.take
state: 'opened')
end end
before do before do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment