aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/IndexingValidationTestCase.java
blob: 71c91533f54526ca54f2208374ae2b2b21a84d58 (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
// Copyright Vespa.ai. 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.derived.AbstractExportingTestCase;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.yolean.Exceptions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;

import static com.yahoo.schema.processing.AssertIndexingScript.assertIndexing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Simon Thoresen Hult
 */
public class IndexingValidationTestCase extends AbstractExportingTestCase {

    @Test
    void testAttributeChanged() throws ParseException {
        try {
            var schema = """
                    search indexing_attribute_changed {
                        document indexing_attribute_changed {
                            field foo type string {
                                indexing: summary | lowercase | attribute
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_attribute_changed', field 'foo': For expression 'attribute foo': " +
                         "Attempting to assign conflicting values to field 'foo'.",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testAttributeOther() throws ParseException {
        try {
            var schema = """
                    search indexing_attribute_other {
                        document indexing_attribute_other {
                            field foo type string {
                                indexing: attribute bar
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_attribute_other', field 'foo': Indexing expression 'attribute bar' " +
                         "attempts to write to a field other than 'foo'.",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testIndexChanged() throws ParseException {
        try {
            var schema = """
                    search indexing_index_changed {
                        document indexing_index_changed {
                            field foo type string {
                                indexing: attribute | lowercase | index
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_index_changed', field 'foo': For expression 'index foo': " +
                         "Attempting to assign conflicting values to field 'foo'.",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testIndexOther() throws ParseException {
        try {
            var schema = """
                    search indexing_index_other {
                        document indexing_index_other {
                            field foo type string {
                                indexing: index bar\s
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_index_other', field 'foo': Indexing expression 'index bar' " +
                         "attempts to write to a field other than 'foo'.",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testSummaryChanged() throws ParseException {
        try {
            var schema = """
                    search indexing_summary_fail {
                        document indexing_summary_fail {
                            field foo type string {
                                indexing: index | lowercase | summary\s
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_summary_fail', field 'foo': For expression 'summary foo': Attempting " +
                         "to assign conflicting values to field 'foo'.",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testSummaryOther() throws ParseException {
        try {
            var schema = """
                    search indexing_summary_other {
                        document indexing_summary_other {
                            field foo type string {
                                indexing: summary bar
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_summary_other', field 'foo': Indexing expression 'summary bar' " +
                         "attempts to write to a field other than 'foo'.",
                         Exceptions.toMessageString(e));
        }
    }

    @Test
    void testExtraField() throws IOException, ParseException {
        assertIndexing(
                Arrays.asList("clear_state | guard { input my_index | tokenize normalize stem:\"BEST\" | index my_index | summary my_index }",
                        "clear_state | guard { input my_input | tokenize normalize stem:\"BEST\" | index my_extra | summary my_extra }"),
                ApplicationBuilder.buildFromFile("src/test/examples/indexing_extra.sd"));
    }

    @Test
    void requireThatMultilineOutputConflictThrows() throws ParseException {
        try {
            var schema = """
                    search indexing_multiline_output_confict {
                        document indexing_multiline_output_confict {
                            field foo type string {
                            }
                            field bar type string {
                            }
                            field baz type string {
                            }
                        }
                        field cox type string {
                            indexing {
                                input foo | attribute;
                                input bar | index;
                                input baz | summary;
                            }
                        }
                    }
                    """;
            ApplicationBuilder.createFromString(schema);
            fail("Expected exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("For schema 'indexing_multiline_output_confict', field 'cox': For expression 'index cox': " +
                         "Attempting to assign conflicting values to field 'cox'.",
                         Exceptions.toMessageString(e));
        }
    }

}