openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Audio/TranslationTests.cs

77lines · modecode

1using NUnit.Framework;
2using OpenAI.Audio;
3using OpenAI.Tests.Utility;
4using System.IO;
5using System.Threading.Tasks;
6using static OpenAI.Tests.TestHelpers;
7
8namespace OpenAI.Tests.Audio;
9
10[TestFixture(true)]
11[TestFixture(false)]
12public partial class TranslationTests : SyncAsyncTestBase
13{
14 public TranslationTests(bool isAsync)
15 : base(isAsync)
16 {
17 }
18
19 public enum AudioSourceKind
20 {
21 UsingStream,
22 UsingFilePath,
23 }
24
25 [Test]
26 [TestCase(AudioSourceKind.UsingStream)]
27 [TestCase(AudioSourceKind.UsingFilePath)]
28 public async Task TranslationWorks(AudioSourceKind audioSourceKind)
29 {
30 AudioClient client = GetTestClient();
31
32 string filename = "audio_french.wav";
33 string path = Path.Combine("Assets", filename);
34 AudioTranslation translation = null;
35
36 if (audioSourceKind == AudioSourceKind.UsingStream)
37 {
38 using FileStream audio = File.OpenRead(path);
39
40 translation = IsAsync
41 ? await client.TranslateAudioAsync(audio, filename)
42 : client.TranslateAudio(audio, filename);
43 }
44 else if (audioSourceKind == AudioSourceKind.UsingFilePath)
45 {
46 translation = IsAsync
47 ? await client.TranslateAudioAsync(path)
48 : client.TranslateAudio(path);
49 }
50 Assert.That(translation?.Text, Is.Not.Null);
51 Assert.That(translation.Text.ToLowerInvariant(), Contains.Substring("whisper"));
52 }
53
54 [Test]
55 [TestCase(AudioTranslationFormat.Simple)]
56 [TestCase(AudioTranslationFormat.Verbose)]
57 [TestCase(AudioTranslationFormat.Srt)]
58 [TestCase(AudioTranslationFormat.Vtt)]
59 public async Task TranslationFormatsWork(AudioTranslationFormat formatToTest)
60 {
61 AudioClient client = GetTestClient();
62 string path = Path.Combine("Assets", "audio_french.wav");
63
64 AudioTranslationOptions options = new()
65 {
66 ResponseFormat = formatToTest,
67 };
68
69 AudioTranslation translation = IsAsync
70 ? await client.TranslateAudioAsync(path, options)
71 : client.TranslateAudio(path, options);
72
73 Assert.That(translation?.Text?.ToLowerInvariant(), Does.Contain("recognition"));
74 }
75
76 private static AudioClient GetTestClient() => GetTestClient<AudioClient>(TestScenario.Transcription);
77}
78