Skip to content

Commit c29ee56

Browse files
committed
Test both JSON and typed parameters because they are accessed differently.
1 parent c25d3e2 commit c29ee56

File tree

5 files changed

+178
-66
lines changed

5 files changed

+178
-66
lines changed

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/groovy/ChatCompletionServiceTest.groovy

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,36 @@ class ChatCompletionServiceTest extends OpenAiTest {
2020

2121
def "create chat/completion test"() {
2222
ChatCompletion resp = runUnderTrace("parent") {
23-
openAiClient.chat().completions().create(chatCompletionCreateParams())
23+
openAiClient.chat().completions().create(params)
2424
}
2525

2626
expect:
2727
resp != null
2828
and:
2929
assertChatCompletionTrace(false)
30+
31+
where:
32+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
3033
}
3134

3235
def "create chat/completion test withRawResponse"() {
3336
HttpResponseFor<ChatCompletion> resp = runUnderTrace("parent") {
34-
openAiClient.chat().withRawResponse().completions().create(chatCompletionCreateParams())
37+
openAiClient.chat().withRawResponse().completions().create(params)
3538
}
3639

3740
expect:
3841
resp.statusCode() == 200
3942
resp.parse().valid // force response parsing, so it sets all the tags
4043
and:
4144
assertChatCompletionTrace(false)
45+
46+
where:
47+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
4248
}
4349

4450
def "create streaming chat/completion test"() {
4551
runnableUnderTrace("parent") {
46-
StreamResponse<ChatCompletionChunk> streamCompletion = openAiClient.chat().completions().createStreaming(chatCompletionCreateParams())
52+
StreamResponse<ChatCompletionChunk> streamCompletion = openAiClient.chat().completions().createStreaming(params)
4753
try (Stream stream = streamCompletion.stream()) {
4854
stream.forEach {
4955
// consume the stream
@@ -53,11 +59,14 @@ class ChatCompletionServiceTest extends OpenAiTest {
5359

5460
expect:
5561
assertChatCompletionTrace(true)
62+
63+
where:
64+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
5665
}
5766

5867
def "create streaming chat/completion test withRawResponse"() {
5968
runnableUnderTrace("parent") {
60-
HttpResponseFor<StreamResponse<ChatCompletionChunk>> streamCompletion = openAiClient.chat().completions().withRawResponse().createStreaming(chatCompletionCreateParams())
69+
HttpResponseFor<StreamResponse<ChatCompletionChunk>> streamCompletion = openAiClient.chat().completions().withRawResponse().createStreaming(params)
6170
try (Stream stream = streamCompletion.parse().stream()) {
6271
stream.forEach {
6372
// consume the stream
@@ -67,46 +76,58 @@ class ChatCompletionServiceTest extends OpenAiTest {
6776

6877
expect:
6978
assertChatCompletionTrace(true)
79+
80+
where:
81+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
7082
}
7183

7284
def "create async chat/completion test"() {
7385
CompletableFuture<ChatCompletion> completionFuture = runUnderTrace("parent") {
74-
openAiClient.async().chat().completions().create(chatCompletionCreateParams())
86+
openAiClient.async().chat().completions().create(params)
7587
}
7688

7789
completionFuture.get()
7890

7991
expect:
8092
assertChatCompletionTrace(false)
93+
94+
where:
95+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
8196
}
8297

8398
def "create async chat/completion test withRawResponse"() {
8499
CompletableFuture<HttpResponseFor<Completion>> completionFuture = runUnderTrace("parent") {
85-
openAiClient.async().chat().completions().withRawResponse().create(chatCompletionCreateParams())
100+
openAiClient.async().chat().completions().withRawResponse().create(params)
86101
}
87102

88103
def resp = completionFuture.get()
89104
resp.parse().valid // force response parsing, so it sets all the tags
90105

91106
expect:
92107
assertChatCompletionTrace(false)
108+
109+
where:
110+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
93111
}
94112

95113
def "create streaming async chat/completion test"() {
96114
AsyncStreamResponse<ChatCompletionChunk> asyncResp = runUnderTrace("parent") {
97-
openAiClient.async().chat().completions().createStreaming(chatCompletionCreateParams())
115+
openAiClient.async().chat().completions().createStreaming(params)
98116
}
99117
asyncResp.subscribe {
100118
// consume completions
101119
}
102120
asyncResp.onCompleteFuture().get()
103121
expect:
104122
assertChatCompletionTrace(true)
123+
124+
where:
125+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
105126
}
106127

107128
def "create streaming async chat/completion test withRawResponse"() {
108129
CompletableFuture<HttpResponseFor<StreamResponse<ChatCompletionChunk>>> future = runUnderTrace("parent") {
109-
openAiClient.async().chat().completions().withRawResponse().createStreaming(chatCompletionCreateParams())
130+
openAiClient.async().chat().completions().withRawResponse().createStreaming(params)
110131
}
111132
HttpResponseFor<StreamResponse<ChatCompletionChunk>> resp = future.get()
112133
try (Stream stream = resp.parse().stream()) {
@@ -117,6 +138,9 @@ class ChatCompletionServiceTest extends OpenAiTest {
117138
expect:
118139
resp.statusCode() == 200
119140
assertChatCompletionTrace(true)
141+
142+
where:
143+
params << [chatCompletionCreateParams(false), chatCompletionCreateParams(true)]
120144
}
121145

122146
def "create chat/completion test with tool calls"() {

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/groovy/CompletionServiceTest.groovy

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,35 @@ import java.util.stream.Stream
1414
class CompletionServiceTest extends OpenAiTest {
1515

1616
def "create completion test"() {
17-
Completion resp = runUnderTrace("parent") {
18-
openAiClient.completions().create(completionCreateParams())
17+
runUnderTrace("parent") {
18+
openAiClient.completions().create(params)
1919
}
2020

2121
expect:
22-
resp != null
23-
and:
2422
assertCompletionTrace(false)
23+
24+
where:
25+
params << [completionCreateParams(true), completionCreateParams(false)]
2526
}
2627

2728
def "create completion test withRawResponse"() {
2829
HttpResponseFor<Completion> resp = runUnderTrace("parent") {
29-
openAiClient.withRawResponse().completions().create(completionCreateParams())
30+
openAiClient.withRawResponse().completions().create(params)
3031
}
3132

3233
expect:
3334
resp.statusCode() == 200
3435
resp.parse().valid // force response parsing, so it sets all the tags
3536
and:
3637
assertCompletionTrace(false)
38+
39+
where:
40+
params << [completionCreateParams(true), completionCreateParams(false)]
3741
}
3842

3943
def "create streaming completion test"() {
4044
runnableUnderTrace("parent") {
41-
StreamResponse<Completion> streamCompletion = openAiClient.completions().createStreaming(completionCreateParams())
45+
StreamResponse<Completion> streamCompletion = openAiClient.completions().createStreaming(params)
4246
try (Stream stream = streamCompletion.stream()) {
4347
stream.forEach {
4448
// consume the stream
@@ -48,11 +52,14 @@ class CompletionServiceTest extends OpenAiTest {
4852

4953
expect:
5054
assertCompletionTrace(true)
55+
56+
where:
57+
params << [completionCreateParams(true), completionCreateParams(false)]
5158
}
5259

5360
def "create streaming completion test withRawResponse"() {
5461
runnableUnderTrace("parent") {
55-
HttpResponseFor<StreamResponse<Completion>> streamCompletion = openAiClient.completions().withRawResponse().createStreaming(completionCreateParams())
62+
HttpResponseFor<StreamResponse<Completion>> streamCompletion = openAiClient.completions().withRawResponse().createStreaming(params)
5663
try (Stream stream = streamCompletion.parse().stream()) {
5764
stream.forEach {
5865
// consume the stream
@@ -62,46 +69,58 @@ class CompletionServiceTest extends OpenAiTest {
6269

6370
expect:
6471
assertCompletionTrace(true)
72+
73+
where:
74+
params << [completionCreateParams(true), completionCreateParams(false)]
6575
}
6676

6777
def "create async completion test"() {
6878
CompletableFuture<Completion> completionFuture = runUnderTrace("parent") {
69-
openAiClient.async().completions().create(completionCreateParams())
79+
openAiClient.async().completions().create(params)
7080
}
7181

7282
completionFuture.get()
7383

7484
expect:
7585
assertCompletionTrace(false)
86+
87+
where:
88+
params << [completionCreateParams(true), completionCreateParams(false)]
7689
}
7790

7891
def "create async completion test withRawResponse"() {
7992
CompletableFuture<HttpResponseFor<Completion>> completionFuture = runUnderTrace("parent") {
80-
openAiClient.async().completions().withRawResponse().create(completionCreateParams())
93+
openAiClient.async().completions().withRawResponse().create(params)
8194
}
8295

8396
def resp = completionFuture.get()
8497
resp.parse().valid // force response parsing, so it sets all the tags
8598

8699
expect:
87100
assertCompletionTrace(false)
101+
102+
where:
103+
params << [completionCreateParams(true), completionCreateParams(false)]
88104
}
89105

90106
def "create streaming async completion test"() {
91107
AsyncStreamResponse<Completion> asyncResp = runUnderTrace("parent") {
92-
openAiClient.async().completions().createStreaming(completionCreateParams())
108+
openAiClient.async().completions().createStreaming(params)
93109
}
94110
asyncResp.subscribe {
95111
// consume completions
96112
}
97113
asyncResp.onCompleteFuture().get()
98114
expect:
99115
assertCompletionTrace(true)
116+
117+
where:
118+
params << [completionCreateParams(true), completionCreateParams(false)]
100119
}
101120

102121
def "create streaming async completion test withRawResponse"() {
103122
CompletableFuture<HttpResponseFor<StreamResponse<Completion>>> future = runUnderTrace("parent") {
104-
openAiClient.async().completions().withRawResponse().createStreaming(completionCreateParams())
123+
openAiClient.async().completions().withRawResponse().createStreaming(params)
105124
}
106125
HttpResponseFor<StreamResponse<Completion>> resp = future.get()
107126
try (Stream stream = resp.parse().stream()) {
@@ -112,6 +131,9 @@ class CompletionServiceTest extends OpenAiTest {
112131
expect:
113132
resp.statusCode() == 200
114133
assertCompletionTrace(true)
134+
135+
where:
136+
params << [completionCreateParams(true), completionCreateParams(false)]
115137
}
116138

117139
private void assertCompletionTrace(boolean isStreaming) {

dd-java-agent/instrumentation/openai-java/openai-java-3.0/src/test/groovy/EmbeddingServiceTest.groovy

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ class EmbeddingServiceTest extends OpenAiTest {
1111

1212
def "create embedding test"() {
1313
CreateEmbeddingResponse resp = runUnderTrace("parent") {
14-
openAiClient.embeddings().create(embeddingCreateParams())
14+
openAiClient.embeddings().create(params)
1515
}
1616

1717
expect:
1818
resp != null
1919
and:
2020
assertEmbeddingTrace()
21+
22+
where:
23+
params << [embeddingCreateParams(false), embeddingCreateParams(true)]
2124
}
2225

2326
def "create embedding test withRawResponse"() {
2427
HttpResponseFor<CreateEmbeddingResponse> resp = runUnderTrace("parent") {
25-
openAiClient.embeddings().withRawResponse().create(embeddingCreateParams())
28+
openAiClient.embeddings().withRawResponse().create(params)
2629
}
2730

2831
expect:
@@ -31,6 +34,9 @@ class EmbeddingServiceTest extends OpenAiTest {
3134
resp.parse().valid // force response parsing, so it sets all the tags
3235
and:
3336
assertEmbeddingTrace()
37+
38+
where:
39+
params << [embeddingCreateParams(false), embeddingCreateParams(true)]
3440
}
3541

3642
private void assertEmbeddingTrace() {

0 commit comments

Comments
 (0)