[fix] Fix event_schedule# happening_now?; Add tests for happening_now JSON endpoint

This commit is contained in:
Jimmy 2021-03-18 18:53:55 +08:00
parent d6e161b7e5
commit cab293e947
2 changed files with 41 additions and 10 deletions

View file

@ -55,16 +55,9 @@ class EventSchedule < ApplicationRecord
# True within `threshold` before and after the event.
#
def happening_now?(threshold = 30.minutes)
in_tz_start = start_time.in_time_zone(timezone)
in_tz_end = end_time.in_time_zone(timezone)
in_tz_start -= in_tz_start.utc_offset
in_tz_end -= in_tz_end.utc_offset
begin_range = Time.now - threshold
end_range = Time.now + threshold
event_time_range = in_tz_start..in_tz_end
now_range = begin_range..end_range
# TODO: There's probably better logic.
event_time_range.overlaps?(now_range) && (in_tz_end > Time.now)
event_time_range = start_time..end_time
now_range = (Time.now - threshold)..(Time.now + threshold)
event_time_range.overlaps?(now_range) && (end_time > Time.now)
end
def self.withdrawn_or_canceled_event_schedules(schedule_ids)

View file

@ -27,4 +27,42 @@ describe SchedulesController do
end
end
end
describe 'GET #happening_now' do
before do
@conference2 = create(:full_conference, start_date: 1.day.ago, end_date: 7.days.from_now)
@program = @conference2.program
@selected_schedule = create(:schedule, program: @program)
@program.update_attributes!(selected_schedule: @selected_schedule)
@scheduled_event1 = create(:event, program: @program, state: 'confirmed')
@event_schedule1 = create(:event_schedule, event: @scheduled_event1, schedule: @selected_schedule, start_time: Time.now)
@scheduled_event2 = create(:event, program: @program, state: 'confirmed')
@event_schedule2 = create(:event_schedule, event: @scheduled_event2, schedule: @selected_schedule, start_time: Time.now + 1.hour)
end
context 'html' do
before :each do
get :happening_now, params: { conference_id: @conference2.short_title }
end
it 'has 200 status code' do
expect(response).to be_success
end
end
context 'json' do
before :each do
get :happening_now, format: :json, params: { conference_id: @conference2.short_title }
end
it 'has 200 status code' do
expect(response).to be_success
end
it 'returns the events that are happening now' do
expect(response.body).to include(@event_schedule1.to_json)
expect(response.body).not_to include(@event_schedule2.to_json)
end
end
end
end