From c9853897229ca5585c69c4675cbeefd9ca53147d Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 15:59:31 +0200 Subject: Add stage tests --- app/models/ci/commit.rb | 3 ++- spec/models/ci/commit_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index c77921979a6..46370034f9a 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -81,7 +81,8 @@ module Ci end def stage - builds_without_retry.group(:stage_idx).select(:stage).last + running_or_pending = builds_without_retry.running_or_pending + running_or_pending.limit(1).pluck(:stage).first end def create_builds(ref, tag, user, trigger_request = nil) diff --git a/spec/models/ci/commit_spec.rb b/spec/models/ci/commit_spec.rb index 91cf96a6666..acff1ddf0fc 100644 --- a/spec/models/ci/commit_spec.rb +++ b/spec/models/ci/commit_spec.rb @@ -74,6 +74,40 @@ describe Ci::Commit do it { expect(commit.sha).to start_with(subject) } end + describe :stage do + subject { commit.stage } + + before do + @second = FactoryGirl.create :ci_build, commit: commit, name: 'deploy', stage: 'deploy', stage_idx: 1, status: :pending + @first = FactoryGirl.create :ci_build, commit: commit, name: 'test', stage: 'test', stage_idx: 0, status: :pending + end + + it 'returns first running stage' do + is_expected.to eq('test') + end + + context 'first build succeeded' do + before do + @first.update_attributes(status: :success) + end + + it 'returns last running stage' do + is_expected.to eq('deploy') + end + end + + context 'all builds succeeded' do + before do + @first.update_attributes(status: :success) + @second.update_attributes(status: :success) + end + + it 'returns nil' do + is_expected.to be_nil + end + end + end + describe :create_next_builds do end -- cgit v1.2.1