-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathImageGenerationEndpointTests.cs
162 lines (137 loc) · 5.35 KB
/
ImageGenerationEndpointTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using NUnit.Framework;
using OpenAI_API.Images;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenAI_Tests
{
public class ImageGenerationEndpointTests
{
[SetUp]
public void Setup()
{
OpenAI_API.APIAuthentication.Default = new OpenAI_API.APIAuthentication(Environment.GetEnvironmentVariable("TEST_OPENAI_SECRET_KEY"));
}
[TestCase(null)]
[TestCase("dall-e-2")]
[TestCase("dall-e-3")]
public void SimpleImageCreation(string model)
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.ImageGenerations);
var results = api.ImageGenerations.CreateImageAsync("A drawing of a computer writing a test", model).Result;
Assert.IsNotNull(results);
if (results.CreatedUnixTime.HasValue)
{
Assert.NotZero(results.CreatedUnixTime.Value);
Assert.NotNull(results.Created);
Assert.Greater(results.Created.Value, new DateTime(2023, 1, 1));
Assert.Less(results.Created.Value, DateTime.Now.AddDays(1));
}
else
{
Assert.Null(results.Created);
}
Assert.NotZero(results.Data.Count);
Assert.AreEqual(results.Data.Count, 1);
Assert.NotNull(results.Data.First().Url);
Assert.That(results.Data.First().Url.Length > 0);
Assert.That(results.Data.First().Url.StartsWith("https://"));
}
[TestCase("256x256")]
[TestCase("512x512")]
[TestCase("1024x1024")]
public void CreateDALLE2ImageWithUrl(string size)
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.ImageGenerations);
var results = api.ImageGenerations.CreateImageAsync(new ImageGenerationRequest("A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art", 2, new ImageSize(size))).Result;
Assert.IsNotNull(results);
if (results.CreatedUnixTime.HasValue)
{
Assert.NotZero(results.CreatedUnixTime.Value);
Assert.NotNull(results.Created);
Assert.Greater(results.Created.Value, new DateTime(2023, 1, 1));
Assert.Less(results.Created.Value, DateTime.Now.AddDays(1));
}
else
{
Assert.Null(results.Created);
}
Assert.NotZero(results.Data.Count);
Assert.AreEqual(results.Data.Count, 2);
Assert.NotNull(results.Data.First().Url);
Assert.That(results.Data.First().Url.Length > 0);
Assert.That(results.Data.First().Url.StartsWith("https://"));
}
[Test]
public void CreateDALLE2ImageBase64Enc()
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.ImageGenerations);
var results = api.ImageGenerations.CreateImageAsync(new ImageGenerationRequest("A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art", 1, ImageSize._256, responseFormat: ImageResponseFormat.B64_json)).Result;
Assert.IsNotNull(results);
if (results.CreatedUnixTime.HasValue)
{
Assert.NotZero(results.CreatedUnixTime.Value);
Assert.NotNull(results.Created);
Assert.Greater(results.Created.Value, new DateTime(2023, 1, 1));
Assert.Less(results.Created.Value, DateTime.Now.AddDays(1));
}
else
{
Assert.Null(results.Created);
}
Assert.NotZero(results.Data.Count);
Assert.NotNull(results.Data.First().Base64Data);
Assert.That(results.Data.First().Base64Data.Length > 0);
}
[TestCase("standard", "1024x1024")]
[TestCase("hd", "1024x1024")]
[TestCase("standard", "1024x1792")]
[TestCase("standard", "1792x1024")]
public void CreateDALLE3ImageWithUrl(string quality, string size)
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.ImageGenerations);
var results = api.ImageGenerations.CreateImageAsync(new ImageGenerationRequest("A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art", OpenAI_API.Models.Model.DALLE3, new ImageSize(size), quality)).Result;
Assert.IsNotNull(results);
if (results.CreatedUnixTime.HasValue)
{
Assert.NotZero(results.CreatedUnixTime.Value);
Assert.NotNull(results.Created);
Assert.Greater(results.Created.Value, new DateTime(2023, 1, 1));
Assert.Less(results.Created.Value, DateTime.Now.AddDays(1));
}
else
{
Assert.Null(results.Created);
}
Assert.NotZero(results.Data.Count);
Assert.NotNull(results.Data.First().Url);
Assert.That(results.Data.First().Url.Length > 0);
Assert.That(results.Data.First().Url.StartsWith("https://"));
}
[TestCase("dall-e-2", "hd", "1024x1024")]
[TestCase("dall-e-2", "invalid-quality", "1024x1024")]
[TestCase("dall-e-2", "standard", "1024x1792")]
[TestCase("dall-e-3", "standard", "256x256")]
[TestCase("dall-e-3", "invalid-quality", "256x256")]
public void BadParameterCombosShouldFail(string model, string quality, string size)
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.ImageGenerations);
Assert.ThrowsAsync<ArgumentException>(async () => await api.ImageGenerations.CreateImageAsync(new ImageGenerationRequest("A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art", model, new ImageSize(size), quality)));
}
[Test]
public void BadNumImagesWithDalle3ShouldFail()
{
var api = new OpenAI_API.OpenAIAPI();
Assert.IsNotNull(api.ImageGenerations);
var req = new ImageGenerationRequest("A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art", OpenAI_API.Models.Model.DALLE3);
req.NumOfImages = 2;
Assert.ThrowsAsync<Newtonsoft.Json.JsonSerializationException>(async () => await api.ImageGenerations.CreateImageAsync(req));
}
}
}