|
17 | 17 | import org.opensearch.common.xcontent.XContentFactory;
|
18 | 18 | import org.opensearch.knn.common.KNNConstants;
|
19 | 19 | import org.opensearch.knn.index.util.KNNEngine;
|
| 20 | +import org.opensearch.knn.training.VectorSpaceInfo; |
20 | 21 |
|
21 | 22 | import java.io.IOException;
|
22 | 23 | import java.util.HashMap;
|
@@ -93,6 +94,52 @@ public void testValidate() throws IOException {
|
93 | 94 | assertNull(knnMethod.validate(knnMethodContext3));
|
94 | 95 | }
|
95 | 96 |
|
| 97 | + /** |
| 98 | + * Test KNNMethod validateWithData |
| 99 | + */ |
| 100 | + public void testValidateWithData() throws IOException { |
| 101 | + String methodName = "test-method"; |
| 102 | + KNNMethod knnMethod = KNNMethod.Builder.builder(MethodComponent.Builder.builder(methodName).build()) |
| 103 | + .addSpaces(SpaceType.L2) |
| 104 | + .build(); |
| 105 | + |
| 106 | + VectorSpaceInfo testVectorSpaceInfo = new VectorSpaceInfo(4); |
| 107 | + |
| 108 | + // Invalid space |
| 109 | + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() |
| 110 | + .startObject() |
| 111 | + .field(NAME, methodName) |
| 112 | + .field(METHOD_PARAMETER_SPACE_TYPE, SpaceType.INNER_PRODUCT.getValue()) |
| 113 | + .endObject(); |
| 114 | + Map<String, Object> in = xContentBuilderToMap(xContentBuilder); |
| 115 | + KNNMethodContext knnMethodContext1 = KNNMethodContext.parse(in); |
| 116 | + assertNotNull(knnMethod.validateWithData(knnMethodContext1, testVectorSpaceInfo)); |
| 117 | + |
| 118 | + // Invalid methodComponent |
| 119 | + xContentBuilder = XContentFactory.jsonBuilder() |
| 120 | + .startObject() |
| 121 | + .field(NAME, methodName) |
| 122 | + .field(METHOD_PARAMETER_SPACE_TYPE, SpaceType.L2.getValue()) |
| 123 | + .startObject(PARAMETERS) |
| 124 | + .field("invalid", "invalid") |
| 125 | + .endObject() |
| 126 | + .endObject(); |
| 127 | + in = xContentBuilderToMap(xContentBuilder); |
| 128 | + KNNMethodContext knnMethodContext2 = KNNMethodContext.parse(in); |
| 129 | + |
| 130 | + assertNotNull(knnMethod.validateWithData(knnMethodContext2, testVectorSpaceInfo)); |
| 131 | + |
| 132 | + // Valid everything |
| 133 | + xContentBuilder = XContentFactory.jsonBuilder() |
| 134 | + .startObject() |
| 135 | + .field(NAME, methodName) |
| 136 | + .field(METHOD_PARAMETER_SPACE_TYPE, SpaceType.L2.getValue()) |
| 137 | + .endObject(); |
| 138 | + in = xContentBuilderToMap(xContentBuilder); |
| 139 | + KNNMethodContext knnMethodContext3 = KNNMethodContext.parse(in); |
| 140 | + assertNull(knnMethod.validateWithData(knnMethodContext3, testVectorSpaceInfo)); |
| 141 | + } |
| 142 | + |
96 | 143 | public void testGetAsMap() {
|
97 | 144 | SpaceType spaceType = SpaceType.DEFAULT;
|
98 | 145 | String methodName = "test-method";
|
|
0 commit comments