Commit 4e2c4f02 by Peter Fogg

Fix times not being parse from HH:MM:SS format into floats and update video module logic tests.

parent 681e96b7
...@@ -6,6 +6,8 @@ import logging ...@@ -6,6 +6,8 @@ import logging
from lxml import etree from lxml import etree
from pkg_resources import resource_string, resource_listdir from pkg_resources import resource_string, resource_listdir
import datetime
import time
from django.http import Http404 from django.http import Http404
...@@ -137,10 +139,10 @@ class VideoDescriptor(VideoFields, ...@@ -137,10 +139,10 @@ class VideoDescriptor(VideoFields,
tag = _get_first_external(xml, 'tag') tag = _get_first_external(xml, 'tag')
if tag: if tag:
video.tag = tag video.tag = tag
start_time = xml.get('from') start_time = _parse_time(xml.get('from'))
if start_time: if start_time:
video.start_time = start_time video.start_time = start_time
end_time = xml.get('to') end_time = _parse_time(xml.get('to'))
if end_time: if end_time:
video.end_time = end_time video.end_time = end_time
return video return video
...@@ -177,3 +179,17 @@ def _parse_youtube(data): ...@@ -177,3 +179,17 @@ def _parse_youtube(data):
# properly. # properly.
ret['%.2f' % float(pieces[0])] = pieces[1] ret['%.2f' % float(pieces[0])] = pieces[1]
return ret return ret
def _parse_time(str_time):
"""Converts s in '12:34:45' format to seconds. If s is
None, returns empty string"""
if str_time is None:
return ''
else:
obj_time = time.strptime(str_time, '%H:%M:%S')
return datetime.timedelta(
hours=obj_time.tm_hour,
minutes=obj_time.tm_min,
seconds=obj_time.tm_sec
).total_seconds()
...@@ -18,7 +18,7 @@ import unittest ...@@ -18,7 +18,7 @@ import unittest
from mock import Mock from mock import Mock
from lxml import etree from lxml import etree
from xmodule.video_module import VideoDescriptor, VideoModule from xmodule.video_module import VideoDescriptor, VideoModule, _parse_time, _parse_youtube
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.tests import test_system from xmodule.tests import test_system
from xmodule.tests.test_logic import LogicTest from xmodule.tests.test_logic import LogicTest
...@@ -67,30 +67,31 @@ class VideoModuleLogicTest(LogicTest): ...@@ -67,30 +67,31 @@ class VideoModuleLogicTest(LogicTest):
'data': '<video />' 'data': '<video />'
} }
def test_get_timeframe_no_parameters(self): def test_parse_time(self):
"""Make sure that timeframe() works correctly w/o parameters""" """Ensure that times are parse correctly into seconds."""
xmltree = etree.fromstring('<video>test</video>') output = _parse_time('00:04:07')
output = self.xmodule.get_timeframe(xmltree) self.assertEqual(output, 247)
self.assertEqual(output, ('', ''))
def test_parse_youtube(self):
def test_get_timeframe_with_one_parameter(self): """Test parsing old-style Youtube ID strings into a dict."""
"""Make sure that timeframe() works correctly with one parameter""" youtube_str = '0.75:jNCf2gIqpeE,1.0:ZwkTiUPN0mg,1.25:rsq9auxASqI,1.50:kMyNdzVHHgg'
xmltree = etree.fromstring( output = _parse_youtube(youtube_str)
'<video from="00:04:07">test</video>' self.assertEqual(output, {'0.75': 'jNCf2gIqpeE',
) '1.00': 'ZwkTiUPN0mg',
output = self.xmodule.get_timeframe(xmltree) '1.25': 'rsq9auxASqI',
self.assertEqual(output, (247, '')) '1.50': 'kMyNdzVHHgg'})
def test_get_timeframe_with_two_parameters(self): def test_parse_youtube_one_video(self):
"""Make sure that timeframe() works correctly with two parameters""" """
xmltree = etree.fromstring( Ensure that all keys are present and missing speeds map to the
'''<video empty string.
from="00:04:07" """
to="13:04:39" youtube_str = '0.75:jNCf2gIqpeE'
>test</video>''' output = _parse_youtube(youtube_str)
) self.assertEqual(output, {'0.75': 'jNCf2gIqpeE',
output = self.xmodule.get_timeframe(xmltree) '1.00': '',
self.assertEqual(output, (247, 47079)) '1.25': '',
'1.50': ''})
class VideoModuleUnitTest(unittest.TestCase): class VideoModuleUnitTest(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment