require 'test_helper'

class DayTest < ActiveSupport::TestCase
  test 'day label matches format' do
    day = create(:day)

    assert_equal day.label, day.start_date.strftime('%Y-%m-%d')
  end

  test 'end date not possible before start date' do
    day = create(:day)
    assert_equal true, day.valid?

    day.start_date = day.start_date.since(3.days)
    assert_equal false, day.valid?

    day = create(:day)
    day.end_date = day.end_date.ago(2.days)
    assert_equal false, day.valid?
  end

  test 'day overlaps with other day' do
    conference = create(:three_day_conference)
    conference.days[1].start_date = conference.days[0].end_date.ago(2.hours)
    assert_equal false, conference.days[1].valid?
  end

  test 'end date is nil' do
    day = create(:day)
    assert_equal true, day.valid?

    day.end_date = nil
    assert_equal false, day.valid?
    assert day.errors.added?  :end_date, :blank
  end

  test 'updating day updates conference' do
    conference = create(:three_day_conference)
    conference.reload
    assert conference.start_date
    assert conference.end_date
    assert conference.subs.last.start_date
    assert conference.subs.last.end_date
  end
end
