class GetFinalSegmentTest(unittest.TestCase): """Test video task's get_final_segment calculation.""" def setUp(self): self.task = VideoUsageTask(interval=sentinel.ignored, output_root=sentinel.ignored) self.usage_map = self.generate_usage_map(20) def generate_segment(self, num_users, num_views): """Constructs an entry for a video segment.""" return { 'users': ['user{}'.format(i) for i in range(num_users)], 'views': num_views } def generate_usage_map(self, num_segments): """Constructs a default usage_map, with constant users and views.""" usage_map = {} for i in range(num_segments): usage_map[i] = self.generate_segment(20, 20) return usage_map @data( (15, 25, 14), (20, 30, 19), ) @unpack def test_with_sudden_dropoff(self, start, end, result): """Sharp drop-off in num_users(20 to 1), triggers the cutoff""" for i in range(start, end): self.usage_map[i] = self.generate_segment(1, 1) self.assertEqual(self.task.get_final_segment(self.usage_map), result) def test_without_dropoff(self): """No dropoff""" self.assertEqual(self.task.get_final_segment(self.usage_map), 19) def test_with_gradual_dropoff(self): """Gradual dropoff of num_users, does not trigger the cutoff""" for i, j in zip(range(20, 39), range(19, 0, -1)): self.usage_map[i] = self.generate_segment(j, j) self.assertEqual(self.task.get_final_segment(self.usage_map), 38) def test_with_dropoff_no_cutoff(self): """Drop-off from 20 to 2 num_users, however, cutoff won't trigger as it does not meet the threshold.""" self.usage_map[30] = self.generate_segment(2, 2) self.assertEqual(self.task.get_final_segment(self.usage_map), 30)
class CompleteEndSegmentTest(unittest.TestCase): """Test video task's complete_end_segment calculation.""" def setUp(self): self.task = VideoUsageTask(interval=sentinel.ignored, output_root=sentinel.ignored) @data( (10, 1), # segment calculated from duration * 0.95 (100, 19), # segment calculated from duration * 0.95 (650, 124), # segment calculated from duration - 30 ) @unpack def test_complete_end_segment(self, duration, expected_segment): self.assertEqual(self.task.complete_end_segment(duration), expected_segment)
def setUp(self): self.task = VideoUsageTask(interval=sentinel.ignored, output_root=sentinel.ignored) self.usage_map = self.generate_usage_map(20)
def setUp(self): self.task = VideoUsageTask(interval=sentinel.ignored, output_root=sentinel.ignored)