Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_utils.py 9.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. def test_to_json(self):
  36. source = utils.Source(self.source, self.path, self.provider)
  37. self.assertEqual(source.to_json(), {"source": self.source, "path": self.path, "provider": self.provider})
  38. # Sad path tests
  39. def test_init_no_source(self):
  40. with self.assertRaises(ValueError):
  41. utils.Source("", self.path, self.provider)
  42. def test_init_no_path(self):
  43. with self.assertRaises(ValueError):
  44. utils.Source(self.source, "", self.provider)
  45. def test_init_no_provider(self):
  46. with self.assertRaises(ValueError):
  47. utils.Source(self.source, self.path, "")
  48. def test_duration_no_file(self):
  49. """Test that duration raises FileNotFoundError if the file does not exist"""
  50. source = utils.Source(self.source, "fakepath-noexist!😇", self.provider)
  51. # if that file actually exists I'll eat my hat
  52. with self.assertRaises(FileNotFoundError):
  53. source.duration()
  54. class TestSourceMedia(unittest.TestCase):
  55. """SourceMedia is a container for Source objects"""
  56. class FakeSource():
  57. """Fake source object for testing SourceMedia
  58. Since SourceMedia doesn't actually access any of the attributes of Source objects,
  59. we can use a very empty class for testing.
  60. """
  61. def setUp(self):
  62. self.source1 = self.FakeSource()
  63. self.source2 = self.FakeSource()
  64. self.source3 = self.FakeSource()
  65. self.sources = [self.source1, self.source2, self.source3]
  66. def test_init(self):
  67. source_media = utils.SourceMedia(sources=self.sources)
  68. self.assertEqual(source_media.sources, self.sources)
  69. def test_iter(self):
  70. source_media = utils.SourceMedia(sources=self.sources)
  71. self.assertEqual(list(source_media), self.sources)
  72. class TestInterval(unittest.TestCase):
  73. """Interval is a container for start, end and duration attributes"""
  74. def setUp(self):
  75. self.start = 0
  76. self.end = 10
  77. self.duration = 10
  78. # Happy path tests
  79. def test_init_start_end(self):
  80. """Create an Interval using start and end attributes"""
  81. interval = utils.Interval(start=self.start, end=self.end)
  82. self.assertEqual(interval.start, self.start)
  83. self.assertEqual(interval.end, self.end)
  84. self.assertEqual(interval.duration, self.duration)
  85. def test_init_start_duration(self):
  86. """Create an Interval using start and duration attributes"""
  87. interval = utils.Interval(start=self.start, duration=self.duration)
  88. self.assertEqual(interval.start, self.start)
  89. self.assertEqual(interval.end, self.end)
  90. self.assertEqual(interval.duration, self.duration)
  91. def test_init_end_duration(self):
  92. """Create an Interval using end and duration attributes"""
  93. interval = utils.Interval(end=self.end, duration=self.duration)
  94. self.assertEqual(interval.start, self.start)
  95. self.assertEqual(interval.end, self.end)
  96. self.assertEqual(interval.duration, self.duration)
  97. def test_from_start_classmethod(self):
  98. """Create an Interval using the from_start classmethod (ie start attribute only- uses default duration)"""
  99. interval = utils.Interval.from_start(start=self.start)
  100. self.assertEqual(interval.start, self.start)
  101. self.assertEqual(interval.end, interval.start + utils.Interval.DEFAULT_DURATION)
  102. self.assertEqual(interval.duration, utils.Interval.DEFAULT_DURATION)
  103. def test_from_end_classmethod(self):
  104. """Create an Interval using the from_end classmethod (ie end attribute only- uses default duration)"""
  105. interval = utils.Interval.from_end(end=self.end)
  106. self.assertEqual(interval.start, interval.end - utils.Interval.DEFAULT_DURATION)
  107. self.assertEqual(interval.end, self.end)
  108. self.assertEqual(interval.duration, utils.Interval.DEFAULT_DURATION)
  109. def test_repr(self):
  110. interval = utils.Interval(start=self.start, end=self.end)
  111. self.assertEqual(repr(interval), f"Interval({self.start}, {self.end}, {self.duration})")
  112. def test_lt(self):
  113. """Test the __lt__ method used for sorting Interval objects based on start time
  114. If the start times are equal, the interval with the smaller end time is considered smaller
  115. """
  116. interval1 = utils.Interval(start=0, end=10)
  117. interval2 = utils.Interval(start=0, end=15)
  118. interval3 = utils.Interval(start=5, end=10)
  119. interval4 = utils.Interval(start=5, end=15)
  120. self.assertTrue(interval1 < interval2) # same start, interval1 has smaller end
  121. self.assertTrue(interval1 < interval3) # interval1 start is smaller
  122. self.assertTrue(interval3 > interval2) # interval3 start is larger
  123. self.assertTrue(interval3 < interval4) # same start, interval3 has smaller end
  124. def test_to_json(self):
  125. """Test to_json method"""
  126. interval = utils.Interval(start=self.start, end=self.end)
  127. self.assertEqual(interval.to_json(), {"start": self.start, "end": self.end, "duration": self.duration})
  128. def test_move_start(self):
  129. """Test the move_start method - changes start time to time specified, keeps end time constant"""
  130. interval = utils.Interval(start=self.start, end=self.end)
  131. interval.move_start(5)
  132. self.assertEqual(interval.start, 5)
  133. self.assertEqual(interval.end, 10)
  134. self.assertEqual(interval.duration, 5)
  135. def test_move_start_relative(self):
  136. """Test the move_start method with relative=True - changes start time by a relative amount"""
  137. interval = utils.Interval(start=self.start, end=self.end)
  138. interval.move_start(2, relative=True)
  139. self.assertEqual(interval.start, 2)
  140. self.assertEqual(interval.end, 10)
  141. self.assertEqual(interval.duration, 8)
  142. def test_move_end(self):
  143. """Test the move_end method - changes end time to time specified, keeps start time constant"""
  144. interval = utils.Interval(start=self.start, end=self.end)
  145. interval.move_end(15)
  146. self.assertEqual(interval.start, 0)
  147. self.assertEqual(interval.end, 15)
  148. self.assertEqual(interval.duration, 15)
  149. def test_update_duration(self):
  150. """Test the update_duration method - changes duration to time specified, keeps start time constant"""
  151. interval = utils.Interval(start=self.start, end=self.end)
  152. interval.update_duration(15)
  153. self.assertEqual(interval.start, 0)
  154. self.assertEqual(interval.end, 15)
  155. self.assertEqual(interval.duration, 15)
  156. # test with relative=True
  157. interval.update_duration(5, relative=True)
  158. self.assertEqual(interval.start, 0)
  159. # Unhappy path tests
  160. def test_init_no_start_end(self):
  161. with self.assertRaises(ValueError):
  162. utils.Interval()
  163. def test_init_start_end_duration(self):
  164. with self.assertRaises(ValueError):
  165. utils.Interval(start=self.start, end=self.end, duration=self.duration)
  166. def test_init_start_after_end(self):
  167. with self.assertRaises(ValueError):
  168. utils.Interval(start=10, end=0)
  169. def test_init_negative_duration(self):
  170. with self.assertRaises(ValueError):
  171. utils.Interval(start=0, duration=-10)
  172. with self.assertRaises(ValueError):
  173. utils.Interval(end=0, duration=-10)
  174. if __name__ == "__main__":
  175. unittest.main()