openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Models/ModelsMockTests.cs
73lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Models; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System.ClientModel; |
| 5 | using System.Linq; |
| 6 | using System.Threading.Tasks; |
| 7 | |
| 8 | namespace OpenAI.Tests.Models; |
| 9 | |
| 10 | [TestFixture(true)] |
| 11 | [TestFixture(false)] |
| 12 | [Parallelizable(ParallelScope.All)] |
| 13 | [Category("Models")] |
| 14 | [Category("Smoke")] |
| 15 | public class ModelsMockTests : SyncAsyncTestBase |
| 16 | { |
| 17 | private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key"); |
| 18 | |
| 19 | public ModelsMockTests(bool isAsync) |
| 20 | : base(isAsync) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | [Test] |
| 25 | public async Task GetModelDeserializesCreatedAt() |
| 26 | { |
| 27 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 28 | { |
| 29 | "created": 1704096000 |
| 30 | } |
| 31 | """); |
| 32 | OpenAIModelClient client = new OpenAIModelClient(s_fakeCredential, clientOptions); |
| 33 | |
| 34 | OpenAIModel modelInfo = IsAsync |
| 35 | ? await client.GetModelAsync("model_name") |
| 36 | : client.GetModel("model_name"); |
| 37 | |
| 38 | Assert.That(modelInfo.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 39 | } |
| 40 | |
| 41 | [Test] |
| 42 | public async Task GetModelsDeserializesCreatedAt() |
| 43 | { |
| 44 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 45 | { |
| 46 | "data": [ |
| 47 | { |
| 48 | "created": 1704096000 |
| 49 | } |
| 50 | ] |
| 51 | } |
| 52 | """); |
| 53 | OpenAIModelClient client = new OpenAIModelClient(s_fakeCredential, clientOptions); |
| 54 | |
| 55 | OpenAIModelCollection modelInfoCollection = IsAsync |
| 56 | ? await client.GetModelsAsync() |
| 57 | : client.GetModels(); |
| 58 | OpenAIModel modelInfo = modelInfoCollection.Single(); |
| 59 | |
| 60 | Assert.That(modelInfo.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000)); |
| 61 | } |
| 62 | |
| 63 | private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content) |
| 64 | { |
| 65 | MockPipelineResponse response = new MockPipelineResponse(status); |
| 66 | response.SetContent(content); |
| 67 | |
| 68 | return new OpenAIClientOptions() |
| 69 | { |
| 70 | Transport = new MockPipelineTransport(response) |
| 71 | }; |
| 72 | } |
| 73 | } |
| 74 | |