summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorGeir Storli <geirst@verizonmedia.com>2019-02-07 13:08:57 +0100
committerGeir Storli <geirst@verizonmedia.com>2019-02-07 13:08:57 +0100
commit1163b817e7b9d7cd9d69a29ae387245c87fba20a (patch)
treec0d4917bbbd3d5c76973b2dffcf7431fe45ab8bc /document
parent7dd4287830ec74a054e1251ae7b63b53481ae8af (diff)
Move roundtrip serialization tests for TensorModifyUpdate to DocumentUpdateJsonSerializerTest.
Diffstat (limited to 'document')
-rw-r--r--document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java22
-rw-r--r--document/src/test/java/com/yahoo/document/update/SerializationTestCase.java73
2 files changed, 21 insertions, 74 deletions
diff --git a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java
index 5f486e9a670..65b51ad34b8 100644
--- a/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java
+++ b/document/src/test/java/com/yahoo/document/json/DocumentUpdateJsonSerializerTest.java
@@ -296,7 +296,7 @@ public class DocumentUpdateJsonSerializerTest {
}
@Test
- public void test_tensor_modify_update() {
+ public void test_tensor_modify_update_on_dense_tensor() {
roundtripSerializeJsonAndMatch(inputJson(
"{",
" 'update': 'DOCUMENT_ID',",
@@ -316,6 +316,26 @@ public class DocumentUpdateJsonSerializerTest {
}
@Test
+ public void test_tensor_modify_update_on_sparse_tensor() {
+ roundtripSerializeJsonAndMatch(inputJson(
+ "{",
+ " 'update': 'DOCUMENT_ID',",
+ " 'fields': {",
+ " 'sparse_tensor': {",
+ " 'modify': {",
+ " 'operation': 'add',",
+ " 'cells': [",
+ " { 'address': { 'x': 'a', 'y': 'b' }, 'value': 2.0 },",
+ " { 'address': { 'x': 'c', 'y': 'd' }, 'value': 3.0 }",
+ " ]",
+ " }",
+ " }",
+ " }",
+ "}"
+ ));
+ }
+
+ @Test
public void test_tensor_add_update() {
roundtripSerializeJsonAndMatch(inputJson(
"{",
diff --git a/document/src/test/java/com/yahoo/document/update/SerializationTestCase.java b/document/src/test/java/com/yahoo/document/update/SerializationTestCase.java
index 104c77ea923..2fc1692d575 100644
--- a/document/src/test/java/com/yahoo/document/update/SerializationTestCase.java
+++ b/document/src/test/java/com/yahoo/document/update/SerializationTestCase.java
@@ -27,20 +27,12 @@ public class SerializationTestCase {
private DocumentType documentType;
private Field field;
- private final static TensorType sparseTensorType = new TensorType.Builder().mapped("x").mapped("y").build();
- private final static TensorType denseTensorType = new TensorType.Builder().indexed("x", 2).indexed("y", 3).build();
- private Field sparseTensorField;
- private Field denseTensorField;
@Before
public void setUp() {
documentType = new DocumentType("document1");
field = new Field("field1", DataType.getArray(DataType.STRING));
documentType.addField(field);
- sparseTensorField = new Field("sparse_tensor", new TensorDataType(sparseTensorType));
- denseTensorField = new Field("dense_tensor", new TensorDataType(denseTensorType));
- documentType.addField(sparseTensorField);
- documentType.addField(denseTensorField);
}
@Test
@@ -73,69 +65,4 @@ public class SerializationTestCase {
assertEquals("'field1' [clear]", deserializedUpdate.toString());
}
- @Test
- public void test_tensor_modify_update_serialization_with_dense_tensor() {
- String tensorString = "{{x:1,y:2}:2}";
- FieldUpdate update = createTensorModifyUpdate(denseTensorField, denseTensorType, tensorString);
-
- FieldUpdate deserializedUpdate = roundtripSerialize(update);
- TensorModifyUpdate modifyUpdate = expectTensorModifyUpdate(deserializedUpdate, "dense_tensor");
-
- assertEquals(TensorModifyUpdate.Operation.REPLACE, modifyUpdate.getOperation());
- assertEquals(TensorType.fromSpec("tensor(x{},y{})"), modifyUpdate.getValue().getDataType().getTensorType());
- assertEquals(createTensor(sparseTensorType, tensorString), modifyUpdate.getValue());
- assertEquals(update, deserializedUpdate);
- }
-
- @Test
- public void test_tensor_modify_update_serialization_with_sparse_tensor() {
- String tensorString = "{{x:a,y:b}:2}";
- FieldUpdate update = createTensorModifyUpdate(sparseTensorField, sparseTensorType, tensorString);
-
- FieldUpdate deserializedUpdate = roundtripSerialize(update);
- TensorModifyUpdate modifyUpdate = expectTensorModifyUpdate(deserializedUpdate, "sparse_tensor");
-
- assertEquals(TensorModifyUpdate.Operation.REPLACE, modifyUpdate.getOperation());
- assertEquals(TensorType.fromSpec("tensor(x{},y{})"), modifyUpdate.getValue().getDataType().getTensorType());
- assertEquals(createTensor(sparseTensorType, tensorString), modifyUpdate.getValue());
- assertEquals(update, deserializedUpdate);
- }
-
- private static FieldUpdate createTensorModifyUpdate(Field tensorField, TensorType tensorType, String tensorString) {
- FieldUpdate result = new FieldUpdate(tensorField);
- // Note that the tensor type is converted to only have mapped dimensions.
- TensorFieldValue tensor = createTensor(TensorModifyUpdate.convertToCompatibleType(tensorType), tensorString);
- result.addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, tensor));
- return result;
- }
-
- private static TensorFieldValue createTensor(TensorType type, String tensorCellString) {
- return new TensorFieldValue(Tensor.from(type, tensorCellString));
- }
-
- private static GrowableByteBuffer serializeUpdate(FieldUpdate update) {
- DocumentSerializer buffer = DocumentSerializerFactory.createHead(new GrowableByteBuffer());
- update.serialize(buffer);
- buffer.getBuf().rewind();
- return buffer.getBuf();
- }
-
- private FieldUpdate deserializeUpdate(GrowableByteBuffer buffer) {
- return new FieldUpdate(DocumentDeserializerFactory.createHead(new DocumentTypeManager(), buffer), documentType, Document.SERIALIZED_VERSION);
- }
-
- private FieldUpdate roundtripSerialize(FieldUpdate update) {
- GrowableByteBuffer buffer = serializeUpdate(update);
- return deserializeUpdate(buffer);
- }
-
- private static TensorModifyUpdate expectTensorModifyUpdate(FieldUpdate update, String tensorFieldName) {
- assertEquals(tensorFieldName, update.getField().getName());
- assertEquals(1, update.getValueUpdates().size());
- ValueUpdate valueUpdate = update.getValueUpdate(0);
- if (!(valueUpdate instanceof TensorModifyUpdate)) {
- throw new IllegalStateException("Expected tensorModifyUpdate");
- }
- return (TensorModifyUpdate)valueUpdate;
- }
}