|
| 1 | +import json |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +import httpx |
| 5 | +import pytest |
| 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 | +def __submit_request(httpx_mock: HTTPXMock, **params) -> Dict[str, Any]: |
| 15 | + """ |
| 16 | + Helper function to abstract calling transcriber with given parameters, |
| 17 | + and perform some common assertions. |
| 18 | +
|
| 19 | + Returns the body (dictionary) of the initial submission request. |
| 20 | + """ |
| 21 | + summary = "example summary" |
| 22 | + |
| 23 | + mock_transcript_response = factories.generate_dict_factory( |
| 24 | + factories.TranscriptCompletedResponseFactory |
| 25 | + )() |
| 26 | + |
| 27 | + # Mock initial submission response |
| 28 | + httpx_mock.add_response( |
| 29 | + url=f"{aai.settings.base_url}/transcript", |
| 30 | + status_code=httpx.codes.OK, |
| 31 | + method="POST", |
| 32 | + json=mock_transcript_response, |
| 33 | + ) |
| 34 | + |
| 35 | + # Mock polling-for-completeness response, with mock summary result |
| 36 | + httpx_mock.add_response( |
| 37 | + url=f"{aai.settings.base_url}/transcript/{mock_transcript_response['id']}", |
| 38 | + status_code=httpx.codes.OK, |
| 39 | + method="GET", |
| 40 | + json={**mock_transcript_response, "summary": summary}, |
| 41 | + ) |
| 42 | + |
| 43 | + # == Make API request via SDK == |
| 44 | + transcript = aai.Transcriber().transcribe( |
| 45 | + data="https://example.org/audio.wav", |
| 46 | + config=aai.TranscriptionConfig( |
| 47 | + **params, |
| 48 | + ), |
| 49 | + ) |
| 50 | + |
| 51 | + # Check that submission and polling requests were made |
| 52 | + assert len(httpx_mock.get_requests()) == 2 |
| 53 | + |
| 54 | + # Check that summary field from response was traced back through SDK classes |
| 55 | + assert transcript.summary == summary |
| 56 | + |
| 57 | + # Extract and return body of initial submission request |
| 58 | + request = httpx_mock.get_requests()[0] |
| 59 | + return json.loads(request.content.decode()) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize("required_field", ["punctuate", "format_text"]) |
| 63 | +def test_summarization_fails_without_required_field( |
| 64 | + httpx_mock: HTTPXMock, required_field: str |
| 65 | +): |
| 66 | + """ |
| 67 | + Tests whether the SDK raises an error before making a request |
| 68 | + if `summarization` is enabled and the given required field is disabled |
| 69 | + """ |
| 70 | + with pytest.raises(ValueError) as error: |
| 71 | + __submit_request(httpx_mock, summarization=True, **{required_field: False}) |
| 72 | + |
| 73 | + # Check that the error message informs the user of the invalid parameter |
| 74 | + assert required_field in str(error) |
| 75 | + |
| 76 | + # Check that the error was raised before any requests were made |
| 77 | + assert len(httpx_mock.get_requests()) == 0 |
| 78 | + |
| 79 | + # Inform httpx_mock that it's okay we didn't make any requests |
| 80 | + httpx_mock.reset(False) |
| 81 | + |
| 82 | + |
| 83 | +def test_summarization_disabled_by_default(httpx_mock: HTTPXMock): |
| 84 | + """ |
| 85 | + Tests that excluding `summarization` from the `TranscriptionConfig` will |
| 86 | + result in the default behavior of it being excluded from the request body |
| 87 | + """ |
| 88 | + request_body = __submit_request(httpx_mock) |
| 89 | + assert request_body.get("summarization") is None |
| 90 | + |
| 91 | + |
| 92 | +def test_default_summarization_params(httpx_mock: HTTPXMock): |
| 93 | + """ |
| 94 | + Tests that including `summarization=True` in the `TranscriptionConfig` |
| 95 | + will result in `summarization=True` in the request body. |
| 96 | + """ |
| 97 | + request_body = __submit_request(httpx_mock, summarization=True) |
| 98 | + assert request_body.get("summarization") == True |
| 99 | + |
| 100 | + |
| 101 | +def test_summarization_with_params(httpx_mock: HTTPXMock): |
| 102 | + """ |
| 103 | + Tests that including additional summarization parameters along with |
| 104 | + `summarization=True` in the `TranscriptionConfig` will result in all |
| 105 | + parameters being included in the request as well. |
| 106 | + """ |
| 107 | + |
| 108 | + summary_model = aai.SummarizationModel.conversational |
| 109 | + summary_type = aai.SummarizationType.bullets |
| 110 | + |
| 111 | + request_body = __submit_request( |
| 112 | + httpx_mock, |
| 113 | + summarization=True, |
| 114 | + summary_model=summary_model, |
| 115 | + summary_type=summary_type, |
| 116 | + ) |
| 117 | + |
| 118 | + assert request_body.get("summarization") == True |
| 119 | + assert request_body.get("summary_model") == summary_model |
| 120 | + assert request_body.get("summary_type") == summary_type |
| 121 | + |
| 122 | + |
| 123 | +def test_summarization_params_excluded_when_disabled(httpx_mock: HTTPXMock): |
| 124 | + """ |
| 125 | + Tests that additional summarization parameters are excluded from the submission |
| 126 | + request body if `summarization` itself is not enabled. |
| 127 | + """ |
| 128 | + request_body = __submit_request( |
| 129 | + httpx_mock, |
| 130 | + summarization=False, |
| 131 | + summary_model=aai.SummarizationModel.conversational, |
| 132 | + summary_type=aai.SummarizationType.bullets, |
| 133 | + ) |
| 134 | + |
| 135 | + assert request_body.get("summarization") is None |
| 136 | + assert request_body.get("summary_model") is None |
| 137 | + assert request_body.get("summary_type") is None |
0 commit comments