Commit 191cf1fa authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Specify default value for Project#build_timeout

parent 54743cbf6a09
......@@ -8,14 +8,14 @@ def chronic_duration_attr_reader(virtual_attribute, source_attribute)
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
......
......@@ -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,
......
......@@ -63,8 +63,8 @@
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 @@
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 @@
end
describe 'ChronicDurationAttribute' do
let(:source_field) {:maximum_timeout}
let(:virtual_field) {:maximum_timeout_human_readable}
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'
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}=")
......
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