aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ChoiceTestCase.java
blob: e6d5c550e931bcd1a5de05c916b37862da2cee09 (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
// Copyright Yahoo. 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.Document;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.LongFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.language.Linguistics;
import com.yahoo.language.process.Embedder;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.vespa.indexinglanguage.ExpressionSearcher;
import com.yahoo.vespa.indexinglanguage.SimpleTestAdapter;
import com.yahoo.vespa.indexinglanguage.parser.ParseException;
import com.yahoo.yolean.Exceptions;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class ChoiceTestCase {

    @Test
    public void testChoiceExecution() {
        var choice = new ChoiceExpression(new InputExpression("foo"), new InputExpression("bar"));

        {   // foo only
            var adapter = new SimpleTestAdapter(new Field("foo", DataType.STRING));
            adapter.setValue("foo", new StringFieldValue("foo1"));
            ExecutionContext context = new ExecutionContext(adapter);
            choice.execute(context);
            assertEquals("foo1", context.getValue().getWrappedValue());
        }

        {   // bar only
            var adapter = new SimpleTestAdapter(new Field("bar", DataType.STRING));
            adapter.setValue("bar", new StringFieldValue("bar1"));
            ExecutionContext context = new ExecutionContext(adapter);
            choice.execute(context);
            assertEquals("bar1", context.getValue().getWrappedValue());
        }

        {   // both foo and bar
            var adapter = new SimpleTestAdapter(new Field("foo", DataType.STRING), new Field("bar", DataType.STRING));
            adapter.setValue("foo", new StringFieldValue("foo1"));
            adapter.setValue("bar", new StringFieldValue("bar1"));
            choice.verify(adapter);
            ExecutionContext context = new ExecutionContext(adapter);
            choice.execute(context);
            assertEquals("foo1", context.getValue().getWrappedValue());
        }
    }

    @Test
    public void testChoiceWithConstant() throws ParseException {
        var choice = parse("input timestamp || 99999999L | attribute timestamp");

        { // value is set
            var adapter = new SimpleTestAdapter(new Field("timestamp", DataType.LONG));
            choice.verify(adapter);
            adapter.setValue("timestamp", new LongFieldValue(34));
            ExecutionContext context = new ExecutionContext(adapter);
            choice.execute(context);
            assertEquals(34L, context.getValue().getWrappedValue());
        }

        { // fallback to default
            var adapter = new SimpleTestAdapter(new Field("timestamp", DataType.LONG));
            choice.verify(adapter);
            ExecutionContext context = new ExecutionContext(adapter);
            choice.execute(context);
            assertEquals(99999999L, context.getValue().getWrappedValue());
        }
    }

    @Test
    public void testIllegalChoiceExpression() throws ParseException {
        try {
            parse("input (foo || 99999999) | attribute");
        }
        catch (IllegalArgumentException e) {
            assertEquals("'input' must be given a field name as argument", Exceptions.toMessageString(e));
        }
    }

    @Test
    public void testInnerConvert() throws ParseException {
        var expression = parse("(input foo || 99999999) | attribute");
        new ExpressionSearcher<>(AttributeExpression.class).searchIn(expression); // trigger innerConvert
    }

    private static Expression parse(String s) throws ParseException {
        return Expression.fromString(s, new SimpleLinguistics(), Embedder.throwsOnUse.asMap());
    }

}