Skip to content

Commit 3e1766c

Browse files
chore(internal): add async service tests (#253)
chore(internal): improve sync service tests docs: readme parameter tweaks
1 parent 88c8149 commit 3e1766c

40 files changed

+2682
-202
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
3+
package com.openai.services.async
4+
5+
import com.openai.TestServerExtension
6+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync
7+
import com.openai.core.JsonValue
8+
import com.openai.models.BatchCancelParams
9+
import com.openai.models.BatchCreateParams
10+
import com.openai.models.BatchRetrieveParams
11+
import com.openai.models.Metadata
12+
import org.junit.jupiter.api.Test
13+
import org.junit.jupiter.api.extension.ExtendWith
14+
15+
@ExtendWith(TestServerExtension::class)
16+
class BatchServiceAsyncTest {
17+
18+
@Test
19+
fun create() {
20+
val client =
21+
OpenAIOkHttpClientAsync.builder()
22+
.baseUrl(TestServerExtension.BASE_URL)
23+
.apiKey("My API Key")
24+
.build()
25+
val batchServiceAsync = client.batches()
26+
27+
val batchFuture =
28+
batchServiceAsync.create(
29+
BatchCreateParams.builder()
30+
.completionWindow(BatchCreateParams.CompletionWindow._24H)
31+
.endpoint(BatchCreateParams.Endpoint.V1_CHAT_COMPLETIONS)
32+
.inputFileId("input_file_id")
33+
.metadata(
34+
Metadata.builder()
35+
.putAdditionalProperty("foo", JsonValue.from("string"))
36+
.build()
37+
)
38+
.build()
39+
)
40+
41+
val batch = batchFuture.get()
42+
batch.validate()
43+
}
44+
45+
@Test
46+
fun retrieve() {
47+
val client =
48+
OpenAIOkHttpClientAsync.builder()
49+
.baseUrl(TestServerExtension.BASE_URL)
50+
.apiKey("My API Key")
51+
.build()
52+
val batchServiceAsync = client.batches()
53+
54+
val batchFuture =
55+
batchServiceAsync.retrieve(BatchRetrieveParams.builder().batchId("batch_id").build())
56+
57+
val batch = batchFuture.get()
58+
batch.validate()
59+
}
60+
61+
@Test
62+
fun list() {
63+
val client =
64+
OpenAIOkHttpClientAsync.builder()
65+
.baseUrl(TestServerExtension.BASE_URL)
66+
.apiKey("My API Key")
67+
.build()
68+
val batchServiceAsync = client.batches()
69+
70+
val pageFuture = batchServiceAsync.list()
71+
72+
val page = pageFuture.get()
73+
page.response().validate()
74+
}
75+
76+
@Test
77+
fun cancel() {
78+
val client =
79+
OpenAIOkHttpClientAsync.builder()
80+
.baseUrl(TestServerExtension.BASE_URL)
81+
.apiKey("My API Key")
82+
.build()
83+
val batchServiceAsync = client.batches()
84+
85+
val batchFuture =
86+
batchServiceAsync.cancel(BatchCancelParams.builder().batchId("batch_id").build())
87+
88+
val batch = batchFuture.get()
89+
batch.validate()
90+
}
91+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
3+
package com.openai.services.async
4+
5+
import com.openai.TestServerExtension
6+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync
7+
import com.openai.core.JsonValue
8+
import com.openai.models.ChatCompletionStreamOptions
9+
import com.openai.models.CompletionCreateParams
10+
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.extension.ExtendWith
12+
13+
@ExtendWith(TestServerExtension::class)
14+
class CompletionServiceAsyncTest {
15+
16+
@Test
17+
fun create() {
18+
val client =
19+
OpenAIOkHttpClientAsync.builder()
20+
.baseUrl(TestServerExtension.BASE_URL)
21+
.apiKey("My API Key")
22+
.build()
23+
val completionServiceAsync = client.completions()
24+
25+
val completionFuture =
26+
completionServiceAsync.create(
27+
CompletionCreateParams.builder()
28+
.model(CompletionCreateParams.Model.GPT_3_5_TURBO_INSTRUCT)
29+
.prompt("This is a test.")
30+
.bestOf(0L)
31+
.echo(true)
32+
.frequencyPenalty(-2.0)
33+
.logitBias(
34+
CompletionCreateParams.LogitBias.builder()
35+
.putAdditionalProperty("foo", JsonValue.from(0))
36+
.build()
37+
)
38+
.logprobs(0L)
39+
.maxTokens(16L)
40+
.n(1L)
41+
.presencePenalty(-2.0)
42+
.seed(0L)
43+
.stop("\n")
44+
.streamOptions(ChatCompletionStreamOptions.builder().includeUsage(true).build())
45+
.suffix("test.")
46+
.temperature(1.0)
47+
.topP(1.0)
48+
.user("user-1234")
49+
.build()
50+
)
51+
52+
val completion = completionFuture.get()
53+
completion.validate()
54+
}
55+
56+
@Test
57+
fun createStreaming() {
58+
val client =
59+
OpenAIOkHttpClientAsync.builder()
60+
.baseUrl(TestServerExtension.BASE_URL)
61+
.apiKey("My API Key")
62+
.build()
63+
val completionServiceAsync = client.completions()
64+
65+
val completionStreamResponse =
66+
completionServiceAsync.createStreaming(
67+
CompletionCreateParams.builder()
68+
.model(CompletionCreateParams.Model.GPT_3_5_TURBO_INSTRUCT)
69+
.prompt("This is a test.")
70+
.bestOf(0L)
71+
.echo(true)
72+
.frequencyPenalty(-2.0)
73+
.logitBias(
74+
CompletionCreateParams.LogitBias.builder()
75+
.putAdditionalProperty("foo", JsonValue.from(0))
76+
.build()
77+
)
78+
.logprobs(0L)
79+
.maxTokens(16L)
80+
.n(1L)
81+
.presencePenalty(-2.0)
82+
.seed(0L)
83+
.stop("\n")
84+
.streamOptions(ChatCompletionStreamOptions.builder().includeUsage(true).build())
85+
.suffix("test.")
86+
.temperature(1.0)
87+
.topP(1.0)
88+
.user("user-1234")
89+
.build()
90+
)
91+
92+
val onCompleteFuture =
93+
completionStreamResponse
94+
.subscribe { completion -> completion.validate() }
95+
.onCompleteFuture()
96+
onCompleteFuture.get()
97+
}
98+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
3+
package com.openai.services.async
4+
5+
import com.openai.TestServerExtension
6+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync
7+
import com.openai.models.EmbeddingCreateParams
8+
import com.openai.models.EmbeddingModel
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.extension.ExtendWith
11+
12+
@ExtendWith(TestServerExtension::class)
13+
class EmbeddingServiceAsyncTest {
14+
15+
@Test
16+
fun create() {
17+
val client =
18+
OpenAIOkHttpClientAsync.builder()
19+
.baseUrl(TestServerExtension.BASE_URL)
20+
.apiKey("My API Key")
21+
.build()
22+
val embeddingServiceAsync = client.embeddings()
23+
24+
val createEmbeddingResponseFuture =
25+
embeddingServiceAsync.create(
26+
EmbeddingCreateParams.builder()
27+
.input("The quick brown fox jumped over the lazy dog")
28+
.model(EmbeddingModel.TEXT_EMBEDDING_ADA_002)
29+
.dimensions(1L)
30+
.encodingFormat(EmbeddingCreateParams.EncodingFormat.FLOAT)
31+
.user("user-1234")
32+
.build()
33+
)
34+
35+
val createEmbeddingResponse = createEmbeddingResponseFuture.get()
36+
createEmbeddingResponse.validate()
37+
}
38+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
3+
package com.openai.services.async
4+
5+
import com.github.tomakehurst.wiremock.client.WireMock.anyUrl
6+
import com.github.tomakehurst.wiremock.client.WireMock.get
7+
import com.github.tomakehurst.wiremock.client.WireMock.ok
8+
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
9+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
10+
import com.github.tomakehurst.wiremock.junit5.WireMockTest
11+
import com.openai.TestServerExtension
12+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync
13+
import com.openai.models.FileContentParams
14+
import com.openai.models.FileDeleteParams
15+
import com.openai.models.FileRetrieveParams
16+
import org.assertj.core.api.Assertions.assertThat
17+
import org.junit.jupiter.api.Test
18+
import org.junit.jupiter.api.extension.ExtendWith
19+
20+
@ExtendWith(TestServerExtension::class)
21+
@WireMockTest
22+
class FileServiceAsyncTest {
23+
24+
@Test
25+
fun retrieve() {
26+
val client =
27+
OpenAIOkHttpClientAsync.builder()
28+
.baseUrl(TestServerExtension.BASE_URL)
29+
.apiKey("My API Key")
30+
.build()
31+
val fileServiceAsync = client.files()
32+
33+
val fileObjectFuture =
34+
fileServiceAsync.retrieve(FileRetrieveParams.builder().fileId("file_id").build())
35+
36+
val fileObject = fileObjectFuture.get()
37+
fileObject.validate()
38+
}
39+
40+
@Test
41+
fun list() {
42+
val client =
43+
OpenAIOkHttpClientAsync.builder()
44+
.baseUrl(TestServerExtension.BASE_URL)
45+
.apiKey("My API Key")
46+
.build()
47+
val fileServiceAsync = client.files()
48+
49+
val pageFuture = fileServiceAsync.list()
50+
51+
val page = pageFuture.get()
52+
page.response().validate()
53+
}
54+
55+
@Test
56+
fun delete() {
57+
val client =
58+
OpenAIOkHttpClientAsync.builder()
59+
.baseUrl(TestServerExtension.BASE_URL)
60+
.apiKey("My API Key")
61+
.build()
62+
val fileServiceAsync = client.files()
63+
64+
val fileDeletedFuture =
65+
fileServiceAsync.delete(FileDeleteParams.builder().fileId("file_id").build())
66+
67+
val fileDeleted = fileDeletedFuture.get()
68+
fileDeleted.validate()
69+
}
70+
71+
@Test
72+
fun content(wmRuntimeInfo: WireMockRuntimeInfo) {
73+
val client =
74+
OpenAIOkHttpClientAsync.builder()
75+
.baseUrl(wmRuntimeInfo.httpBaseUrl)
76+
.apiKey("My API Key")
77+
.build()
78+
val fileServiceAsync = client.files()
79+
stubFor(get(anyUrl()).willReturn(ok().withBody("abc")))
80+
81+
val responseFuture =
82+
fileServiceAsync.content(FileContentParams.builder().fileId("file_id").build())
83+
84+
val response = responseFuture.get()
85+
assertThat(response.body()).hasContent("abc")
86+
}
87+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
3+
package com.openai.services.async
4+
5+
import com.openai.TestServerExtension
6+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync
7+
import com.openai.models.ImageGenerateParams
8+
import com.openai.models.ImageModel
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.extension.ExtendWith
11+
12+
@ExtendWith(TestServerExtension::class)
13+
class ImageServiceAsyncTest {
14+
15+
@Test
16+
fun generate() {
17+
val client =
18+
OpenAIOkHttpClientAsync.builder()
19+
.baseUrl(TestServerExtension.BASE_URL)
20+
.apiKey("My API Key")
21+
.build()
22+
val imageServiceAsync = client.images()
23+
24+
val imagesResponseFuture =
25+
imageServiceAsync.generate(
26+
ImageGenerateParams.builder()
27+
.prompt("A cute baby sea otter")
28+
.model(ImageModel.DALL_E_2)
29+
.n(1L)
30+
.quality(ImageGenerateParams.Quality.STANDARD)
31+
.responseFormat(ImageGenerateParams.ResponseFormat.URL)
32+
.size(ImageGenerateParams.Size._256X256)
33+
.style(ImageGenerateParams.Style.VIVID)
34+
.user("user-1234")
35+
.build()
36+
)
37+
38+
val imagesResponse = imagesResponseFuture.get()
39+
imagesResponse.validate()
40+
}
41+
}

0 commit comments

Comments
 (0)