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