Commit 2b0b53cd authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add tests for stage API endpoint

parent ac86c495
......@@ -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|
......
......@@ -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
......
......@@ -18,6 +18,10 @@ module Ci
name
end
def statuses_count
@statuses_count ||= statuses.count
end
def status
@status ||= statuses.latest.status
end
......
......@@ -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) }
......
......@@ -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) }
......
......@@ -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) }
......
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