From a17c90b2a7331a7427813684b04095b55c4b3cc1 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 4 Jul 2017 15:31:15 +0200 Subject: Use enumerated status in persisted stage class --- app/models/ci/stage.rb | 3 +++ app/models/concerns/has_status.rb | 10 ++++++++++ spec/factories/ci/stages.rb | 6 ++++++ spec/migrations/migrate_stages_statuses_spec.rb | 5 +++-- spec/models/ci/stage_spec.rb | 21 +++++++++++++++++++++ 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 spec/models/ci/stage_spec.rb diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb index 59570924c8d..0c7f8c7f485 100644 --- a/app/models/ci/stage.rb +++ b/app/models/ci/stage.rb @@ -1,6 +1,9 @@ module Ci class Stage < ActiveRecord::Base extend Ci::Model + include HasStatus + + enumerated_status! belongs_to :project belongs_to :pipeline diff --git a/app/models/concerns/has_status.rb b/app/models/concerns/has_status.rb index 32af5566135..235196cae13 100644 --- a/app/models/concerns/has_status.rb +++ b/app/models/concerns/has_status.rb @@ -8,6 +8,8 @@ module HasStatus ACTIVE_STATUSES = %w[pending running].freeze COMPLETED_STATUSES = %w[success failed canceled skipped].freeze ORDERED_STATUSES = %w[failed pending running manual canceled success skipped created].freeze + STATUSES_ENUM = { created: 0, pending: 1, running: 2, success: 3, + failed: 4, canceled: 5, skipped: 6, manual: 7 } class_methods do def status_sql @@ -54,6 +56,14 @@ module HasStatus def all_state_names state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) } end + + private + + def enumerated_status! + @status_strategy = :enumerator + + enum status: HasStatus::STATUSES_ENUM + end end included do diff --git a/spec/factories/ci/stages.rb b/spec/factories/ci/stages.rb index d3c8bf9d54f..ee8ac85c92e 100644 --- a/spec/factories/ci/stages.rb +++ b/spec/factories/ci/stages.rb @@ -15,4 +15,10 @@ FactoryGirl.define do warnings: warnings) end end + + factory :ci_stage_entity, class: Ci::Stage do + project factory: :empty_project + pipeline factory: :ci_empty_pipeline + status 'pending' + end end diff --git a/spec/migrations/migrate_stages_statuses_spec.rb b/spec/migrations/migrate_stages_statuses_spec.rb index 81bc38fea10..478ddad262a 100644 --- a/spec/migrations/migrate_stages_statuses_spec.rb +++ b/spec/migrations/migrate_stages_statuses_spec.rb @@ -9,7 +9,6 @@ describe MigrateStagesStatuses, :migration do STATUSES = { created: 0, pending: 1, running: 2, success: 3, failed: 4, canceled: 5, skipped: 6, manual: 7 } - STAGES = { test: 1, build: 2, deploy: 3} before do projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1') @@ -42,8 +41,10 @@ describe MigrateStagesStatuses, :migration do end def create_job(project:, pipeline:, stage:, status:, **opts) + stages = { test: 1, build: 2, deploy: 3} + jobs.create!(project_id: project, commit_id: pipeline, - stage_idx: STAGES[stage.to_sym], stage: stage, + stage_idx: stages[stage.to_sym], stage: stage, status: status, **opts) end end diff --git a/spec/models/ci/stage_spec.rb b/spec/models/ci/stage_spec.rb new file mode 100644 index 00000000000..911c468ff1a --- /dev/null +++ b/spec/models/ci/stage_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Ci::Stage, :models do + describe '#status' do + context 'when stage is pending' do + let(:stage) { create(:ci_stage_entity, status: 'pending') } + + it 'has a correct status value' do + expect(stage.status).to eq 'pending' + end + end + + context 'when stage is success' do + let(:stage) { create(:ci_stage_entity, status: 'success') } + + it 'has a correct status value' do + expect(stage.status).to eq 'success' + end + end + end +end -- cgit v1.2.1