Commit 2ced6baa by Afzal Wali

refactored the humanized_time method in utils to improve coverage.

Added the singleton test to the test_services to improve coverage.
parent f8310339
...@@ -23,3 +23,11 @@ class TestProctoringService(unittest.TestCase): ...@@ -23,3 +23,11 @@ class TestProctoringService(unittest.TestCase):
attr = getattr(edx_proctoring_api, attr_name, None) attr = getattr(edx_proctoring_api, attr_name, None)
if isinstance(attr, types.FunctionType) and not attr_name.startswith('_'): if isinstance(attr, types.FunctionType) and not attr_name.startswith('_'):
self.assertTrue(hasattr(service, attr_name)) self.assertTrue(hasattr(service, attr_name))
def test_singleton(self):
"""
Test to make sure the ProctoringService is a singleton.
"""
service1 = ProctoringService()
service2 = ProctoringService()
self.assertIs(service1, service2)
...@@ -42,3 +42,6 @@ class TestHumanizedTime(unittest.TestCase): ...@@ -42,3 +42,6 @@ class TestHumanizedTime(unittest.TestCase):
human_time = humanized_time(180) human_time = humanized_time(180)
self.assertEqual(human_time, "3 Hours") self.assertEqual(human_time, "3 Hours")
human_time = humanized_time(-60)
self.assertEqual(human_time, "error")
...@@ -28,31 +28,33 @@ def humanized_time(time_in_minutes): ...@@ -28,31 +28,33 @@ def humanized_time(time_in_minutes):
hours = int(time_in_minutes / 60) hours = int(time_in_minutes / 60)
minutes = time_in_minutes % 60 minutes = time_in_minutes % 60
template = ""
hours_present = False hours_present = False
if hours == 0: if hours == 0:
hours_present = False hours_present = False
template = ""
elif hours == 1: elif hours == 1:
template = _("{num_of_hours} Hour") template = _("{num_of_hours} Hour")
hours_present = True hours_present = True
elif hours >= 2: elif hours >= 2:
template = _("{num_of_hours} Hours") template = _("{num_of_hours} Hours")
hours_present = True hours_present = True
else:
if minutes == 0: template = "error"
if not hours_present:
template = _("{num_of_minutes} Minutes") if template != "error":
elif minutes == 1: if minutes == 0:
if hours_present: if not hours_present:
template += _(" and {num_of_minutes} Minute") template = _("{num_of_minutes} Minutes")
else: elif minutes == 1:
template += _("{num_of_minutes} Minute") if hours_present:
elif minutes >= 2: template += _(" and {num_of_minutes} Minute")
if hours_present: else:
template += _(" and {num_of_minutes} Minutes") template += _("{num_of_minutes} Minute")
else: else:
template += _("{num_of_minutes} Minutes") if hours_present:
template += _(" and {num_of_minutes} Minutes")
else:
template += _("{num_of_minutes} Minutes")
human_time = template.format(num_of_hours=hours, num_of_minutes=minutes) human_time = template.format(num_of_hours=hours, num_of_minutes=minutes)
return human_time return human_time
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