aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/test/java/com/yahoo/document/predicate/PredicateTest.java
blob: 094f58d705df804f43077632579db5697774d5b1 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

import org.junit.jupiter.api.Test;

import static com.yahoo.document.predicate.Predicates.and;
import static com.yahoo.document.predicate.Predicates.feature;
import static com.yahoo.document.predicate.Predicates.not;
import static com.yahoo.document.predicate.Predicates.or;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Simon Thoresen Hult
 */
public class PredicateTest {

    @Test
    void requireThatPredicateIsCloneable() {
        assertTrue(Cloneable.class.isAssignableFrom(Predicate.class));
    }

    @Test
    void requireThatANDConstructsAConjunction() {
        Predicate foo = SimplePredicates.newString("foo");
        Predicate bar = SimplePredicates.newString("bar");
        Predicate predicate = and(foo, bar);
        assertEquals(Conjunction.class, predicate.getClass());
        assertEquals(new Conjunction(foo, bar), predicate);
    }

    @Test
    void requireThatORConstructsADisjunction() {
        Predicate foo = SimplePredicates.newString("foo");
        Predicate bar = SimplePredicates.newString("bar");
        Predicate predicate = or(foo, bar);
        assertEquals(Disjunction.class, predicate.getClass());
        assertEquals(new Disjunction(foo, bar), predicate);
    }

    @Test
    void requireThatNOTConstructsANegation() {
        Predicate foo = SimplePredicates.newString("foo");
        Predicate predicate = not(foo);
        assertEquals(Negation.class, predicate.getClass());
        assertEquals(new Negation(foo), predicate);
    }

    @Test
    void requireThatFeatureBuilderCanConstructFeatureRange() {
        assertEquals(new FeatureRange("key", 6L, 9L),
                feature("key").inRange(6, 9));
        assertEquals(new Negation(new FeatureRange("key", 6L, 9L)),
                feature("key").notInRange(6, 9));
        assertEquals(new FeatureRange("key", 7L, null),
                feature("key").greaterThan(6));
        assertEquals(new FeatureRange("key", 6L, null),
                feature("key").greaterThanOrEqualTo(6));
        assertEquals(new FeatureRange("key", null, 5L),
                feature("key").lessThan(6));
        assertEquals(new FeatureRange("key", null, 9L),
                feature("key").lessThanOrEqualTo(9));
    }

    @Test
    void requireThatFeatureBuilderCanConstructFeatureSet() {
        assertEquals(new FeatureSet("key", "valueA", "valueB"),
                feature("key").inSet("valueA", "valueB"));
        assertEquals(new Negation(new FeatureSet("key", "valueA", "valueB")),
                feature("key").notInSet("valueA", "valueB"));
    }

    @Test
    void requireThatPredicatesCanBeConstructedUsingConstructors() {
        assertEquals("country in [no, se] and age in [20..30]",
                new Conjunction(new FeatureSet("country", "no", "se"),
                        new FeatureRange("age", 20L, 30L)).toString());
        assertEquals("country not in [no, se] or age in [20..] or height in [..160]",
                new Disjunction(new Negation(new FeatureSet("country", "no", "se")),
                        new FeatureRange("age", 20L, null),
                        new FeatureRange("height", null, 160L)).toString());
    }

    @Test
    void requireThatPredicatesCanBeBuiltUsingChainedMethodCalls() {
        assertEquals("country not in [no, se] or age in [20..] or height in [..160]",
                new Disjunction()
                        .addOperand(new Negation(new FeatureSet("country").addValue("no").addValue("se")))
                        .addOperand(new FeatureRange("age").setFromInclusive(20L))
                        .addOperand(new FeatureRange("height").setToInclusive(160L))
                        .toString());
    }

    @Test
    void requireThatPredicatesCanBeBuiltUsingSeparateMethodCalls() {
        Conjunction conjunction = new Conjunction();
        FeatureSet countrySet = new FeatureSet("country");
        countrySet.addValue("no");
        countrySet.addValue("se");
        conjunction.addOperand(countrySet);
        FeatureRange ageRange = new FeatureRange("age");
        ageRange.setFromInclusive(20L);
        conjunction.addOperand(ageRange);
        FeatureRange heightRange = new FeatureRange("height");
        heightRange.setToInclusive(160L);
        conjunction.addOperand(heightRange);
        assertEquals("country in [no, se] and age in [20..] and height in [..160]",
                conjunction.toString());
    }
}