Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

174 rader
7.0 KiB

  1. """Test cases for utils"""
  2. import unittest
  3. import pipeline.utils as utils
  4. class TestSource(unittest.TestCase):
  5. """Source is a container for source, path, and provider of a media file
  6. source -- the source of the media file (eg, a URL or a local path)
  7. path -- the path to the media file
  8. provider -- the provider of the media file (eg, "FileInputJSON")
  9. Accessing the object should return the path to the media file.
  10. Methods:
  11. duration() -- return the duration of the media file (uses ffprobe, result is cached)
  12. """
  13. # Happy path tests
  14. def setUp(self):
  15. self.source = "audio_clips/testclip-5min.aac"
  16. self.path = "audio_clips/testclip-5min.aac"
  17. self.provider = "FileInputJSON"
  18. self._duration = 306.121214 # duration of testclip-5min.aac
  19. def test_init(self):
  20. source = utils.Source(self.source, self.path, self.provider)
  21. self.assertEqual(source.source, self.source)
  22. self.assertEqual(source.path, self.path)
  23. self.assertEqual(source.provider, self.provider)
  24. def test_str(self):
  25. """Accessing the object should return the path to the media file"""
  26. source = utils.Source(self.source, self.path, self.provider)
  27. self.assertEqual(str(source), self.path)
  28. def test_repr(self):
  29. source = utils.Source(self.source, self.path, self.provider)
  30. self.assertEqual(repr(source), f"Source({self.source}, {self.path}, {self.provider})")
  31. def test_duration(self):
  32. """Use a mock duration of 306.121214 for the test clip"""
  33. source = utils.Source(self.source, self.path, self.provider)
  34. self.assertEqual(source.duration(), self._duration)
  35. # Sad path tests
  36. def test_init_no_source(self):
  37. with self.assertRaises(ValueError):
  38. utils.Source("", self.path, self.provider)
  39. def test_init_no_path(self):
  40. with self.assertRaises(ValueError):
  41. utils.Source(self.source, "", self.provider)
  42. def test_init_no_provider(self):
  43. with self.assertRaises(ValueError):
  44. utils.Source(self.source, self.path, "")
  45. def test_duration_no_file(self):
  46. """Test that duration raises FileNotFoundError if the file does not exist"""
  47. source = utils.Source(self.source, "fakepath-noexist!😇", self.provider)
  48. # if that file actually exists I'll eat my hat
  49. with self.assertRaises(FileNotFoundError):
  50. source.duration()
  51. class TestSourceMedia(unittest.TestCase):
  52. """SourceMedia is a container for Source objects"""
  53. class FakeSource():
  54. """Fake source object for testing SourceMedia
  55. Since SourceMedia doesn't actually access any of the attributes of Source objects,
  56. we can use a very empty class for testing.
  57. """
  58. def setUp(self):
  59. self.source1 = self.FakeSource()
  60. self.source2 = self.FakeSource()
  61. self.source3 = self.FakeSource()
  62. self.sources = [self.source1, self.source2, self.source3]
  63. def test_init(self):
  64. source_media = utils.SourceMedia(sources=self.sources)
  65. self.assertEqual(source_media.sources, self.sources)
  66. def test_iter(self):
  67. source_media = utils.SourceMedia(sources=self.sources)
  68. self.assertEqual(list(source_media), self.sources)
  69. class TestInterval(unittest.TestCase):
  70. """Interval is a container for start, end and duration attributes"""
  71. def setUp(self):
  72. self.start = 0
  73. self.end = 10
  74. self.duration = 10
  75. # Happy path tests
  76. def test_init_start_end(self):
  77. """Create an Interval using start and end attributes"""
  78. interval = utils.Interval(start=self.start, end=self.end)
  79. self.assertEqual(interval.start, self.start)
  80. self.assertEqual(interval.end, self.end)
  81. self.assertEqual(interval.duration, self.duration)
  82. def test_init_start_duration(self):
  83. """Create an Interval using start and duration attributes"""
  84. interval = utils.Interval(start=self.start, duration=self.duration)
  85. self.assertEqual(interval.start, self.start)
  86. self.assertEqual(interval.end, self.end)
  87. self.assertEqual(interval.duration, self.duration)
  88. def test_init_end_duration(self):
  89. """Create an Interval using end and duration attributes"""
  90. interval = utils.Interval(end=self.end, duration=self.duration)
  91. self.assertEqual(interval.start, self.start)
  92. self.assertEqual(interval.end, self.end)
  93. self.assertEqual(interval.duration, self.duration)
  94. def test_from_start_classmethod(self):
  95. """Create an Interval using the from_start classmethod (ie start attribute only- uses default duration)"""
  96. interval = utils.Interval.from_start(start=self.start)
  97. self.assertEqual(interval.start, self.start)
  98. self.assertEqual(interval.end, interval.start + utils.Interval.DEFAULT_DURATION)
  99. self.assertEqual(interval.duration, utils.Interval.DEFAULT_DURATION)
  100. def test_from_end_classmethod(self):
  101. """Create an Interval using the from_end classmethod (ie end attribute only- uses default duration)"""
  102. interval = utils.Interval.from_end(end=self.end)
  103. self.assertEqual(interval.start, interval.end - utils.Interval.DEFAULT_DURATION)
  104. self.assertEqual(interval.end, self.end)
  105. self.assertEqual(interval.duration, utils.Interval.DEFAULT_DURATION)
  106. def test_repr(self):
  107. interval = utils.Interval(start=self.start, end=self.end)
  108. self.assertEqual(repr(interval), f"Interval({self.start}, {self.end}, {self.duration})")
  109. def test_lt(self):
  110. """Test the __lt__ method used for sorting Interval objects based on start time
  111. If the start times are equal, the interval with the smaller end time is considered smaller
  112. """
  113. interval1 = utils.Interval(start=0, end=10)
  114. interval2 = utils.Interval(start=0, end=15)
  115. interval3 = utils.Interval(start=5, end=10)
  116. interval4 = utils.Interval(start=5, end=15)
  117. self.assertTrue(interval1 < interval2) # same start, interval1 has smaller end
  118. self.assertTrue(interval1 < interval3) # interval1 start is smaller
  119. self.assertTrue(interval3 > interval2) # interval3 start is larger
  120. self.assertTrue(interval3 < interval4) # same start, interval3 has smaller end
  121. def test_move_start(self):
  122. """Test the move_start method - changes start time to time specified, keeps end time constant"""
  123. interval = utils.Interval(start=self.start, end=self.end)
  124. interval.move_start(5)
  125. self.assertEqual(interval.start, 5)
  126. self.assertEqual(interval.end, 10)
  127. self.assertEqual(interval.duration, 5)
  128. def test_move_start_relative(self):
  129. """Test the move_start method with relative=True - changes start time by a relative amount"""
  130. interval = utils.Interval(start=self.start, end=self.end)
  131. interval.move_start(2, relative=True)
  132. self.assertEqual(interval.start, 2)
  133. self.assertEqual(interval.end, 10)
  134. self.assertEqual(interval.duration, 8)
  135. if __name__ == "__main__":
  136. unittest.main()