#!/usr/bin/python """ Tests for gtimelog/main.py """ import doctest import unittest def doctest_as_hours(): """Tests for as_hours >>> from gtimelog.main import as_hours >>> from datetime import timedelta >>> as_hours(timedelta(0)) 0.0 >>> as_hours(timedelta(minutes=30)) 0.5 >>> as_hours(timedelta(minutes=60)) 1.0 >>> as_hours(timedelta(days=2)) 48.0 """ def doctest_format_duration(): """Tests for format_duration. >>> from gtimelog.main import format_duration >>> from datetime import timedelta >>> format_duration(timedelta(0)) '0 h 0 min' >>> format_duration(timedelta(minutes=1)) '0 h 1 min' >>> format_duration(timedelta(minutes=60)) '1 h 0 min' """ def doctest_format_short(): """Tests for format_duration_short. >>> from gtimelog.main import format_duration_short >>> from datetime import timedelta >>> format_duration_short(timedelta(0)) '0:00' >>> format_duration_short(timedelta(minutes=1)) '0:01' >>> format_duration_short(timedelta(minutes=59)) '0:59' >>> format_duration_short(timedelta(minutes=60)) '1:00' >>> format_duration_short(timedelta(days=1, hours=2, minutes=3)) '26:03' """ def doctest_format_duration_long(): """Tests for format_duration_long. >>> from gtimelog.main import format_duration_long >>> from datetime import timedelta >>> format_duration_long(timedelta(0)) '0 min' >>> format_duration_long(timedelta(minutes=1)) '1 min' >>> format_duration_long(timedelta(minutes=60)) '1 hour' >>> format_duration_long(timedelta(minutes=65)) '1 hour 5 min' >>> format_duration_long(timedelta(hours=2)) '2 hours' >>> format_duration_long(timedelta(hours=2, minutes=1)) '2 hours 1 min' """ def doctest_parse_datetime(): """Tests for parse_datetime >>> from gtimelog.main import parse_datetime >>> parse_datetime('2005-02-03 02:13') datetime.datetime(2005, 2, 3, 2, 13) >>> parse_datetime('xyzzy') Traceback (most recent call last): ... ValueError: ('bad date time: ', 'xyzzy') """ def doctest_parse_time(): """Tests for parse_time >>> from gtimelog.main import parse_time >>> parse_time('02:13') datetime.time(2, 13) >>> parse_time('xyzzy') Traceback (most recent call last): ... ValueError: ('bad time: ', 'xyzzy') """ def doctest_virtual_day(): """Tests for virtual_day >>> from datetime import datetime, time >>> from gtimelog.main import virtual_day Virtual midnight >>> vm = time(2, 0) The tests themselves: >>> virtual_day(datetime(2005, 2, 3, 1, 15), vm) datetime.date(2005, 2, 2) >>> virtual_day(datetime(2005, 2, 3, 1, 59), vm) datetime.date(2005, 2, 2) >>> virtual_day(datetime(2005, 2, 3, 2, 0), vm) datetime.date(2005, 2, 3) >>> virtual_day(datetime(2005, 2, 3, 12, 0), vm) datetime.date(2005, 2, 3) >>> virtual_day(datetime(2005, 2, 3, 23, 59), vm) datetime.date(2005, 2, 3) """ def doctest_different_days(): """Tests for different_days >>> from datetime import datetime, time >>> from gtimelog.main import different_days Virtual midnight >>> vm = time(2, 0) The tests themselves: >>> different_days(datetime(2005, 2, 3, 1, 15), ... datetime(2005, 2, 3, 2, 15), vm) True >>> different_days(datetime(2005, 2, 3, 11, 15), ... datetime(2005, 2, 3, 12, 15), vm) False """ def doctest_first_of_month(): """Tests for first_of_month >>> from gtimelog.main import first_of_month >>> from datetime import date, timedelta >>> first_of_month(date(2007, 1, 1)) datetime.date(2007, 1, 1) >>> first_of_month(date(2007, 1, 7)) datetime.date(2007, 1, 1) >>> first_of_month(date(2007, 1, 31)) datetime.date(2007, 1, 1) >>> first_of_month(date(2007, 2, 1)) datetime.date(2007, 2, 1) >>> first_of_month(date(2007, 2, 28)) datetime.date(2007, 2, 1) >>> first_of_month(date(2007, 3, 1)) datetime.date(2007, 3, 1) Why not test extensively? >>> d = date(2000, 1, 1) >>> while d < date(2005, 1, 1): ... f = first_of_month(d) ... if (f.year, f.month, f.day) != (d.year, d.month, 1): ... print "WRONG: first_of_month(%r) returned %r" % (d, f) ... d += timedelta(1) """ def doctest_next_month(): """Tests for next_month >>> from gtimelog.main import next_month >>> from datetime import date, timedelta >>> next_month(date(2007, 1, 1)) datetime.date(2007, 2, 1) >>> next_month(date(2007, 1, 7)) datetime.date(2007, 2, 1) >>> next_month(date(2007, 1, 31)) datetime.date(2007, 2, 1) >>> next_month(date(2007, 2, 1)) datetime.date(2007, 3, 1) >>> next_month(date(2007, 2, 28)) datetime.date(2007, 3, 1) >>> next_month(date(2007, 3, 1)) datetime.date(2007, 4, 1) Why not test extensively? >>> d = date(2000, 1, 1) >>> while d < date(2005, 1, 1): ... f = next_month(d) ... prev = f - timedelta(1) ... if f.day != 1 or (prev.year, prev.month) != (d.year, d.month): ... print "WRONG: next_month(%r) returned %r" % (d, f) ... d += timedelta(1) """ def doctest_uniq(): """Tests for uniq >>> from gtimelog.main import uniq >>> uniq(['a', 'b', 'b', 'c', 'd', 'b', 'd']) ['a', 'b', 'c', 'd', 'b', 'd'] >>> uniq(['a']) ['a'] >>> uniq([]) [] """ def doctest_TimeWindow_to_csv_daily(): r"""Tests for TimeWindow.to_csv_daily >>> from datetime import datetime, time >>> min = datetime(2008, 6, 1) >>> max = datetime(2008, 7, 1) >>> vm = time(2, 0) >>> from StringIO import StringIO >>> sampledata = StringIO(''' ... 2008-06-03 12:45: start ... 2008-06-03 13:00: something ... 2008-06-03 14:45: something else ... 2008-06-03 15:45: etc ... 2008-06-05 12:45: start ... 2008-06-05 13:15: something ... ''') >>> from gtimelog.main import TimeWindow >>> window = TimeWindow(sampledata, min, max, vm) >>> import sys >>> window.to_csv_daily(sys.stdout) date,day-start (hours),slacking (hours),work (hours) 2008-06-03,12.75,0.0,3.0 2008-06-04,0.0,0.0,0.0 2008-06-05,12.75,0.0,0.5 """ def doctest_Reports_weekly_report_categorized(): r"""Tests for Reports.weekly_report_categorized >>> import sys >>> from datetime import datetime, time >>> from tempfile import NamedTemporaryFile >>> from gtimelog.main import TimeWindow, Reports >>> vm = time(2, 0) >>> min = datetime(2010, 1, 25) >>> max = datetime(2010, 1, 31) >>> fh = NamedTemporaryFile() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.weekly_report_categorized(sys.stdout, 'foo@bar.com', ... 'Bob Jones') To: foo@bar.com Subject: Weekly report for Bob Jones (week 04) No work done this week. >>> _ = [fh.write(s + '\n') for s in [ ... '2010-01-30 09:00: start', ... '2010-01-30 09:23: Bing: stuff', ... '2010-01-30 12:54: Bong: other stuff', ... '2010-01-30 13:32: lunch **', ... '2010-01-30 23:46: misc', ... '']] >>> fh.flush() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.weekly_report_categorized(sys.stdout, 'foo@bar.com', ... 'Bob Jones') To: foo@bar.com Subject: Weekly report for Bob Jones (week 04) time Bing: Stuff 0:23 ---------------------------------------------------------------------- 0:23 Bong: Other stuff 3:31 ---------------------------------------------------------------------- 3:31 No category: Misc 10:14 ---------------------------------------------------------------------- 10:14 Total work done this week: 14:08 Categories by time spent: No category 10:14 Bong 3:31 Bing 0:23 """ def doctest_Reports_monthly_report_categorized(): r"""Tests for Reports.monthly_report_categorized >>> import sys >>> from datetime import datetime, time >>> from tempfile import NamedTemporaryFile >>> from gtimelog.main import TimeWindow, Reports >>> vm = time(2, 0) >>> min = datetime(2010, 1, 25) >>> max = datetime(2010, 1, 31) >>> fh = NamedTemporaryFile() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.monthly_report_categorized(sys.stdout, 'foo@bar.com', ... 'Bob Jones') To: foo@bar.com Subject: Monthly report for Bob Jones (2010/01) No work done this month. >>> _ = [fh.write(s + '\n') for s in [ ... '2010-01-30 09:00: start', ... '2010-01-30 09:23: Bing: stuff', ... '2010-01-30 12:54: Bong: other stuff', ... '2010-01-30 13:32: lunch **', ... '2010-01-30 23:46: misc', ... '']] >>> fh.flush() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.monthly_report_categorized(sys.stdout, 'foo@bar.com', ... 'Bob Jones') To: foo@bar.com Subject: Monthly report for Bob Jones (2010/01) time Bing: Stuff 0:23 ---------------------------------------------------------------------- 0:23 Bong: Other stuff 3:31 ---------------------------------------------------------------------- 3:31 No category: Misc 10:14 ---------------------------------------------------------------------- 10:14 Total work done this month: 14:08 Categories by time spent: No category 10:14 Bong 3:31 Bing 0:23 """ def doctest_Reports_report_categories(): r"""Tests for Reports._report_categories >>> import sys >>> from datetime import datetime, time, timedelta >>> from tempfile import NamedTemporaryFile >>> from gtimelog.main import TimeWindow, Reports >>> vm = time(2, 0) >>> min = datetime(2010, 1, 25) >>> max = datetime(2010, 1, 31) >>> fh = NamedTemporaryFile() >>> categories = { ... 'Bing': timedelta(2), ... None: timedelta(1)} >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports._report_categories(sys.stdout, categories) By category: Bing 48 hours (none) 24 hours """ def doctest_Reports_daily_report(): r"""Tests for Reports.daily_report >>> import sys >>> from datetime import datetime, time >>> from tempfile import NamedTemporaryFile >>> from gtimelog.main import TimeWindow, Reports >>> vm = time(2, 0) >>> min = datetime(2010, 1, 30) >>> max = datetime(2010, 1, 31) >>> fh = NamedTemporaryFile() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.daily_report(sys.stdout, 'foo@bar.com', 'Bob Jones') To: foo@bar.com Subject: 2010-01-30 report for Bob Jones (Sat, week 04) No work done today. >>> _ = [fh.write(s + '\n') for s in [ ... '2010-01-30 09:00: start', ... '2010-01-30 09:23: Bing: stuff', ... '2010-01-30 12:54: Bong: other stuff', ... '2010-01-30 13:32: lunch **', ... '2010-01-30 15:46: misc', ... '']] >>> fh.flush() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.daily_report(sys.stdout, 'foo@bar.com', 'Bob Jones') To: foo@bar.com Subject: 2010-01-30 report for Bob Jones (Sat, week 04) Start at 09:00 Bing: stuff 23 min Bong: other stuff 3 hours 31 min Misc 2 hours 14 min Total work done: 6 hours 8 min By category: Bing 23 min Bong 3 hours 31 min (none) 2 hours 14 min Slacking: Lunch ** 38 min Time spent slacking: 38 min """ def doctest_Reports_weekly_report_plain(): r"""Tests for Reports.weekly_report_plain >>> import sys >>> from datetime import datetime, time >>> from tempfile import NamedTemporaryFile >>> from gtimelog.main import TimeWindow, Reports >>> vm = time(2, 0) >>> min = datetime(2010, 1, 25) >>> max = datetime(2010, 1, 31) >>> fh = NamedTemporaryFile() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.weekly_report_plain(sys.stdout, 'foo@bar.com', 'Bob Jones') To: foo@bar.com Subject: Weekly report for Bob Jones (week 04) No work done this week. >>> _ = [fh.write(s + '\n') for s in [ ... '2010-01-30 09:00: start', ... '2010-01-30 09:23: Bing: stuff', ... '2010-01-30 12:54: Bong: other stuff', ... '2010-01-30 13:32: lunch **', ... '2010-01-30 15:46: misc', ... '']] >>> fh.flush() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.weekly_report_plain(sys.stdout, 'foo@bar.com', 'Bob Jones') To: foo@bar.com Subject: Weekly report for Bob Jones (week 04) time Bing: stuff 23 min Bong: other stuff 3 hours 31 min Misc 2 hours 14 min Total work done this week: 6 hours 8 min By category: Bing 23 min Bong 3 hours 31 min (none) 2 hours 14 min """ def doctest_Reports_monthly_report_plain(): r"""Tests for Reports.monthly_report_plain >>> import sys >>> from datetime import datetime, time >>> from tempfile import NamedTemporaryFile >>> from gtimelog.main import TimeWindow, Reports >>> vm = time(2, 0) >>> min = datetime(2007, 9, 1) >>> max = datetime(2007, 10, 1) >>> fh = NamedTemporaryFile() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.monthly_report_plain(sys.stdout, 'foo@bar.com', 'Bob Jones') To: foo@bar.com Subject: Monthly report for Bob Jones (2007/09) No work done this month. >>> _ = [fh.write(s + '\n') for s in [ ... '2007-09-30 09:00: start', ... '2007-09-30 09:23: Bing: stuff', ... '2007-09-30 12:54: Bong: other stuff', ... '2007-09-30 13:32: lunch **', ... '2007-09-30 15:46: misc', ... '']] >>> fh.flush() >>> window = TimeWindow(fh.name, min, max, vm) >>> reports = Reports(window) >>> reports.monthly_report_plain(sys.stdout, 'foo@bar.com', 'Bob Jones') To: foo@bar.com Subject: Monthly report for Bob Jones (2007/09) time Bing: stuff 23 min Bong: other stuff 3 hours 31 min Misc 2 hours 14 min Total work done this month: 6 hours 8 min By category: Bing 23 min Bong 3 hours 31 min (none) 2 hours 14 min """ def additional_tests(): # for setup.py return doctest.DocTestSuite(optionflags=doctest.NORMALIZE_WHITESPACE) def main(): unittest.TextTestRunner().run(additional_tests()) if __name__ == '__main__': main()