aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ValueUpdateToDocumentTestCase.java
blob: 9c92c282f7fe269a0c597bc14db31d9ab5b5b14c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.*;
import com.yahoo.document.datatypes.*;
import com.yahoo.document.update.ValueUpdate;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author Simon Thoresen Hult
 */
@SuppressWarnings({ "rawtypes" })
public class ValueUpdateToDocumentTestCase {

    @Test
    public void requireThatIntegerFieldsAreConverted() {
        DocumentType docType = new DocumentType("my_type");
        Field field = new Field("my_int", DataType.INT);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createAssign(new IntegerFieldValue(42));
        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        assertEquals(42, ((IntegerFieldValue)doc.getFieldValue("my_int")).getInteger());
    }


    @Test
    public void requireThatClearValueUpdatesAreConverted() {
        DocumentType docType = new DocumentType("my_type");
        Field field = new Field("my_int", DataType.INT);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createClear();
        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        assertNotNull(doc.getFieldValue("my_int"));
        assertEquals(new IntegerFieldValue(), doc.getFieldValue("my_int"));
    }


    @Test
    public void requireThatStringFieldsAreConverted() {
        DocumentType docType = new DocumentType("my_type");
        Field field = new Field("my_str", DataType.STRING);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createAssign(new StringFieldValue("42"));
        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        assertEquals("42", ((StringFieldValue)doc.getFieldValue("my_str")).getString());
    }

    @SuppressWarnings({ "unchecked" })
    @Test
    public void requireThatArrayFieldsAreConverted() {
        DocumentType docType = new DocumentType("my_type");
        ArrayDataType arrType = DataType.getArray(DataType.INT);
        Field field = new Field("my_arr", arrType);
        docType.addField(field);

        Array<IntegerFieldValue> arrVal = arrType.createFieldValue();
        arrVal.add(new IntegerFieldValue(6));
        arrVal.add(new IntegerFieldValue(9));
        ValueUpdate update = ValueUpdate.createAssign(arrVal);

        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        FieldValue obj = doc.getFieldValue("my_arr");
        assertTrue(obj instanceof Array);
        Array arr = (Array)obj;
        assertEquals(2, arr.size());
        assertEquals(new IntegerFieldValue(6), arr.get(0));
        assertEquals(new IntegerFieldValue(9), arr.get(1));
    }

    @Test
    public void requireThatWsetKeysAreConverted() {
        DocumentType docType = new DocumentType("my_type");
        WeightedSetDataType wsetType = DataType.getWeightedSet(DataType.STRING);
        Field field = new Field("my_wset", wsetType);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createMap(new StringFieldValue("69"), ValueUpdate.createAssign(new IntegerFieldValue(96)));

        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        FieldValue obj = doc.getFieldValue("my_wset");
        assertTrue(obj instanceof WeightedSet);
        WeightedSet wset = (WeightedSet)obj;
        assertEquals(1, wset.size());
        assertEquals(96, wset.get(new StringFieldValue("69")).intValue());
    }

    @Test
    public void requireThatNestedStructsAreConverted() {
        DocumentType docType = new DocumentType("my_type");
        StructDataType structType = new StructDataType("my_struct");
        structType.addField(new Field("b", DataType.INT));
        Field field = new Field("a", structType);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createMap(new StringFieldValue("b"), ValueUpdate.createAssign(new IntegerFieldValue(42)));
        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        FieldValue obj = doc.getFieldValue("a");
        assertTrue(obj instanceof Struct);
        Struct struct = (Struct)obj;
        assertEquals(new IntegerFieldValue(42), struct.getFieldValue("b"));
    }

    @SuppressWarnings({ "unchecked" })
    @Test
    public void requireThatAddIsConverted() {
        DocumentType docType = new DocumentType("my_type");
        ArrayDataType arrType = DataType.getArray(DataType.INT);
        Field field = new Field("my_arr", arrType);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createMap(new IntegerFieldValue(0), ValueUpdate.createAdd(new IntegerFieldValue(6)));

        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        FieldValue obj = doc.getFieldValue("my_arr");
        assertTrue(obj instanceof Array);
        Array arr = (Array)obj;
        assertEquals(1, arr.size());
        assertEquals(new IntegerFieldValue(6), arr.get(0));
    }

    @Test
    public void array_of_struct_assign_is_converted() {
        DocumentType docType = new DocumentType("my_type");
        StructDataType structType = new StructDataType("my_struct");
        structType.addField(new Field("a", DataType.INT));
        ArrayDataType arrType = DataType.getArray(structType);
        Field field = new Field("my_arr", arrType);
        docType.addField(field);

        var updatedStruct = new Struct(structType);
        updatedStruct.setFieldValue("a", new IntegerFieldValue(42));
        ValueUpdate update = ValueUpdate.createMap(new IntegerFieldValue(2), ValueUpdate.createAssign(updatedStruct));

        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);
        // Due to how the roller coaster ride of the partial documents appear to work, we end up creating
        // a document with an array that only contains the to-be-updated element, but always at the first
        // index rather than the arbitrary updated index (which is good because otherwise it'd have to
        // be pre-allocated).
        FieldValue obj = doc.getFieldValue("my_arr");
        assertTrue(obj instanceof Array);
        Array arr = (Array)obj;
        assertEquals(1, arr.size());
        assertEquals(updatedStruct, arr.get(0));
    }

    @Test
    public void requireThatRemoveIsConverted() {
        DocumentType docType = new DocumentType("my_type");
        ArrayDataType arrType = DataType.getArray(DataType.INT);
        Field field = new Field("my_arr", arrType);
        docType.addField(field);

        ValueUpdate update = ValueUpdate.createClear();

        Document doc = FieldUpdateHelper.newPartialDocument(docType, new DocumentId("id:foo:my_type::1"), field, update);
        assertNotNull(doc);

        FieldValue obj = doc.getFieldValue("my_arr");
        assertTrue(obj instanceof Array);
        Array arr = (Array)obj;
        assertEquals(0, arr.size());
    }
}