|
| 1 | +"""Unit tests for AsyncClient.submit_job, focusing on the parallel_engines feature.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from io import BytesIO |
| 5 | +from unittest.mock import AsyncMock |
| 6 | +from unittest.mock import MagicMock |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from speechmatics.batch import AsyncClient |
| 14 | +from speechmatics.batch import JobConfig |
| 15 | +from speechmatics.batch import JobStatus |
| 16 | +from speechmatics.batch import JobType |
| 17 | +from speechmatics.batch import TranscriptionConfig |
| 18 | +from speechmatics.batch import PROCESSING_DATA_HEADER |
| 19 | + |
| 20 | + |
| 21 | +def _make_client(api_key: str = "test-key") -> AsyncClient: |
| 22 | + return AsyncClient(api_key=api_key) |
| 23 | + |
| 24 | + |
| 25 | +def _job_response(job_id: str = "job-123") -> dict: |
| 26 | + return {"id": job_id, "created_at": "2024-01-01T00:00:00Z"} |
| 27 | + |
| 28 | + |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +# Helpers |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | + |
| 33 | + |
| 34 | +def _captured_extra_headers(mock_post: AsyncMock) -> Optional[dict]: |
| 35 | + """Return the extra_headers kwarg from the first call to transport.post.""" |
| 36 | + _, kwargs = mock_post.call_args |
| 37 | + return kwargs.get("extra_headers") |
| 38 | + |
| 39 | + |
| 40 | +# --------------------------------------------------------------------------- |
| 41 | +# Tests |
| 42 | +# --------------------------------------------------------------------------- |
| 43 | + |
| 44 | + |
| 45 | +class TestRequestedParallelHeader: |
| 46 | + """X-SM-Processing-Data header is set correctly based on parallel_engines.""" |
| 47 | + |
| 48 | + @pytest.mark.asyncio |
| 49 | + async def test_header_sent_when_parallel_engines_provided(self): |
| 50 | + client = _make_client() |
| 51 | + audio = BytesIO(b"fake-audio") |
| 52 | + |
| 53 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 54 | + mock_post.return_value = _job_response() |
| 55 | + await client.submit_job(audio, parallel_engines=4) |
| 56 | + |
| 57 | + extra_headers = _captured_extra_headers(mock_post) |
| 58 | + assert extra_headers is not None |
| 59 | + assert PROCESSING_DATA_HEADER in extra_headers |
| 60 | + payload = extra_headers[PROCESSING_DATA_HEADER] |
| 61 | + assert payload == {"parallel_engines": 4} |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_header_not_sent_when_parallel_engines_is_none(self): |
| 65 | + client = _make_client() |
| 66 | + audio = BytesIO(b"fake-audio") |
| 67 | + |
| 68 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 69 | + mock_post.return_value = _job_response() |
| 70 | + await client.submit_job(audio) |
| 71 | + |
| 72 | + extra_headers = _captured_extra_headers(mock_post) |
| 73 | + assert extra_headers is None |
| 74 | + |
| 75 | + @pytest.mark.asyncio |
| 76 | + async def test_header_value_is_valid_json(self): |
| 77 | + client = _make_client() |
| 78 | + audio = BytesIO(b"fake-audio") |
| 79 | + |
| 80 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 81 | + mock_post.return_value = _job_response() |
| 82 | + await client.submit_job(audio, parallel_engines=8) |
| 83 | + |
| 84 | + extra_headers = _captured_extra_headers(mock_post) |
| 85 | + # Must be parseable JSON |
| 86 | + assert extra_headers is not None |
| 87 | + parsed = extra_headers[PROCESSING_DATA_HEADER] |
| 88 | + assert parsed["parallel_engines"] == 8 |
| 89 | + |
| 90 | + @pytest.mark.asyncio |
| 91 | + async def test_parallel_engines_one(self): |
| 92 | + client = _make_client() |
| 93 | + audio = BytesIO(b"fake-audio") |
| 94 | + |
| 95 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 96 | + mock_post.return_value = _job_response() |
| 97 | + await client.submit_job(audio, parallel_engines=1) |
| 98 | + |
| 99 | + extra_headers = _captured_extra_headers(mock_post) |
| 100 | + assert extra_headers is not None |
| 101 | + payload = extra_headers[PROCESSING_DATA_HEADER] |
| 102 | + assert payload["parallel_engines"] == 1 |
| 103 | + |
| 104 | + @pytest.mark.asyncio |
| 105 | + async def test_header_sent_with_fetch_data_config(self): |
| 106 | + """parallel_engines works with fetch_data submissions too.""" |
| 107 | + client = _make_client() |
| 108 | + config = JobConfig( |
| 109 | + type=JobType.TRANSCRIPTION, |
| 110 | + fetch_data=MagicMock(url="https://example.com/audio.wav"), |
| 111 | + transcription_config=TranscriptionConfig(language="en"), |
| 112 | + ) |
| 113 | + # Patch to_dict so fetch_data key is present |
| 114 | + config_dict = { |
| 115 | + "type": "transcription", |
| 116 | + "fetch_data": {"url": "https://example.com/audio.wav"}, |
| 117 | + "transcription_config": {"language": "en"}, |
| 118 | + } |
| 119 | + with patch.object(config, "to_dict", return_value=config_dict): |
| 120 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 121 | + mock_post.return_value = _job_response() |
| 122 | + await client.submit_job(None, config=config, parallel_engines=2) |
| 123 | + |
| 124 | + extra_headers = _captured_extra_headers(mock_post) |
| 125 | + assert extra_headers is not None |
| 126 | + payload = extra_headers[PROCESSING_DATA_HEADER] |
| 127 | + assert payload == {"parallel_engines": 2} |
| 128 | + |
| 129 | + |
| 130 | +class TestSubmitJobReturnValue: |
| 131 | + """submit_job still returns the correct JobDetails regardless of parallel_engines.""" |
| 132 | + |
| 133 | + @pytest.mark.asyncio |
| 134 | + async def test_returns_job_details_with_correct_id(self): |
| 135 | + client = _make_client() |
| 136 | + audio = BytesIO(b"fake-audio") |
| 137 | + |
| 138 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 139 | + mock_post.return_value = _job_response("abc-456") |
| 140 | + job = await client.submit_job(audio, parallel_engines=3) |
| 141 | + |
| 142 | + assert job.id == "abc-456" |
| 143 | + assert job.status == JobStatus.RUNNING |
| 144 | + |
| 145 | + @pytest.mark.asyncio |
| 146 | + async def test_post_called_with_jobs_path(self): |
| 147 | + client = _make_client() |
| 148 | + audio = BytesIO(b"fake-audio") |
| 149 | + |
| 150 | + with patch.object(client._transport, "post", new_callable=AsyncMock) as mock_post: |
| 151 | + mock_post.return_value = _job_response() |
| 152 | + await client.submit_job(audio, parallel_engines=2) |
| 153 | + |
| 154 | + args, _ = mock_post.call_args |
| 155 | + assert args[0] == "/jobs" |
0 commit comments