Commit a52e3edd authored by Tomasz Maczukin's avatar Tomasz Maczukin

Specify default value for Project#build_timeout

parent 909c277e
...@@ -8,14 +8,14 @@ module ChronicDurationAttribute ...@@ -8,14 +8,14 @@ module ChronicDurationAttribute
end end
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) chronic_duration_attr_reader(virtual_attribute, source_attribute)
define_method("#{virtual_attribute}=") do |value| 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 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) assign_attributes(source_attribute => new_value)
rescue ChronicDuration::DurationParseError rescue ChronicDuration::DurationParseError
# ignore error as it will be caught by validation # ignore error as it will be caught by validation
......
...@@ -326,7 +326,7 @@ class Project < ActiveRecord::Base ...@@ -326,7 +326,7 @@ class Project < ActiveRecord::Base
enum auto_cancel_pending_pipelines: { disabled: 0, enabled: 1 } 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, validates :build_timeout, allow_nil: true,
numericality: { greater_than_or_equal_to: 600, numericality: { greater_than_or_equal_to: 600,
......
...@@ -63,8 +63,8 @@ shared_examples 'ChronicDurationAttribute writer' do ...@@ -63,8 +63,8 @@ shared_examples 'ChronicDurationAttribute writer' do
subject.send("#{virtual_field}=", '') subject.send("#{virtual_field}=", '')
end end
it 'writes nil' do it 'writes default value' do
expect(subject.send(source_field)).to be_nil expect(subject.send(source_field)).to eq(default_value)
end end
it 'passes validation' do it 'passes validation' do
...@@ -77,8 +77,8 @@ shared_examples 'ChronicDurationAttribute writer' do ...@@ -77,8 +77,8 @@ shared_examples 'ChronicDurationAttribute writer' do
subject.send("#{virtual_field}=", nil) subject.send("#{virtual_field}=", nil)
end end
it 'writes nil' do it 'writes default value' do
expect(subject.send(source_field)).to be_nil expect(subject.send(source_field)).to eq(default_value)
end end
it 'passes validation' do it 'passes validation' do
...@@ -92,20 +92,34 @@ shared_examples 'ChronicDurationAttribute writer' do ...@@ -92,20 +92,34 @@ shared_examples 'ChronicDurationAttribute writer' do
end end
describe 'ChronicDurationAttribute' do describe 'ChronicDurationAttribute' do
context 'when default value is not set' do
let(:source_field) {:maximum_timeout} let(:source_field) {:maximum_timeout}
let(:virtual_field) {:maximum_timeout_human_readable} 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 reader'
it_behaves_like 'ChronicDurationAttribute writer' 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 end
describe 'ChronicDurationAttribute - reader' do describe 'ChronicDurationAttribute - reader' do
let(:source_field) {:timeout} let(:source_field) {:timeout}
let(:virtual_field) {:timeout_human_readable} 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 it "doesn't contain dynamically created writer method" do
expect(subject.class).not_to be_public_method_defined("#{virtual_field}=") expect(subject.class).not_to be_public_method_defined("#{virtual_field}=")
......
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