-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: John Mazanec <[email protected]>
- Loading branch information
1 parent
91365c9
commit 1471b4e
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...ava/org/opensearch/knn/index/codec/KNN9120Codec/DerivedSourceStoredFieldsWriterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec.KNN9120Codec; | ||
|
||
import lombok.SneakyThrows; | ||
import org.apache.lucene.codecs.StoredFieldsWriter; | ||
import org.apache.lucene.index.FieldInfo; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.core.xcontent.MediaTypeRegistry; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.knn.KNNTestCase; | ||
import org.opensearch.knn.index.codec.KNNCodecTestUtil; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
public class DerivedSourceStoredFieldsWriterTests extends KNNTestCase { | ||
|
||
@SneakyThrows | ||
public void testWriteField() { | ||
StoredFieldsWriter delegate = mock(StoredFieldsWriter.class); | ||
FieldInfo fieldInfo = KNNCodecTestUtil.FieldInfoBuilder.builder("_source").build(); | ||
List<String> fields = List.of("test"); | ||
|
||
DerivedSourceStoredFieldsWriter derivedSourceStoredFieldsWriter = new DerivedSourceStoredFieldsWriter(delegate, fields); | ||
|
||
Map<String, Object> source = Map.of("test", new float[] { 1.0f, 2.0f, 3.0f }, "text_field", "text_value"); | ||
BytesStreamOutput bStream = new BytesStreamOutput(); | ||
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.JSON, bStream).map(source); | ||
builder.close(); | ||
byte[] originalBytes = bStream.bytes().toBytesRef().bytes; | ||
byte[] shiftedBytes = new byte[originalBytes.length + 2]; | ||
System.arraycopy(originalBytes, 0, shiftedBytes, 1, originalBytes.length); | ||
derivedSourceStoredFieldsWriter.writeField(fieldInfo, new BytesRef(shiftedBytes, 1, originalBytes.length)); | ||
} | ||
} |