TI-40: totals.py extension script fails with an error

- Improve totals.py extension script:
- Fix display issue if tags are shorter than string 'Totals'
- Fix issue when totals.py is called on empty database
- Add tests: test_totals.t
This commit is contained in:
Thomas Lauf 2017-02-14 23:33:02 +01:00
parent 3fe704ce80
commit 8ad54f9bfd
3 changed files with 105 additions and 7 deletions

View file

@ -6,6 +6,8 @@
(thanks to hosaka).
- TI-39 Bogus command line option causes segfault
(thanks to Jan Stolarek, Thomas Lauf).
- TI-40 totals.py extension script fails with an error
(thanks to Jan Stolarek)
- TI-42 refresh holiday script throws an error on nb-NO locale
(thanks to Jelle van der Waa).
- TI-47 first call successfully creates new database but returns exit status 1
@ -20,7 +22,7 @@
(thanks to Thomas Lauf).
- TI-64 Command 'stop' with date before current interval's start date causes segfault
(thanks to Thomas Lauf).
- Fixed Python 3 support of the holdiay/refresh script
- Fixed Python 3 support of the holiday/refresh script
(thanks to Jelle van der Waa).
- Added missing man page link
(thanks to David Patrick).

View file

@ -60,6 +60,7 @@ for line in sys.stdin:
# Sum the second tracked by tag.
totals = dict()
untagged = None
j = json.loads(body)
for object in j:
start = datetime.datetime.strptime(object['start'], DATEFORMAT)
@ -71,20 +72,31 @@ for object in j:
tracked = end - start
for tag in object['tags']:
if tag in totals:
totals[tag] += tracked
if 'tags' not in object or object['tags'] == []:
if untagged is None:
untagged = tracked
else:
totals[tag] = tracked
untagged += tracked
else:
for tag in object['tags']:
if tag in totals:
totals[tag] += tracked
else:
totals[tag] = tracked
# Determine largest tag width.
max_width = 0
max_width = len('Total')
for tag in totals:
if len(tag) > max_width:
max_width = len(tag)
if 'temp.report.start' not in configuration:
print 'There is no data in the database'
exit()
start = datetime.datetime.strptime(configuration['temp.report.start'], DATEFORMAT)
end = datetime.datetime.strptime(configuration['temp.report.end'], DATEFORMAT)
if max_width > 0:
# Compose report header.
print '\nTotal by Tag, for %s - %s\n' % (start, end)
@ -103,6 +115,11 @@ if max_width > 0:
grand_total += totals[tag].seconds
print '%-*s %10s' % (max_width, tag, formatted)
if untagged is not None:
formatted = formatSeconds(untagged.seconds)
grand_total += untagged.seconds
print '%-*s %10s' % (max_width, '', formatted)
# Compose total.
if configuration['color'] == 'on':
print ' ' * max_width, ' '
@ -113,4 +130,3 @@ if max_width > 0:
else:
print 'No data in the range %s - %s' % (start, end)

80
test/test_totals.t Executable file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
###############################################################################
#
# Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# http://www.opensource.org/licenses/mit-license.php
#
###############################################################################
import sys
import os
import subprocess
import unittest
# Ensure python finds the local simpletap module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from basetest import TestCase
class TestTotals(TestCase):
def setUp(self):
current_dir = os.path.dirname(os.path.abspath(__file__))
self.process = subprocess.Popen([os.path.join(current_dir, '../ext/totals.py')],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def test_totals_with_empty_database(self):
"""totals extension should report error on empty database"""
out, err = self.process.communicate(input="color: off\ndebug: on\ntemp.report.start: \ntemp.report.end: \n\n[]")
self.assertEqual('There is no data in the database\n', out)
self.assertEqual('', err)
def test_totals_with_filled_database(self):
"""totals extension should print report for filled database"""
out, err = self.process.communicate(input="color: off\ndebug: on\ntemp.report.start: 20160101T070000Z\ntemp.report.end: 20160101T080000Z\n\n[{\"start\":\"20160101T070000Z\",\"end\":\"20160101T080000Z\",\"tags\":[\"foo\"]}]")
self.assertEqual('\nTotal by Tag, for 2016-01-01 07:00:00 - 2016-01-01 08:00:00\n\nTag Total\n----- ----------\nfoo 1:00:00\n ----------\nTotal 1:00:00\n', out)
self.assertEqual('', err)
def test_totals_with_interval_without_tags(self):
"""totals extension should handle interval without tags"""
out, err = self.process.communicate(input="color: off\ndebug: on\ntemp.report.start: 20160101T070000Z\ntemp.report.end: 20160101T080000Z\n\n[{\"start\":\"20160101T070000Z\",\"end\":\"20160101T080000Z\"}]")
self.assertEqual('\nTotal by Tag, for 2016-01-01 07:00:00 - 2016-01-01 08:00:00\n\nTag Total\n----- ----------\n 1:00:00\n ----------\nTotal 1:00:00\n', out)
self.assertEqual('', err)
def test_totals_with_interval_with_empty_tag_list(self):
"""totals extension should handle interval with empty tag list"""
out, err = self.process.communicate(
input="color: off\ndebug: on\ntemp.report.start: 20160101T070000Z\ntemp.report.end: 20160101T080000Z\n\n[{\"start\":\"20160101T070000Z\",\"end\":\"20160101T080000Z\",\"tags\":[]}]")
self.assertEqual('\nTotal by Tag, for 2016-01-01 07:00:00 - 2016-01-01 08:00:00\n\nTag Total\n----- ----------\n 1:00:00\n ----------\nTotal 1:00:00\n', out)
self.assertEqual('', err)
if __name__ == "__main__":
from simpletap import TAPTestRunner
unittest.main(testRunner=TAPTestRunner())
# vim: ai sts=4 et sw=4 ft=python