summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/SchemaTestCase.java
blob: 3413fe3c37309c409035d33c0d6cff8864b3796c (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
264
265
266
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.searchdefinition.document.Stemming;
import com.yahoo.searchdefinition.parser.ParseException;
import com.yahoo.searchdefinition.processing.ImportedFieldsResolver;
import com.yahoo.searchdefinition.processing.OnnxModelTypeResolver;
import com.yahoo.vespa.model.test.utils.DeployLoggerStub;
import org.junit.Test;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Schema tests that don't depend on files.
 *
 * @author bratseth
 */
public class SchemaTestCase {

    @Test
    public void testValidationOfInheritedSchema() throws ParseException {
        try {
            String schema = joinLines(
                    "schema test inherits nonesuch {" +
                    "  document test inherits nonesuch {" +
                    "  }" +
                    "}");
            DeployLoggerStub logger = new DeployLoggerStub();
            SchemaBuilder.createFromStrings(logger, schema);
            assertEquals("schema 'test' inherits 'nonesuch', but this schema does not exist",
                         logger.entries.get(0).message);
            fail("Expected failure");
        }
        catch (IllegalArgumentException e) {
            assertEquals("schema 'test' inherits 'nonesuch', but this schema does not exist", e.getMessage());
        }
    }

    @Test
    public void testValidationOfSchemaAndDocumentInheritanceConsistency() throws ParseException {
        try {
            String parent = joinLines(
                    "schema parent {" +
                    "  document parent {" +
                    "    field pf1 type string {" +
                    "      indexing: summary" +
                    "    }" +
                    "  }" +
                    "}");
            String child = joinLines(
                    "schema child inherits parent {" +
                    "  document child {" +
                    "    field cf1 type string {" +
                    "      indexing: summary" +
                    "    }" +
                    "  }" +
                    "}");
            SchemaBuilder.createFromStrings(new DeployLoggerStub(), parent, child);
        }
        catch (IllegalArgumentException e) {
            assertEquals("schema 'child' inherits 'parent', " +
                         "but its document type does not inherit the parent's document type"
                         , e.getMessage());
        }
    }

    @Test
    public void testSchemaInheritance() throws ParseException {
        String parentLines = joinLines(
                "schema parent {" +
                "  document parent {" +
                "    field pf1 type string {" +
                "      indexing: summary" +
                "    }" +
                "  }" +
                "  fieldset parent_set {" +
                "    fields: pf1" +
                "  }" +
                "  stemming: none" +
                "  index parent_index {" +
                "    stemming: best" +
                "  }" +
                "  field parent_field type string {" +
                "      indexing: input pf1 | lowercase | index | attribute | summary" +
                "  }" +
                "  rank-profile parent_profile {" +
                "  }" +
                "  constant parent_constant {" +
                "    file: constants/my_constant_tensor_file.json" +
                "    type: tensor<float>(x{},y{})" +
                "  }" +
                "  onnx-model parent_model {" +
                "    file: models/my_model.onnx" +
                "  }" +
                "  document-summary parent_summary {" +
                "    summary pf1 type string {}" +
                "  }" +
                "  import field parentschema_ref.name as parent_imported {}" +
                "  raw-as-base64-in-summary" +
                "}");
        String childLines = joinLines(
                "schema child inherits parent {" +
                "  document child inherits parent {" +
                "    field cf1 type string {" +
                "      indexing: summary" +
                "    }" +
                "  }" +
                "  fieldset child_set {" +
                "    fields: cf1, pf1" +
                "  }" +
                "  stemming: shortest" +
                "  index child_index {" +
                "    stemming: shortest" +
                "  }" +
                "  field child_field type string {" +
                "      indexing: input pf1 | lowercase | index | attribute | summary" +
                "  }" +
                "  rank-profile child_profile inherits parent_profile {" +
                "  }" +
                "  constant child_constant {" +
                "    file: constants/my_constant_tensor_file.json" +
                "    type: tensor<float>(x{},y{})" +
                "  }" +
                "  onnx-model child_model {" +
                "    file: models/my_model.onnx" +
                "  }" +
                "  document-summary child_summary inherits parent_summary {" +
                "    summary cf1 type string {}" +
                "  }" +
                "  import field parentschema_ref.name as child_imported {}" +
                "}");

        System.out.println(parentLines);
        SchemaBuilder builder = new SchemaBuilder(new DeployLoggerStub());
        builder.processorsToSkip().add(OnnxModelTypeResolver.class); // Avoid discovering the Onnx model referenced does not exist
        builder.processorsToSkip().add(ImportedFieldsResolver.class); // Avoid discovering the document reference leads nowhere
        builder.importString(parentLines);
        builder.importString(childLines);
        builder.build(true);
        var application = builder.application();

        var child = application.schemas().get("child");
        assertEquals("pf1", child.fieldSets().userFieldSets().get("parent_set").getFieldNames().stream().findFirst().get());
        assertEquals("[cf1, pf1]", child.fieldSets().userFieldSets().get("child_set").getFieldNames().toString());
        assertEquals(Stemming.SHORTEST, child.getStemming());
        assertEquals(Stemming.BEST, child.getIndex("parent_index").getStemming());
        assertEquals(Stemming.SHORTEST, child.getIndex("child_index").getStemming());
        assertNotNull(child.getField("parent_field"));
        assertNotNull(child.getField("child_field"));
        assertNotNull(child.getExtraField("parent_field"));
        assertNotNull(child.getExtraField("child_field"));
        assertNotNull(application.rankProfileRegistry().get(child, "parent_profile"));
        assertNotNull(application.rankProfileRegistry().get(child, "child_profile"));
        assertEquals("parent_profile", application.rankProfileRegistry().get(child, "child_profile").getInheritedName());
        assertNotNull(child.rankingConstants().get("parent_constant"));
        assertNotNull(child.rankingConstants().get("child_constant"));
        assertTrue(child.rankingConstants().asMap().containsKey("parent_constant"));
        assertTrue(child.rankingConstants().asMap().containsKey("child_constant"));
        assertNotNull(child.onnxModels().get("parent_model"));
        assertNotNull(child.onnxModels().get("child_model"));
        assertTrue(child.onnxModels().asMap().containsKey("parent_model"));
        assertTrue(child.onnxModels().asMap().containsKey("child_model"));
        assertNotNull(child.getSummary("parent_summary"));
        assertNotNull(child.getSummary("child_summary"));
        assertEquals("parent_summary", child.getSummary("child_summary").inherited().get().getName());
        assertTrue(child.getSummaries().containsKey("parent_summary"));
        assertTrue(child.getSummaries().containsKey("child_summary"));
        assertNotNull(child.getSummaryField("pf1"));
        assertNotNull(child.getSummaryField("cf1"));
        assertNotNull(child.getExplicitSummaryField("pf1"));
        assertNotNull(child.getExplicitSummaryField("cf1"));
        assertNotNull(child.getUniqueNamedSummaryFields().get("pf1"));
        assertNotNull(child.getUniqueNamedSummaryFields().get("cf1"));
        assertNotNull(child.temporaryImportedFields().get().fields().get("parent_imported"));
        assertNotNull(child.temporaryImportedFields().get().fields().get("child_imported"));
    }

    @Test
    public void testSchemaInheritanceEmptyChildren() throws ParseException {
        String parentLines = joinLines(
                "schema parent {" +
                "  document parent {" +
                "    field pf1 type string {" +
                "      indexing: summary" +
                "    }" +
                "  }" +
                "  fieldset parent_set {" +
                "    fields: pf1" +
                "  }" +
                "  stemming: none" +
                "  index parent_index {" +
                "    stemming: best" +
                "  }" +
                "  field parent_field type string {" +
                "      indexing: input pf1 | lowercase | index | attribute | summary" +
                "  }" +
                "  rank-profile parent_profile {" +
                "  }" +
                "  constant parent_constant {" +
                "    file: constants/my_constant_tensor_file.json" +
                "    type: tensor<float>(x{},y{})" +
                "  }" +
                "  onnx-model parent_model {" +
                "    file: models/my_model.onnx" +
                "  }" +
                "  document-summary parent_summary {" +
                "    summary pf1 type string {}" +
                "  }" +
                "  import field parentschema_ref.name as parent_imported {}" +
                "  raw-as-base64-in-summary" +
                "}");
        String childLines = joinLines(
                "schema child inherits parent {" +
                "  document child inherits parent {" +
                "    field cf1 type string {" +
                "      indexing: summary" +
                "    }" +
                "  }" +
                "}");
        String grandchildLines = joinLines(
                "schema grandchild inherits child {" +
                "  document grandchild inherits child {" +
                "    field gf1 type string {" +
                "      indexing: summary" +
                "    }" +
                "  }" +
                "}");

        SchemaBuilder builder = new SchemaBuilder(new DeployLoggerStub());
        builder.processorsToSkip().add(OnnxModelTypeResolver.class); // Avoid discovering the Onnx model referenced does not exist
        builder.processorsToSkip().add(ImportedFieldsResolver.class); // Avoid discovering the document reference leads nowhere
        builder.importString(parentLines);
        builder.importString(childLines);
        builder.importString(grandchildLines);
        builder.build(true);
        var application = builder.application();

        assertInheritedFromParent(application.schemas().get("child"), application);
        assertInheritedFromParent(application.schemas().get("grandchild"), application);
    }

    private void assertInheritedFromParent(Schema schema, Application application) {
        assertEquals("pf1", schema.fieldSets().userFieldSets().get("parent_set").getFieldNames().stream().findFirst().get());
        assertEquals(Stemming.NONE, schema.getStemming());
        assertEquals(Stemming.BEST, schema.getIndex("parent_index").getStemming());
        assertNotNull(schema.getField("parent_field"));
        assertNotNull(schema.getExtraField("parent_field"));
        assertNotNull(application.rankProfileRegistry().get(schema, "parent_profile"));
        assertNotNull(schema.rankingConstants().get("parent_constant"));
        assertTrue(schema.rankingConstants().asMap().containsKey("parent_constant"));
        assertNotNull(schema.onnxModels().get("parent_model"));
        assertTrue(schema.onnxModels().asMap().containsKey("parent_model"));
        assertNotNull(schema.getSummary("parent_summary"));
        assertTrue(schema.getSummaries().containsKey("parent_summary"));
        assertNotNull(schema.getSummaryField("pf1"));
        assertNotNull(schema.getExplicitSummaryField("pf1"));
        assertNotNull(schema.getUniqueNamedSummaryFields().get("pf1"));
        assertNotNull(schema.temporaryImportedFields().get().fields().get("parent_imported"));
        assertTrue(schema.isRawAsBase64());
    }

}