openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/MockClientExamples.cs
44lines · modeblame
a330c2e7Jose Arriaga Maldonado1 years ago | 1 | using System.ClientModel.Primitives; |
| 2 | using System.ClientModel; | |
| 3 | using Moq; | |
| 4 | using NUnit.Framework; | |
| 5 | using OpenAI.Audio; | |
| 6 | | |
| 7 | namespace OpenAI.Examples.Miscellaneous; | |
| 8 | | |
| 9 | public partial class MockClientExamples | |
| 10 | { | |
| 11 | [Test] | |
| 12 | public void MockClient() | |
| 13 | { | |
| 14 | // Instantiate mocks and the AudioTranscription object. | |
| 15 | | |
| 16 | Mock<AudioClient> mockClient = new(); | |
| 17 | Mock<ClientResult<AudioTranscription>> mockResult = new(null, Mock.Of<PipelineResponse>()); | |
| 18 | AudioTranscription transcription = OpenAIAudioModelFactory.AudioTranscription(text: "I swear I saw an apple flying yesterday!"); | |
| 19 | | |
| 20 | // Set up mocks' properties and methods. | |
| 21 | | |
| 22 | mockResult | |
| 23 | .SetupGet(result => result.Value) | |
| 24 | .Returns(transcription); | |
| 25 | | |
| 26 | mockClient.Setup(client => client.TranscribeAudio( | |
| 27 | It.IsAny<string>(), | |
| 28 | It.IsAny<AudioTranscriptionOptions>())) | |
| 29 | .Returns(mockResult.Object); | |
| 30 | | |
| 31 | // Perform validation. | |
| 32 | | |
| 33 | AudioClient client = mockClient.Object; | |
| 34 | bool containsSecretWord = ContainsSecretWord(client, "<audioFilePath>", "apple"); | |
| 35 | | |
| 36 | Assert.That(containsSecretWord, Is.True); | |
| 37 | } | |
| 38 | | |
| 39 | public bool ContainsSecretWord(AudioClient client, string audioFilePath, string secretWord) | |
| 40 | { | |
| 41 | AudioTranscription transcription = client.TranscribeAudio(audioFilePath); | |
| 42 | return transcription.Text.Contains(secretWord); | |
| 43 | } | |
| 44 | } |