aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/IndexingInputsTestCase.java
blob: 675168ca6c2c5adff57781d02dfb5ec6eec33020 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.yolean.Exceptions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Simon Thoresen Hult
 * @author bratseth
 */
public class IndexingInputsTestCase {

    @Test
    void requireThatExtraFieldInputExtraFieldThrows() throws ParseException {
        try {
            var schema = """
                    search indexing_extra_field_input_extra_field {
                        document indexing_extra_field_input_extra_field {
                        }
                        field foo type string {
                        }
                        field bar type string {
                            indexing: input bar | index
                        }
                    }
                   """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_extra_field_input_extra_field', field 'bar': Indexing script refers " +
                         "to field 'bar' which is neither a field in document type " +
                         "'indexing_extra_field_input_extra_field' nor a mutable attribute",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatExtraFieldInputImplicitThrows() throws ParseException {
        try {
            var schema = """
                    search indexing_extra_field_input_implicit {
                        document indexing_extra_field_input_implicit {
                        }
                        field foo type string {
                            indexing: index
                        }
                    }
                   """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_extra_field_input_implicit', field 'foo': " +
                         "For expression '{ tokenize normalize stem:\"BEST\" | index foo; }': Expected string input, but no input is specified",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatExtraFieldInputNullThrows() throws ParseException {
        try {
            var schema = """
                    search indexing_extra_field_input_null {
                        document indexing_extra_field_input_null {
                        }
                        field foo type string {
                            indexing: input foo | index
                        }
                    }
                   """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_extra_field_input_null', field 'foo': Indexing script refers to field " +
                         "'foo' which is neither a field in document type 'indexing_extra_field_input_null' nor a mutable attribute",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void requireThatExtraFieldInputSelfThrows() throws ParseException {
        try {
            var schema = """
                        search indexing_extra_field_input_self {
                            document indexing_extra_field_input_self {
                            }
                            field foo type string {
                                indexing: input foo | index
                            }
                        }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_extra_field_input_self', field 'foo': Indexing script refers to field " +
                         "'foo' which is neither a field in document type 'indexing_extra_field_input_self' nor a mutable attribute",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testPlainInputInDerivedField() throws ParseException {
        var schema = """
        schema test {
            document test {
                field field1 type int {
                }
            }
            field derived1 type int {
                indexing: input field1 | attribute
            }
        }
        """;
        ApplicationBuilder.createFromString(schema);
    }

    @Test
    void testWrappedInputInDerivedField() throws ParseException {
        var schema = """
        schema test {
            document test {
                field field1 type int {
                }
            }
            field derived1 type int {
                indexing: if (input field1 == 0) { 0 } else { 1 } | attribute
            }
        }
        """;
        ApplicationBuilder.createFromString(schema);
    }

    @Test
    void testNoInputInDerivedField() throws ParseException {
        try {
            var schema = """
                    schema test {
                        document test {
                            field field1 type int {
                            }
                    }
                        field derived1 type int {
                            indexing: attribute
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'test', field 'derived1': For expression '{ attribute derived1; }': " +
                         "Expected any input, but no input is specified",
                         Exceptions.toMessageString(e));
        }
    }

}