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

import com.yahoo.schema.document.ImmutableSDField;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.yahoo.document.DataType;
import com.yahoo.schema.parser.ParseException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Lester Solbakken
 */

public class PredicateDataTypeTestCase {

    private String searchSd(String field) {
        return "search p {\n document p {\n" + field + "}\n}\n";
    }

    private String predicateFieldSd(String index) {
        return "field pf type predicate {\n" + index + "}\n";
    }

    private String arrayPredicateFieldSd(String index) {
        return "field apf type array<predicate> {\n" + index + "}\n";
    }

    private String stringFieldSd(String index) {
        return "field sf type string {\n" + index + "}\n";
    }

    private String attributeFieldSd(String terms) {
        return "indexing: attribute\n index {\n" + terms + "}\n";
    }

    private String arityParameter(int arity) {
        return "arity: " + arity + "\n";
    }

    private String lowerBoundParameter(long bound) {
        return "lower-bound: " + bound + "\n";
    }

    private String upperBoundParameter(long bound) {
        return "upper-bound: " + bound + "\n";
    }

    @SuppressWarnings("deprecation")
    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void requireThatBuilderSetsIndexParametersCorrectly() throws ParseException {
        int arity = 2;
        long lowerBound = -100;
        long upperBound = 100;
        String sd = searchSd(
                        predicateFieldSd(
                            attributeFieldSd(
                                    arityParameter(arity) +
                                            lowerBoundParameter(lowerBound) +
                                            upperBoundParameter(upperBound))));

        ApplicationBuilder sb = ApplicationBuilder.createFromString(sd);
        for (ImmutableSDField field : sb.getSchema().allConcreteFields()) {
              if (field.getDataType() == DataType.PREDICATE) {
                for (Index index : field.getIndices().values()) {
                    assertTrue(index.getBooleanIndexDefiniton().hasArity());
                    assertEquals(arity, index.getBooleanIndexDefiniton().getArity());
                    assertTrue(index.getBooleanIndexDefiniton().hasLowerBound());
                    assertEquals(lowerBound, index.getBooleanIndexDefiniton().getLowerBound());
                    assertTrue(index.getBooleanIndexDefiniton().hasUpperBound());
                    assertEquals(upperBound, index.getBooleanIndexDefiniton().getUpperBound());
                }
            }
        }
    }

    @Test
    public void requireThatBuilderHandlesLongValues() throws ParseException {
        int arity = 2;
        long lowerBound = -100000000000000000L;
        long upperBound = 1000000000000000000L;
        String sd = searchSd(
                        predicateFieldSd(
                            attributeFieldSd(
                                    arityParameter(arity) +
                                            "lower-bound: -100000000000000000L\n" + // +'L'
                                            upperBoundParameter(upperBound))));

        ApplicationBuilder sb = ApplicationBuilder.createFromString(sd);
        for (ImmutableSDField field : sb.getSchema().allConcreteFields()) {
              if (field.getDataType() == DataType.PREDICATE) {
                for (Index index : field.getIndices().values()) {
                    assertEquals(arity, index.getBooleanIndexDefiniton().getArity());
                    assertEquals(lowerBound, index.getBooleanIndexDefiniton().getLowerBound());
                    assertEquals(upperBound, index.getBooleanIndexDefiniton().getUpperBound());
                }
            }
        }
    }

    @Test
    public void requireThatBuilderHandlesMissingParameters() throws ParseException {
        String sd = searchSd(
                        predicateFieldSd(
                            attributeFieldSd(
                                    arityParameter(2))));
        ApplicationBuilder sb = ApplicationBuilder.createFromString(sd);
        for (ImmutableSDField field : sb.getSchema().allConcreteFields()) {
            if (field.getDataType() == DataType.PREDICATE) {
                for (Index index : field.getIndices().values()) {
                    assertTrue(index.getBooleanIndexDefiniton().hasArity());
                    assertFalse(index.getBooleanIndexDefiniton().hasLowerBound());
                    assertFalse(index.getBooleanIndexDefiniton().hasUpperBound());
                }
            }
        }
    }

    @Test
    public void requireThatBuilderFailsIfNoArityValue() throws ParseException {
        String sd = searchSd(predicateFieldSd(attributeFieldSd("")));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Missing arity value in predicate field.");
        ApplicationBuilder.createFromString(sd);
        fail();
    }

    @Test
    public void requireThatBuilderFailsIfBothIndexAndAttribute() throws ParseException {
        String sd = searchSd(predicateFieldSd("indexing: summary | index | attribute\nindex { arity: 2 }"));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("For schema 'p', field 'pf': Use 'attribute' instead of 'index'. This will require a refeed if you have upgraded.");
        ApplicationBuilder.createFromString(sd);
    }

    @Test
    public void requireThatBuilderFailsIfIndex() throws ParseException {
        String sd = searchSd(predicateFieldSd("indexing: summary | index \nindex { arity: 2 }"));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("For schema 'p', field 'pf': Use 'attribute' instead of 'index'. This will require a refeed if you have upgraded.");
        ApplicationBuilder.createFromString(sd);
    }


    @Test
    public void requireThatBuilderFailsIfIllegalArityValue() throws ParseException {
        String sd = searchSd(predicateFieldSd(attributeFieldSd(arityParameter(0))));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Invalid arity value in predicate field, must be greater than 1.");
        ApplicationBuilder.createFromString(sd);
    }

    @Test
    public void requireThatBuilderFailsIfArityParameterExistButNotPredicateField() throws ParseException {
        String sd = searchSd(stringFieldSd(attributeFieldSd(arityParameter(2))));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Arity parameter is used only for predicate type fields.");
        ApplicationBuilder.createFromString(sd);
    }

    @Test
    public void requireThatBuilderFailsIfBoundParametersExistButNotPredicateField() throws ParseException {
        String sd = searchSd(
                        stringFieldSd(
                            attributeFieldSd(
                                    lowerBoundParameter(100) + upperBoundParameter(1000))));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Parameters lower-bound and upper-bound are used only for predicate type fields.");
        ApplicationBuilder.createFromString(sd);
    }

    @Test
    public void requireThatArrayOfPredicateFails() throws ParseException {
        String sd = searchSd(
                        arrayPredicateFieldSd(
                                attributeFieldSd(
                                        arityParameter(1))));

        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("Collections of predicates are not allowed.");
        ApplicationBuilder.createFromString(sd);
    }

}