aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ForEachTestCase.java
blob: 4d77721a3f6e39b663505874f740c0b0089e9538 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.document.StructDataType;
import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.vespa.indexinglanguage.SimpleTestAdapter;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerify;
import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

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

    @Test
    public void requireThatAccessorsWork() {
        Expression innerExp = new AttributeExpression("foo");
        ForEachExpression exp = new ForEachExpression(innerExp);
        assertSame(innerExp, exp.getInnerExpression());
    }

    @Test
    public void requireThatHashCodeAndEqualsAreImplemented() {
        Expression innerExp = new AttributeExpression("foo");
        Expression exp = new ForEachExpression(innerExp);
        assertFalse(exp.equals(new Object()));
        assertFalse(exp.equals(new ForEachExpression(new AttributeExpression("bar"))));
        assertEquals(exp, new ForEachExpression(innerExp));
        assertEquals(exp.hashCode(), new ForEachExpression(innerExp).hashCode());
    }

    @Test
    public void requireThatExpressionCanBeVerified() {
        Expression exp = new ForEachExpression(SimpleExpression.newConversion(DataType.INT, DataType.STRING));
        assertVerify(DataType.getArray(DataType.INT), exp, DataType.getArray(DataType.STRING));
        assertVerifyThrows(null, exp, "Expected any input, but no input is specified");
        assertVerifyThrows(DataType.INT, exp, "Expected Array, Struct or WeightedSet input, got int");
        assertVerifyThrows(DataType.getArray(DataType.STRING), exp, "Expected int input, got string");
    }

    @Test
    public void requireThatStructFieldCompatibilityIsVerified() {
        StructDataType type = new StructDataType("my_struct");
        type.addField(new Field("foo", DataType.INT));
        assertVerify(type, new ForEachExpression(new SimpleExpression()), type);
        assertVerifyThrows(type, new ForEachExpression(SimpleExpression.newConversion(DataType.STRING, DataType.INT)),
                           "Expected string input, got int");
        assertVerifyThrows(type, new ForEachExpression(SimpleExpression.newConversion(DataType.INT, DataType.STRING)),
                           "Expected int output, got string");
    }

    @Test
    public void requireThatEachTokenIsExecutedSeparately() {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        Array<StringFieldValue> arr = new Array<>(DataType.getArray(DataType.STRING));
        arr.add(new StringFieldValue("6"));
        arr.add(new StringFieldValue("9"));
        ctx.setValue(arr);

        MyCollector exp = new MyCollector();
        new ForEachExpression(exp).execute(ctx);

        assertEquals(2, exp.lst.size());

        FieldValue val = exp.lst.get(0);
        assertTrue(val instanceof StringFieldValue);
        assertEquals("6", ((StringFieldValue)val).getString());

        val = exp.lst.get(1);
        assertTrue(val instanceof StringFieldValue);
        assertEquals("9", ((StringFieldValue)val).getString());
    }

    @Test
    public void requireThatCreatedOutputTypeDependsOnInnerExpression() {
        assertNull(new ForEachExpression(new SimpleExpression()).createdOutputType());
        assertNotNull(new ForEachExpression(new ConstantExpression(new IntegerFieldValue(69))).createdOutputType());
    }

    @Test
    public void requireThatArrayCanBeConverted() {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        Array<StringFieldValue> before = new Array<>(DataType.getArray(DataType.STRING));
        before.add(new StringFieldValue("6"));
        before.add(new StringFieldValue("9"));
        ctx.setValue(before);

        new ForEachExpression(new ToIntegerExpression()).execute(ctx);
        FieldValue val = ctx.getValue();
        assertTrue(val instanceof Array);

        Array after = (Array)val;
        assertEquals(2, after.size());
        assertEquals(new IntegerFieldValue(6), after.get(0));
        assertEquals(new IntegerFieldValue(9), after.get(1));
    }

    @Test
    public void requireThatEmptyArrayCanBeConverted() {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        ctx.setValue(new Array<StringFieldValue>(DataType.getArray(DataType.STRING)));

        new ForEachExpression(new ToIntegerExpression()).execute(ctx);

        FieldValue val = ctx.getValue();
        assertTrue(val instanceof Array);
        assertEquals(DataType.INT, ((Array)val).getDataType().getNestedType());
        assertTrue(((Array)val).isEmpty());
    }

    @Test
    public void requireThatIllegalInputValueThrows() {
        try {
            new ForEachExpression(new SimpleExpression()).execute(new StringFieldValue("foo"));
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Expected Array, Struct or WeightedSet input, got string", e.getMessage());
        }
    }

    @Test
    public void requireThatArrayWithNullCanBeConverted() {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        Array<StringFieldValue> arr = new Array<>(DataType.getArray(DataType.STRING));
        arr.add(new StringFieldValue("foo"));
        ctx.setValue(arr);

        new ForEachExpression(SimpleExpression.newConversion(DataType.STRING, DataType.INT)
                                              .setExecuteValue(null)).execute(ctx);

        FieldValue val = ctx.getValue();
        assertTrue(val instanceof Array);
        assertEquals(DataType.INT, ((Array)val).getDataType().getNestedType());
        assertTrue(((Array)val).isEmpty());
    }

    @Test
    public void requireThatWsetCanBeConverted() {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        WeightedSet<StringFieldValue> before = new WeightedSet<>(DataType.getWeightedSet(DataType.STRING));
        before.put(new StringFieldValue("6"), 9);
        before.put(new StringFieldValue("9"), 6);
        ctx.setValue(before);

        new ForEachExpression(new ToIntegerExpression()).execute(ctx);
        FieldValue val = ctx.getValue();
        assertTrue(val instanceof WeightedSet);

        WeightedSet after = (WeightedSet)val;
        assertEquals(2, after.size());
        assertEquals(Integer.valueOf(9), after.get(new IntegerFieldValue(6)));
        assertEquals(Integer.valueOf(6), after.get(new IntegerFieldValue(9)));
    }

    @Test
    public void requireThatEmptyWsetCanBeConverted() {
        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        ctx.setValue(new WeightedSet<StringFieldValue>(DataType.getWeightedSet(DataType.STRING)));

        new ForEachExpression(new ToIntegerExpression()).execute(ctx);

        FieldValue val = ctx.getValue();
        assertTrue(val instanceof WeightedSet);
        assertEquals(DataType.INT, ((WeightedSet)val).getDataType().getNestedType());
        assertTrue(((WeightedSet)val).isEmpty());
    }

    @Test
    public void requireThatStructContentCanBeConverted() {
        StructDataType type = new StructDataType("my_type");
        type.addField(new Field("my_str", DataType.STRING));
        Struct struct = new Struct(type);
        struct.setFieldValue("my_str", new StringFieldValue("  foo  "));

        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        ctx.setValue(struct);

        new ForEachExpression(new TrimExpression()).execute(ctx);

        FieldValue val = ctx.getValue();
        assertTrue(val instanceof Struct);
        assertEquals(type, val.getDataType());
        assertEquals(new StringFieldValue("foo"), ((Struct)val).getFieldValue("my_str"));
    }

    @Test
    public void requireThatIncompatibleStructFieldsFailToValidate() {
        StructDataType type = new StructDataType("my_type");
        type.addField(new Field("my_int", DataType.INT));

        VerificationContext ctx = new VerificationContext(new SimpleTestAdapter());
        ctx.setValueType(type);

        try {
            new ForEachExpression(new ToArrayExpression()).verify(ctx);
            fail();
        } catch (VerificationException e) {
            assertEquals("Expected int output, got Array<int>", e.getMessage());
        }
    }

    @Test
    public void requireThatIncompatibleStructFieldsFailToExecute() {
        StructDataType type = new StructDataType("my_type");
        type.addField(new Field("my_int", DataType.INT));
        Struct struct = new Struct(type);
        struct.setFieldValue("my_int", new IntegerFieldValue(69));

        ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
        ctx.setValue(struct);

        try {
            new ForEachExpression(new ToArrayExpression()).execute(ctx);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Class class com.yahoo.document.datatypes.Array not applicable to an class " +
                         "com.yahoo.document.datatypes.IntegerFieldValue instance.", e.getMessage());
        }
    }

    private static class MyCollector extends Expression {

        List<FieldValue> lst = new LinkedList<>();

        MyCollector() {
            super(null);
        }
        @Override
        protected void doExecute(ExecutionContext context) {
            lst.add(context.getValue());
        }

        @Override
        protected void doVerify(VerificationContext context) {

        }

        @Override
        public DataType createdOutputType() {
            return null;
        }
    }
}