您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

77 行
2.6 KiB

  1. """test_adjusters.py -- test pipeline Adjusters (eg TargetTimeAdjuster)"""
  2. import unittest
  3. import unittest.mock as mock
  4. import pipeline.adjusters as adjusters
  5. class TestAdjuster(unittest.TestCase):
  6. """Test the generic Adjuster class"""
  7. def test_init(self):
  8. """Test the Adjuster can be initialised"""
  9. adjuster = adjusters.Adjuster()
  10. self.assertEqual(adjuster.features, [])
  11. def test_adjust(self):
  12. """Test the generic adjust"""
  13. adjuster = adjusters.Adjuster()
  14. self.assertEqual(adjuster.adjust(), [])
  15. self.assertEqual(adjuster.features, [])
  16. class TestTargetTimeAdjuster(unittest.TestCase):
  17. """Test the TargetTimeAdjuster
  18. TTA drops Features until the target time is reached (or within a margin)"""
  19. def test_init(self):
  20. """Test the TTA can be initialised"""
  21. tta = adjusters.TargetTimeAdjuster()
  22. self.assertEqual(tta.features, [])
  23. def test_features_total_time(self):
  24. """Test the TTA can calculate the total time of Features
  25. Test:
  26. - input duration floats: 1.0, 2.0, 3.0, 4.0 == 10.0
  27. """
  28. tta = adjusters.TargetTimeAdjuster()
  29. features = []
  30. for i in range(1, 5):
  31. features.append(mock.Mock(duration=i*1.0))
  32. self.assertEqual(tta._features_total_time(features), 10.0)
  33. self.assertEqual(tta._features_total_time([]), 0.0)
  34. self.assertIs(type(tta._features_total_time([])), float)
  35. def test_determine_margin(self):
  36. """Test the TTA can determine the target time margins
  37. Args: time, margin, strategy (strategy in: ABSOLUTE, PERCENT)
  38. Test:
  39. - margin of zero
  40. - margin of 5.0
  41. - margin of 10.0
  42. - margin of 100.0
  43. - both ABSOLUTE and PERCENT strategies
  44. TODO: figure out what should be done with negative margins & margins > 100.0
  45. """
  46. tta = adjusters.TargetTimeAdjuster()
  47. with self.subTest("ABSOLUTE"):
  48. strategy = adjusters.TargetTimeAdjuster._STRATEGY.ABSOLUTE
  49. test_cases = []
  50. # populate test cases with tuples of (time, margin, expected)
  51. # zero margin
  52. test_cases.append((60.0, 0.0, (60.0, 60.0)))
  53. # margin of 5.0
  54. test_cases.append((60.0, 5.0, (55.0, 65.0)))
  55. # margin of 10.0
  56. test_cases.append((60.0, 10.0, (50.0, 70.0)))
  57. # margin of 100.0
  58. test_cases.append((60.0, 100.0, (0.0, 160.0)))
  59. # test
  60. for time, margin, expected in test_cases:
  61. self.assertEqual(tta._determine_margin(time, margin, strategy), expected)