|
| 1 | +import json |
| 2 | +from typing import Any, Dict, Tuple |
| 3 | + |
| 4 | +import factory |
| 5 | +import httpx |
| 6 | +from pytest_httpx import HTTPXMock |
| 7 | + |
| 8 | +import assemblyai as aai |
| 9 | +from tests.unit import factories |
| 10 | + |
| 11 | +aai.settings.api_key = "test" |
| 12 | + |
| 13 | + |
| 14 | +class AutohighlightResultFactory(factory.Factory): |
| 15 | + class Meta: |
| 16 | + model = aai.types.AutohighlightResult |
| 17 | + |
| 18 | + count = factory.Faker("pyint") |
| 19 | + rank = factory.Faker("pyfloat") |
| 20 | + text = factory.Faker("sentence") |
| 21 | + timestamps = factory.List([factory.SubFactory(factories.TimestampFactory)]) |
| 22 | + |
| 23 | + |
| 24 | +class AutohighlightResponseFactory(factory.Factory): |
| 25 | + class Meta: |
| 26 | + model = aai.types.AutohighlightResponse |
| 27 | + |
| 28 | + status = aai.types.StatusResult.success |
| 29 | + results = factory.List([factory.SubFactory(AutohighlightResultFactory)]) |
| 30 | + |
| 31 | + |
| 32 | +class AutohighlightTranscriptResponseFactory( |
| 33 | + factories.TranscriptCompletedResponseFactory |
| 34 | +): |
| 35 | + auto_highlights_result = factory.SubFactory(AutohighlightResponseFactory) |
| 36 | + |
| 37 | + |
| 38 | +def __submit_mock_request( |
| 39 | + httpx_mock: HTTPXMock, |
| 40 | + mock_response: Dict[str, Any], |
| 41 | + config: aai.TranscriptionConfig, |
| 42 | +) -> Tuple[Dict[str, Any], aai.Transcript]: |
| 43 | + """ |
| 44 | + Helper function to abstract mock transcriber calls with given `TranscriptionConfig`, |
| 45 | + and perform some common assertions. |
| 46 | + """ |
| 47 | + |
| 48 | + mock_transcript_id = mock_response.get("id", "mock_id") |
| 49 | + |
| 50 | + # Mock initial submission response (transcript is processing) |
| 51 | + mock_processing_response = factories.generate_dict_factory( |
| 52 | + factories.TranscriptProcessingResponseFactory |
| 53 | + )() |
| 54 | + |
| 55 | + httpx_mock.add_response( |
| 56 | + url=f"{aai.settings.base_url}/transcript", |
| 57 | + status_code=httpx.codes.OK, |
| 58 | + method="POST", |
| 59 | + json={ |
| 60 | + **mock_processing_response, |
| 61 | + "id": mock_transcript_id, # inject ID from main mock response |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + # Mock polling-for-completeness response, with completed transcript |
| 66 | + httpx_mock.add_response( |
| 67 | + url=f"{aai.settings.base_url}/transcript/{mock_transcript_id}", |
| 68 | + status_code=httpx.codes.OK, |
| 69 | + method="GET", |
| 70 | + json=mock_response, |
| 71 | + ) |
| 72 | + |
| 73 | + # == Make API request via SDK == |
| 74 | + transcript = aai.Transcriber().transcribe( |
| 75 | + data="https://example.org/audio.wav", |
| 76 | + config=config, |
| 77 | + ) |
| 78 | + |
| 79 | + # Check that submission and polling requests were made |
| 80 | + assert len(httpx_mock.get_requests()) == 2 |
| 81 | + |
| 82 | + # Extract body of initial submission request |
| 83 | + request = httpx_mock.get_requests()[0] |
| 84 | + request_body = json.loads(request.content.decode()) |
| 85 | + |
| 86 | + return request_body, transcript |
| 87 | + |
| 88 | + |
| 89 | +def test_auto_highlights_disabled_by_default(httpx_mock: HTTPXMock): |
| 90 | + """ |
| 91 | + Tests that excluding `auto_highlights` from the `TranscriptionConfig` will |
| 92 | + result in the default behavior of it being excluded from the request body |
| 93 | + """ |
| 94 | + request_body, transcript = __submit_mock_request( |
| 95 | + httpx_mock, |
| 96 | + mock_response=factories.generate_dict_factory( |
| 97 | + factories.TranscriptCompletedResponseFactory |
| 98 | + )(), |
| 99 | + config=aai.TranscriptionConfig(), |
| 100 | + ) |
| 101 | + assert request_body.get("auto_highlights") is None |
| 102 | + assert transcript.auto_highlights_result is None |
| 103 | + |
| 104 | + |
| 105 | +def test_auto_highlights_enabled(httpx_mock: HTTPXMock): |
| 106 | + """ |
| 107 | + Tests that including `auto_highlights=True` in the `TranscriptionConfig` |
| 108 | + will result in `auto_highlights=True` in the request body, and that the |
| 109 | + response is properly parsed into a `Transcript` object |
| 110 | + """ |
| 111 | + mock_response = factories.generate_dict_factory( |
| 112 | + AutohighlightTranscriptResponseFactory |
| 113 | + )() |
| 114 | + request_body, transcript = __submit_mock_request( |
| 115 | + httpx_mock, |
| 116 | + mock_response=mock_response, |
| 117 | + config=aai.TranscriptionConfig(auto_highlights=True), |
| 118 | + ) |
| 119 | + |
| 120 | + # Check that request body was properly defined |
| 121 | + assert request_body.get("auto_highlights") == True |
| 122 | + |
| 123 | + # Check that transcript was properly parsed from JSON response |
| 124 | + assert transcript.error is None |
| 125 | + assert transcript.auto_highlights_result is not None |
| 126 | + assert ( |
| 127 | + transcript.auto_highlights_result.status |
| 128 | + == mock_response["auto_highlights_result"]["status"] |
| 129 | + ) |
| 130 | + |
| 131 | + assert transcript.auto_highlights_result.results is not None |
| 132 | + assert len(transcript.auto_highlights_result.results) > 0 |
| 133 | + assert len(transcript.auto_highlights_result.results) == len( |
| 134 | + mock_response["auto_highlights_result"]["results"] |
| 135 | + ) |
| 136 | + |
| 137 | + for response_result, transcript_result in zip( |
| 138 | + mock_response["auto_highlights_result"]["results"], |
| 139 | + transcript.auto_highlights_result.results, |
| 140 | + ): |
| 141 | + assert transcript_result.count == response_result["count"] |
| 142 | + assert transcript_result.rank == response_result["rank"] |
| 143 | + assert transcript_result.text == response_result["text"] |
| 144 | + |
| 145 | + for response_timestamp, transcript_timestamp in zip( |
| 146 | + response_result["timestamps"], transcript_result.timestamps |
| 147 | + ): |
| 148 | + assert transcript_timestamp.start == response_timestamp["start"] |
| 149 | + assert transcript_timestamp.end == response_timestamp["end"] |
0 commit comments