PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/work_time_schedule_test.rb

https://bitbucket.org/srmoscow/holiday_calendar
Ruby | 260 lines | 147 code | 89 blank | 24 comment | 0 complexity | f1a5423ef5dab3bacac423dd39a66cc6 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../lib/work_time_schedule'
  2. require File.dirname(__FILE__) + '/test_helper'
  3. require 'test/unit'
  4. class WorkTimeScheduleTest < Test::Unit::TestCase
  5. def setup
  6. calendar = HolidayCalendar.load(:uk_en)
  7. @schedule = WorkTimeSchedule.new(calendar, 9, 0, 17, 30)
  8. @christmas_day = DateTime.new(2010, 12, 25, 11, 10, 0)
  9. @day_after_boxing_day_0900 = DateTime.new(2010, 12, 29, 9, 0, 0)
  10. @day_after_boxing_day_1025 = DateTime.new(2010, 12, 29, 10, 25, 0)
  11. @lieu_boxing_day = DateTime.new(2010, 12, 28, 9, 0, 0)
  12. @christmas_eve_1025 = DateTime.new(2010, 12, 24, 10, 25, 0)
  13. @christmas_eve_1900 = DateTime.new(2010, 12, 24, 19, 0, 0)
  14. @sunday_1025 = DateTime.new(2010, 12, 5, 10, 25, 0)
  15. @monday_0845 = DateTime.new(2010, 12, 6, 8, 45, 0)
  16. @monday_0900 = DateTime.new(2010, 12, 6, 9, 0, 0)
  17. @monday_1025 = DateTime.new(2010, 12, 6, 10, 25, 0)
  18. @monday_1543 = DateTime.new(2010, 12, 6, 15, 43, 10)
  19. @monday_1730 = DateTime.new(2010, 12, 6, 17, 30, 0)
  20. @thursday_1025 = DateTime.new(2010, 12, 2, 10, 25, 0)
  21. @thursday_1730 = DateTime.new(2010, 12, 2, 17, 30, 0)
  22. @thursday_1812 = DateTime.new(2010, 12, 2, 18, 12, 0)
  23. @friday_0845 = DateTime.new(2010, 12, 3, 8, 45, 0)
  24. @friday_1812 = DateTime.new(2010, 12, 3, 18, 12, 0)
  25. @friday_0900 = DateTime.new(2010, 12, 3, 9, 0, 0)
  26. @friday_1025 = DateTime.new(2010, 12, 3, 10, 25, 0)
  27. @friday_1730 = DateTime.new(2010, 12, 3, 17, 30, 0)
  28. @saturday_1025 = DateTime.new(2010, 12, 4, 10, 25, 0)
  29. @saturday_1410 = DateTime.new(2010, 12, 4, 14, 10, 0)
  30. end
  31. def test_we_can_retrieve_workday_object
  32. friday = @schedule.workday(5)
  33. assert_instance_of WorkDay, friday
  34. assert_equal 9*60, friday.start_in_minutes
  35. expected = (17*60) + 30
  36. assert_equal expected, friday.end_in_minutes
  37. end
  38. def test_we_can_retrieve_a_weekend_workday_object
  39. sunday = @schedule.workday(6)
  40. assert_equal 0, sunday.start_in_minutes
  41. assert_equal 0, sunday.end_in_minutes
  42. end
  43. def test_we_can_set_new_times_for_a_particular_day_by_day_number
  44. @schedule.set_day(5, WorkTime.new(9, 0), WorkTime.new(17, 0))
  45. friday = @schedule.workday(5)
  46. assert_equal 9*60, friday.start_in_minutes
  47. assert_equal 17*60, friday.end_in_minutes
  48. end
  49. def test_we_can_set_new_times_for_a_particular_day_by_day_name
  50. @schedule.friday(WorkTime.new(9, 0), WorkTime.new(16, 0))
  51. friday = @schedule.workday(5)
  52. assert_equal 9*60, friday.start_in_minutes
  53. assert_equal 16*60, friday.end_in_minutes
  54. end
  55. def test_that_working_time_returns_false_for_public_holidays
  56. assert_false @schedule.working_time?(@christmas_day)
  57. end
  58. def test_that_working_time_returns_false_for_work_day_during_weekend
  59. assert_false @schedule.working_time?(@saturday_1410)
  60. end
  61. def test_that_working_time_returns_false_for_working_day_before_start_of_working_hours
  62. assert_false @schedule.working_time?(@friday_0845)
  63. end
  64. def test_that_working_time_returns_false_for_working_day_after_end_of_working_hours
  65. assert_false @schedule.working_time?(@friday_1812)
  66. end
  67. def test_next_working_day_always_gives_the_same_time_on_the_next_working_day
  68. assert_dates_equal @friday_1025, @schedule.next_working_day(@thursday_1025)
  69. assert_dates_equal @monday_1025, @schedule.next_working_day(@friday_1025)
  70. end
  71. def test_next_working_day_always_gives_the_next_working_day_even_if_its_a_weekend
  72. @schedule.saturday(WorkTime.new(9,0), WorkTime.new(12,30)) # make Saturday a half day
  73. assert_dates_equal @saturday_1025, @schedule.next_working_day(@friday_1025)
  74. end
  75. def test_that_working_time_returns_true_for_working_day_during_working_hours
  76. assert_true @schedule.working_time?(@friday_0900)
  77. assert_true @schedule.working_time?(@friday_1025)
  78. assert_true @schedule.working_time?(@friday_1730)
  79. end
  80. def test_that_start_of_day_returns_expected_value_for_working_day
  81. assert_dates_equal @friday_0900, @schedule.start_of_day(@friday_0845)
  82. assert_dates_equal @friday_0900, @schedule.start_of_day(@friday_1025)
  83. # but next working day if after end of work
  84. assert_dates_equal @monday_0900, @schedule.start_of_day(@friday_1812)
  85. # next working day if not a working day
  86. assert_dates_equal @monday_0900, @schedule.start_of_day(@sunday_1025)
  87. end
  88. def test_that_end_of_day_returns_expected_value_for_working_day
  89. # during work time, should give the end of the working day
  90. assert_dates_equal @friday_1730, @schedule.end_of_day(@friday_1025)
  91. # after work time, should give the end of the working day
  92. assert_dates_equal @friday_1730, @schedule.end_of_day(@friday_1812)
  93. # before work time, should give the end of the previous working day
  94. assert_dates_equal @thursday_1730, @schedule.end_of_day(@friday_0845)
  95. assert_dates_equal @friday_1730, @schedule.end_of_day(@monday_0845)
  96. # on a non-working day, should give the end of the previous working day
  97. assert_dates_equal @friday_1730, @schedule.end_of_day(@sunday_1025)
  98. end
  99. def test_time_returns_time_if_during_working_hours_of_a_working_day
  100. assert_dates_equal @monday_1025, @schedule.time(@monday_1025)
  101. end
  102. def test_time_returns_start_of_working_day_if_before_start_on_working_day
  103. assert_dates_equal @friday_0900, @schedule.time(@friday_0845)
  104. end
  105. def test_time_returns_start_of_next_working_day_if_after_end_of_working_day
  106. assert_dates_equal @monday_0900, @schedule.time(@friday_1812)
  107. end
  108. def test_time_returns_start_of_next_working_day_if_not_a_working_day
  109. assert_dates_equal @monday_0900, @schedule.time(@saturday_1410)
  110. assert_dates_equal @day_after_boxing_day_0900, @schedule.time(@christmas_eve_1900)
  111. end
  112. def test_time_or_end_of_day_returns_expected_values
  113. # during working day, the same time should be returned
  114. assert_dates_equal @monday_1025, @schedule.time_or_end_of_day(@monday_1025)
  115. # after work hours, the end of working hours shoudl be returned
  116. assert_dates_equal @thursday_1730, @schedule.time_or_end_of_day(@thursday_1812)
  117. # on a non working day, the end of the previson working day should be returned
  118. assert_dates_equal @friday_1730, @schedule.time_or_end_of_day(@sunday_1025)
  119. end
  120. def test_total_working_minutes_gives_the_correct_value
  121. # for a normal working day, should give 09:00 - 17:30 = 8.5 hours = 510 minutes
  122. assert_equal 510, @schedule.total_working_minutes(@friday_1812)
  123. # for a non_working day, should give 0 minutes
  124. assert_equal 0, @schedule.total_working_minutes(@sunday_1025)
  125. assert_equal 0, @schedule.total_working_minutes(@lieu_boxing_day)
  126. # if we set Saturday to be half day....
  127. @schedule.set_day(6, WorkTime.new(9, 0), WorkTime.new(12, 30))
  128. assert_equal 210, @schedule.total_working_minutes(@saturday_1025)
  129. end
  130. def test_minutes_worked_today_until
  131. # for a normal working day, should be the number of minutes between start and date_time
  132. assert_equal 85, @schedule.minutes_worked_today_until(@monday_1025)
  133. # for a time before the start of work it should be zero
  134. assert_equal 0, @schedule.minutes_worked_today_until(@monday_0845)
  135. # for a time after the end of the woring day, the total_working_minutes weill be returned
  136. assert_equal @schedule.total_working_minutes(@friday_1812), @schedule.minutes_worked_today_until(@friday_1812)
  137. # For a non working day, zero should be returned
  138. assert_equal 0, @schedule.minutes_worked_today_until(@sunday_1025)
  139. end
  140. def test_elapsed_minutes
  141. # between two times during working hours on the same day should give the difference in minutes between the two times
  142. assert_equal 318, @schedule.elapsed_minutes(@monday_1025, @monday_1543)
  143. # between a time during working hours and time after working hours on same day should give time up to end of working hours
  144. assert_equal 425, @schedule.elapsed_minutes(@friday_1025, @friday_1812)
  145. # between a time before working hours and a time after working hours on same day should tive total_working_minutes
  146. assert_equal 510, @schedule.elapsed_minutes(@friday_0845, @friday_1812)
  147. # between a time before working hours and a time during working hours on the same day should give time from start of working day
  148. assert_equal 85, @schedule.elapsed_minutes(@friday_0845, @friday_1025)
  149. # between time after working hours and time before working hours the next day should give zero
  150. assert_equal 0, @schedule.elapsed_minutes(@thursday_1812, @friday_0845)
  151. # between time after working hours and time dureing working hours the next day should give time from start of work next day
  152. assert_equal 85, @schedule.elapsed_minutes(@thursday_1812, @friday_1025)
  153. # over the holiday period.....
  154. # last working day before CHristmas is 24th, next is 29th
  155. assert_equal 510, @schedule.elapsed_minutes(@christmas_eve_1025, @day_after_boxing_day_1025)
  156. assert_equal 85, @schedule.elapsed_minutes(@christmas_eve_1900, @day_after_boxing_day_1025)
  157. end
  158. def test_private_method_time_to_end_of_day
  159. assert_equal 107, @schedule.send(:minutes_to_end_of_day, @monday_1543)
  160. end
  161. def test_private_method_year_day
  162. assert_equal 2010001, @schedule.send(:year_day, DateTime.new(2010, 1, 1, 15, 45, 0))
  163. assert_equal 2010337, @schedule.send(:year_day, @friday_1812)
  164. assert_equal 2010340, @schedule.send(:year_day, @monday_0900)
  165. end
  166. def test_private_method_to_modified_date
  167. expected = DateTime.new(2010, 12, 4, 0, 0, 0)
  168. actual = @schedule.send(:to_modified_date_time, DateTime.new(2010, 12, 4, 18, 12, 0), WorkTime.new(0,0))
  169. assert_equal expected, actual
  170. end
  171. end