Permalink
Browse files

Adding tests, refactoring get_month_key.

  • Loading branch information...
Sean Reifschneider
Sean Reifschneider committed Dec 11, 2011
1 parent 148270f commit 7126ef85777d6f884e2979aa351afb89cda59e2d
Showing with 57 additions and 16 deletions.
  1. +11 −16 gnome-shell-google-calendar.py
  2. +46 −0 test
@@ -12,6 +12,7 @@
import gtk
import iso8601
import keyring
+import calendar
# change to "True" to get debugging messages
@@ -25,22 +26,16 @@ def get_month_key(date, first_day_of_week=7):
- `first_day_of_week`: integer representing first day of week used by
calendar; Monday -> 1, ..., Sunday -> 7
"""
- start_date = date.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
- while start_date.isocalendar()[2] != first_day_of_week:
- start_date -= timedelta(days=1)
-
- end_date = date.replace(day=28, hour=23, minute=59, second=59,
- microsecond=999999)
- initial_month = end_date.month
- while end_date.month == initial_month:
- end_date += timedelta(days=1)
- end_date -= timedelta(days=1)
- last_day_of_week = first_day_of_week - 1 or 7
- while end_date.isocalendar()[2] != last_day_of_week:
- end_date += timedelta(days=1)
-
- return int(mktime(start_date.timetuple())),\
- int(mktime(end_date.timetuple()))
+ month_calendar = list(calendar.Calendar(first_day_of_week - 1
+ ).itermonthdates(date.year, date.month))
+
+ start_date = datetime(month_calendar[0].year, month_calendar[0].month,
+ month_calendar[0].day)
+ end_date = datetime(month_calendar[-1].year, month_calendar[-1].month,
+ month_calendar[-1].day, hour = 23, minute = 59, second = 59)
+
+ return ( int(mktime(start_date.timetuple())),
+ int(mktime(end_date.timetuple())) )
class MonthEvents(object):
View
46 test
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+#
+# Unit tests for gnome-shell-google-calendar
+
+
+import unittest
+import datetime
+
+# remove the copied module
+def cleanup():
+ import os
+ os.remove('gsgc_test.py')
+ if os.path.exists('gsgc_test.pyc'):
+ os.remove('gsgc_test.pyc')
+import atexit
+atexit.register(cleanup)
+
+# import, but have to use a python-compatible name
+import shutil
+shutil.copy('gnome-shell-google-calendar.py', 'gsgc_test.py')
+import gsgc_test as gsgc
+
+
+#################################################
+class TestGnomeGoogleCalendar(unittest.TestCase):
+ def test_GetMonthKey(self):
+ from time import strftime, localtime
+
+ def tostr(x):
+ return [ strftime('%Y%m%d %H%M%S', localtime(x)) for x in x ]
+
+ self.assertEqual(tostr(
+ gsgc.get_month_key(datetime.datetime(2011, 12, 12))),
+ ['20111127 000000', '20111231 235959'])
+ self.assertEqual(tostr(
+ gsgc.get_month_key(datetime.datetime(2011, 11, 1))),
+ ['20111030 000000', '20111203 235959'])
+ self.assertEqual(tostr(
+ gsgc.get_month_key(datetime.datetime(2011, 1, 31))),
+ ['20101226 000000', '20110205 235959'])
+ self.assertEqual(tostr(
+ gsgc.get_month_key(datetime.datetime(2012, 1, 31))),
+ ['20120101 000000', '20120204 235959'])
+
+suite = unittest.TestLoader().loadTestsFromTestCase(TestGnomeGoogleCalendar)
+unittest.TextTestRunner(verbosity = 2).run(suite)

0 comments on commit 7126ef8

Please sign in to comment.