/test/test_date_helper.py

https://github.com/awolf/Foojal · Python · 248 lines · 167 code · 62 blank · 19 comment · 0 complexity · e805152ca9ffe0cf48e1eccd04903158 MD5 · raw file

  1. from calendar import monthrange
  2. from time import strftime
  3. import unittest
  4. from foo.date_helper import week_begin_end_dates, get_day_data, get_week_data, get_month_data
  5. from datetime import date, datetime, timedelta
  6. from foo.models import Account
  7. from google.appengine.ext import testbed
  8. import pytz
  9. class TestWeekDatesHelper(unittest.TestCase):
  10. """ Testing the week begin and end
  11. helpers in the date helper module."""
  12. def test_week_begins_on_monday(self):
  13. """ Weeks begin dates should be a monday."""
  14. beginning, end = week_begin_end_dates(1, 2011)
  15. assert(beginning.weekday() == 0)
  16. def test_week_ends_on_sunday(self):
  17. """ Week end dates should be a sunday."""
  18. beginning, end = week_begin_end_dates(1, 2011)
  19. assert(end.weekday() == 6)
  20. def test_first_monday_2011(self):
  21. """ The first Monday of 2011 is 1/3/2011."""
  22. beginning, end = week_begin_end_dates(1, 2011)
  23. assert(beginning == date(2011, 1, 3))
  24. def test_first_sunday_2011(self):
  25. """ The first Sunday of 2011 is 1/9/2011."""
  26. beginning, end = week_begin_end_dates(1, 2011)
  27. assert(end == date(2011, 1, 9))
  28. def test_last_monday_2011(self):
  29. """ The last monday of 2011 is 12/26/2011."""
  30. beginning, end = week_begin_end_dates(52, 2011)
  31. assert(beginning == date(2011, 12, 26))
  32. def test_last_sunday_2011(self):
  33. """ The last Sunday of 2011 is 1/1/2012."""
  34. beginning, end = week_begin_end_dates(52, 2011)
  35. assert(end == date(2012, 1, 1))
  36. class TestDateHelpersTimeSpans(unittest.TestCase):
  37. """ Testing the single day helpers in
  38. the date helpers module."""
  39. account = None
  40. def setUp(self):
  41. self.testbed = testbed.Testbed()
  42. self.testbed.activate()
  43. self.testbed.init_datastore_v3_stub()
  44. self.account = Account(timezone='America/Phoenix')
  45. def tearDown(self):
  46. self.testbed.deactivate()
  47. def test_single_day_is_24_hours(self):
  48. values = get_day_data(self.account, date(2011, 1, 1))
  49. td = values['to_date'] - values['from_date']
  50. days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60
  51. assert(days == 0)
  52. assert(hours == 23)
  53. assert(minutes == 59)
  54. def test_single_week_is_7_days(self):
  55. values = get_week_data(self.account, 1, 2011)
  56. td = values['to_date'] - values['from_date']
  57. days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60
  58. assert(days == 6)
  59. assert(hours == 23)
  60. assert(minutes == 59)
  61. def test_first_month_of_2011_is_31_days(self):
  62. values = get_month_data(self.account, 1, 2011)
  63. td = values['to_date'] - values['from_date']
  64. days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60
  65. assert(days == 30)
  66. assert(hours == 23)
  67. assert(minutes == 59)
  68. class TestDayDateHelpers(unittest.TestCase):
  69. """ Testing the single day helpers in
  70. the date helpers module."""
  71. account = None
  72. def setUp(self):
  73. self.testbed = testbed.Testbed()
  74. self.testbed.activate()
  75. self.testbed.init_datastore_v3_stub()
  76. self.account = Account(timezone='America/Phoenix')
  77. def tearDown(self):
  78. self.testbed.deactivate()
  79. def test_day_in_the_past(self):
  80. day = date(2011, 2, 22)
  81. values = get_day_data(self.account, day)
  82. from_date = datetime(hour=0, minute=0, day=day.day, year=day.year, month=day.month).replace(
  83. tzinfo=self.account.tz)
  84. to_date = datetime(hour=23, minute=59, second=59, day=day.day, year=day.year, month=day.month).replace(
  85. tzinfo=self.account.tz)
  86. assert from_date == values['from_date']
  87. assert to_date == values['to_date']
  88. assert from_date == values['target_day']
  89. assert from_date == values['display_date']
  90. assert values['previous_date_url'] == "/day/21/02/2011"
  91. assert values['next_date_url'] == "/day/23/02/2011"
  92. def test_today(self):
  93. """ Test that getting the current day will
  94. return the correct dates including a
  95. none for the next day """
  96. today = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(self.account.tz)
  97. day = datetime(hour=18, minute=0, day=today.day, year=today.year, month=today.month).replace(tzinfo=self.account.tz)
  98. yesterday = day - timedelta(days=1)
  99. from_date = datetime(hour=0, minute=0, day=day.day, year=day.year, month=day.month).replace(
  100. tzinfo=self.account.tz)
  101. to_date = datetime(hour=23, minute=59, second=59, day=day.day, year=day.year, month=day.month).replace(
  102. tzinfo=self.account.tz)
  103. values = get_day_data(self.account, day)
  104. assert from_date == values['from_date']
  105. assert to_date == values['to_date']
  106. assert from_date == values['target_day']
  107. assert from_date == values['display_date']
  108. assert values['previous_date_url'] == strftime("/day/%d/%m/%Y", yesterday.timetuple())
  109. assert values['next_date_url'] is None
  110. class TestWeekDateHelpers(unittest.TestCase):
  111. """ Testing the week helpers in
  112. the date helpers module."""
  113. account = None
  114. def setUp(self):
  115. self.testbed = testbed.Testbed()
  116. self.testbed.activate()
  117. self.testbed.init_datastore_v3_stub()
  118. self.account = Account(timezone='America/Phoenix')
  119. def tearDown(self):
  120. self.testbed.deactivate()
  121. def test_week_in_the_past(self):
  122. values = get_week_data(self.account, "8", "2011")
  123. from_date = datetime(hour=0, minute=0, day=21, year=2011, month=2).replace(tzinfo=self.account.tz)
  124. to_date = datetime(hour=23, minute=59, day=27, year=2011, month=2).replace(tzinfo=self.account.tz)
  125. assert from_date == values['from_date']
  126. assert to_date == values['to_date']
  127. assert from_date == values['target_day']
  128. assert from_date == values['display_date']
  129. assert values['previous_date_url'] == "/week/07/2011"
  130. assert values['next_date_url'] == "/week/09/2011"
  131. def test_today(self):
  132. calendar = datetime.utcnow().isocalendar()
  133. week = calendar[1]
  134. year = calendar[0]
  135. week_begin, week_end = week_begin_end_dates(week, year)
  136. values = get_week_data(self.account, week, year)
  137. from_date = datetime(hour=0, minute=0, day=week_begin.day, year=week_begin.year,
  138. month=week_begin.month).replace(tzinfo=self.account.tz)
  139. to_date = datetime(hour=23, minute=59, day=week_end.day, year=week_end.year, month=week_end.month).replace(
  140. tzinfo=self.account.tz)
  141. previous_date_url = "/week/" + str(week - 1) + "/2011"
  142. assert from_date == values['from_date']
  143. assert to_date == values['to_date']
  144. assert from_date == values['target_day']
  145. assert from_date == values['display_date']
  146. assert values['previous_date_url'] == previous_date_url
  147. assert values['next_date_url'] is None
  148. class TestMonthDateHelpers(unittest.TestCase):
  149. """ Testing the month helpers in
  150. the date helpers module."""
  151. account = None
  152. def setUp(self):
  153. self.testbed = testbed.Testbed()
  154. self.testbed.activate()
  155. self.testbed.init_datastore_v3_stub()
  156. self.account = Account(timezone='America/Phoenix')
  157. def tearDown(self):
  158. self.testbed.deactivate()
  159. def test_month_in_the_past(self):
  160. values = get_month_data(self.account, "1", "2011")
  161. from_date = datetime(hour=0, minute=0, day=1, year=2011, month=1).replace(tzinfo=self.account.tz)
  162. to_date = datetime(hour=23, minute=59, day=31, year=2011, month=1).replace(tzinfo=self.account.tz)
  163. assert from_date == values['from_date']
  164. assert to_date == values['to_date']
  165. assert from_date == values['target_day']
  166. assert from_date == values['display_date']
  167. assert values['previous_date_url'] == "/month/12/2010"
  168. assert values['next_date_url'] == "/month/02/2011"
  169. def test_today(self):
  170. today = datetime.date(datetime.now())
  171. month = today.month
  172. year = today.year
  173. values = get_month_data(self.account, month, year)
  174. days_in_month = monthrange(int(year), int(month))[1]
  175. from_date = datetime(hour=0, minute=0, day=1, year=int(year), month=int(month)).replace(tzinfo=self.account.tz)
  176. to_date = datetime(hour=23, minute=59, day=days_in_month, year=int(year), month=int(month)).replace(
  177. tzinfo=self.account.tz)
  178. a_day = timedelta(days=1)
  179. previous_date = from_date - a_day
  180. previous_date_url = strftime("/month/%m/%Y", previous_date.timetuple())
  181. assert from_date == values['from_date']
  182. assert to_date == values['to_date']
  183. assert from_date == values['target_day']
  184. assert from_date == values['display_date']
  185. assert values['previous_date_url'] == previous_date_url
  186. assert values['next_date_url'] is None