openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Images/ImageVariationsTests.cs
233lines · modecode
| 1 | using Microsoft.ClientModel.TestFramework; |
| 2 | using NUnit.Framework; |
| 3 | using OpenAI.Images; |
| 4 | using System; |
| 5 | using System.ClientModel; |
| 6 | using System.IO; |
| 7 | using System.Threading.Tasks; |
| 8 | using static OpenAI.Tests.TestHelpers; |
| 9 | |
| 10 | namespace OpenAI.Tests.Images; |
| 11 | |
| 12 | public partial class ImageVariationsTests : ImageTestFixtureBase |
| 13 | { |
| 14 | public ImageVariationsTests(bool isAsync) : base(isAsync) |
| 15 | { |
| 16 | TestTimeoutInSeconds = 30; |
| 17 | } |
| 18 | |
| 19 | [RecordedTest] |
| 20 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 21 | public async Task GenerateImageVariationWorks(ImageSourceKind imageSourceKind) |
| 22 | { |
| 23 | ImageClient client = GetProxiedOpenAIClient<ImageClient>(TestScenario.Images, "dall-e-2"); |
| 24 | string imageFilename = "images_dog_and_cat.png"; |
| 25 | string imagePath = Path.Combine("Assets", imageFilename); |
| 26 | GeneratedImage image = null; |
| 27 | |
| 28 | ImageVariationOptions options = new() |
| 29 | { |
| 30 | ResponseFormat = GeneratedImageFormat.Uri, |
| 31 | Size = GeneratedImageSize.W256xH256 |
| 32 | }; |
| 33 | |
| 34 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 35 | { |
| 36 | using FileStream imageFile = File.OpenRead(imagePath); |
| 37 | |
| 38 | image = await client.GenerateImageVariationAsync(imageFile, imageFilename, options); |
| 39 | } |
| 40 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 41 | { |
| 42 | image = await client.GenerateImageVariationAsync(imagePath, options); |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | Assert.Fail("Invalid source kind."); |
| 47 | } |
| 48 | |
| 49 | Assert.That(image.ImageUri, Is.Not.Null); |
| 50 | Assert.That(image.ImageBytes, Is.Null); |
| 51 | |
| 52 | Console.WriteLine(image.ImageUri.AbsoluteUri); |
| 53 | |
| 54 | await ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal."); |
| 55 | } |
| 56 | |
| 57 | [RecordedTest] |
| 58 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 59 | public async Task GenerateImageVariationWithBytesResponseWorks(ImageSourceKind imageSourceKind) |
| 60 | { |
| 61 | ImageClient client = GetProxiedOpenAIClient<ImageClient>(TestScenario.Images, "dall-e-2"); |
| 62 | string imageFilename = "images_dog_and_cat.png"; |
| 63 | string imagePath = Path.Combine("Assets", imageFilename); |
| 64 | GeneratedImage image = null; |
| 65 | |
| 66 | ImageVariationOptions options = new() |
| 67 | { |
| 68 | ResponseFormat = GeneratedImageFormat.Bytes, |
| 69 | Size = GeneratedImageSize.W256xH256 |
| 70 | }; |
| 71 | |
| 72 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 73 | { |
| 74 | using FileStream imageFile = File.OpenRead(imagePath); |
| 75 | |
| 76 | image = await client.GenerateImageVariationAsync(imageFile, imageFilename, options); |
| 77 | } |
| 78 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 79 | { |
| 80 | image = await client.GenerateImageVariationAsync(imagePath, options); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | Assert.Fail("Invalid source kind."); |
| 85 | } |
| 86 | |
| 87 | Assert.That(image.ImageUri, Is.Null); |
| 88 | Assert.That(image.ImageBytes, Is.Not.Null); |
| 89 | |
| 90 | await ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal."); |
| 91 | } |
| 92 | |
| 93 | [RecordedTest] |
| 94 | public void GenerateImageVariationFromStreamCanParseServiceError() |
| 95 | { |
| 96 | ImageClient client = CreateProxyFromClient(new ImageClient("dall-e-2", new ApiKeyCredential("fake_key"), InstrumentClientOptions(new OpenAIClientOptions()))); |
| 97 | string imageFilename = "images_dog_and_cat.png"; |
| 98 | string imagePath = Path.Combine("Assets", imageFilename); |
| 99 | using FileStream imageFile = File.OpenRead(imagePath); |
| 100 | |
| 101 | ClientResultException ex = null; |
| 102 | |
| 103 | ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationAsync(imageFile, imageFilename)); |
| 104 | Assert.That(ex.Status, Is.EqualTo(401)); |
| 105 | } |
| 106 | |
| 107 | [RecordedTest] |
| 108 | public void GenerateImageVariationFromPathCanParseServiceError() |
| 109 | { |
| 110 | ImageClient client = CreateProxyFromClient(new ImageClient("dall-e-2", new ApiKeyCredential("fake_key"), InstrumentClientOptions(new OpenAIClientOptions()))); |
| 111 | string imageFilename = "images_dog_and_cat.png"; |
| 112 | string imagePath = Path.Combine("Assets", imageFilename); |
| 113 | |
| 114 | ClientResultException ex = null; |
| 115 | |
| 116 | ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationAsync(imagePath)); |
| 117 | Assert.That(ex.Status, Is.EqualTo(401)); |
| 118 | } |
| 119 | |
| 120 | [RecordedTest] |
| 121 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 122 | public async Task GenerateMultipleImageVariationsWorks(ImageSourceKind imageSourceKind) |
| 123 | { |
| 124 | ImageClient client = GetProxiedOpenAIClient<ImageClient>(TestScenario.Images, "dall-e-2"); |
| 125 | string imageFilename = "images_dog_and_cat.png"; |
| 126 | string imagePath = Path.Combine("Assets", imageFilename); |
| 127 | GeneratedImageCollection images = null; |
| 128 | |
| 129 | ImageVariationOptions options = new() |
| 130 | { |
| 131 | ResponseFormat = GeneratedImageFormat.Uri, |
| 132 | Size = GeneratedImageSize.W256xH256 |
| 133 | }; |
| 134 | |
| 135 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 136 | { |
| 137 | using FileStream imageFile = File.OpenRead(imagePath); |
| 138 | |
| 139 | images = await client.GenerateImageVariationsAsync(imageFile, imageFilename, 2, options); |
| 140 | } |
| 141 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 142 | { |
| 143 | images = await client.GenerateImageVariationsAsync(imagePath, 2, options); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | Assert.Fail("Invalid source kind."); |
| 148 | } |
| 149 | |
| 150 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 151 | |
| 152 | Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024)); |
| 153 | Assert.That(images.Count, Is.EqualTo(2)); |
| 154 | |
| 155 | foreach (GeneratedImage image in images) |
| 156 | { |
| 157 | Assert.That(image.ImageUri, Is.Not.Null); |
| 158 | Assert.That(image.ImageBytes, Is.Null); |
| 159 | Console.WriteLine(image.ImageUri.AbsoluteUri); |
| 160 | await ValidateGeneratedImage(image.ImageUri, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal."); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | [RecordedTest] |
| 165 | [TestCaseSource(nameof(s_imageSourceKindSource))] |
| 166 | public async Task GenerateMultipleImageVariationsWithBytesResponseWorks(ImageSourceKind imageSourceKind) |
| 167 | { |
| 168 | ImageClient client = GetProxiedOpenAIClient<ImageClient>(TestScenario.Images, "dall-e-2"); |
| 169 | string imageFilename = "images_dog_and_cat.png"; |
| 170 | string imagePath = Path.Combine("Assets", imageFilename); |
| 171 | GeneratedImageCollection images = null; |
| 172 | |
| 173 | ImageVariationOptions options = new() |
| 174 | { |
| 175 | ResponseFormat = GeneratedImageFormat.Bytes, |
| 176 | Size = GeneratedImageSize.W256xH256 |
| 177 | }; |
| 178 | |
| 179 | if (imageSourceKind == ImageSourceKind.UsingStream) |
| 180 | { |
| 181 | using FileStream imageFile = File.OpenRead(imagePath); |
| 182 | |
| 183 | images = await client.GenerateImageVariationsAsync(imageFile, imageFilename, 2, options); |
| 184 | } |
| 185 | else if (imageSourceKind == ImageSourceKind.UsingFilePath) |
| 186 | { |
| 187 | images = await client.GenerateImageVariationsAsync(imagePath, 2, options); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | Assert.Fail("Invalid source kind."); |
| 192 | } |
| 193 | |
| 194 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 195 | |
| 196 | Assert.That(images.CreatedAt.ToUnixTimeSeconds(), Is.GreaterThan(unixTime2024)); |
| 197 | Assert.That(images.Count, Is.EqualTo(2)); |
| 198 | |
| 199 | foreach (GeneratedImage image in images) |
| 200 | { |
| 201 | Assert.That(image.ImageUri, Is.Null); |
| 202 | Assert.That(image.ImageBytes, Is.Not.Null); |
| 203 | await ValidateGeneratedImage(image.ImageBytes, ["cat", "owl", "animal"], "Note that it likely depicts some sort of animal."); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | [RecordedTest] |
| 208 | public void GenerateMultipleImageVariationsFromStreamCanParseServiceError() |
| 209 | { |
| 210 | ImageClient client = CreateProxyFromClient(new ImageClient("dall-e-2", new ApiKeyCredential("fake_key"), InstrumentClientOptions(new OpenAIClientOptions()))); |
| 211 | string imageFilename = "images_dog_and_cat.png"; |
| 212 | string imagePath = Path.Combine("Assets", imageFilename); |
| 213 | using FileStream imageFile = File.OpenRead(imagePath); |
| 214 | |
| 215 | ClientResultException ex = null; |
| 216 | |
| 217 | ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationsAsync(imageFile, imageFilename, 2)); |
| 218 | Assert.That(ex.Status, Is.EqualTo(401)); |
| 219 | } |
| 220 | |
| 221 | [RecordedTest] |
| 222 | public void GenerateMultipleImageVariationsFromPathCanParseServiceError() |
| 223 | { |
| 224 | ImageClient client = CreateProxyFromClient(new ImageClient("dall-e-2", new ApiKeyCredential("fake_key"), InstrumentClientOptions(new OpenAIClientOptions()))); |
| 225 | string imageFilename = "images_dog_and_cat.png"; |
| 226 | string imagePath = Path.Combine("Assets", imageFilename); |
| 227 | |
| 228 | ClientResultException ex = null; |
| 229 | |
| 230 | ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GenerateImageVariationsAsync(imagePath, 2)); |
| 231 | Assert.That(ex.Status, Is.EqualTo(401)); |
| 232 | } |
| 233 | } |