Skip to content

Commit

Permalink
Don't fail if final track has single trackpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vkurup committed Aug 14, 2016
1 parent f849492 commit b378f33
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tcxparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def completed_at(self):

@property
def distance(self):
return self.activity.Lap[-1].Track.Trackpoint[-2].DistanceMeters.pyval
distance_values = self.root.findall('.//ns:DistanceMeters', namespaces={'ns': namespace})
if distance_values:
return distance_values[-1]
return 0

@property
def distance_units(self):
Expand Down
27 changes: 27 additions & 0 deletions test_tcxparser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from StringIO import StringIO
import unittest

from tcxparser import TCXParser


Expand Down Expand Up @@ -71,5 +73,30 @@ def test_ascent_is_correct(self):
def test_descent_is_correct(self):
self.assertAlmostEqual(self.tcx.descent, 166.307128903)


class BugTest(unittest.TestCase):

def test_single_trackpoint_in_track_is_ok(self):
"https://github.com/vkurup/python-tcxparser/issues/9"
xml = """
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity>
<Lap>
<Track>
<Trackpoint>
<DistanceMeters>5</DistanceMeters>
</Trackpoint>
</Track>
</Lap>
</Activity>
</Activities>
</TrainingCenterDatabase>
"""
tcx_file = StringIO(xml)
tcx = TCXParser(tcx_file)
self.assertEqual(tcx.distance, 5)


if __name__ == '__main__':
unittest.main()

0 comments on commit b378f33

Please sign in to comment.