2018-02-20 19:09:59 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-02-20 22:03:12 -05:00
|
|
|
shared_examples 'ChronicDurationAttribute reader' do
|
|
|
|
it 'contains dynamically created reader method' do
|
|
|
|
expect(subject.class).to be_public_method_defined(virtual_field)
|
|
|
|
end
|
2018-02-20 19:09:59 -05:00
|
|
|
|
2018-02-28 14:56:14 -05:00
|
|
|
it 'outputs chronic duration formatted value' do
|
2018-02-20 22:03:12 -05:00
|
|
|
subject.send("#{source_field}=", 120)
|
2018-02-20 19:09:59 -05:00
|
|
|
|
2018-02-20 22:03:12 -05:00
|
|
|
expect(subject.send(virtual_field)).to eq('2m')
|
|
|
|
end
|
2018-02-28 14:56:14 -05:00
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
context 'when value is set to nil' do
|
2018-03-22 19:12:15 -04:00
|
|
|
it 'outputs nil' do
|
2018-03-21 12:22:52 -04:00
|
|
|
subject.send("#{source_field}=", nil)
|
2018-02-28 14:56:14 -05:00
|
|
|
|
2018-03-22 19:12:15 -04:00
|
|
|
expect(subject.send(virtual_field)).to be_nil
|
2018-03-21 12:22:52 -04:00
|
|
|
end
|
2018-02-28 14:56:14 -05:00
|
|
|
end
|
2018-02-20 22:03:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'ChronicDurationAttribute writer' do
|
|
|
|
it 'contains dynamically created writer method' do
|
|
|
|
expect(subject.class).to be_public_method_defined("#{virtual_field}=")
|
|
|
|
end
|
2018-02-20 19:09:59 -05:00
|
|
|
|
2018-03-22 19:12:15 -04:00
|
|
|
before do
|
2018-02-28 14:56:14 -05:00
|
|
|
subject.send("#{virtual_field}=", '10m')
|
2018-03-22 19:12:15 -04:00
|
|
|
end
|
2018-02-20 19:09:59 -05:00
|
|
|
|
2018-03-22 19:12:15 -04:00
|
|
|
it 'parses chronic duration input' do
|
2018-02-20 22:03:12 -05:00
|
|
|
expect(subject.send(source_field)).to eq(600)
|
2018-02-20 19:09:59 -05:00
|
|
|
end
|
2018-02-28 14:56:14 -05:00
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
it 'passes validation' do
|
|
|
|
expect(subject.valid?).to be_truthy
|
2018-02-28 14:56:14 -05:00
|
|
|
end
|
2018-03-05 14:22:00 -05:00
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
context 'when negative input is used' do
|
|
|
|
before do
|
|
|
|
subject.send("#{source_field}=", 3600)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise exception" do
|
|
|
|
expect { subject.send("#{virtual_field}=", '-10m') }.not_to raise_error(ChronicDuration::DurationParseError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't change value" do
|
|
|
|
expect { subject.send("#{virtual_field}=", '-10m') }.not_to change { subject.send(source_field) }
|
|
|
|
end
|
2018-03-05 14:22:00 -05:00
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
it "doesn't pass validation" do
|
|
|
|
subject.send("#{virtual_field}=", '-10m')
|
2018-03-05 14:22:00 -05:00
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
expect(subject.valid?).to be_falsey
|
2018-12-05 07:09:43 -05:00
|
|
|
expect(subject.errors&.messages)
|
|
|
|
.to include(base: ['Maximum job timeout has a value which could not be accepted'])
|
2018-03-21 12:22:52 -04:00
|
|
|
end
|
2018-03-05 14:22:00 -05:00
|
|
|
end
|
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
context 'when empty input is used' do
|
2018-03-22 19:12:15 -04:00
|
|
|
before do
|
2018-03-21 12:22:52 -04:00
|
|
|
subject.send("#{virtual_field}=", '')
|
2018-03-22 19:12:15 -04:00
|
|
|
end
|
2018-03-21 12:22:52 -04:00
|
|
|
|
2018-04-04 15:31:56 -04:00
|
|
|
it 'writes default value' do
|
|
|
|
expect(subject.send(source_field)).to eq(default_value)
|
2018-03-21 12:22:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes validation' do
|
|
|
|
expect(subject.valid?).to be_truthy
|
|
|
|
end
|
2018-03-05 14:22:00 -05:00
|
|
|
end
|
|
|
|
|
2018-03-21 12:22:52 -04:00
|
|
|
context 'when nil input is used' do
|
2018-03-22 19:12:15 -04:00
|
|
|
before do
|
2018-03-21 12:22:52 -04:00
|
|
|
subject.send("#{virtual_field}=", nil)
|
2018-03-22 19:12:15 -04:00
|
|
|
end
|
2018-03-21 12:22:52 -04:00
|
|
|
|
2018-04-04 15:31:56 -04:00
|
|
|
it 'writes default value' do
|
|
|
|
expect(subject.send(source_field)).to eq(default_value)
|
2018-03-21 12:22:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes validation' do
|
|
|
|
expect(subject.valid?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise exception" do
|
|
|
|
expect { subject.send("#{virtual_field}=", nil) }.not_to raise_error(NoMethodError)
|
|
|
|
end
|
2018-03-05 14:22:00 -05:00
|
|
|
end
|
2018-02-20 19:09:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ChronicDurationAttribute' do
|
2018-04-04 15:31:56 -04:00
|
|
|
context 'when default value is not set' do
|
|
|
|
let(:source_field) {:maximum_timeout}
|
|
|
|
let(:virtual_field) {:maximum_timeout_human_readable}
|
|
|
|
let(:default_value) { nil }
|
2018-03-01 13:43:10 -05:00
|
|
|
|
2018-04-04 15:31:56 -04:00
|
|
|
subject { create(:ci_runner) }
|
2018-02-20 22:03:12 -05:00
|
|
|
|
2018-04-04 15:31:56 -04:00
|
|
|
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
|
2018-02-20 22:03:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ChronicDurationAttribute - reader' do
|
2018-03-06 10:25:13 -05:00
|
|
|
let(:source_field) {:timeout}
|
|
|
|
let(:virtual_field) {:timeout_human_readable}
|
2018-03-22 19:12:15 -04:00
|
|
|
|
2018-04-04 15:31:56 -04:00
|
|
|
subject { create(:ci_build).ensure_metadata }
|
2018-02-20 22:03:12 -05:00
|
|
|
|
|
|
|
it "doesn't contain dynamically created writer method" do
|
|
|
|
expect(subject.class).not_to be_public_method_defined("#{virtual_field}=")
|
|
|
|
end
|
2018-02-20 19:09:59 -05:00
|
|
|
|
2018-02-20 22:03:12 -05:00
|
|
|
it_behaves_like 'ChronicDurationAttribute reader'
|
2018-02-20 19:09:59 -05:00
|
|
|
end
|