|
| 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 IABLabelResultFactory(factory.Factory): |
| 15 | + class Meta: |
| 16 | + model = aai.types.IABLabelResult |
| 17 | + |
| 18 | + relevance = factory.Faker("pyfloat", min_value=0, max_value=1) |
| 19 | + label = factory.Faker("word") |
| 20 | + |
| 21 | + |
| 22 | +class IABResultFactory(factory.Factory): |
| 23 | + class Meta: |
| 24 | + model = aai.types.IABResult |
| 25 | + |
| 26 | + text = factory.Faker("sentence") |
| 27 | + labels = factory.List([factory.SubFactory(IABLabelResultFactory)]) |
| 28 | + timestamp = factory.SubFactory(factories.TimestampFactory) |
| 29 | + |
| 30 | + |
| 31 | +class IABResponseFactory(factory.Factory): |
| 32 | + class Meta: |
| 33 | + model = aai.types.IABResponse |
| 34 | + |
| 35 | + status = aai.types.StatusResult.success.value |
| 36 | + results = factory.List([factory.SubFactory(IABResultFactory)]) |
| 37 | + summary = factory.Dict( |
| 38 | + { |
| 39 | + "Automotive>AutoType>ConceptCars": factory.Faker( |
| 40 | + "pyfloat", min_value=0, max_value=1 |
| 41 | + ) |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +class IABCategoriesResponseFactory(factories.TranscriptCompletedResponseFactory): |
| 47 | + iab_categories_result = factory.SubFactory(IABResponseFactory) |
| 48 | + |
| 49 | + |
| 50 | +def __submit_mock_request( |
| 51 | + httpx_mock: HTTPXMock, |
| 52 | + mock_response: Dict[str, Any], |
| 53 | + config: aai.TranscriptionConfig, |
| 54 | +) -> Tuple[Dict[str, Any], aai.Transcript]: |
| 55 | + """ |
| 56 | + Helper function to abstract mock transcriber calls with given `TranscriptionConfig`, |
| 57 | + and perform some common assertions. |
| 58 | + """ |
| 59 | + |
| 60 | + mock_transcript_id = mock_response.get("id", "mock_id") |
| 61 | + |
| 62 | + # Mock initial submission response (transcript is processing) |
| 63 | + mock_processing_response = factories.generate_dict_factory( |
| 64 | + factories.TranscriptProcessingResponseFactory |
| 65 | + )() |
| 66 | + |
| 67 | + httpx_mock.add_response( |
| 68 | + url=f"{aai.settings.base_url}/transcript", |
| 69 | + status_code=httpx.codes.OK, |
| 70 | + method="POST", |
| 71 | + json={ |
| 72 | + **mock_processing_response, |
| 73 | + "id": mock_transcript_id, # inject ID from main mock response |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + # Mock polling-for-completeness response, with completed transcript |
| 78 | + httpx_mock.add_response( |
| 79 | + url=f"{aai.settings.base_url}/transcript/{mock_transcript_id}", |
| 80 | + status_code=httpx.codes.OK, |
| 81 | + method="GET", |
| 82 | + json=mock_response, |
| 83 | + ) |
| 84 | + |
| 85 | + # == Make API request via SDK == |
| 86 | + transcript = aai.Transcriber().transcribe( |
| 87 | + data="https://example.org/audio.wav", |
| 88 | + config=config, |
| 89 | + ) |
| 90 | + |
| 91 | + # Check that submission and polling requests were made |
| 92 | + assert len(httpx_mock.get_requests()) == 2 |
| 93 | + |
| 94 | + # Extract body of initial submission request |
| 95 | + request = httpx_mock.get_requests()[0] |
| 96 | + request_body = json.loads(request.content.decode()) |
| 97 | + |
| 98 | + return request_body, transcript |
| 99 | + |
| 100 | + |
| 101 | +def test_iab_categories_disabled_by_default(httpx_mock: HTTPXMock): |
| 102 | + """ |
| 103 | + Tests that excluding `iab_categories` from the `TranscriptionConfig` will |
| 104 | + result in the default behavior of it being excluded from the request body |
| 105 | + """ |
| 106 | + |
| 107 | + request_body, transcript = __submit_mock_request( |
| 108 | + httpx_mock, |
| 109 | + mock_response=factories.generate_dict_factory( |
| 110 | + factories.TranscriptCompletedResponseFactory |
| 111 | + )(), |
| 112 | + config=aai.TranscriptionConfig(), |
| 113 | + ) |
| 114 | + assert request_body.get("iab_categories") is None |
| 115 | + assert transcript.iab_categories is None |
| 116 | + |
| 117 | + |
| 118 | +def test_iab_categories_enabled(httpx_mock: HTTPXMock): |
| 119 | + """ |
| 120 | + Tests that including `iab_categories=True` in the `TranscriptionConfig` will |
| 121 | + result in `iab_categories` being included in the request body, and that |
| 122 | + the response will be properly parsed into the `Transcript` object |
| 123 | + """ |
| 124 | + |
| 125 | + mock_response = factories.generate_dict_factory(IABCategoriesResponseFactory)() |
| 126 | + |
| 127 | + request_body, transcript = __submit_mock_request( |
| 128 | + httpx_mock, |
| 129 | + mock_response=mock_response, |
| 130 | + config=aai.TranscriptionConfig(iab_categories=True), |
| 131 | + ) |
| 132 | + |
| 133 | + assert request_body.get("iab_categories") is True |
| 134 | + |
| 135 | + assert transcript.error is None |
| 136 | + |
| 137 | + assert transcript.iab_categories is not None |
| 138 | + assert transcript.iab_categories.status == mock_response.get( |
| 139 | + "iab_categories_result", {} |
| 140 | + ).get("status") |
| 141 | + |
| 142 | + # Validate results |
| 143 | + response_results = mock_response.get("iab_categories_result", {}).get("results", []) |
| 144 | + transcript_results = transcript.iab_categories.results |
| 145 | + |
| 146 | + assert transcript_results is not None |
| 147 | + assert len(transcript_results) == len(response_results) |
| 148 | + assert len(transcript_results) > 0 |
| 149 | + |
| 150 | + for response_result, transcript_result in zip(response_results, transcript_results): |
| 151 | + assert transcript_result.text == response_result.get("text") |
| 152 | + assert len(transcript_result.text) > 0 |
| 153 | + |
| 154 | + assert len(transcript_result.labels) > 0 |
| 155 | + assert len(transcript_result.labels) == len(response_result.get("labels", [])) |
| 156 | + for response_label, transcript_label in zip( |
| 157 | + response_result.get("labels", []), transcript_result.labels |
| 158 | + ): |
| 159 | + assert transcript_label.relevance == response_label.get("relevance") |
| 160 | + assert transcript_label.label == response_label.get("label") |
| 161 | + |
| 162 | + # Validate summary |
| 163 | + response_summary = mock_response.get("iab_categories_result", {}).get("summary", {}) |
| 164 | + transcript_summary = transcript.iab_categories.summary |
| 165 | + |
| 166 | + assert transcript_summary is not None |
| 167 | + assert len(transcript_summary) > 0 |
| 168 | + assert transcript_summary == response_summary |
0 commit comments