aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/searchers/OpportunisticWeakAndSearcherTestCase.java
blob: 642b9bc7bc443b08e08d211113955e169cb12642 (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
package com.yahoo.search.searchers;

import com.yahoo.prelude.query.AndItem;
import com.yahoo.prelude.query.CompositeItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.OrItem;
import com.yahoo.prelude.query.WeakAndItem;
import com.yahoo.prelude.query.WordItem;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class OpportunisticWeakAndSearcherTestCase {
    private static Item buildQueryItem(CompositeItem root, CompositeItem injectAtLevel2) {
        root.addItem(new WordItem("text"));
        injectAtLevel2.addItem(new WordItem("a"));
        injectAtLevel2.addItem(new WordItem("b"));
        root.addItem(injectAtLevel2);
        return root;
    }

    @Test
    public void requireThatWeakAndIsDetected() {
        assertEquals(-1, OpportunisticWeakAndSearcher.targetHits(new OrItem()));
        assertEquals(33, OpportunisticWeakAndSearcher.targetHits(new WeakAndItem(33)));
        assertEquals(77, OpportunisticWeakAndSearcher.targetHits(buildQueryItem(new OrItem(), new WeakAndItem(77))));
        assertEquals(77, OpportunisticWeakAndSearcher.targetHits(buildQueryItem(new AndItem(), new WeakAndItem(77))));
        assertEquals(-1, OpportunisticWeakAndSearcher.targetHits(buildQueryItem(new OrItem(), new AndItem())));
    }

    @Test
    public void requireThatWeakAndIsReplacedWithAnd() {
        assertEquals(buildQueryItem(new OrItem(), new AndItem()),
                OpportunisticWeakAndSearcher.weakAnd2AndRecurse(buildQueryItem(new OrItem(), new WeakAndItem())));
        assertEquals(buildQueryItem(new AndItem(), new AndItem()),
                OpportunisticWeakAndSearcher.weakAnd2AndRecurse(buildQueryItem(new AndItem(), new WeakAndItem())));
    }

}