From 6245a3c0157923cff69b3619580a1b7e0f6fe752 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sun, 10 Apr 2016 21:01:54 +0200 Subject: [PATCH] Introduce unit tests (init, main, listprojects, tracker). --- lib/ctt/test/__init__.py | 29 ++++ lib/ctt/test/__main__.py | 48 +++++++ .../.ctt/foo1/2016-04-07-0826/comment | 1 + .../fixtures/.ctt/foo1/2016-04-07-0826/delta | 1 + .../.ctt/foo2/2016-04-07-0810/comment | 1 + .../fixtures/.ctt/foo2/2016-04-07-0810/delta | 1 + .../.ctt/foo3/2016-04-02-2110/comment | 1 + .../fixtures/.ctt/foo3/2016-04-02-2110/delta | 1 + .../.ctt/spam-eggs/2016-04-07-1229/comment | 1 + .../.ctt/spam-eggs/2016-04-07-1229/delta | 1 + .../.ctt/test-1/2016-04-02-2109/comment | 1 + .../.ctt/test-1/2016-04-02-2109/delta | 1 + .../.ctt/test-2/2016-04-02-2109/comment | 1 + .../.ctt/test-2/2016-04-02-2109/delta | 1 + .../.ctt/test-3/2016-04-02-2109/comment | 1 + .../.ctt/test-3/2016-04-02-2109/delta | 1 + lib/ctt/test/test_listprojects.py | 38 +++++ lib/ctt/test/test_report.py | 67 +++++++++ lib/ctt/test/test_tracker.py | 132 ++++++++++++++++++ 19 files changed, 328 insertions(+) create mode 100644 lib/ctt/test/__init__.py create mode 100644 lib/ctt/test/__main__.py create mode 100644 lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/comment create mode 100644 lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/delta create mode 100644 lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/comment create mode 100644 lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/delta create mode 100644 lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/comment create mode 100644 lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/delta create mode 100644 lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/comment create mode 100644 lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/delta create mode 100644 lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/comment create mode 100644 lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/delta create mode 100644 lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/comment create mode 100644 lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/delta create mode 100644 lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/comment create mode 100644 lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/delta create mode 100755 lib/ctt/test/test_listprojects.py create mode 100755 lib/ctt/test/test_report.py create mode 100755 lib/ctt/test/test_tracker.py diff --git a/lib/ctt/test/__init__.py b/lib/ctt/test/__init__.py new file mode 100644 index 0000000..71ea5a3 --- /dev/null +++ b/lib/ctt/test/__init__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# +# 2016 Darko Poljak (darko.poljak at gmail.com)) +# +# This file is part of ctt. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import os +import unittest + +fixtures_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "fixtures")) + +class CttTestCase(unittest.TestCase): + def setUp(self): + os.environ['HOME'] = fixtures_dir diff --git a/lib/ctt/test/__main__.py b/lib/ctt/test/__main__.py new file mode 100644 index 0000000..418ca27 --- /dev/null +++ b/lib/ctt/test/__main__.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2011 Nico Schottelius (nico-cdist at schottelius.org) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import imp +import os +import os.path +import sys +import unittest + +base_dir = os.path.dirname(os.path.realpath(__file__)) + +test_modules = [] +for possible_test in os.listdir(base_dir): + if possible_test.startswith('test_'): + mod_path = os.path.join(base_dir, possible_test) + if os.path.isfile(mod_path): + module = os.path.splitext(os.path.basename(possible_test))[0] + test_modules.append(module) + +suites = [] +for test_module in test_modules: + module_parameters = imp.find_module(test_module, [base_dir]) + module = imp.load_module("ctt.test." + test_module, *module_parameters) + + suite = unittest.defaultTestLoader.loadTestsFromModule(module) + suites.append(suite) + +all_suites = unittest.TestSuite(suites) +unittest.TextTestRunner(verbosity=2).run(all_suites) diff --git a/lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/comment b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/comment new file mode 100644 index 0000000..1715acd --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/comment @@ -0,0 +1 @@ +foo1 diff --git a/lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/delta b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/delta new file mode 100644 index 0000000..ee0c264 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo1/2016-04-07-0826/delta @@ -0,0 +1 @@ +6.248274 diff --git a/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/comment b/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/comment new file mode 100644 index 0000000..54b060e --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/comment @@ -0,0 +1 @@ +foo2 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 new file mode 100644 index 0000000..45cbdd1 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo2/2016-04-07-0810/delta @@ -0,0 +1 @@ +10.312733 diff --git a/lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/comment b/lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/comment new file mode 100644 index 0000000..c1ec6c6 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/comment @@ -0,0 +1 @@ +foo3 diff --git a/lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/delta b/lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/delta new file mode 100644 index 0000000..98330a4 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/foo3/2016-04-02-2110/delta @@ -0,0 +1 @@ +4.744116 diff --git a/lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/comment b/lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/comment new file mode 100644 index 0000000..36c95ea --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/comment @@ -0,0 +1 @@ +spam-eggs diff --git a/lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/delta b/lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/delta new file mode 100644 index 0000000..4b20c52 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/spam-eggs/2016-04-07-1229/delta @@ -0,0 +1 @@ +3.307185 diff --git a/lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/comment b/lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/comment new file mode 100644 index 0000000..8e2baef --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/comment @@ -0,0 +1 @@ +test-1 diff --git a/lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/delta b/lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/delta new file mode 100644 index 0000000..bbec04c --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/test-1/2016-04-02-2109/delta @@ -0,0 +1 @@ +2.972467 diff --git a/lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/comment b/lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/comment new file mode 100644 index 0000000..38c98d9 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/comment @@ -0,0 +1 @@ +test-2 diff --git a/lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/delta b/lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/delta new file mode 100644 index 0000000..d1bf127 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/test-2/2016-04-02-2109/delta @@ -0,0 +1 @@ +2.558247 diff --git a/lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/comment b/lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/comment new file mode 100644 index 0000000..1be6ab6 --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/comment @@ -0,0 +1 @@ +test-3 diff --git a/lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/delta b/lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/delta new file mode 100644 index 0000000..6a2731f --- /dev/null +++ b/lib/ctt/test/fixtures/.ctt/test-3/2016-04-02-2109/delta @@ -0,0 +1 @@ +1.821598 diff --git a/lib/ctt/test/test_listprojects.py b/lib/ctt/test/test_listprojects.py new file mode 100755 index 0000000..3946470 --- /dev/null +++ b/lib/ctt/test/test_listprojects.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2012 Nico Schottelius (nico-ctt at schottelius.org) +# +# This file is part of ctt. +# +# ctt is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# ctt is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ctt. If not, see . +# +# + +import unittest +import ctt +import ctt.listprojects as cttls +import ctt.test + +class ListProjectsTestCase(ctt.test.CttTestCase): + + def test_list_projects(self): + projects = cttls.ListProjects.list_projects() + expected_projects = [ 'foo1', 'foo2', 'foo3', 'spam-eggs', + 'test-1', 'test-2', 'test-3', ] + gotten_projects = sorted(projects) + self.assertEqual(gotten_projects, expected_projects) + +if __name__ == '__main__': + unittest.main() diff --git a/lib/ctt/test/test_report.py b/lib/ctt/test/test_report.py new file mode 100755 index 0000000..512e9c0 --- /dev/null +++ b/lib/ctt/test/test_report.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2016 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of ctt. +# +# ctt is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# ctt is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ctt. If not, see . +# +# + +import unittest +import ctt +import ctt.test +import ctt.tracker as tr +import os +import datetime +import shutil + + +# TODO implement tests +class ReportTestCase(ctt.test.CttTestCase): + + def test_commandline(self): + pass + + def test_print_report_time_entries(self): + pass + + def print_reports(self): + pass + + def test__init_date(self): + pass + + def test__init_report_db(self): + pass + + def test_header(self): + pass + + def test_summary(self): + pass + + def test_total_time(self): + pass + + def test__get_report_entry(self): + pass + + def test_report(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/ctt/test/test_tracker.py b/lib/ctt/test/test_tracker.py new file mode 100755 index 0000000..bc96b9b --- /dev/null +++ b/lib/ctt/test/test_tracker.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# 2016 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of ctt. +# +# ctt is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# ctt is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with ctt. If not, see . +# +# + +import unittest +import ctt +import ctt.test +import ctt.tracker as tr +import os +import datetime +import shutil + +class TrackerTestCase(ctt.test.CttTestCase): + def test___init__(self): + project = 'foo1' + expected_project_dir = os.path.join(ctt.test.fixtures_dir, + os.path.join('.ctt', project)) + tracker = tr.Tracker(project) + self.assertEqual(tracker.project, project) + self.assertEqual(tracker.project_dir, expected_project_dir) + self.assertIsNone(tracker.start_datetime) + self.assertIsNone(tracker.end_datetime) + self.assertIsNone(tracker.comment) + self.assertFalse(tracker._tracked_time) + + tracker = tr.Tracker(project, start_datetime=('2016-04-09-0900',)) + self.assertEqual(tracker.start_datetime, + datetime.datetime(2016, 4, 9, 9, 0)) + self.assertIsNone(tracker.end_datetime) + self.assertFalse(tracker._tracked_time) + + tracker = tr.Tracker(project, start_datetime=('2016-04-04-0900',), + end_datetime=('2016-04-09-2000',)) + self.assertEqual(tracker.start_datetime, + datetime.datetime(2016, 4, 4, 9, 0)) + self.assertEqual(tracker.end_datetime, + datetime.datetime(2016, 4, 9, 20, 0)) + self.assertTrue(tracker._tracked_time) + + @unittest.expectedFailure + def test__init__fail(self): + project = 'foo1' + tracker = tr.Tracker(project, start_datetime=('2016-04-090900',)) + + def test_delta(self): + project = 'foo1' + start_dt = datetime.datetime(2016, 4, 4, 9, 0) + end_dt = datetime.datetime(2016, 4, 9, 20, 0) + tracker = tr.Tracker(project, start_datetime=('2016-04-04-0900',), + end_datetime=('2016-04-09-2000',)) + expected_delta = end_dt - start_dt + tracker._tracked_time = True + delta = tracker.delta(True) + self.assertEqual(delta, expected_delta.total_seconds()) + delta = tracker.delta(False) + self.assertEqual(delta, expected_delta) + tracker._tracked_time = False + delta = tracker.delta(True) + self.assertEqual(delta, 0) + delta = tracker.delta(False) + self.assertEqual(delta, datetime.timedelta()) + + def test_write_time(self): + project = 'foo1' + start_dt = '2016-04-09-1730' + tracker = tr.Tracker(project, start_datetime=(start_dt,), + comment=True) + end_dt = datetime.datetime(2016, 4, 9, hour=17, minute=45) + expected_delta = str(15 * 60) + '.0\n' # seconds + tracker.end_datetime = end_dt + tracker._tracked_time = True + 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)), + '2016-04-09-1730') + if os.path.exists(timedir): + shutil.rmtree(timedir) + tracker.write_time() + timefile = os.path.join(timedir, ctt.FILE_DELTA) + self.assertTrue(os.path.exists(timefile)) + with open(timefile, "r") as f: + delta = f.read() + self.assertEqual(delta, expected_delta) + commentfile = os.path.join(timedir, ctt.FILE_COMMENT) + self.assertTrue(os.path.exists(commentfile)) + with open(commentfile, "r") as f: + comment = f.read() + self.assertEqual(comment, expected_comment) + + @unittest.expectedFailure + def test_write_time_fail(self): + project = 'foo1' + start_dt = '2016-04-09-1730' + tracker = tr.Tracker(project, start_datetime=(start_dt,), + comment=True) + end_dt = datetime.datetime(2016, 4, 9, hour=17, minute=45) + expected_delta = 15 * 60 # seconds + tracker.end_datetime = end_dt + 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)), + '2016-04-09-1730') + if os.path.exists(timedir): + shutil.rmtree(timedir) + os.makedirs(timedir, mode=0o700) + tracker.write_time() + + +if __name__ == '__main__': + unittest.main()