Specify default value for Project#build_timeout

This commit is contained in:
Tomasz Maczukin 2018-04-04 21:31:56 +02:00
parent 909c277e56
commit a52e3edd1a
No known key found for this signature in database
GPG key ID: 7E9EB2E4B0F625CD
3 changed files with 28 additions and 14 deletions

View file

@ -8,14 +8,14 @@ module ChronicDurationAttribute
end
end
def chronic_duration_attr_writer(virtual_attribute, source_attribute)
def chronic_duration_attr_writer(virtual_attribute, source_attribute, parameters = {})
chronic_duration_attr_reader(virtual_attribute, source_attribute)
define_method("#{virtual_attribute}=") do |value|
chronic_duration_attributes[virtual_attribute] = value.presence || ''
chronic_duration_attributes[virtual_attribute] = value.presence || parameters[:default].presence.to_s
begin
new_value = ChronicDuration.parse(value).to_i if value.present?
new_value = value.present? ? ChronicDuration.parse(value).to_i : parameters[:default].presence
assign_attributes(source_attribute => new_value)
rescue ChronicDuration::DurationParseError
# ignore error as it will be caught by validation

View file

@ -326,7 +326,7 @@ class Project < ActiveRecord::Base
enum auto_cancel_pending_pipelines: { disabled: 0, enabled: 1 }
chronic_duration_attr :build_timeout_human_readable, :build_timeout
chronic_duration_attr :build_timeout_human_readable, :build_timeout, default: 3600
validates :build_timeout, allow_nil: true,
numericality: { greater_than_or_equal_to: 600,

View file

@ -63,8 +63,8 @@ shared_examples 'ChronicDurationAttribute writer' do
subject.send("#{virtual_field}=", '')
end
it 'writes nil' do
expect(subject.send(source_field)).to be_nil
it 'writes default value' do
expect(subject.send(source_field)).to eq(default_value)
end
it 'passes validation' do
@ -77,8 +77,8 @@ shared_examples 'ChronicDurationAttribute writer' do
subject.send("#{virtual_field}=", nil)
end
it 'writes nil' do
expect(subject.send(source_field)).to be_nil
it 'writes default value' do
expect(subject.send(source_field)).to eq(default_value)
end
it 'passes validation' do
@ -92,20 +92,34 @@ shared_examples 'ChronicDurationAttribute writer' do
end
describe 'ChronicDurationAttribute' do
context 'when default value is not set' do
let(:source_field) {:maximum_timeout}
let(:virtual_field) {:maximum_timeout_human_readable}
let(:default_value) { nil }
subject { Ci::Runner.new }
subject { create(:ci_runner) }
it_behaves_like 'ChronicDurationAttribute reader'
it_behaves_like 'ChronicDurationAttribute writer'
end
context 'when default value is set' do
let(:source_field) {:build_timeout}
let(:virtual_field) {:build_timeout_human_readable}
let(:default_value) { 3600 }
subject { create(:project) }
it_behaves_like 'ChronicDurationAttribute reader'
it_behaves_like 'ChronicDurationAttribute writer'
end
end
describe 'ChronicDurationAttribute - reader' do
let(:source_field) {:timeout}
let(:virtual_field) {:timeout_human_readable}
subject {Ci::BuildMetadata.new}
subject { create(:ci_build).ensure_metadata }
it "doesn't contain dynamically created writer method" do
expect(subject.class).not_to be_public_method_defined("#{virtual_field}=")