123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- require 'test_helper'
- class CalendarTest < ActiveSupport::TestCase
- test 'default test' do
- Calendar.destroy_all
- calendar1 = Calendar.create_or_update(
- name: 'US 1',
- timezone: 'America/Los_Angeles',
- business_hours: {
- mon: { '09:00' => '17:00' },
- tue: { '09:00' => '17:00' },
- wed: { '09:00' => '17:00' },
- thu: { '09:00' => '17:00' },
- fri: { '09:00' => '17:00' }
- },
- default: true,
- ical_url: nil,
- updated_by_id: 1,
- created_by_id: 1,
- )
- travel 1.second
- calendar2 = Calendar.create_or_update(
- name: 'US 2',
- timezone: 'America/Los_Angeles',
- business_hours: {
- mon: { '09:00' => '17:00' },
- tue: { '09:00' => '17:00' },
- wed: { '09:00' => '17:00' },
- thu: { '09:00' => '17:00' },
- fri: { '09:00' => '17:00' }
- },
- default: false,
- ical_url: nil,
- updated_by_id: 1,
- created_by_id: 1,
- )
- calendar3 = Calendar.create_or_update(
- name: 'US 3',
- timezone: 'America/Los_Angeles',
- business_hours: {
- mon: { '09:00' => '17:00' },
- tue: { '09:00' => '17:00' },
- wed: { '09:00' => '17:00' },
- thu: { '09:00' => '17:00' },
- fri: { '09:00' => '17:00' }
- },
- default: true,
- ical_url: nil,
- updated_by_id: 1,
- created_by_id: 1,
- )
- calendar1 = Calendar.find_by(name: 'US 1')
- calendar2 = Calendar.find_by(name: 'US 2')
- calendar3 = Calendar.find_by(name: 'US 3')
- assert_equal(false, calendar1.default)
- assert_equal(false, calendar2.default)
- assert_equal(true, calendar3.default)
- calendar2.default = true
- calendar2.save!
- calendar1 = Calendar.find_by(name: 'US 1')
- calendar2 = Calendar.find_by(name: 'US 2')
- calendar3 = Calendar.find_by(name: 'US 3')
- assert_equal(false, calendar1.default)
- assert_equal(true, calendar2.default)
- assert_equal(false, calendar3.default)
- calendar2.default = false
- calendar2.save!
- calendar1 = Calendar.find_by(name: 'US 1')
- calendar2 = Calendar.find_by(name: 'US 2')
- calendar3 = Calendar.find_by(name: 'US 3')
- assert_equal(true, calendar1.default)
- assert_equal(false, calendar2.default)
- assert_equal(false, calendar3.default)
- calendar1.destroy!
- calendar2 = Calendar.find_by(name: 'US 2')
- calendar3 = Calendar.find_by(name: 'US 3')
- assert_equal(true, calendar2.default)
- assert_equal(false, calendar3.default)
- calendar2.destroy!
- calendar3.destroy!
- travel_back
- end
- test 'sync test' do
- Calendar.destroy_all
- travel_to Time.zone.parse('2017-08-24T01:04:44Z0')
- calendar1 = Calendar.create_or_update(
- name: 'Sync 1',
- timezone: 'America/Los_Angeles',
- business_hours: {
- mon: { '09:00' => '17:00' },
- tue: { '09:00' => '17:00' },
- wed: { '09:00' => '17:00' },
- thu: { '09:00' => '17:00' },
- fri: { '09:00' => '17:00' }
- },
- default: true,
- ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar1.ics'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2016-12-25'])
- assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2017-12-25'])
- assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2018-12-25'])
- assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2019-12-25'])
- assert_nil(calendar1.public_holidays['2020-12-24'])
- Calendar.sync
- assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2016-12-25'])
- assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2017-12-25'])
- assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2018-12-25'])
- assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2019-12-25'])
- assert_nil(calendar1.public_holidays['2020-12-24'])
- cache_key = "CalendarIcal::#{calendar1.id}"
- cache = Cache.get(cache_key)
- calendar1.update_columns(ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar2.ics').to_s)
- cache_key = "CalendarIcal::#{calendar1.id}"
- cache = Cache.get(cache_key)
- cache[:ical_url] = calendar1.ical_url
- Cache.write(
- cache_key,
- cache,
- { expires_in: 1.day },
- )
- Calendar.sync
- calendar1.reload
- assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2016-12-25'])
- assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2017-12-25'])
- assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2018-12-25'])
- assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2019-12-25'])
- assert_nil(calendar1.public_holidays['2020-12-24'])
- travel 2.days
- Calendar.sync
- calendar1.reload
- assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2016-12-25']['active'])
- assert_equal('Christmas2', calendar1.public_holidays['2016-12-25']['summary'])
- assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2017-12-25']['active'])
- assert_equal('Christmas2', calendar1.public_holidays['2017-12-25']['summary'])
- assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2018-12-25']['active'])
- assert_equal('Christmas2', calendar1.public_holidays['2018-12-25']['summary'])
- assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2019-12-25']['active'])
- assert_equal('Christmas2', calendar1.public_holidays['2019-12-25']['summary'])
- assert_nil(calendar1.public_holidays['2020-12-24'])
- assert_nil(calendar1.public_holidays['2020-12-25'])
- Calendar.destroy_all
- calendar1 = Calendar.create_or_update(
- name: 'Sync 2',
- timezone: 'America/Los_Angeles',
- business_hours: {
- mon: { '09:00' => '17:00' },
- tue: { '09:00' => '17:00' },
- wed: { '09:00' => '17:00' },
- thu: { '09:00' => '17:00' },
- fri: { '09:00' => '17:00' }
- },
- default: true,
- ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar3.ics'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2016-12-26']['active'])
- assert_equal('day3', calendar1.public_holidays['2016-12-26']['summary'])
- assert_equal(true, calendar1.public_holidays['2016-12-28']['active'])
- assert_equal('day5', calendar1.public_holidays['2016-12-28']['summary'])
- assert_equal(true, calendar1.public_holidays['2017-01-26']['active'])
- assert_equal('day3', calendar1.public_holidays['2017-01-26']['summary'])
- assert_equal(true, calendar1.public_holidays['2017-02-26']['active'])
- assert_equal('day3', calendar1.public_holidays['2017-02-26']['summary'])
- assert_equal(true, calendar1.public_holidays['2017-03-26']['active'])
- assert_equal('day3', calendar1.public_holidays['2017-03-26']['summary'])
- assert_equal(true, calendar1.public_holidays['2017-04-26']['active'])
- assert_equal('day3', calendar1.public_holidays['2017-04-26']['summary'])
- assert_nil(calendar1.public_holidays['2017-05-26'])
- assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
- assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
- assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
- assert_nil(calendar1.public_holidays['2020-12-24'])
- calendar1.destroy!
- travel_back
- end
- end
|