Commit 935dc667 authored by Steve Azzopardi's avatar Steve Azzopardi

Add explicit test for #latest_successful_build_for!

project.latest_successful_build_for! is being tested inside of `describe
explicit. In doing so some duplication was generated but not
`#latest_successful_build_for!` has full coverage unlike before.

Move `create_pipeline` & `create_build` as helper methods for this spec
to reduce duplication.
parent f9c8822a
...@@ -1104,13 +1104,13 @@ describe Project do ...@@ -1104,13 +1104,13 @@ describe Project do
describe '#pipeline_for' do describe '#pipeline_for' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let!(:pipeline) { create_pipeline } let!(:pipeline) { create_pipeline(project) }
shared_examples 'giving the correct pipeline' do shared_examples 'giving the correct pipeline' do
it { is_expected.to eq(pipeline) } it { is_expected.to eq(pipeline) }
context 'return latest' do context 'return latest' do
let!(:pipeline2) { create_pipeline } let!(:pipeline2) { create_pipeline(project) }
it { is_expected.to eq(pipeline2) } it { is_expected.to eq(pipeline2) }
end end
...@@ -1127,13 +1127,6 @@ describe Project do ...@@ -1127,13 +1127,6 @@ describe Project do
it_behaves_like 'giving the correct pipeline' it_behaves_like 'giving the correct pipeline'
end end
def create_pipeline
create(:ci_pipeline,
project: project,
ref: 'master',
sha: project.commit('master').sha)
end
end end
describe '#builds_enabled' do describe '#builds_enabled' do
...@@ -1922,35 +1915,20 @@ describe Project do ...@@ -1922,35 +1915,20 @@ describe Project do
end end
describe '#latest_successful_build_for' do describe '#latest_successful_build_for' do
def create_pipeline(status = 'success')
create(:ci_pipeline, project: project,
sha: project.commit.sha,
ref: project.default_branch,
status: status)
end
def create_build(new_pipeline = pipeline, name = 'test')
create(:ci_build, :success, :artifacts,
pipeline: new_pipeline,
status: new_pipeline.status,
name: name)
end
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:pipeline) { create_pipeline } let(:pipeline) { create_pipeline(project) }
context 'with many builds' do context 'with many builds' do
it 'gives the latest builds from latest pipeline' do it 'gives the latest builds from latest pipeline' do
pipeline1 = create_pipeline pipeline1 = create_pipeline(project)
pipeline2 = create_pipeline pipeline2 = create_pipeline(project)
create_build(pipeline1, 'test') create_build(pipeline1, 'test')
create_build(pipeline1, 'test2') create_build(pipeline1, 'test2')
build1_p2 = create_build(pipeline2, 'test') build1_p2 = create_build(pipeline2, 'test')
create_build(pipeline2, 'test2') create_build(pipeline2, 'test2')
single_build = project.latest_successful_build_for(build1_p2.name) expect(project.latest_successful_build_for(build1_p2.name))
.to eq(build1_p2)
expect(single_build).to eq(build1_p2)
end end
end end
...@@ -1959,31 +1937,79 @@ describe Project do ...@@ -1959,31 +1937,79 @@ describe Project do
context 'standalone pipeline' do context 'standalone pipeline' do
it 'returns builds for ref for default_branch' do it 'returns builds for ref for default_branch' do
single_build = project.latest_successful_build_for(build.name) expect(project.latest_successful_build_for(build.name))
.to eq(build)
end
expect(single_build).to eq(build) it 'returns empty relation if the build cannot be found' do
expect(project.latest_successful_build_for('TAIL'))
.to be_nil
end end
end
it 'returns empty relation if the build cannot be found for #latest_successful_build_for' do context 'with some pending pipeline' do
builds = project.latest_successful_build_for('TAIL') before do
create_build(create_pipeline(project, 'pending'))
end
expect(builds).to be_nil it 'gives the latest build from latest pipeline' do
expect(project.latest_successful_build_for(build.name))
.to eq(build)
end end
end
end
context 'with pending pipeline' do
it 'returns empty relation' do
pipeline.update(status: 'pending')
pending_build = create_build(pipeline)
it 'returns exception if the build cannot be found for #latest_successful_build_for!' do expect(project.latest_successful_build_for(pending_build.name)).to be_nil
expect { project.latest_successful_build_for!(build.name, 'TAIL') }.to raise_error(ActiveRecord::RecordNotFound) end
end
end
describe '#latest_successful_build_for!' do
let(:project) { create(:project, :repository) }
let(:pipeline) { create_pipeline(project) }
context 'with many builds' do
it 'gives the latest builds from latest pipeline' do
pipeline1 = create_pipeline(project)
pipeline2 = create_pipeline(project)
create_build(pipeline1, 'test')
create_build(pipeline1, 'test2')
build1_p2 = create_build(pipeline2, 'test')
create_build(pipeline2, 'test2')
expect(project.latest_successful_build_for(build1_p2.name))
.to eq(build1_p2)
end
end
context 'with succeeded pipeline' do
let!(:build) { create_build }
context 'standalone pipeline' do
it 'returns builds for ref for default_branch' do
expect(project.latest_successful_build_for!(build.name))
.to eq(build)
end
it 'returns exception if the build cannot be found' do
expect { project.latest_successful_build_for!(build.name, 'TAIL') }
.to raise_error(ActiveRecord::RecordNotFound)
end end
end end
context 'with some pending pipeline' do context 'with some pending pipeline' do
before do before do
create_build(create_pipeline('pending')) create_build(create_pipeline(project, 'pending'))
end end
it 'gives the latest build from latest pipeline' do it 'gives the latest build from latest pipeline' do
last_single_build = project.latest_successful_build_for(build.name) expect(project.latest_successful_build_for!(build.name))
.to eq(build)
expect(last_single_build).to eq(build)
end end
end end
end end
...@@ -1993,9 +2019,8 @@ describe Project do ...@@ -1993,9 +2019,8 @@ describe Project do
pipeline.update(status: 'pending') pipeline.update(status: 'pending')
pending_build = create_build(pipeline) pending_build = create_build(pipeline)
expect(project.latest_successful_build_for(pending_build.name)).to be_nil expect { project.latest_successful_build_for!(pending_build.name) }
.to raise_error(ActiveRecord::RecordNotFound)
expect { project.latest_successful_build_for!(pending_build.name) }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end
end end
...@@ -4365,4 +4390,18 @@ describe Project do ...@@ -4365,4 +4390,18 @@ describe Project do
def rugged_config def rugged_config
rugged_repo(project.repository).config rugged_repo(project.repository).config
end end
def create_pipeline(project, status = 'success')
create(:ci_pipeline, project: project,
sha: project.commit.sha,
ref: project.default_branch,
status: status)
end
def create_build(new_pipeline = pipeline, name = 'test')
create(:ci_build, :success, :artifacts,
pipeline: new_pipeline,
status: new_pipeline.status,
name: name)
end
end end
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