Skip to content

Commit 23e7595

Browse files
committed
Add ElasticInferenceServiceDenseTextEmbeddingsRequestEntityTests
1 parent 3e8c70a commit 23e7595

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.inference.services.elastic.request;
9+
10+
import org.elasticsearch.common.Strings;
11+
import org.elasticsearch.test.ESTestCase;
12+
import org.elasticsearch.xcontent.XContentBuilder;
13+
import org.elasticsearch.xcontent.XContentFactory;
14+
import org.elasticsearch.xcontent.XContentType;
15+
import org.elasticsearch.xpack.inference.services.elastic.ElasticInferenceServiceUsageContext;
16+
17+
import java.io.IOException;
18+
import java.util.List;
19+
20+
import static org.elasticsearch.xpack.inference.MatchersUtils.equalToIgnoringWhitespaceInJsonString;
21+
22+
public class ElasticInferenceServiceDenseTextEmbeddingsRequestEntityTests extends ESTestCase {
23+
24+
public void testToXContent_SingleInput_UnspecifiedUsageContext() throws IOException {
25+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
26+
List.of("abc"),
27+
"my-model-id",
28+
ElasticInferenceServiceUsageContext.UNSPECIFIED
29+
);
30+
String xContentString = xContentEntityToString(entity);
31+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
32+
{
33+
"input": ["abc"],
34+
"model": "my-model-id"
35+
}"""));
36+
}
37+
38+
public void testToXContent_MultipleInputs_UnspecifiedUsageContext() throws IOException {
39+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
40+
List.of("abc", "def"),
41+
"my-model-id",
42+
ElasticInferenceServiceUsageContext.UNSPECIFIED
43+
);
44+
String xContentString = xContentEntityToString(entity);
45+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
46+
{
47+
"input": [
48+
"abc",
49+
"def"
50+
],
51+
"model": "my-model-id"
52+
}
53+
"""));
54+
}
55+
56+
public void testToXContent_SingleInput_SearchUsageContext() throws IOException {
57+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
58+
List.of("abc"),
59+
"my-model-id",
60+
ElasticInferenceServiceUsageContext.SEARCH
61+
);
62+
String xContentString = xContentEntityToString(entity);
63+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
64+
{
65+
"input": ["abc"],
66+
"model": "my-model-id",
67+
"usage_context": "search"
68+
}
69+
"""));
70+
}
71+
72+
public void testToXContent_SingleInput_IngestUsageContext() throws IOException {
73+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
74+
List.of("abc"),
75+
"my-model-id",
76+
ElasticInferenceServiceUsageContext.INGEST
77+
);
78+
String xContentString = xContentEntityToString(entity);
79+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
80+
{
81+
"input": ["abc"],
82+
"model": "my-model-id",
83+
"usage_context": "ingest"
84+
}
85+
"""));
86+
}
87+
88+
public void testToXContent_MultipleInputs_SearchUsageContext() throws IOException {
89+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
90+
List.of("first input", "second input", "third input"),
91+
"my-dense-model",
92+
ElasticInferenceServiceUsageContext.SEARCH
93+
);
94+
String xContentString = xContentEntityToString(entity);
95+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
96+
{
97+
"input": [
98+
"first input",
99+
"second input",
100+
"third input"
101+
],
102+
"model": "my-dense-model",
103+
"usage_context": "search"
104+
}
105+
"""));
106+
}
107+
108+
public void testToXContent_MultipleInputs_IngestUsageContext() throws IOException {
109+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
110+
List.of("document one", "document two"),
111+
"embedding-model-v2",
112+
ElasticInferenceServiceUsageContext.INGEST
113+
);
114+
String xContentString = xContentEntityToString(entity);
115+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
116+
{
117+
"input": [
118+
"document one",
119+
"document two"
120+
],
121+
"model": "embedding-model-v2",
122+
"usage_context": "ingest"
123+
}
124+
"""));
125+
}
126+
127+
public void testToXContent_EmptyInput_UnspecifiedUsageContext() throws IOException {
128+
var entity = new ElasticInferenceServiceDenseTextEmbeddingsRequestEntity(
129+
List.of(""),
130+
"my-model-id",
131+
ElasticInferenceServiceUsageContext.UNSPECIFIED
132+
);
133+
String xContentString = xContentEntityToString(entity);
134+
assertThat(xContentString, equalToIgnoringWhitespaceInJsonString("""
135+
{
136+
"input": [""],
137+
"model": "my-model-id"
138+
}
139+
"""));
140+
}
141+
142+
private String xContentEntityToString(ElasticInferenceServiceDenseTextEmbeddingsRequestEntity entity) throws IOException {
143+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
144+
entity.toXContent(builder, null);
145+
return Strings.toString(builder);
146+
}
147+
}

0 commit comments

Comments
 (0)