From dda4ff807d96091aa033a903028dbcff6a7a7a46 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 11 Apr 2016 17:16:33 +0200 Subject: [PATCH] Add remaining tests. Merge bugfix for report printing. --- .../.ctt/foo1/2016-04-08-1200/comment | 1 + .../fixtures/.ctt/foo1/2016-04-08-1200/delta | 1 + .../fixtures/.ctt/foo2/2016-04-07-0810/delta | 2 +- lib/ctt/test/test_report.py | 194 ++++++++++++++++-- lib/ctt/test/test_tracker.py | 21 +- 5 files changed, 196 insertions(+), 23 deletions(-) create mode 100644 lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/comment create mode 100644 lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/delta diff --git a/lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/comment b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/comment new file mode 100644 index 0000000..d8c668a --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/comment @@ -0,0 +1 @@ +foo1 12 diff --git a/lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/delta b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/delta new file mode 100644 index 0000000..e9c02da --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-08-1200/delta @@ -0,0 +1 @@ +5000 diff --git a/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/delta b/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/delta index 45cbdd1..f599e28 100644 --- a/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/delta +++ b/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/delta @@ -1 +1 @@ -10.312733 +10 diff --git a/lib/ctt/test/test_report.py b/lib/ctt/test/test_report.py index 512e9c0..c715a1d 100755 --- a/lib/ctt/test/test_report.py +++ b/lib/ctt/test/test_report.py @@ -23,44 +23,202 @@ import unittest import ctt import ctt.test -import ctt.tracker as tr -import os +import ctt.report as report +import sys +from io import StringIO import datetime -import shutil +import collections -# TODO implement tests class ReportTestCase(ctt.test.CttTestCase): + def setUp(self): + super(ReportTestCase, self).setUp() + self.sys_stdout = sys.stdout + out = StringIO() + sys.stdout = out + self.maxDiff = None - def test_commandline(self): - pass + def _get_output(self): + sys.stdout.flush() + output = sys.stdout.getvalue().strip() + return output + + def tearDown(self): + sys.stdout = self.sys_stdout + super(ReportTestCase, self).tearDown() def test_print_report_time_entries(self): - pass + report_data = { + '2016-04-07-0826': [ + { + 'start_datetime': '2016-04-07-0826', + 'end_datetime': '2016-04-07-2359', + 'comment': 'foo1', + 'delta': '6', + 'delta_seconds': '6', + 'delta_minutes': '0', + }, + ], + } + report.Report.print_report_time_entries(report_data, ctt.REPORTFORMAT, + False) + output = self._get_output() + expected_output = "2016-04-07-0826 (6): foo1" + self.assertEqual(output, expected_output) + + def test_print_report_time_entries_summary(self): + report_data = { + '2016-04-07-0826': [ + { + 'start_datetime': '2016-04-07-0826', + 'end_datetime': '2016-04-07-2359', + 'comment': 'foo1', + 'delta': '6', + 'delta_seconds': '6', + 'delta_minutes': '0', + }, + ], + '2016-04-07-0926': [ + { + 'start_datetime': '2016-04-07-0926', + 'end_datetime': '2016-04-07-2359', + 'comment': 'foo12', + 'delta': '10', + 'delta_seconds': '10', + 'delta_minutes': '0', + }, + ], + } + report.Report.print_report_time_entries(report_data, ctt.REPORTFORMAT, + True) + output = self._get_output() + expected_output = ("2016-04-07-0826 (6): foo1\n" + "2016-04-07-0926 (10): foo12") + self.assertEqual(output, expected_output) + + def test_print_reports(self): + reports = collections.OrderedDict() + for project in ('foo1', 'foo2'): + rep = report.Report(project, ('2016-04-07',), ('2016-04-08',), + ctt.REPORTFORMAT, None, None) + report_data = rep.report() + reports[project] = (rep, report_data) + expected_output = ( + "Report for foo1 between 2016-04-07 00:00:00 and 2016-04-08 23:59:59\n" + "2016-04-07-0826 (0:00:06): foo1\n" + "2016-04-08-1200 (1:23:20): foo1 12\n" + "Report for foo2 between 2016-04-07 00:00:00 and 2016-04-08 23:59:59\n" + "2016-04-07-0810 (0:00:10): foo2" + ) + rep.print_reports(reports, ctt.REPORTFORMAT, summary=False) + output = self._get_output() + self.assertEqual(output, expected_output) + + def test_print_reports_summary(self): + reports = collections.OrderedDict() + for project in ('foo1', 'foo2'): + rep = report.Report(project, ('2016-04-07',), ('2016-04-08',), + ctt.REPORTFORMAT, None, None) + report_data = rep.report() + reports[project] = (rep, report_data) + expected_output = ( + "2016-04-07-0810 (0:00:10): foo2\n" + "2016-04-07-0826 (0:00:06): foo1\n" + "2016-04-08-1200 (1:23:20): foo1 12" + ) + rep.print_reports(reports, ctt.REPORTFORMAT, summary=True) + output = self._get_output() + self.assertEqual(output, expected_output) - def print_reports(self): - pass def test__init_date(self): - pass + rep = report.Report('foo1', ('2016-04-07',), ('2016-04-07',), + ctt.REPORTFORMAT, None, None) + expected_start_date = datetime.datetime(2016, 4, 7) + expected_end_date = datetime.datetime(2016, 4, 7, 23, 59, 59) + self.assertEqual(rep.start_date, expected_start_date) + self.assertEqual(rep.end_date, expected_end_date) + + @unittest.expectedFailure + def test__init_date_fail(self): + rep = report.Report('foo1', ('2016-04-08',), ('2016-04-07',), + ctt.REPORTFORMAT, None, None) + + def test__init_date_defaults(self): + rep = report.Report('foo1', None, None, + ctt.REPORTFORMAT, None, None) + now = datetime.datetime.now() + expected_start_date = now.replace(day=1, hour=0, minute=0, second=0, + microsecond=0) + next_month = expected_start_date.replace(day=28) + datetime.timedelta(days=4) + first_day_next_month = next_month.replace(day=1) + expected_end_date = first_day_next_month - datetime.timedelta(seconds=1) + self.assertEqual(rep.start_date, expected_start_date) + self.assertEqual(rep.end_date, expected_end_date) + + @unittest.expectedFailure + def test__init_report_db_fail(self): + rep = report.Report('unexisting', ('2016-04-07',), ('2016-04-07',), + ctt.REPORTFORMAT, None, None) def test__init_report_db(self): - pass + rep = report.Report('foo1', ('2016-04-07',), ('2016-04-07',), + ctt.REPORTFORMAT, None, None) + expected_db = { + '2016-04-07-0826': { + 'comment': 'foo1', + 'delta': '6.248274' + }, + } + self.assertEqual(rep._report_db, expected_db) def test_header(self): - pass + rep = report.Report('foo1', ('2016-04-07',), ('2016-04-07',), + ctt.REPORTFORMAT, None, None) + rep.header() + output = self._get_output() + self.assertEqual(output, ("Report for foo1 between 2016-04-07 00:00:00" + " and 2016-04-07 23:59:59")) def test_summary(self): - pass + report.Report.summary(10) + output = self._get_output() + self.assertEqual(output, "Total time tracked: 0h 0m 10s.") def test_total_time(self): - pass - - def test__get_report_entry(self): - pass + rep = report.Report('foo1', ('2016-04-07',), ('2016-04-07',), + ctt.REPORTFORMAT, None, None) + total_time = rep.total_time + expected_total_time = 6.248274 + self.assertEqual(total_time, expected_total_time) def test_report(self): - pass + rep = report.Report('foo1', ('2016-04-07',), ('2016-04-08',), + ctt.REPORTFORMAT, None, None) + expected_entries = { + '2016-04-07-0826': [ + { + 'start_datetime': '2016-04-07-0826', + 'end_datetime': '2016-04-07-0826', + 'comment': 'foo1', + 'delta': datetime.timedelta(seconds=6), + 'delta_seconds': 6, + 'delta_minutes': 0, + }, + ], + '2016-04-08-1200': [ + { + 'start_datetime': '2016-04-08-1200', + 'end_datetime': '2016-04-08-1323', + 'comment': 'foo1 12', + 'delta': datetime.timedelta(seconds=5000), + 'delta_seconds': 5000, + 'delta_minutes': 83, + }, + ], + } + entries = rep.report() + self.assertEqual(entries, expected_entries) if __name__ == '__main__': diff --git a/lib/ctt/test/test_tracker.py b/lib/ctt/test/test_tracker.py index bc96b9b..c7c6caf 100755 --- a/lib/ctt/test/test_tracker.py +++ b/lib/ctt/test/test_tracker.py @@ -29,6 +29,17 @@ import datetime import shutil class TrackerTestCase(ctt.test.CttTestCase): + + def setUp(self): + super(TrackerTestCase, self).setUp() + self.rm_dirs = [] + + def tearDown(self): + for d in self.rm_dirs: + if os.path.exists(d): + shutil.rmtree(d) + super(TrackerTestCase, self).tearDown() + def test___init__(self): project = 'foo1' expected_project_dir = os.path.join(ctt.test.fixtures_dir, @@ -90,9 +101,9 @@ class TrackerTestCase(ctt.test.CttTestCase): expected_comment = "test" tracker.comment = expected_comment expected_comment += "\n" - timedir = os.path.join(os.path.join( - ctt.test.fixtures_dir, os.path.join('.ctt', project)), + timedir = os.path.join(ctt.test.fixtures_dir, '.ctt', project, '2016-04-09-1730') + self.rm_dirs.append(timedir) if os.path.exists(timedir): shutil.rmtree(timedir) tracker.write_time() @@ -106,6 +117,7 @@ class TrackerTestCase(ctt.test.CttTestCase): with open(commentfile, "r") as f: comment = f.read() self.assertEqual(comment, expected_comment) + print("timedir: ", timedir) @unittest.expectedFailure def test_write_time_fail(self): @@ -119,13 +131,14 @@ class TrackerTestCase(ctt.test.CttTestCase): tracker._tracked_time = True expected_comment = "test" tracker.comment = expected_comment - timedir = os.path.join(os.path.join( - ctt.test.fixtures_dir, os.path.join('.ctt', project)), + timedir = os.path.join(ctt.test.fixtures_dir, '.ctt', project, '2016-04-09-1730') + self.rm_dirs.append(timedir) if os.path.exists(timedir): shutil.rmtree(timedir) os.makedirs(timedir, mode=0o700) tracker.write_time() + print("timedir: ", timedir) if __name__ == '__main__':