openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Audio/GenerateSpeechMockTests.cs
42lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.Threading; |
| 4 | using NUnit.Framework; |
| 5 | using OpenAI.Audio; |
| 6 | using OpenAI.Tests.Utility; |
| 7 | |
| 8 | namespace OpenAI.Tests.Audio; |
| 9 | |
| 10 | [TestFixture(true)] |
| 11 | [TestFixture(false)] |
| 12 | [Parallelizable(ParallelScope.All)] |
| 13 | [Category("Audio")] |
| 14 | [Category("Smoke")] |
| 15 | internal class GenerateSpeechMockTests : SyncAsyncTestBase |
| 16 | { |
| 17 | private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key"); |
| 18 | |
| 19 | public GenerateSpeechMockTests(bool isAsync) |
| 20 | : base(isAsync) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | [Test] |
| 25 | public void GenerateSpeechRespectsTheCancellationToken() |
| 26 | { |
| 27 | AudioClient client = new AudioClient("model", s_fakeCredential); |
| 28 | using CancellationTokenSource cancellationSource = new(); |
| 29 | cancellationSource.Cancel(); |
| 30 | |
| 31 | if (IsAsync) |
| 32 | { |
| 33 | Assert.That(async () => await client.GenerateSpeechAsync("text", GeneratedSpeechVoice.Echo, cancellationToken: cancellationSource.Token), |
| 34 | Throws.InstanceOf<OperationCanceledException>()); |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | Assert.That(() => client.GenerateSpeech("text", GeneratedSpeechVoice.Echo, cancellationToken: cancellationSource.Token), |
| 39 | Throws.InstanceOf<OperationCanceledException>()); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |