calendar_test.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. require 'test_helper'
  2. class CalendarTest < ActiveSupport::TestCase
  3. test 'default test' do
  4. Calendar.destroy_all
  5. calendar1 = Calendar.create_or_update(
  6. name: 'US 1',
  7. timezone: 'America/Los_Angeles',
  8. business_hours: {
  9. mon: { '09:00' => '17:00' },
  10. tue: { '09:00' => '17:00' },
  11. wed: { '09:00' => '17:00' },
  12. thu: { '09:00' => '17:00' },
  13. fri: { '09:00' => '17:00' }
  14. },
  15. default: true,
  16. ical_url: nil,
  17. updated_by_id: 1,
  18. created_by_id: 1,
  19. )
  20. travel 1.second
  21. calendar2 = Calendar.create_or_update(
  22. name: 'US 2',
  23. timezone: 'America/Los_Angeles',
  24. business_hours: {
  25. mon: { '09:00' => '17:00' },
  26. tue: { '09:00' => '17:00' },
  27. wed: { '09:00' => '17:00' },
  28. thu: { '09:00' => '17:00' },
  29. fri: { '09:00' => '17:00' }
  30. },
  31. default: false,
  32. ical_url: nil,
  33. updated_by_id: 1,
  34. created_by_id: 1,
  35. )
  36. calendar3 = Calendar.create_or_update(
  37. name: 'US 3',
  38. timezone: 'America/Los_Angeles',
  39. business_hours: {
  40. mon: { '09:00' => '17:00' },
  41. tue: { '09:00' => '17:00' },
  42. wed: { '09:00' => '17:00' },
  43. thu: { '09:00' => '17:00' },
  44. fri: { '09:00' => '17:00' }
  45. },
  46. default: true,
  47. ical_url: nil,
  48. updated_by_id: 1,
  49. created_by_id: 1,
  50. )
  51. calendar1 = Calendar.find_by(name: 'US 1')
  52. calendar2 = Calendar.find_by(name: 'US 2')
  53. calendar3 = Calendar.find_by(name: 'US 3')
  54. assert_equal(false, calendar1.default)
  55. assert_equal(false, calendar2.default)
  56. assert_equal(true, calendar3.default)
  57. calendar2.default = true
  58. calendar2.save!
  59. calendar1 = Calendar.find_by(name: 'US 1')
  60. calendar2 = Calendar.find_by(name: 'US 2')
  61. calendar3 = Calendar.find_by(name: 'US 3')
  62. assert_equal(false, calendar1.default)
  63. assert_equal(true, calendar2.default)
  64. assert_equal(false, calendar3.default)
  65. calendar2.default = false
  66. calendar2.save!
  67. calendar1 = Calendar.find_by(name: 'US 1')
  68. calendar2 = Calendar.find_by(name: 'US 2')
  69. calendar3 = Calendar.find_by(name: 'US 3')
  70. assert_equal(true, calendar1.default)
  71. assert_equal(false, calendar2.default)
  72. assert_equal(false, calendar3.default)
  73. calendar1.destroy!
  74. calendar2 = Calendar.find_by(name: 'US 2')
  75. calendar3 = Calendar.find_by(name: 'US 3')
  76. assert_equal(true, calendar2.default)
  77. assert_equal(false, calendar3.default)
  78. calendar2.destroy!
  79. calendar3.destroy!
  80. travel_back
  81. end
  82. test 'sync test' do
  83. Calendar.destroy_all
  84. travel_to Time.zone.parse('2017-08-24T01:04:44Z0')
  85. calendar1 = Calendar.create_or_update(
  86. name: 'Sync 1',
  87. timezone: 'America/Los_Angeles',
  88. business_hours: {
  89. mon: { '09:00' => '17:00' },
  90. tue: { '09:00' => '17:00' },
  91. wed: { '09:00' => '17:00' },
  92. thu: { '09:00' => '17:00' },
  93. fri: { '09:00' => '17:00' }
  94. },
  95. default: true,
  96. ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar1.ics'),
  97. updated_by_id: 1,
  98. created_by_id: 1,
  99. )
  100. assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
  101. assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
  102. assert_nil(calendar1.public_holidays['2016-12-25'])
  103. assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
  104. assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
  105. assert_nil(calendar1.public_holidays['2017-12-25'])
  106. assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
  107. assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
  108. assert_nil(calendar1.public_holidays['2018-12-25'])
  109. assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
  110. assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
  111. assert_nil(calendar1.public_holidays['2019-12-25'])
  112. assert_nil(calendar1.public_holidays['2020-12-24'])
  113. Calendar.sync
  114. assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
  115. assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
  116. assert_nil(calendar1.public_holidays['2016-12-25'])
  117. assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
  118. assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
  119. assert_nil(calendar1.public_holidays['2017-12-25'])
  120. assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
  121. assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
  122. assert_nil(calendar1.public_holidays['2018-12-25'])
  123. assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
  124. assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
  125. assert_nil(calendar1.public_holidays['2019-12-25'])
  126. assert_nil(calendar1.public_holidays['2020-12-24'])
  127. cache_key = "CalendarIcal::#{calendar1.id}"
  128. cache = Cache.get(cache_key)
  129. calendar1.update_columns(ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar2.ics').to_s)
  130. cache_key = "CalendarIcal::#{calendar1.id}"
  131. cache = Cache.get(cache_key)
  132. cache[:ical_url] = calendar1.ical_url
  133. Cache.write(
  134. cache_key,
  135. cache,
  136. { expires_in: 1.day },
  137. )
  138. Calendar.sync
  139. calendar1.reload
  140. assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
  141. assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
  142. assert_nil(calendar1.public_holidays['2016-12-25'])
  143. assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
  144. assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
  145. assert_nil(calendar1.public_holidays['2017-12-25'])
  146. assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
  147. assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
  148. assert_nil(calendar1.public_holidays['2018-12-25'])
  149. assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
  150. assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
  151. assert_nil(calendar1.public_holidays['2019-12-25'])
  152. assert_nil(calendar1.public_holidays['2020-12-24'])
  153. travel 2.days
  154. Calendar.sync
  155. calendar1.reload
  156. assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
  157. assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
  158. assert_equal(true, calendar1.public_holidays['2016-12-25']['active'])
  159. assert_equal('Christmas2', calendar1.public_holidays['2016-12-25']['summary'])
  160. assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
  161. assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
  162. assert_equal(true, calendar1.public_holidays['2017-12-25']['active'])
  163. assert_equal('Christmas2', calendar1.public_holidays['2017-12-25']['summary'])
  164. assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
  165. assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
  166. assert_equal(true, calendar1.public_holidays['2018-12-25']['active'])
  167. assert_equal('Christmas2', calendar1.public_holidays['2018-12-25']['summary'])
  168. assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
  169. assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
  170. assert_equal(true, calendar1.public_holidays['2019-12-25']['active'])
  171. assert_equal('Christmas2', calendar1.public_holidays['2019-12-25']['summary'])
  172. assert_nil(calendar1.public_holidays['2020-12-24'])
  173. assert_nil(calendar1.public_holidays['2020-12-25'])
  174. Calendar.destroy_all
  175. calendar1 = Calendar.create_or_update(
  176. name: 'Sync 2',
  177. timezone: 'America/Los_Angeles',
  178. business_hours: {
  179. mon: { '09:00' => '17:00' },
  180. tue: { '09:00' => '17:00' },
  181. wed: { '09:00' => '17:00' },
  182. thu: { '09:00' => '17:00' },
  183. fri: { '09:00' => '17:00' }
  184. },
  185. default: true,
  186. ical_url: Rails.root.join('test', 'data', 'calendar', 'calendar3.ics'),
  187. updated_by_id: 1,
  188. created_by_id: 1,
  189. )
  190. assert_equal(true, calendar1.public_holidays['2016-12-24']['active'])
  191. assert_equal('Christmas1', calendar1.public_holidays['2016-12-24']['summary'])
  192. assert_equal(true, calendar1.public_holidays['2016-12-26']['active'])
  193. assert_equal('day3', calendar1.public_holidays['2016-12-26']['summary'])
  194. assert_equal(true, calendar1.public_holidays['2016-12-28']['active'])
  195. assert_equal('day5', calendar1.public_holidays['2016-12-28']['summary'])
  196. assert_equal(true, calendar1.public_holidays['2017-01-26']['active'])
  197. assert_equal('day3', calendar1.public_holidays['2017-01-26']['summary'])
  198. assert_equal(true, calendar1.public_holidays['2017-02-26']['active'])
  199. assert_equal('day3', calendar1.public_holidays['2017-02-26']['summary'])
  200. assert_equal(true, calendar1.public_holidays['2017-03-26']['active'])
  201. assert_equal('day3', calendar1.public_holidays['2017-03-26']['summary'])
  202. assert_equal(true, calendar1.public_holidays['2017-04-26']['active'])
  203. assert_equal('day3', calendar1.public_holidays['2017-04-26']['summary'])
  204. assert_nil(calendar1.public_holidays['2017-05-26'])
  205. assert_equal(true, calendar1.public_holidays['2017-12-24']['active'])
  206. assert_equal('Christmas1', calendar1.public_holidays['2017-12-24']['summary'])
  207. assert_equal(true, calendar1.public_holidays['2018-12-24']['active'])
  208. assert_equal('Christmas1', calendar1.public_holidays['2018-12-24']['summary'])
  209. assert_equal(true, calendar1.public_holidays['2019-12-24']['active'])
  210. assert_equal('Christmas1', calendar1.public_holidays['2019-12-24']['summary'])
  211. assert_nil(calendar1.public_holidays['2020-12-24'])
  212. calendar1.destroy!
  213. travel_back
  214. end
  215. end