-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_transcriber.py
More file actions
785 lines (615 loc) · 24.1 KB
/
test_transcriber.py
File metadata and controls
785 lines (615 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
import copy
import json
import os
from unittest.mock import mock_open, patch
from urllib.parse import urlencode
import httpx
import pytest
from pytest_httpx import HTTPXMock
import assemblyai as aai
from assemblyai.api import (
ENDPOINT_TRANSCRIPT,
ENDPOINT_UPLOAD,
)
from tests.unit import factories
aai.settings.api_key = "test"
def test_upload_file_succeeds(httpx_mock: HTTPXMock):
"""
Tests whether the submission of a file fails.
"""
# our local audio file that we want to transcribe
local_file = os.urandom(10)
# this is the url that we should receive when uploading the local file
expected_upload_url = "https://example.org/audio.wav"
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_UPLOAD}",
status_code=httpx.codes.OK,
method="POST",
json={"upload_url": expected_upload_url},
match_content=local_file,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
# patch the reading of a local file
with patch("builtins.open", mock_open(read_data=local_file)):
audio_url = transcriber.upload_file("audio.wav")
# check whether returned audio_url is correct
assert audio_url == expected_upload_url
def test_upload_file_fails(httpx_mock: HTTPXMock):
"""
Tests whether the submission of a file fails.
"""
returned_error_message = "something went wrong"
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_UPLOAD}",
status_code=httpx.codes.INTERNAL_SERVER_ERROR,
method="POST",
json={"error": returned_error_message},
)
# check whether uploading a file raises a TranscriptError
with pytest.raises(aai.TranscriptError) as excinfo:
aai.Transcriber().upload_file(os.urandom(10))
# check whether the TranscriptError contains the specified error message
assert returned_error_message in str(excinfo.value)
assert httpx.codes.INTERNAL_SERVER_ERROR == excinfo.value.status_code
def test_submit_url_succeeds(httpx_mock: HTTPXMock):
"""
Tests whether the submission of an URL works.
"""
# create a mock response of a completed transcript
mock_transcript_response = factories.generate_dict_factory(
factories.TranscriptProcessingResponseFactory
)()
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_transcript_response,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript = transcriber.submit("https://example.org/audio.wav")
# ensure integrity
assert transcript.id == mock_transcript_response["id"]
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 1
def test_submit_url_fails(httpx_mock: HTTPXMock):
"""
Tests whether the submission of an URL fails.
"""
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.INTERNAL_SERVER_ERROR,
method="POST",
json={"error": "something went wrong"},
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
with pytest.raises(aai.TranscriptError) as excinfo:
transcriber.submit("https://example.org/audio.wav")
assert "something went wrong" in str(excinfo)
assert httpx.codes.INTERNAL_SERVER_ERROR == excinfo.value.status_code
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 1
def test_submit_file_fails_due_api_error(httpx_mock: HTTPXMock):
"""
Tests whether the submission of a file fails due to an API error.
"""
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_UPLOAD}",
status_code=httpx.codes.INTERNAL_SERVER_ERROR,
method="POST",
json={"error": "something went wrong"},
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
# patch the reading of a local file
with patch("builtins.open", mock_open(read_data=os.urandom(10))):
with pytest.raises(aai.TranscriptError) as excinfo:
transcriber.transcribe("audio.wav")
# check whether the Exception contains the specified error message
assert "something went wrong" in str(excinfo.value)
assert httpx.codes.INTERNAL_SERVER_ERROR == excinfo.value.status_code
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 1
def test_submit_file_fails_due_file_not_found():
"""
Tests whether the submission of a file fails due to a file not found error.
"""
# mimic the usage of the SDK
transcriber = aai.Transcriber()
# check whether the file not found error is raised
with pytest.raises(FileNotFoundError):
transcriber.submit("audio.wav")
def test_transcribe_url_succeeds(httpx_mock: HTTPXMock):
"""
Tests whether the transcription of an URL works.
"""
# create a mock response of a completed transcript
mock_transcript_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_transcript_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_response['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_transcript_response,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript = transcriber.transcribe("https://example.org/audio.wav")
# ensure integrity
assert transcript.id == mock_transcript_response["id"]
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 2
def test_transcribe_file_succeeds(httpx_mock: HTTPXMock):
"""
Tests whether the transcription of a local file works.
"""
# create a mock response of a completed transcript
mock_transcript_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
# our local audio file that we want to transcribe
local_file = os.urandom(10)
# this is the url that we should receive when uploading the local file
expected_upload_url = "https://example.org/audio.wav"
mock_transcript_response["audio_url"] = expected_upload_url
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_UPLOAD}",
status_code=httpx.codes.OK,
method="POST",
json={"upload_url": expected_upload_url},
match_content=local_file,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_transcript_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_response['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_transcript_response,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
# patch the reading of a local file
with patch("builtins.open", mock_open(read_data=local_file)):
transcript = transcriber.transcribe("audio.wav")
# ensure integrity
assert transcript.id == mock_transcript_response["id"]
assert transcript.audio_url == expected_upload_url
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 3
def test_transcribe_file_binary_succeeds(httpx_mock: HTTPXMock):
"""
Tests whether the transcription of a binary file works.
"""
# create a mock response of a completed transcript
mock_transcript_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
# our binary audio file that we want to transcribe
file_data = os.urandom(10)
# this is the url that we should receive when uploading the local file
expected_upload_url = "https://example.org/audio.wav"
mock_transcript_response["audio_url"] = expected_upload_url
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_UPLOAD}",
status_code=httpx.codes.OK,
method="POST",
json={"upload_url": expected_upload_url},
match_content=file_data,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_transcript_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_response['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_transcript_response,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript = transcriber.transcribe(file_data)
# ensure integrity
assert transcript.id == mock_transcript_response["id"]
assert transcript.audio_url == expected_upload_url
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 3
def test_transcribe_group_urls_succeeds(httpx_mock: HTTPXMock):
"""
Tests whether the transcription of multiple URLs work.
"""
# create a mock response of a completed transcript
mock_transcript_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
# create different response mock objects
response_1 = copy.deepcopy(mock_transcript_response)
response_2 = copy.deepcopy(mock_transcript_response)
expected_audio_urls = ["https://example.org/1.wav", "https://example.org/2.wav"]
response_1["audio_url"] = expected_audio_urls[0]
response_2["audio_url"] = expected_audio_urls[1]
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=response_1,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=response_2,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{response_1['id']}",
status_code=httpx.codes.OK,
method="GET",
json=response_1,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{response_2['id']}",
status_code=httpx.codes.OK,
method="GET",
json=response_2,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript_group = transcriber.transcribe_group(expected_audio_urls)
# ensure integrity
assert len(transcript_group.transcripts) == 2
# check whether the audio urls match
assert {t.audio_url for t in transcript_group} == set(expected_audio_urls)
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 4
def test_transcribe_group_urls_fails_during_upload(httpx_mock: HTTPXMock):
"""
Tests the scenario of when a list of paths or URLs are being transcribed
and one (or more) items of that list fail due a HTTP-error (e.g. Timeout, Internal Server Error, etc.)
In this case we need to ensure that the application flow does not interrupt unexpectedly and the user
is able to backtrace the reason of those failed uploads.
"""
# create a mock response of a completed transcript
succeeds_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
expect_succeeded_audio_url = "https://example.org/succeeds.wav"
succeeds_response["audio_url"] = expect_succeeded_audio_url
expect_failed_audio_url = "https://example.org/fails.wav"
fails_json = {"error": "something went wrong"}
# mock the specific endpoints
# the first file fails (see `.transcripe_group(...)` below)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.INTERNAL_SERVER_ERROR,
method="POST",
json=fails_json,
)
# the second one succeeds (see `.transcribe_group(...) below`)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=succeeds_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{succeeds_response['id']}",
status_code=httpx.codes.OK,
method="GET",
json=succeeds_response,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript_group, failures = transcriber.transcribe_group(
[
expect_failed_audio_url,
expect_succeeded_audio_url,
],
return_failures=True,
)
# ensure integrity
assert len(transcript_group.transcripts) == 1
assert len(failures) == 1
# Check whether the error message corresponds to the raised TranscriptError message
assert "failed to transcribe url" in str(failures[0])
assert failures[0].status_code == httpx.codes.INTERNAL_SERVER_ERROR
def test_transcribe_group_urls_fails_during_polling(httpx_mock: HTTPXMock):
"""
Tests the scenario of when a list of paths or URLs are being transcribed
and one (or more) items of that list fail during the polling process.
In this case we need to ensure that the application flow does not interrupt unexpectedly and the user
is able to backtrace the reason
"""
# create a mock response for two processing transcripts
mock_processing_response_1 = factories.generate_dict_factory(
factories.TranscriptProcessingResponseFactory
)()
mock_processing_response_2 = factories.generate_dict_factory(
factories.TranscriptProcessingResponseFactory
)()
expected_audio_urls = ["https://example.org/1.wav", "https://example.org/2.wav"]
mock_processing_response_1["audio_url"] = expected_audio_urls[0]
mock_processing_response_2["audio_url"] = expected_audio_urls[1]
# mock both URLs succeeding on submission
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_processing_response_1,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_processing_response_2,
)
# create a mock response for a successful GET and a failed GET
mock_succeeds_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
failed_json = {"error": "something went wrong"}
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_processing_response_1['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_succeeds_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_processing_response_2['id']}",
status_code=httpx.codes.INTERNAL_SERVER_ERROR,
method="GET",
json=failed_json,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript_group, failures = transcriber.transcribe_group(
expected_audio_urls,
return_failures=True,
)
# ensure integrity
assert len(transcript_group.transcripts) == 1
assert len(failures) == 1
# Check whether the error message is correct
assert "failed to retrieve transcript" in str(failures[0])
assert failures[0].status_code == httpx.codes.INTERNAL_SERVER_ERROR
def test_transcribe_async_url_succeeds(httpx_mock: HTTPXMock):
# create a mock response of a processing transcript
mock_processing_response = factories.generate_dict_factory(
factories.TranscriptProcessingResponseFactory
)()
# create a mock response of a completed transcript
mock_completed_response = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
mock_completed_response["id"] = mock_processing_response["id"]
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_processing_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_processing_response['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_processing_response,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_processing_response['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_completed_response,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript_future = transcriber.transcribe_async(
"https://example.org/audio.wav",
)
# `transcribe` is being called with a callback the operation works asynchronously
transcript = transcript_future.result()
# ensure integrity
assert transcript.id == mock_completed_response["id"]
assert transcript.status == aai.TranscriptStatus.completed
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 3
def test_transcribe_async_url_fails(httpx_mock: HTTPXMock):
# create a mock response of a processing transcript
mock_processing_transcript = factories.generate_dict_factory(
factories.TranscriptProcessingResponseFactory
)()
# create a mock response of a error transcript
mock_error_transcript = factories.generate_dict_factory(
factories.TranscriptErrorResponseFactory
)()
mock_error_transcript["id"] = mock_processing_transcript["id"]
# mock the specific endpoints
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_processing_transcript,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_processing_transcript['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_processing_transcript,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_error_transcript['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_error_transcript,
)
# mimic the usage of the SDK
transcriber = aai.Transcriber()
transcript_future = transcriber.transcribe_async(
"https://example.org/audio.wav",
)
# `transcribe` is being called with a callback the operation works asynchronously
transcript = transcript_future.result()
# ensure integrity
assert transcript.id == mock_error_transcript["id"]
assert transcript.status == aai.TranscriptStatus.error
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 3
def test_language_detection(httpx_mock: HTTPXMock):
mock_completed_json = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_completed_json,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_completed_json['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_completed_json,
)
aai.Transcriber().transcribe(
"https://example.org/audio.wav",
config=aai.TranscriptionConfig(
language_code=None,
language_detection=True,
),
)
request = json.loads(httpx_mock.get_requests()[0].content.decode())
assert request["language_detection"] is True
assert request.get("language_code") is None
def test_language_codes_request(httpx_mock: HTTPXMock):
mock_completed_json = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_completed_json,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_completed_json['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_completed_json,
)
aai.Transcriber().transcribe(
"https://example.org/audio.wav",
config=aai.TranscriptionConfig(
language_codes=["en", "es"],
),
)
request = json.loads(httpx_mock.get_requests()[0].content.decode())
assert request.get("language_codes") == ["en", "es"]
def test_language_code_string(httpx_mock: HTTPXMock):
mock_completed_json = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_completed_json,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_completed_json['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_completed_json,
)
aai.Transcriber().transcribe(
"https://example.org/audio.wav",
config=aai.TranscriptionConfig(
language_code="en",
),
)
request = json.loads(httpx_mock.get_requests()[0].content.decode())
assert request.get("language_code") == "en"
def test_language_code_enum(httpx_mock: HTTPXMock):
mock_completed_json = factories.generate_dict_factory(
factories.TranscriptCompletedResponseFactory
)()
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="POST",
json=mock_completed_json,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_completed_json['id']}",
status_code=httpx.codes.OK,
method="GET",
json=mock_completed_json,
)
with pytest.deprecated_call():
language_code = aai.LanguageCode.en
transcript = aai.Transcriber().transcribe(
"https://example.org/audio.wav",
config=aai.TranscriptionConfig(
language_code=language_code,
),
)
assert transcript.config.language_code == language_code
def test_list_transcripts(httpx_mock: HTTPXMock):
mock_list_transcript_response = factories.generate_dict_factory(
factories.ListTranscriptResponse
)()
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
status_code=httpx.codes.OK,
method="GET",
json=mock_list_transcript_response,
)
page = aai.Transcriber().list_transcripts()
assert isinstance(page, aai.ListTranscriptResponse)
assert page.page_details is not None
assert page.transcripts is not None
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 1
def test_list_transcripts_parameters(httpx_mock: HTTPXMock):
mock_list_transcript_response = factories.generate_dict_factory(
factories.ListTranscriptResponse
)()
params = aai.ListTranscriptParameters(
limit=2,
status=aai.TranscriptStatus.completed,
)
httpx_mock.add_response(
url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}?{urlencode(params.dict(exclude_none=True))}",
status_code=httpx.codes.OK,
method="GET",
json=mock_list_transcript_response,
)
page = aai.Transcriber().list_transcripts(params)
assert isinstance(page, aai.ListTranscriptResponse)
assert page.page_details is not None
assert page.transcripts is not None
# check whether we mocked everything
assert len(httpx_mock.get_requests()) == 1