You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
5.0 KiB

  1. """test_producers.py -- test the producers in the pipeline (eg ffmpeg, visualisation, json)"""
  2. import unittest
  3. import pipeline.producers as producers
  4. import test.mocks as mocks
  5. class TestFfmpegVideoProducer(unittest.TestCase):
  6. """Test FfmpegVideoProducer (produces videos using ffmpeg)"""
  7. def mock_run_no_output(self, cmd, cwd="."):
  8. """Mock out _run_no_output"""
  9. return None
  10. def test_init_empty(self):
  11. # test with no features -- should raise ValueError
  12. with self.assertRaises(ValueError):
  13. producers.FfmpegVideoProducer(features=None)
  14. def test_init(self):
  15. # test with features
  16. feature = mocks.MockFeature(interval="")
  17. features = [feature]
  18. vidprod = producers.FfmpegVideoProducer(features=features)
  19. self.assertEqual(vidprod.features, features)
  20. def test_ffmpeg_feature_to_clip(self):
  21. """Test without a feature and without output path -- unhappy path"""
  22. feature = mocks.MockFeature(interval=mocks.MockInterval(start=0, end=5),
  23. source=mocks.MockSource(path="test.mp4"))
  24. features = [feature]
  25. vidprod = producers.FfmpegVideoProducer(features=features)
  26. with self.assertRaises(ValueError):
  27. vidprod._ffmpeg_feature_to_clip(feature=None)
  28. with self.assertRaises(ValueError):
  29. vidprod._ffmpeg_feature_to_clip(feature=feature, output_filepath=None)
  30. def test_ffmpeg_concat_clips(self):
  31. """Test without clips and without output path -- unhappy path"""
  32. feature = mocks.MockFeature(interval="")
  33. features = [feature]
  34. vidprod = producers.FfmpegVideoProducer(features=features)
  35. with self.assertRaises(ValueError):
  36. vidprod._ffmpeg_concat_clips(clips=None)
  37. with self.assertRaises(ValueError):
  38. vidprod._ffmpeg_concat_clips(clips=features, output_filepath=None)
  39. # happy path
  40. def test_produce(self):
  41. """Test we can call produce without error"""
  42. feature = mocks.MockFeature(interval=mocks.MockInterval(start=0, end=5),
  43. source=mocks.MockSource(path="test.mp4"))
  44. features = [feature]
  45. vidprod = producers.FfmpegVideoProducer(features=features)
  46. vidprod.produce()
  47. class TestVisualisationProducer(unittest.TestCase):
  48. """Test VisualisationProducer (produces matplotlib visualisations)"""
  49. def test_init_empty(self):
  50. # test with no features -- should raise ValueError
  51. with self.assertRaises(ValueError):
  52. producers.VisualisationProducer(features=None)
  53. def test_init(self):
  54. # test with features
  55. feature = mocks.MockFeature(interval="")
  56. features = [feature]
  57. visprod = producers.VisualisationProducer(features=features)
  58. self.assertEqual(visprod.features, features)
  59. def test_produce(self):
  60. """Test we can call produce without error"""
  61. # to do this, we need to mock out plt.savefig(path)
  62. feature = mocks.MockFeature(interval=mocks.MockInterval(start=0, end=5),
  63. source=mocks.MockSource(path="test.mp4"))
  64. features = [feature]
  65. visprod = producers.VisualisationProducer(features=features)
  66. with unittest.mock.patch("matplotlib.pyplot.savefig") as mock_savefig:
  67. visprod.produce()
  68. mock_savefig.assert_called()
  69. # TODO: more more granular test coverage of produce behaviour
  70. def test_fe_colour(self):
  71. """test that colour picker returns the right colours#
  72. laughter=red, loudness=blue, video activity=green, words=yellow
  73. """
  74. # this is a pretty poor test really
  75. expected = {"laughter": "red", "loudness": "blue", "videoactivity": "green", "words": "purple"}
  76. feature = mocks.MockFeature(interval="")
  77. features = [feature]
  78. visprod = producers.VisualisationProducer(features=features)
  79. for given, expected in expected.items():
  80. feature.feature_extractor = given
  81. self.assertEqual(visprod._fe_colour(feature), expected)
  82. class TestJSONProducer(unittest.TestCase):
  83. def test_init_empty(self):
  84. # test with no features -- should raise ValueError
  85. with self.assertRaises(ValueError):
  86. producers.JSONProducer(features=None)
  87. def test_init(self):
  88. # test with features
  89. feature = mocks.MockFeature(interval="")
  90. features = [feature]
  91. jsonprod = producers.JSONProducer(features=features)
  92. self.assertEqual(jsonprod.features, features)
  93. def test_produce(self):
  94. """Test we can call produce without error"""
  95. feature = mocks.MockFeature(interval="")
  96. features = [feature]
  97. jsonprod = producers.JSONProducer(features=features)
  98. jsonprod.produce()
  99. class TestPipelineJSONEncoder(unittest.TestCase):
  100. """Test PipelineJSONEncoder"""
  101. # unhappy path
  102. def test_default_unserializable(self):
  103. pje = producers.PipelineJSONEncoder()
  104. with self.assertRaises(TypeError):
  105. pje.default(object())