aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/QueryTreeTest.java
blob: 6c73937f4fadb78827e57c79363e1c5b0b211437 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query;

import com.yahoo.prelude.query.NotItem;
import com.yahoo.prelude.query.NullItem;
import com.yahoo.prelude.query.WordItem;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class QueryTreeTest {

    @Test
    public void testAddQueryItemWithRoot() {
        Assert.assertEquals("AND a b",
                            new QueryTree(new WordItem("a")).and(new WordItem("b")).toString());

        NotItem not = new NotItem();
        not.addNegativeItem(new WordItem("b"));
        assertEquals("+a -b",
                     new QueryTree(new WordItem("a")).and(not).toString());
     }

     @Test
     public void addNotToNot() {
         NotItem not1 = new NotItem();
         not1.addPositiveItem(new WordItem("p1"));
         not1.addNegativeItem(new WordItem("n1.1"));
         not1.addNegativeItem(new WordItem("n1.2"));

         NotItem not2 = new NotItem();
         not2.addPositiveItem(new WordItem("p2"));
         not2.addNegativeItem(new WordItem("n2.1"));
         not2.addNegativeItem(new WordItem("n2.2"));

         QueryTree tree = new QueryTree(not1);
         tree.and(not2);

         assertEquals("+(AND p1 p2) -n1.1 -n1.2 -n2.1 -n2.2", tree.toString());
     }

     @Test
     public void getCorrectTreeSize() {
        QueryTree nullTree = new QueryTree(new NullItem());
        assertEquals(0, nullTree.treeSize());

         NotItem not1 = new NotItem();
         not1.addPositiveItem(new WordItem("p1"));
         not1.addNegativeItem(new WordItem("n1.1"));
         not1.addNegativeItem(new WordItem("n1.2"));

         NotItem not2 = new NotItem();
         not2.addPositiveItem(new WordItem("p2"));
         not2.addNegativeItem(new WordItem("n2.1"));
         not2.addNegativeItem(new WordItem("n2.2"));

         QueryTree tree = new QueryTree(not1);
         tree.and(not2);

         assertEquals(8, tree.treeSize());
     }

}