Add tests for stage API endpoint

This commit is contained in:
Kamil Trzcinski 2016-12-20 11:00:56 +01:00
parent ac86c495a3
commit 2b0b53cddd
No known key found for this signature in database
GPG Key ID: 4505F5C7E12C6A5A
6 changed files with 70 additions and 3 deletions

View File

@ -42,9 +42,7 @@ class Projects::PipelinesController < Projects::ApplicationController
end
def stage
@stage = pipeline.stages.find do |stage|
stage.name == params[:stage]
end
@stage = pipeline.stage(params[:stage])
return not_found unless @stage
respond_to do |format|

View File

@ -116,6 +116,11 @@ module Ci
where.not(duration: nil).sum(:duration)
end
def stage(name)
stage = Ci::Stage.new(self, name: name)
stage unless stage.statuses_count.zero?
end
def stages_count
statuses.select(:stage).distinct.count
end

View File

@ -18,6 +18,10 @@ module Ci
name
end
def statuses_count
@statuses_count ||= statuses.count
end
def status
@status ||= statuses.latest.status
end

View File

@ -152,6 +152,35 @@ describe "Pipelines" do
end
end
describe 'GET /:project/pipelines/stage?name=stage' do
let!(:pipeline) do
create(:ci_empty_pipeline, project: project, ref: 'master',
status: 'running')
end
context 'when accessing existing stage' do
let!(:build) do
create(:ci_build, pipeline: pipeline, stage: 'build')
end
before do
visit stage_namespace_project_pipeline_path(
project.namespace, project, pipeline, format: :json, stage: 'build')
end
it { expect(page).to have_http_status(:ok) }
end
context 'when accessing unknown stage' do
before do
visit stage_namespace_project_pipeline_path(
project.namespace, project, pipeline, format: :json, stage: 'test')
end
it { expect(page).to have_http_status(:not_found) }
end
end
describe 'POST /:project/pipelines' do
let(:project) { create(:project) }

View File

@ -175,6 +175,26 @@ describe Ci::Pipeline, models: true do
end
end
describe '#stage' do
subject { pipeline.stage('test') }
context 'with status in stage' do
let!(:status) { create(:commit_status, pipeline: pipeline, stage: 'test') }
it 'return stage object' do
is_expected.to be_a(Ci::Stage)
end
end
context 'without status in stage' do
let!(:status) { create(:commit_status, pipeline: pipeline, stage: 'build') }
it 'return stage object' do
is_expected.to be_nil
end
end
end
describe 'state machine' do
let(:current) { Time.now.change(usec: 0) }
let(:build) { create_build('build1', 0) }

View File

@ -28,6 +28,17 @@ describe Ci::Stage, models: true do
end
end
describe '#statuses_count' do
let!(:stage_build) { create_job(:ci_build) }
let!(:other_build) { create_job(:ci_build, stage: 'other stage') }
subject { stage.statuses_count }
it "statuses only from current stage" do
is_expected.to eq(1)
end
end
describe '#builds' do
let!(:stage_build) { create_job(:ci_build) }
let!(:commit_status) { create_job(:commit_status) }