|
| 1 | +import json |
| 2 | +from typing import Any, Dict, Tuple |
| 3 | + |
| 4 | +import factory |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | +from pytest_httpx import HTTPXMock |
| 8 | + |
| 9 | +import assemblyai as aai |
| 10 | +from tests.unit import factories |
| 11 | + |
| 12 | +aai.settings.api_key = "test" |
| 13 | + |
| 14 | + |
| 15 | +class AutoChaptersResponseFactory(factories.TranscriptCompletedResponseFactory): |
| 16 | + chapters = factory.List([factory.SubFactory(factories.ChapterFactory)]) |
| 17 | + |
| 18 | + |
| 19 | +def __submit_mock_request( |
| 20 | + httpx_mock: HTTPXMock, |
| 21 | + mock_response: Dict[str, Any], |
| 22 | + config: aai.TranscriptionConfig, |
| 23 | +) -> Tuple[Dict[str, Any], aai.Transcript]: |
| 24 | + """ |
| 25 | + Helper function to abstract mock transcriber calls with given `TranscriptionConfig`, |
| 26 | + and perform some common assertions. |
| 27 | + """ |
| 28 | + |
| 29 | + mock_transcript_id = mock_response.get("id", "mock_id") |
| 30 | + |
| 31 | + # Mock initial submission response (transcript is processing) |
| 32 | + mock_processing_response = factories.generate_dict_factory( |
| 33 | + factories.TranscriptProcessingResponseFactory |
| 34 | + )() |
| 35 | + |
| 36 | + httpx_mock.add_response( |
| 37 | + url=f"{aai.settings.base_url}/transcript", |
| 38 | + status_code=httpx.codes.OK, |
| 39 | + method="POST", |
| 40 | + json={ |
| 41 | + **mock_processing_response, |
| 42 | + "id": mock_transcript_id, # inject ID from main mock response |
| 43 | + }, |
| 44 | + ) |
| 45 | + |
| 46 | + # Mock polling-for-completeness response, with completed transcript |
| 47 | + httpx_mock.add_response( |
| 48 | + url=f"{aai.settings.base_url}/transcript/{mock_transcript_id}", |
| 49 | + status_code=httpx.codes.OK, |
| 50 | + method="GET", |
| 51 | + json=mock_response, |
| 52 | + ) |
| 53 | + |
| 54 | + # == Make API request via SDK == |
| 55 | + transcript = aai.Transcriber().transcribe( |
| 56 | + data="https://example.org/audio.wav", |
| 57 | + config=config, |
| 58 | + ) |
| 59 | + |
| 60 | + # Check that submission and polling requests were made |
| 61 | + assert len(httpx_mock.get_requests()) == 2 |
| 62 | + |
| 63 | + # Extract body of initial submission request |
| 64 | + request = httpx_mock.get_requests()[0] |
| 65 | + request_body = json.loads(request.content.decode()) |
| 66 | + |
| 67 | + return request_body, transcript |
| 68 | + |
| 69 | + |
| 70 | +def test_auto_chapters_fails_without_punctuation(httpx_mock: HTTPXMock): |
| 71 | + """ |
| 72 | + Tests whether the SDK raises an error before making a request |
| 73 | + if `auto_chapters` is enabled and `punctuation` is disabled |
| 74 | + """ |
| 75 | + |
| 76 | + with pytest.raises(ValueError) as error: |
| 77 | + __submit_mock_request( |
| 78 | + httpx_mock, |
| 79 | + mock_response={}, # response doesn't matter, since it shouldn't occur |
| 80 | + config=aai.TranscriptionConfig( |
| 81 | + auto_chapters=True, |
| 82 | + punctuate=False, |
| 83 | + ), |
| 84 | + ) |
| 85 | + # Check that the error message informs the user of the invalid parameter |
| 86 | + assert "punctuate" in str(error) |
| 87 | + |
| 88 | + # Check that the error was raised before any requests were made |
| 89 | + assert len(httpx_mock.get_requests()) == 0 |
| 90 | + |
| 91 | + # Inform httpx_mock that it's okay we didn't make any requests |
| 92 | + httpx_mock.reset(False) |
| 93 | + |
| 94 | + |
| 95 | +def test_auto_chapters_disabled_by_default(httpx_mock: HTTPXMock): |
| 96 | + """ |
| 97 | + Tests that excluding `auto_chapters` from the `TranscriptionConfig` will |
| 98 | + result in the default behavior of it being excluded from the request body |
| 99 | + """ |
| 100 | + request_body, transcript = __submit_mock_request( |
| 101 | + httpx_mock, |
| 102 | + mock_response=factories.generate_dict_factory( |
| 103 | + factories.TranscriptCompletedResponseFactory |
| 104 | + )(), |
| 105 | + config=aai.TranscriptionConfig(), |
| 106 | + ) |
| 107 | + assert request_body.get("auto_chapters") is None |
| 108 | + assert transcript.chapters is None |
| 109 | + |
| 110 | + |
| 111 | +def test_auto_chapters_enabled(httpx_mock: HTTPXMock): |
| 112 | + """ |
| 113 | + Tests that including `auto_chapters=True` in the `TranscriptionConfig` |
| 114 | + will result in `auto_chapters=True` in the request body, and that the |
| 115 | + response is properly parsed into a `Transcript` object |
| 116 | + """ |
| 117 | + mock_response = factories.generate_dict_factory(AutoChaptersResponseFactory)() |
| 118 | + request_body, transcript = __submit_mock_request( |
| 119 | + httpx_mock, |
| 120 | + mock_response=mock_response, |
| 121 | + config=aai.TranscriptionConfig(auto_chapters=True), |
| 122 | + ) |
| 123 | + |
| 124 | + # Check that request body was properly defined |
| 125 | + assert request_body.get("auto_chapters") == True |
| 126 | + |
| 127 | + # Check that transcript was properly parsed from JSON response |
| 128 | + assert transcript.error is None |
| 129 | + assert transcript.chapters is not None |
| 130 | + assert len(transcript.chapters) > 0 |
| 131 | + assert len(transcript.chapters) == len(mock_response["chapters"]) |
| 132 | + |
| 133 | + for response_chapter, transcript_chapter in zip( |
| 134 | + mock_response["chapters"], transcript.chapters |
| 135 | + ): |
| 136 | + assert transcript_chapter.summary == response_chapter["summary"] |
| 137 | + assert transcript_chapter.headline == response_chapter["headline"] |
| 138 | + assert transcript_chapter.gist == response_chapter["gist"] |
| 139 | + assert transcript_chapter.start == response_chapter["start"] |
| 140 | + assert transcript_chapter.end == response_chapter["end"] |
0 commit comments