Skip to content

Commit af24a54

Browse files
committed
Add more UTs
Signed-off-by: zane-neo <zaniu@amazon.com>
1 parent ccd7d88 commit af24a54

File tree

6 files changed

+248
-5
lines changed

6 files changed

+248
-5
lines changed

src/main/java/org/opensearch/neuralsearch/processor/InferenceProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private void validateEmbeddingFieldsValue(IngestDocument ingestDocument) {
211211
Map<String, Object> sourceAndMetadataMap = ingestDocument.getSourceAndMetadata();
212212
String indexName = sourceAndMetadataMap.get(IndexFieldMapper.NAME).toString();
213213
ProcessorDocumentUtils.validateMapTypeValue(
214-
"field_map",
214+
FIELD_MAP_FIELD,
215215
sourceAndMetadataMap,
216216
fieldMap,
217217
1,

src/main/java/org/opensearch/neuralsearch/processor/TextChunkingProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public IngestDocument execute(final IngestDocument ingestDocument) {
166166
Map<String, Object> sourceAndMetadataMap = ingestDocument.getSourceAndMetadata();
167167
String indexName = sourceAndMetadataMap.get(IndexFieldMapper.NAME).toString();
168168
ProcessorDocumentUtils.validateMapTypeValue(
169-
"field_map",
169+
FIELD_MAP_FIELD,
170170
sourceAndMetadataMap,
171171
fieldMap,
172172
1,

src/main/java/org/opensearch/neuralsearch/processor/TextImageEmbeddingProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private void validateEmbeddingFieldsValue(final IngestDocument ingestDocument) {
174174
Map<String, Object> sourceAndMetadataMap = ingestDocument.getSourceAndMetadata();
175175
String indexName = sourceAndMetadataMap.get(IndexFieldMapper.NAME).toString();
176176
ProcessorDocumentUtils.validateMapTypeValue(
177-
"field_map",
177+
FIELD_MAP_FIELD,
178178
sourceAndMetadataMap,
179179
fieldMap,
180180
1,

src/test/java/org/opensearch/neuralsearch/processor/TextChunkingProcessorTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
import java.util.List;
1414
import java.util.Locale;
1515
import java.util.Map;
16-
import java.util.Objects;
1716
import java.util.Set;
1817

1918
import static java.util.Collections.singletonList;
2019
import static java.util.Collections.singletonMap;
2120
import static org.mockito.ArgumentMatchers.anyString;
22-
import static org.mockito.ArgumentMatchers.eq;
2321
import static org.mockito.Mockito.when;
2422
import static org.mockito.Mockito.mock;
2523

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.opensearch.neuralsearch.util;
6+
7+
import org.junit.Before;
8+
import org.mockito.Mock;
9+
import org.mockito.MockitoAnnotations;
10+
import org.opensearch.cluster.service.ClusterService;
11+
import org.opensearch.common.settings.Settings;
12+
import org.opensearch.common.xcontent.XContentHelper;
13+
import org.opensearch.common.xcontent.XContentType;
14+
import org.opensearch.env.Environment;
15+
import org.opensearch.neuralsearch.query.OpenSearchQueryTestCase;
16+
17+
import java.io.IOException;
18+
import java.net.URISyntaxException;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.util.Map;
22+
23+
import static org.mockito.ArgumentMatchers.anyString;
24+
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
27+
28+
public class ProcessorDocumentUtilsTests extends OpenSearchQueryTestCase {
29+
30+
private ClusterService clusterService = mock(ClusterService.class, RETURNS_DEEP_STUBS);
31+
32+
@Mock
33+
private Environment environment;
34+
35+
@Before
36+
public void setup() {
37+
MockitoAnnotations.openMocks(this);
38+
}
39+
40+
public void test_with_different_configurations() throws URISyntaxException, IOException {
41+
Settings settings = Settings.builder().put("index.mapping.depth.limit", 20).build();
42+
when(clusterService.state().metadata().index(anyString()).getSettings()).thenReturn(settings);
43+
String processorDocumentTestJson = Files.readString(
44+
Path.of(ProcessorDocumentUtils.class.getClassLoader().getResource("util/ProcessorDocumentUtils.json").toURI())
45+
);
46+
Map<String, Object> processorDocumentTestMap = XContentHelper.convertToMap(
47+
XContentType.JSON.xContent(),
48+
processorDocumentTestJson,
49+
false
50+
);
51+
for (Map.Entry<String, Object> entry : processorDocumentTestMap.entrySet()) {
52+
String testCaseName = entry.getKey();
53+
Map<String, Object> metadata = (Map<String, Object>) entry.getValue();
54+
55+
Map<String, Object> fieldMap = (Map<String, Object>) metadata.get("field_map");
56+
Map<String, Object> source = (Map<String, Object>) metadata.get("source");
57+
Map<String, Object> expectation = (Map<String, Object>) metadata.get("expectation");
58+
try {
59+
ProcessorDocumentUtils.validateMapTypeValue(
60+
"field_map",
61+
source,
62+
fieldMap,
63+
1,
64+
"test_index",
65+
clusterService,
66+
environment,
67+
false
68+
);
69+
} catch (Exception e) {
70+
if (expectation != null) {
71+
if (expectation.containsKey("type")) {
72+
assertEquals("test case: " + testCaseName + " failed", expectation.get("type"), e.getClass().getSimpleName());
73+
}
74+
if (expectation.containsKey("message")) {
75+
assertEquals("test case: " + testCaseName + " failed", expectation.get("message"), e.getMessage());
76+
}
77+
} else {
78+
fail("test case: " + testCaseName + " failed: " + e.getMessage());
79+
}
80+
}
81+
}
82+
}
83+
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
{
2+
"simpleMapConfiguration": {
3+
"field_map": {
4+
"body": "body_embedding"
5+
},
6+
"source": {
7+
"body": "This is a test body."
8+
}
9+
},
10+
"doublyMapConfiguration": {
11+
"field_map": {
12+
"passage": {
13+
"body": "body_embedding"
14+
}
15+
},
16+
"source": {
17+
"passage": {
18+
"body": "This is a test body."
19+
}
20+
}
21+
},
22+
"mapWithNestedConfiguration": {
23+
"field_map": {
24+
"passage": {
25+
"bodies": "bodies_embedding"
26+
}
27+
},
28+
"source": {
29+
"passage": {
30+
"bodies": ["test body 1", "test body 2", "test body 3"]
31+
}
32+
}
33+
},
34+
"nestedConfiguration": {
35+
"field_map": {
36+
"bodies": "bodies_embedding"
37+
},
38+
"source": {
39+
"bodies": ["test body 1", "test body 2", "test body 3"]
40+
}
41+
},
42+
"nestedWithMapConfiguration": {
43+
"field_map": {
44+
"bodies": {
45+
"body": "body_embedding"
46+
}
47+
},
48+
"source": {
49+
"bodies": [
50+
{
51+
"body": "This is a test body.",
52+
"seq": 1
53+
},
54+
{
55+
"body": "This is another test body.",
56+
"seq": 2
57+
}]
58+
}
59+
},
60+
"sourceMapFieldNotMapConfiguration": {
61+
"field_map": {
62+
"passage": "passage_embedding"
63+
},
64+
"source": {
65+
"passage": {
66+
"body": "This is a test body."
67+
}
68+
},
69+
"expectation": {
70+
"type": "IllegalArgumentException",
71+
"message": "[passage] configuration doesn't match actual value type, configuration type is: java.lang.String, actual value type is: java.util.HashMap"
72+
}
73+
},
74+
"sourceMapTypeHasNonNestedNonStringConfiguration": {
75+
"field_map": {
76+
"passage": {
77+
"body": "body_embedding"
78+
}
79+
},
80+
"source": {
81+
"passage": {
82+
"body": 12345
83+
}
84+
},
85+
"expectation": {
86+
"type": "IllegalArgumentException",
87+
"message": "map type field [body] is neither string nor nested type, cannot process it"
88+
}
89+
},
90+
"sourceMapTypeHasEmptyStringConfiguration": {
91+
"field_map": {
92+
"passage": {
93+
"body": "body_embedding"
94+
}
95+
},
96+
"source": {
97+
"passage": {
98+
"body": ""
99+
}
100+
},
101+
"expectation": {
102+
"type": "IllegalArgumentException",
103+
"message": "map type field [body] has empty string value, cannot process it"
104+
}
105+
},
106+
"sourceListTypeHasNullConfiguration": {
107+
"field_map": {
108+
"bodies": "bodies_embedding"
109+
},
110+
"source": {
111+
"bodies": ["This is a test", null, "This is another test"]
112+
},
113+
"expectation": {
114+
"type": "IllegalArgumentException",
115+
"message": "list type field [bodies] has null, cannot process it"
116+
}
117+
},
118+
"sourceListTypeHasEmptyConfiguration": {
119+
"field_map": {
120+
"bodies": "bodies_embedding"
121+
},
122+
"source": {
123+
"bodies": ["This is a test", "", "This is another test"]
124+
},
125+
"expectation": {
126+
"type": "IllegalArgumentException",
127+
"message": "list type field [bodies] has empty string, cannot process it"
128+
}
129+
},
130+
"sourceListTypeHasNonStringConfiguration": {
131+
"field_map": {
132+
"bodies": "bodies_embedding"
133+
},
134+
"source": {
135+
"bodies": ["This is a test", 1, "This is another test"]
136+
},
137+
"expectation": {
138+
"type": "IllegalArgumentException",
139+
"message": "list type field [bodies] has non string value, cannot process it"
140+
}
141+
},
142+
"sourceDoublyListTypeConfiguration": {
143+
"field_map": {
144+
"bodies": "bodies_embedding"
145+
},
146+
"source": {
147+
"bodies": [
148+
[
149+
"This is a test"
150+
],
151+
[
152+
"This is another tetst"
153+
]
154+
]
155+
},
156+
"expectation": {
157+
"type": "IllegalArgumentException",
158+
"message": "list type field [bodies] is nested list type, cannot process it"
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)