From bee133d53ff1e035eefbc56ecdb52767c6638202 Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Sun, 18 Aug 2024 00:56:02 +0100 Subject: [PATCH] test: Add mocked test calls to TestLFE --- test/test_feature_extractors.py | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/test/test_feature_extractors.py b/test/test_feature_extractors.py index 4a4f0ce..737077c 100644 --- a/test/test_feature_extractors.py +++ b/test/test_feature_extractors.py @@ -20,6 +20,42 @@ class TestSourceMedia(): return SourceMedia(sources=[TestSource().one_colour_silent_audio()]) class TestLaughterFeatureExtractor(unittest.TestCase): + def _mock_laughdetect_callout(self, *args, **kwargs): + """Mock _laughdetect callout + + **kwargs: + - n : int >=0, number of laughter instances to generate + Return a list of 2-tuple floats (start, end) representing laughter instances + """ + laughs = [] + n = kwargs.get("n", 0) + for i in range(n): + laughs.append((i, i+1)) + + return laughs + + def _mock_run_get_output(self, *args, **kwargs) -> str: + """Mock run_get_output callout + + kwargs: + - n : int >=0, number of laughter instances to generate + + Return a string of laughter instance of the form: + instance: (1.234, 5.678) + """ + # TODO: decide if we want non-"instance" output for testing parsing? + # (maybe) + output = [] + n = kwargs.get("n", 0) + for i in range(n): + output.append(f"instance: ({i}.{i+1}{i+2}{i+3}, {i+4}.{i+5}{i+6}{i+7})") + return "\n".join(output) + + def _sgo5(self, *args, **kwargs): + """Mock run_get_output callout""" + return self._mock_run_get_output(*args, **kwargs, n=5) + + """Test LaughterFeatureExtractor""" def test_init(self): test_extractor = extractors.LaughterFeatureExtractor() @@ -32,6 +68,48 @@ class TestLaughterFeatureExtractor(unittest.TestCase): test_extractor.setup() # NB test WITH sources implicitly tested in test_extract + @pytest.mark.slow + def test_extract_mocked_nolaughs(self): + """Test extract with mocked laughter detection - no laughs""" + video_source = TestSource().one_colour_silent_audio() + test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source]) + test_extractor._laughdetect = self._mock_laughdetect_callout + test_extractor.setup() + test_extractor.run() + test_extractor.teardown() + self.assertEqual(len(test_extractor.features), 0) + + def test_extract_mocked_run_get_output_none(self): + """Test extract with mocked laughter detection - no laughs""" + video_source = TestSource().one_colour_silent_audio() + test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source]) + test_extractor._run_get_output = self._mock_run_get_output + test_extractor.setup() + test_extractor.run() + test_extractor.teardown() + self.assertEqual(len(test_extractor.features), 0) + + def test_extract_mocked_run_get_output_5(self): + """Test extract with mocked laughter detection - 5 laughs""" + video_source = TestSource().one_colour_silent_audio() + test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source]) + test_extractor._run_get_output = self._sgo5 + test_extractor.setup() + test_extractor.run() + test_extractor.teardown() + self.assertEqual(len(test_extractor.features), 5) + + def test_run_get_output(self): + """Test run_get_output""" + video_source = TestSource().one_colour_silent_audio() + test_extractor = extractors.LaughterFeatureExtractor(input_files=[video_source]) + test_cmd = ["echo", "foo"] + test_extractor.setup() + output = test_extractor._run_get_output(test_cmd) + self.assertEqual(output, "foo\n") + + # TODO: add sample video with laughs to test _laughdetect() + class TestRandomFeatureExtractor(unittest.TestCase): """Test RandomFeatureExtractor""" def test_init(self):