Introduce unit tests (init, main, listprojects, tracker).

This commit is contained in:
Darko Poljak 2016-04-10 21:01:54 +02:00
parent 2cf503abac
commit 6245a3c015
19 changed files with 328 additions and 0 deletions

29
lib/ctt/test/__init__.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
#
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

48
lib/ctt/test/__main__.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
#
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)

View File

@ -0,0 +1 @@
foo1

View File

@ -0,0 +1 @@
6.248274

View File

@ -0,0 +1 @@
foo2

View File

@ -0,0 +1 @@
10.312733

View File

@ -0,0 +1 @@
foo3

View File

@ -0,0 +1 @@
4.744116

View File

@ -0,0 +1 @@
spam-eggs

View File

@ -0,0 +1 @@
3.307185

View File

@ -0,0 +1 @@
test-1

View File

@ -0,0 +1 @@
2.972467

View File

@ -0,0 +1 @@
test-2

View File

@ -0,0 +1 @@
2.558247

View File

@ -0,0 +1 @@
test-3

View File

@ -0,0 +1 @@
1.821598

View File

@ -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 <http://www.gnu.org/licenses/>.
#
#
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()

67
lib/ctt/test/test_report.py Executable file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
#
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()

132
lib/ctt/test/test_tracker.py Executable file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
#
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()