Added tests for program#any_event_for_this_date?

This commit is contained in:
Naman Gupta 2017-11-17 00:02:23 +05:30 committed by Stella Rouzi
parent 6a1d757f43
commit 6eb8c8d6ae
3 changed files with 51 additions and 3 deletions

View file

@ -165,7 +165,7 @@ class Program < ApplicationRecord
# * +False+ -> If there is not any event for the given date # * +False+ -> If there is not any event for the given date
def any_event_for_this_date?(date) def any_event_for_this_date?(date)
parsed_date = DateTime.parse("#{date} 00:00").utc parsed_date = DateTime.parse("#{date} 00:00").utc
EventSchedule.where(schedule: selected_schedule).where(start_time: parsed_date..(parsed_date + 1)).any? EventSchedule.where(schedule: selected_schedule).where(start_time: parsed_date..(parsed_date + 1.day)).any?
end end
## ##

View file

@ -5,7 +5,7 @@ FactoryGirl.define do
title { Faker::Book.title } title { Faker::Book.title }
short_title { SecureRandom.urlsafe_base64(4) } short_title { SecureRandom.urlsafe_base64(4) }
timezone { Faker::Address.time_zone } timezone { Faker::Address.time_zone }
start_date { Date.today } start_date { Date.current }
end_date { 6.days.from_now } end_date { 6.days.from_now }
start_hour 9 start_hour 9
end_hour 20 end_hour 20

View file

@ -2,7 +2,7 @@ require 'spec_helper'
describe Program do describe Program do
subject { create(:program) } subject { create(:program) }
let!(:conference) { create(:conference, end_date: Date.today + 3) } let!(:conference) { create(:conference, end_date: Date.current + 3) }
let!(:program) { conference.program } let!(:program) { conference.program }
describe 'association' do describe 'association' do
@ -241,6 +241,54 @@ describe Program do
end end
end end
describe '#any_event_for_this_date?' do
let(:event){ create(:event, program: program) }
context 'when no schedule is selected for the conference' do
let(:schedule) { create(:schedule, program: program) }
let!(:event_schedule) { create(:event_schedule, event: event, schedule: schedule, start_time: DateTime.parse("#{Date.current + 1} 10:00").utc) }
it 'returns false irrespective of any date' do
expect(program.any_event_for_this_date?(Date.current + 1)).to eq false
end
it 'returns false if date passed is empty' do
expect(program.any_event_for_this_date?('')).to eq false
end
it 'returns false if date passed is nil' do
expect(program.any_event_for_this_date?(nil)).to eq false
end
end
context 'when schedule is selected for the conference' do
let(:schedule) { create(:schedule, program: program) }
let!(:event_schedule) { create(:event_schedule, event: event, schedule: schedule, start_time: DateTime.parse("#{Date.current + 1} 10:00").utc) }
before :each do
program.selected_schedule = event_schedule.schedule
program.save!
end
it 'returns true if there is any event for this date' do
expect(program.any_event_for_this_date?(Date.current + 1)).to eq true
end
it 'returns false if there is no event for this date' do
expect(program.any_event_for_this_date?(Date.current + 2)).to eq false
end
it 'returns false if date passed is empty' do
expect(program.any_event_for_this_date?('')).to eq false
end
it 'returns false if date passed is nil' do
expect(program.any_event_for_this_date?(nil)).to eq false
end
end
end
describe '#cfp' do describe '#cfp' do
it 'returns the cfp for events' do it 'returns the cfp for events' do
create(:cfp, cfp_type: 'events', program: program, end_date: Date.current + 1) create(:cfp, cfp_type: 'events', program: program, end_date: Date.current + 1)