Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

112 rindas
4.4 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. class TestJSONProducer(unittest.TestCase):
  71. def test_init_empty(self):
  72. # test with no features -- should raise ValueError
  73. with self.assertRaises(ValueError):
  74. producers.JSONProducer(features=None)
  75. def test_init(self):
  76. # test with features
  77. feature = mocks.MockFeature(interval="")
  78. features = [feature]
  79. jsonprod = producers.JSONProducer(features=features)
  80. self.assertEqual(jsonprod.features, features)
  81. def test_produce(self):
  82. """Test we can call produce without error"""
  83. feature = mocks.MockFeature(interval="")
  84. features = [feature]
  85. jsonprod = producers.JSONProducer(features=features)
  86. jsonprod.produce()
  87. class TestPipelineJSONEncoder(unittest.TestCase):
  88. """Test PipelineJSONEncoder"""
  89. # unhappy path
  90. def test_default_unserializable(self):
  91. pje = producers.PipelineJSONEncoder()
  92. with self.assertRaises(TypeError):
  93. pje.default(object())