浏览代码

test: add TestJSONFE with mocks

main
Rob Hallam 2 个月前
父节点
当前提交
028adbc640
共有 1 个文件被更改,包括 45 次插入0 次删除
  1. +45
    -0
      test/test_feature_extractors.py

+ 45
- 0
test/test_feature_extractors.py 查看文件

@@ -5,6 +5,8 @@ import pytest
import pipeline.feature_extractors as extractors

from pipeline.utils import Source, SourceMedia # technically makes this an integration test, but...
from unittest.mock import patch, mock_open

class TestSource():
"""Provide utils.Source for testing"""
def one_colour_silent_audio(self):
@@ -19,6 +21,19 @@ class TestSourceMedia():
"""Provide a source with a silent mono-colour video"""
return SourceMedia(sources=[TestSource().one_colour_silent_audio()])

class MockReadJSON():
"""Mock read_json"""
def mock_read_json_from_file(self, *args, **kwargs):
"""Mock _read_json_from_file()"""
rJSON = [{"interval": {"start": 0.0, "duration": 1.0},
"source": {"source": "test_video_red_silentaudio.mp4",
"path": "test_video_red_silentaudio.mp4",
"provider": "mock"},
"feature_extractor": "MockFeatureExtractor",
"score": 0.5
}]
return rJSON

class TestLaughterFeatureExtractor(unittest.TestCase):
def _mock_laughdetect_callout(self, *args, **kwargs):
"""Mock _laughdetect callout
@@ -183,3 +198,33 @@ class TestVideoActivityFeatureExtractor(unittest.TestCase):

# TODO: add sample video with activity to test _activitydetect()

class TestJSONFeatureExtractor(unittest.TestCase):
"""Test JSONFeatureExtractor"""
def test_init(self):
video_source = TestSourceMedia().one_colour_silent_audio()
test_extractor = extractors.JSONFeatureExtractor(input_files=video_source)
self.assertTrue(test_extractor)

def test_init_noinput(self):
"""test init - no input files"""
with self.assertRaises(ValueError):
test_extractor = extractors.JSONFeatureExtractor()

def test_extract(self):
"""Test extract with basic input file runs with no errors"""
video_source = TestSourceMedia().one_colour_silent_audio()
test_extractor = extractors.JSONFeatureExtractor(input_files=video_source)
# mock _read_json_from_file
test_extractor._read_json_from_file = MockReadJSON().mock_read_json_from_file
test_extractor.setup()
test_extractor.run()
test_extractor.teardown()
self.assertTrue(test_extractor.features)

def test_read_json_from_file(self):
"""Test _read_json_from_file"""
video_source = TestSourceMedia().one_colour_silent_audio()
test_extractor = extractors.JSONFeatureExtractor(input_files=video_source)
m = unittest.mock.mock_open(read_data='[{"foo": "bar"}]')
with unittest.mock.patch("builtins.open", m):
test_extractor._read_json_from_file("foo.json")

正在加载...
取消
保存