aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/GuardTestCase.java
blob: b8f73c14156cf764da8332fbdc317d7fc9d483a0 (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
// 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.*;
import com.yahoo.document.datatypes.LongFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.update.AssignValueUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.language.Language;
import com.yahoo.vespa.indexinglanguage.SimpleAdapterFactory;
import com.yahoo.vespa.indexinglanguage.UpdateAdapter;
import com.yahoo.vespa.indexinglanguage.parser.ParseException;
import org.junit.Test;

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.*;

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

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

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

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

    @Test
    public void requireThatInputFieldsAreIncludedByDocument() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_lng", DataType.LONG));
        docType.addField(new Field("my_str", DataType.STRING));

        Document doc = new Document(docType, "id:scheme:my_input::");
        doc.setFieldValue("my_str", new StringFieldValue("69"));
        assertNotNull(doc = Expression.execute(Expression.fromString("guard { input my_str | to_int | attribute my_lng }"), doc));
        assertEquals(new LongFieldValue(69), doc.getFieldValue("my_lng"));
    }

    @Test
    public void requireThatInputFieldsAreIncludedByUpdate() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_lng", DataType.LONG));
        docType.addField(new Field("my_str", DataType.STRING));

        DocumentUpdate docUpdate = new DocumentUpdate(docType, "id:scheme:my_input::");
        docUpdate.addFieldUpdate(FieldUpdate.createAssign(docType.getField("my_str"), new StringFieldValue("69")));
        assertNotNull(docUpdate = Expression.execute(Expression.fromString("guard { input my_str | to_int | attribute my_lng }"), docUpdate));

        assertEquals(0, docUpdate.fieldPathUpdates().size());
        assertEquals(1, docUpdate.fieldUpdates().size());

        FieldUpdate fieldUpd = docUpdate.fieldUpdates().iterator().next();
        assertNotNull(fieldUpd);
        assertEquals(docType.getField("my_lng"), fieldUpd.getField());
        assertEquals(1, fieldUpd.getValueUpdates().size());

        ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
        assertNotNull(valueUpd);
        assertTrue(valueUpd instanceof AssignValueUpdate);
        assertEquals(new LongFieldValue(69), valueUpd.getValue());
    }

    @Test
    public void requireThatConstFieldsAreIncludedByDocument() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_lng", DataType.LONG));
        docType.addField(new Field("my_str", DataType.STRING));

        Document doc = new Document(docType, "id:scheme:my_input::");
        doc.setFieldValue("my_str", new StringFieldValue("foo"));
        assertNotNull(doc = Expression.execute(Expression.fromString("guard { now | attribute my_lng }"), doc));
        assertTrue(doc.getFieldValue("my_lng") instanceof LongFieldValue);
    }

    @Test
    public void requireThatConstFieldsAreSkippedByUpdate() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_int", DataType.INT));
        docType.addField(new Field("my_str", DataType.STRING));

        DocumentUpdate docUpdate = new DocumentUpdate(docType, "id:scheme:my_input::");
        docUpdate.addFieldUpdate(FieldUpdate.createAssign(docType.getField("my_str"), new StringFieldValue("foo")));
        assertNull(Expression.execute(Expression.fromString("guard { now | attribute my_int }"), docUpdate));
    }

    @Test
    public void requireThatLanguageCanBeSetByUpdate() throws ParseException {
        DocumentType docType = new DocumentType("my_input");
        docType.addField(new Field("my_str", DataType.STRING));
        DocumentUpdate docUpdate = new DocumentUpdate(docType, "id:scheme:my_input::");
        docUpdate.addFieldUpdate(FieldUpdate.createAssign(docType.getField("my_str"), new StringFieldValue("foo")));

        SimpleAdapterFactory factory = new SimpleAdapterFactory();
        List<UpdateAdapter> lst = factory.newUpdateAdapterList(docUpdate);
        assertEquals(1, lst.size());

        ExecutionContext ctx = new ExecutionContext(lst.get(0));
        Expression.fromString("guard { 'en' | set_language }").execute(ctx);
        assertEquals(Language.ENGLISH, ctx.getLanguage());
    }
}