|
| 1 | +# pyright: reportPrivateUsage=false |
| 2 | + |
| 3 | +"""Tests for partition_audio (speech-to-text in multimodal pipeline).""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import tempfile |
| 8 | +from pathlib import Path |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from unstructured.documents.elements import NarrativeText |
| 14 | +from unstructured.file_utils.model import FileType |
| 15 | +from unstructured.partition.audio import partition_audio |
| 16 | + |
| 17 | + |
| 18 | +def test_partition_audio_raises_with_neither_filename_nor_file(): |
| 19 | + with pytest.raises(ValueError, match="Exactly one of .* must be specified"): |
| 20 | + partition_audio() |
| 21 | + |
| 22 | + |
| 23 | +def test_partition_audio_raises_with_both_filename_and_file(): |
| 24 | + with pytest.raises(ValueError, match="Exactly one of .* must be specified"): |
| 25 | + with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: |
| 26 | + partition_audio(filename=tmp.name, file=tmp) |
| 27 | + |
| 28 | + |
| 29 | +@patch( |
| 30 | + "unstructured.partition.audio.SpeechToTextAgent.get_instance", |
| 31 | +) |
| 32 | +def test_partition_audio_from_filename_returns_transcript_elements(mock_get_instance): |
| 33 | + mock_agent = mock_get_instance.return_value |
| 34 | + mock_agent.transcribe.return_value = "Hello, this is a test transcript." |
| 35 | + |
| 36 | + with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: |
| 37 | + path = tmp.name |
| 38 | + tmp.write(b"\x00" * 44) # minimal WAV-like header |
| 39 | + tmp.flush() |
| 40 | + |
| 41 | + try: |
| 42 | + elements = partition_audio(filename=path) |
| 43 | + finally: |
| 44 | + Path(path).unlink(missing_ok=True) |
| 45 | + |
| 46 | + assert len(elements) == 1 |
| 47 | + assert isinstance(elements[0], NarrativeText) |
| 48 | + assert elements[0].text == "Hello, this is a test transcript." |
| 49 | + assert elements[0].metadata.detection_origin == "speech_to_text" |
| 50 | + mock_agent.transcribe.assert_called_once_with(path, language=None) |
| 51 | + |
| 52 | + |
| 53 | +@patch( |
| 54 | + "unstructured.partition.audio.SpeechToTextAgent.get_instance", |
| 55 | +) |
| 56 | +def test_partition_audio_from_file_uses_temp_path_and_cleans_up(mock_get_instance): |
| 57 | + mock_agent = mock_get_instance.return_value |
| 58 | + mock_agent.transcribe.return_value = "From file object." |
| 59 | + |
| 60 | + with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: |
| 61 | + tmp.write(b"\x00" * 44) |
| 62 | + tmp.flush() |
| 63 | + tmp.seek(0) |
| 64 | + elements = partition_audio(file=tmp, metadata_filename="recording.wav") |
| 65 | + |
| 66 | + assert len(elements) == 1 |
| 67 | + assert elements[0].text == "From file object." |
| 68 | + assert elements[0].metadata.filename == "recording.wav" |
| 69 | + |
| 70 | + |
| 71 | +@patch( |
| 72 | + "unstructured.partition.audio.SpeechToTextAgent.get_instance", |
| 73 | +) |
| 74 | +def test_partition_audio_empty_transcript_returns_empty_list(mock_get_instance): |
| 75 | + mock_agent = mock_get_instance.return_value |
| 76 | + mock_agent.transcribe.return_value = " " |
| 77 | + |
| 78 | + with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: |
| 79 | + path = tmp.name |
| 80 | + tmp.write(b"\x00" * 44) |
| 81 | + tmp.flush() |
| 82 | + |
| 83 | + try: |
| 84 | + elements = partition_audio(filename=path) |
| 85 | + finally: |
| 86 | + Path(path).unlink(missing_ok=True) |
| 87 | + |
| 88 | + assert elements == [] |
| 89 | + |
| 90 | + |
| 91 | +def test_wav_file_type_is_partitionable(): |
| 92 | + assert FileType.WAV.is_partitionable |
| 93 | + assert FileType.WAV.partitioner_shortname == "audio" |
| 94 | + assert FileType.WAV.partitioner_function_name == "partition_audio" |
0 commit comments