def test_TokenizerWorker(self): with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "file.log") logger = make_logger(file=file, name="test_TokenizerWorker") tokenizer = TokenizerWorker( self.reader, logger=logger, min_dur=0.3, max_dur=2, max_silence=0.2, drop_trailing_silence=False, strict_min_dur=False, eth=50, ) tokenizer.start_all() tokenizer.join() # Get logged text with open(file) as fp: log_lines = fp.readlines() log_fmt = "[DET]: Detection {} (start: {:.3f}, " log_fmt += "end: {:.3f}, duration: {:.3f})" self.assertEqual(len(tokenizer.detections), len(self.expected)) for i, (det, exp, log_line) in enumerate( zip(tokenizer.detections, self.expected, log_lines), 1): start, end = exp exp_log_line = log_fmt.format(i, start, end, end - start) self.assertAlmostEqual(det.start, start) self.assertAlmostEqual(det.end, end) # remove timestamp part and strip new line self.assertEqual(log_line[28:].strip(), exp_log_line)
def test_RegionSaverWorker(self): filename_format = ( "Region_{id}_{start:.6f}-{end:.3f}_{duration:.3f}.wav") with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "file.log") logger = make_logger(file=file, name="test_RegionSaverWorker") observers = [RegionSaverWorker(filename_format, logger=logger)] tokenizer = TokenizerWorker( self.reader, logger=logger, observers=observers, min_dur=0.3, max_dur=2, max_silence=0.2, drop_trailing_silence=False, strict_min_dur=False, eth=50, ) with patch("auditok.core.AudioRegion.save") as patched_save: tokenizer.start_all() tokenizer.join() tokenizer._observers[0].join() # Get logged text with open(file) as fp: log_lines = [ line for line in fp.readlines() if line.startswith("[SAVE]") ] # Assert RegionSaverWorker ran as expected expected_save_calls = [ call( filename_format.format(id=i, start=exp[0], end=exp[1], duration=exp[1] - exp[0]), None, ) for i, exp in enumerate(self.expected, 1) ] # Get calls to 'AudioRegion.save' mock_calls = [ c for i, c in enumerate(patched_save.mock_calls) if i % 2 == 0 ] self.assertEqual(mock_calls, expected_save_calls) self.assertEqual(len(tokenizer.detections), len(self.expected)) log_fmt = "[SAVE]: Detection {id} saved as '{filename}'" for i, (det, exp, log_line) in enumerate( zip(tokenizer.detections, self.expected, log_lines), 1): start, end = exp expected_filename = filename_format.format(id=i, start=start, end=end, duration=end - start) exp_log_line = log_fmt.format(i, expected_filename) self.assertAlmostEqual(det.start, start) self.assertAlmostEqual(det.end, end) # Remove timestamp part and strip new line self.assertEqual(log_line[28:].strip(), exp_log_line)
def test_make_logger_stderr_and_file(self): with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "file.log") logger = make_logger(stderr=True, file=file) self.assertEqual(logger.name, _AUDITOK_LOGGER) self.assertEqual(len(logger.handlers), 2) self.assertEqual(logger.handlers[0].stream.name, "<stderr>") self.assertEqual(logger.handlers[1].stream.name, file)
def test_CommandLineWorker(self): command_format = "do nothing with" with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "file.log") logger = make_logger(file=file, name="test_CommandLineWorker") observers = [CommandLineWorker(command_format, logger=logger)] tokenizer = TokenizerWorker( self.reader, logger=logger, observers=observers, min_dur=0.3, max_dur=2, max_silence=0.2, drop_trailing_silence=False, strict_min_dur=False, eth=50, ) with patch("auditok.workers.os.system") as patched_os_system: tokenizer.start_all() tokenizer.join() tokenizer._observers[0].join() # Get logged text with open(file) as fp: log_lines = [ line for line in fp.readlines() if line.startswith("[COMMAND]") ] # Assert CommandLineWorker ran as expected expected_save_calls = [call(command_format) for _ in self.expected] self.assertEqual(patched_os_system.mock_calls, expected_save_calls) self.assertEqual(len(tokenizer.detections), len(self.expected)) log_fmt = "[COMMAND]: Detection {id} command '{command}'" for i, (det, exp, log_line) in enumerate( zip(tokenizer.detections, self.expected, log_lines), 1 ): start, end = exp exp_log_line = log_fmt.format(i, command_format) self.assertAlmostEqual(det.start, start) self.assertAlmostEqual(det.end, end) # Remove timestamp part and strip new line self.assertEqual(log_line[28:].strip(), exp_log_line)
def test_PlayerWorker(self): with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "file.log") logger = make_logger(file=file, name="test_RegionSaverWorker") player_mock = Mock() observers = [PlayerWorker(player_mock, logger=logger)] tokenizer = TokenizerWorker( self.reader, logger=logger, observers=observers, min_dur=0.3, max_dur=2, max_silence=0.2, drop_trailing_silence=False, strict_min_dur=False, eth=50, ) tokenizer.start_all() tokenizer.join() tokenizer._observers[0].join() # Get logged text with open(file) as fp: log_lines = [ line for line in fp.readlines() if line.startswith("[PLAY]") ] self.assertTrue(player_mock.play.called) self.assertEqual(len(tokenizer.detections), len(self.expected)) log_fmt = "[PLAY]: Detection {id} played" for i, (det, exp, log_line) in enumerate( zip(tokenizer.detections, self.expected, log_lines), 1 ): start, end = exp exp_log_line = log_fmt.format(id=i) self.assertAlmostEqual(det.start, start) self.assertAlmostEqual(det.end, end) # Remove timestamp part and strip new line self.assertEqual(log_line[28:].strip(), exp_log_line)
def test_make_logger_None(self): logger = make_logger(stderr=False, file=None) self.assertIsNone(logger)