Added validation in EventSchedule model and test in event_schedule_spec

EventSchedule start time should be in hours range of the conference.Therefore it adds validation on start_time attribute of event schedule model. It also adds test for the same.
This commit is contained in:
Siddhant Bajaj 2017-04-22 01:20:05 +05:30
parent be33f345a1
commit 95d459f058
6 changed files with 48 additions and 8 deletions

View file

@ -10,6 +10,8 @@ class EventSchedule < ActiveRecord::Base
validates :room, presence: true
validates :start_time, presence: true
validates :event, uniqueness: { scope: :schedule }
validate :start_after_end_hour
validate :start_before_start_hour
scope :confirmed, -> { joins(:event).where('state = ?', 'confirmed') }
scope :canceled, -> { joins(:event).where('state = ?', 'canceled') }
@ -37,6 +39,16 @@ class EventSchedule < ActiveRecord::Base
private
def start_after_end_hour
return unless event && start_time && event.program && event.program.conference && event.program.conference.end_hour
errors.add(:start_time, "can't be after the conference end hour (#{event.program.conference.end_hour})") if start_time.hour >= event.program.conference.end_hour
end
def start_before_start_hour
return unless event && start_time && event.program && event.program.conference && event.program.conference.start_hour
errors.add(:start_time, "can't be before the conference start hour (#{event.program.conference.start_hour})") if start_time.hour < event.program.conference.start_hour
end
def conference_id
schedule.program.conference_id
end

View file

@ -23,7 +23,7 @@ describe Admin::EventSchedulesController do
schedule_id: schedule.id,
event_id: create(:event, program: conference.program).id,
room_id: create(:room, venue: venue).id,
start_time: conference.start_date)
start_time: conference.start_date + conference.start_hour.hours)
end
it 'saves the event schedule to the database' do
@ -66,7 +66,7 @@ describe Admin::EventSchedulesController do
schedule_id: schedule.id,
event_id: create(:event, program: conference.program).id,
room_id: room.id,
start_time: conference.start_date)
start_time: conference.start_date + conference.start_hour.hours)
event_schedule.reload
end
@ -75,7 +75,7 @@ describe Admin::EventSchedulesController do
end
it 'updates the start_time' do
expect(event_schedule.start_time).to eq(conference.start_date)
expect(event_schedule.start_time).to eq(conference.start_date + conference.start_hour.hours)
end
it 'has 200 status code' do

View file

@ -9,7 +9,7 @@ FactoryGirl.define do
venue = create(:venue, conference: program.conference)
end
(event_schedule.room = create(:room, venue: venue)) unless event_schedule.room.present?
(event_schedule.start_time = program.conference.start_date.to_time) unless event_schedule.start_time.present?
(event_schedule.start_time = program.conference.start_date + program.conference.start_hour.hours) unless event_schedule.start_time.present?
unless event_schedule.schedule.present?
unless program.selected_schedule.present?
schedule = create(:schedule, program: program)

View file

@ -98,8 +98,8 @@ describe ApplicationHelper, type: :helper do
@other_event = create(:event, program: conference.program, state: 'confirmed')
schedule = create(:schedule, program: conference.program)
conference.program.update_attributes!(selected_schedule: schedule)
@event_schedule = create(:event_schedule, event: event, start_time: conference.start_date, room: create(:room), schedule: schedule)
@other_event_schedule = create(:event_schedule, event: @other_event, start_time: conference.start_date, room: create(:room), schedule: schedule)
@event_schedule = create(:event_schedule, event: event, start_time: conference.start_date + conference.start_hour.hours, room: create(:room), schedule: schedule)
@other_event_schedule = create(:event_schedule, event: @other_event, start_time: conference.start_date + conference.start_hour.hours, room: create(:room), schedule: schedule)
end
describe 'does return correct concurrent events' do

View file

@ -1,6 +1,7 @@
require 'spec_helper'
describe EventSchedule do
let(:conference) { create(:conference) }
describe 'association' do
it { should belong_to(:schedule) }
@ -17,5 +18,32 @@ describe EventSchedule do
it { is_expected.to validate_presence_of(:event) }
it { is_expected.to validate_presence_of(:room) }
it { is_expected.to validate_presence_of(:start_time) }
describe '#start_after_end_hour' do
context 'is invalid' do
it 'when event schedule start_time is after the conference end_hour, and returns an error message' do
new_scheduled_event = build(:event_scheduled, program: conference.program, hour: conference.start_date + conference.end_hour.hours + 1.hour)
expect(new_scheduled_event.valid?).to eq false
expect(new_scheduled_event.event_schedules.first.errors[:start_time]).to eq ["can't be after the conference end hour (#{conference.end_hour})"]
end
end
context 'is valid' do
it 'when event schedule start_time is between the conference end_hour and start_hour' do
new_scheduled_event = build(:event_scheduled, program: conference.program, hour: conference.start_date + conference.end_hour.hours - 1.hour)
expect(new_scheduled_event.valid?).to eq true
end
end
end
describe '#start_before_start_hour' do
context 'is invalid' do
it 'when event schedule start_time is before the conference start_hour, and returns an error message' do
new_scheduled_event = build(:event_scheduled, program: conference.program, hour: conference.start_date)
expect(new_scheduled_event.valid?).to eq false
expect(new_scheduled_event.event_schedules.first.errors[:start_time]).to eq ["can't be before the conference start hour (#{conference.start_hour})"]
end
end
end
end
end

View file

@ -32,7 +32,7 @@ describe EventSerializer, type: :serializer do
before do
event.language = 'English'
event.speakers = [speaker]
create(:event_schedule, event: event, room: room, start_time: Date.new(2014, 03, 04))
create(:event_schedule, event: event, room: room, start_time: Date.new(2014, 03, 04) + 9.hours)
event.track = track
end
@ -42,7 +42,7 @@ describe EventSerializer, type: :serializer do
guid: event.guid,
title: 'Some Talk',
length: 30,
scheduled_date: ' 2014-03-04T00:00:00+0000 ',
scheduled_date: ' 2014-03-04T09:00:00+0000 ',
language: 'English',
abstract: 'Lorem ipsum dolor sit amet',
speaker_ids: [speaker.id],