Skip to content

Commit 297a5c7

Browse files
committed
add more tests
1 parent bb1f2b2 commit 297a5c7

21 files changed

+685
-51
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/action/alibabacloudsearch/AlibabaCloudSearchEmbeddingsAction.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
package org.elasticsearch.xpack.inference.external.action.alibabacloudsearch;
99

10-
import org.apache.logging.log4j.LogManager;
11-
import org.apache.logging.log4j.Logger;
1210
import org.elasticsearch.ElasticsearchException;
1311
import org.elasticsearch.action.ActionListener;
1412
import org.elasticsearch.core.TimeValue;

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/request/alibabacloudsearch/AlibabaCloudSearchEmbeddingsRequestEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
5353

5454
// default for testing
5555
static String covertToString(InputType inputType) {
56-
if(inputType == null) {
56+
if (inputType == null) {
5757
return null;
5858
}
5959

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/request/alibabacloudsearch/AlibabaCloudSearchSparseRequestEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public record AlibabaCloudSearchSparseRequestEntity(
2121
AlibabaCloudSearchSparseTaskSettings taskSettings,
2222
@Nullable String model
2323
) implements ToXContentObject {
24+
2425
private static final String TEXTS_FIELD = "input";
2526

2627
static final String INPUT_TYPE_FIELD = "input_type";

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/response/alibabacloudsearch/AlibabaCloudSearchSparseResponseEntity.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,32 @@ public class AlibabaCloudSearchSparseResponseEntity extends AlibabaCloudSearchRe
5353
* "index": 0,
5454
* "embedding": [
5555
* {
56-
* "tokenId": 6,
56+
* "token_id": 6,
5757
* "weight": 0.1014404296875,
5858
* "token": ""
5959
* },
6060
* {
61-
* "tokenId": 163040,
61+
* "token_id": 163040,
6262
* "weight": 0.2841796875,
6363
* "token": "科学技术"
6464
* },
6565
* {
66-
* "tokenId": 354,
66+
* "token_id": 354,
6767
* "weight": 0.1431884765625,
6868
* "token": "是"
6969
* },
7070
* {
71-
* "tokenId": 5998,
71+
* "token_id": 5998,
7272
* "weight": 0.1614990234375,
7373
* "token": "第一"
7474
* },
7575
* {
76-
* "tokenId": 8550,
76+
* "token_id": 8550,
7777
* "weight": 0.239013671875,
7878
* "token": "生产"
7979
* },
8080
* {
81-
* "tokenId": 2017,
81+
* "token_id": 2017,
8282
* "weight": 0.161376953125,
8383
* "token": "力"
8484
* }
@@ -88,27 +88,27 @@ public class AlibabaCloudSearchSparseResponseEntity extends AlibabaCloudSearchRe
8888
* "index": 1,
8989
* "embedding": [
9090
* {
91-
* "tokenId": 9803,
91+
* "token_id": 9803,
9292
* "weight": 0.1949462890625,
9393
* "token": "open"
9494
* },
9595
* {
96-
* "tokenId": 86250,
96+
* "token_id": 86250,
9797
* "weight": 0.317138671875,
9898
* "token": "search"
9999
* },
100100
* {
101-
* "tokenId": 5889,
101+
* "token_id": 5889,
102102
* "weight": 0.175048828125,
103103
* "token": "产品"
104104
* },
105105
* {
106-
* "tokenId": 2564,
106+
* "token_id": 2564,
107107
* "weight": 0.1163330078125,
108108
* "token": "文"
109109
* },
110110
* {
111-
* "tokenId": 59529,
111+
* "token_id": 59529,
112112
* "weight": 0.16650390625,
113113
* "token": "档"
114114
* }

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/alibabacloudsearch/embeddings/AlibabaCloudSearchEmbeddingsTaskSettings.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public class AlibabaCloudSearchEmbeddingsTaskSettings implements TaskSettings {
4141
(InputType) null
4242
);
4343
static final String INPUT_TYPE = "input_type";
44-
static final EnumSet<InputType> VALID_REQUEST_VALUES = EnumSet.of(
45-
InputType.INGEST,
46-
InputType.SEARCH
47-
);
44+
static final EnumSet<InputType> VALID_REQUEST_VALUES = EnumSet.of(InputType.INGEST, InputType.SEARCH);
4845

4946
public static AlibabaCloudSearchEmbeddingsTaskSettings fromMap(Map<String, Object> map) {
5047
if (map == null || map.isEmpty()) {

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/alibabacloudsearch/rerank/AlibabaCloudSearchRerankTaskSettings.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,16 @@
99

1010
import org.elasticsearch.TransportVersion;
1111
import org.elasticsearch.TransportVersions;
12-
import org.elasticsearch.common.Strings;
1312
import org.elasticsearch.common.ValidationException;
1413
import org.elasticsearch.common.io.stream.StreamInput;
1514
import org.elasticsearch.common.io.stream.StreamOutput;
16-
import org.elasticsearch.core.Nullable;
17-
import org.elasticsearch.inference.InputType;
18-
import org.elasticsearch.inference.ModelConfigurations;
1915
import org.elasticsearch.inference.TaskSettings;
2016
import org.elasticsearch.xcontent.XContentBuilder;
2117

2218
import java.io.IOException;
2319
import java.util.Map;
2420
import java.util.Objects;
2521

26-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalBoolean;
27-
import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalPositiveInteger;
28-
2922
/**
3023
* Defines the task settings for the AlibabaCloudSearch rerank service.
3124
*
@@ -72,8 +65,7 @@ public AlibabaCloudSearchRerankTaskSettings(StreamInput in) {
7265
this();
7366
}
7467

75-
public AlibabaCloudSearchRerankTaskSettings() {
76-
}
68+
public AlibabaCloudSearchRerankTaskSettings() {}
7769

7870
@Override
7971
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
@@ -93,8 +85,7 @@ public TransportVersion getMinimalSupportedVersion() {
9385
}
9486

9587
@Override
96-
public void writeTo(StreamOutput out) throws IOException {
97-
}
88+
public void writeTo(StreamOutput out) throws IOException {}
9889

9990
@Override
10091
public boolean equals(Object o) {

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/alibabacloudsearch/sparse/AlibabaCloudSearchSparseTaskSettings.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public class AlibabaCloudSearchSparseTaskSettings implements TaskSettings {
4141
public static final AlibabaCloudSearchSparseTaskSettings EMPTY_SETTINGS = new AlibabaCloudSearchSparseTaskSettings(null, null);
4242
static final String INPUT_TYPE = "input_type";
4343
static final String RETURN_TOKEN = "return_token";
44-
static final EnumSet<InputType> VALID_REQUEST_VALUES = EnumSet.of(
45-
InputType.INGEST,
46-
InputType.SEARCH
47-
);
44+
static final EnumSet<InputType> VALID_REQUEST_VALUES = EnumSet.of(InputType.INGEST, InputType.SEARCH);
4845

4946
public static AlibabaCloudSearchSparseTaskSettings fromMap(Map<String, Object> map) {
5047
if (map == null || map.isEmpty()) {

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/InputTypeTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ public class InputTypeTests extends ESTestCase {
1414
public static InputType randomWithoutUnspecified() {
1515
return randomFrom(InputType.INGEST, InputType.SEARCH, InputType.CLUSTERING, InputType.CLASSIFICATION);
1616
}
17+
18+
public static InputType randomWithIngestAndSearch() {
19+
return randomFrom(InputType.INGEST, InputType.SEARCH);
20+
}
1721
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/request/alibabacloudsearch/AlibabaCloudSearchEmbeddingsRequestEntityTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ public void testXContent_WritesNoOptionalFields_WhenTheyAreNotDefined() throws I
5151
MatcherAssert.assertThat(xContentResult, is("""
5252
{"input":["abc"]}"""));
5353
}
54+
55+
public void testConvertToString_ThrowsAssertionFailure_WhenInputTypeIsUnspecified() {
56+
var thrownException = expectThrows(
57+
AssertionError.class,
58+
() -> AlibabaCloudSearchEmbeddingsRequestEntity.covertToString(InputType.UNSPECIFIED)
59+
);
60+
MatcherAssert.assertThat(thrownException.getMessage(), is("received invalid input type value [unspecified]"));
61+
}
5462
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/external/request/alibabacloudsearch/AlibabaCloudSearchEmbeddingsRequestTests.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hamcrest.MatcherAssert;
2121

2222
import java.io.IOException;
23-
import java.net.URISyntaxException;
2423
import java.util.List;
2524
import java.util.Map;
2625

@@ -30,13 +29,13 @@
3029
import static org.hamcrest.Matchers.is;
3130

3231
public class AlibabaCloudSearchEmbeddingsRequestTests extends ESTestCase {
33-
public void testCreateRequest() throws URISyntaxException, IOException {
32+
public void testCreateRequest() throws IOException {
3433
var request = createRequest(
3534
List.of("abc"),
3635
AlibabaCloudSearchEmbeddingsModelTests.createModel(
3736
"embedding_test",
3837
TaskType.TEXT_EMBEDDING,
39-
AlibabaCloudSearchEmbeddingsServiceSettingsTests.getServiceSettingsMap("embeddings_test", "host", "default"),
38+
AlibabaCloudSearchEmbeddingsServiceSettingsTests.getServiceSettingsMap(null, "embeddings_test", "host", "default"),
4039
AlibabaCloudSearchEmbeddingsTaskSettingsTests.getTaskSettingsMap(null),
4140
getSecretSettingsMap("secret")
4241
)
@@ -57,8 +56,32 @@ public void testCreateRequest() throws URISyntaxException, IOException {
5756
MatcherAssert.assertThat(requestMap, is(Map.of("input", List.of("abc"))));
5857
}
5958

60-
public static AlibabaCloudSearchEmbeddingsRequest createRequest(List<String> input, AlibabaCloudSearchEmbeddingsModel model)
61-
throws URISyntaxException {
59+
public void testCreateRequest_UrlDefined() throws IOException {
60+
var request = createRequest(
61+
List.of("abc"),
62+
AlibabaCloudSearchEmbeddingsModelTests.createModel(
63+
"embedding_test",
64+
TaskType.TEXT_EMBEDDING,
65+
AlibabaCloudSearchEmbeddingsServiceSettingsTests.getServiceSettingsMap("url", "embeddings_test", "host", "default"),
66+
AlibabaCloudSearchEmbeddingsTaskSettingsTests.getTaskSettingsMap(null),
67+
getSecretSettingsMap("secret")
68+
)
69+
);
70+
71+
var httpRequest = request.createHttpRequest();
72+
MatcherAssert.assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class));
73+
74+
var httpPost = (HttpPost) httpRequest.httpRequestBase();
75+
76+
MatcherAssert.assertThat(httpPost.getURI().toString(), is("url"));
77+
MatcherAssert.assertThat(httpPost.getLastHeader(HttpHeaders.CONTENT_TYPE).getValue(), is(XContentType.JSON.mediaType()));
78+
MatcherAssert.assertThat(httpPost.getLastHeader(HttpHeaders.AUTHORIZATION).getValue(), is("Bearer secret"));
79+
80+
var requestMap = entityAsMap(httpPost.getEntity().getContent());
81+
MatcherAssert.assertThat(requestMap, is(Map.of("input", List.of("abc"))));
82+
}
83+
84+
public static AlibabaCloudSearchEmbeddingsRequest createRequest(List<String> input, AlibabaCloudSearchEmbeddingsModel model) {
6285
var account = new AlibabaCloudSearchAccount(
6386
model.getServiceSettings().getCommonSettings().getUri(),
6487
model.getSecretSettings().apiKey()

0 commit comments

Comments
 (0)