2023-01-12 17:42:02 +01:00
|
|
|
describe Champs::DatetimeChamp do
|
|
|
|
let(:datetime_champ) { build(:champ_datetime) }
|
|
|
|
|
|
|
|
describe '#convert_to_iso8601' do
|
|
|
|
it 'preserves nil' do
|
|
|
|
champ = champ_with_value(nil)
|
|
|
|
champ.save
|
|
|
|
expect(champ.reload.value).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if empty string' do
|
|
|
|
champ = champ_with_value("")
|
|
|
|
champ.save
|
|
|
|
expect(champ.reload.value).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if not ISO8601' do
|
|
|
|
champ = champ_with_value("12-21-2023 03:20")
|
|
|
|
champ.save
|
|
|
|
expect(champ.reload.value).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to nil if not datetime' do
|
|
|
|
champ = champ_with_value("value")
|
|
|
|
champ.save
|
|
|
|
expect(champ.reload.value).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'preserves if ISO8601' do
|
|
|
|
champ = champ_with_value("2023-12-21T03:20")
|
|
|
|
champ.save
|
2023-07-06 02:53:57 +02:00
|
|
|
expect(champ.reload.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to ISO8601 if form format' do
|
|
|
|
champ = champ_with_value("{3=>21, 2=>12, 1=>2023, 4=>3, 5=>20}")
|
|
|
|
champ.save
|
2023-07-06 02:53:57 +02:00
|
|
|
expect(champ.reload.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts to ISO8601 if old browser form format' do
|
|
|
|
champ = champ_with_value("21/12/2023 03:20")
|
|
|
|
champ.save
|
2023-07-06 02:53:57 +02:00
|
|
|
expect(champ.reload.value).to eq(Time.zone.parse("2023-12-21T03:20:00").iso8601)
|
2023-01-12 17:42:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def champ_with_value(number)
|
|
|
|
datetime_champ.tap { |c| c.value = number }
|
|
|
|
end
|
|
|
|
end
|