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 ...@@ -42,9 +42,7 @@ class Projects::PipelinesController < Projects::ApplicationController
end end
def stage def stage
@stage = pipeline.stages.find do |stage| @stage = pipeline.stage(params[:stage])
stage.name == params[:stage]
end
return not_found unless @stage return not_found unless @stage
respond_to do |format| respond_to do |format|
......
...@@ -116,6 +116,11 @@ module Ci ...@@ -116,6 +116,11 @@ module Ci
where.not(duration: nil).sum(:duration) where.not(duration: nil).sum(:duration)
end end
def stage(name)
stage = Ci::Stage.new(self, name: name)
stage unless stage.statuses_count.zero?
end
def stages_count def stages_count
statuses.select(:stage).distinct.count statuses.select(:stage).distinct.count
end end
......
...@@ -18,6 +18,10 @@ module Ci ...@@ -18,6 +18,10 @@ module Ci
name name
end end
def statuses_count
@statuses_count ||= statuses.count
end
def status def status
@status ||= statuses.latest.status @status ||= statuses.latest.status
end end
......
...@@ -152,6 +152,35 @@ describe "Pipelines" do ...@@ -152,6 +152,35 @@ describe "Pipelines" do
end end
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 describe 'POST /:project/pipelines' do
let(:project) { create(:project) } let(:project) { create(:project) }
......
...@@ -175,6 +175,26 @@ describe Ci::Pipeline, models: true do ...@@ -175,6 +175,26 @@ describe Ci::Pipeline, models: true do
end end
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 describe 'state machine' do
let(:current) { Time.now.change(usec: 0) } let(:current) { Time.now.change(usec: 0) }
let(:build) { create_build('build1', 0) } let(:build) { create_build('build1', 0) }
......
...@@ -28,6 +28,17 @@ describe Ci::Stage, models: true do ...@@ -28,6 +28,17 @@ describe Ci::Stage, models: true do
end end
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 describe '#builds' do
let!(:stage_build) { create_job(:ci_build) } let!(:stage_build) { create_job(:ci_build) }
let!(:commit_status) { create_job(:commit_status) } 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