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

import com.yahoo.search.Query;
import com.yahoo.prelude.query.CompositeItem;
import com.yahoo.prelude.query.Item;
import com.yahoo.search.query.parser.Parsable;
import com.yahoo.search.query.parser.Parser;
import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.query.parser.ParserFactory;
import org.junit.jupiter.api.Test;

import java.util.Iterator;

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

/**
 * Tests query trees
 *
 * @author bratseth
 */
public class QueryTestCase {

    /** Tests that query hash and equality is value dependent only */
    @Test
    void testQueryEquality() {
        String query = "RANK (+(AND \"baz gaz faz\" bazar) -\"foo bar foobar\") foofoo xyzzy";
        String filter = "foofoo -\"foo bar foobar\" xyzzy +\"baz gaz faz\" +bazar";

        Item root1 = parseQuery(query, filter, Query.Type.ANY);
        Item root2 = parseQuery(query, filter, Query.Type.ANY);

        assertEquals(root1.hashCode(), root2.hashCode());
        assertEquals(root1, root2);
    }

    /** Check copy of query trees is a deep copy */
    @Test
    void testDeepCopy() {
        Item root1 = parseQuery("a and b and (c or d) and e rank f andnot g", null, Query.Type.ADVANCED);
        Item root2 = root1.clone();

        assertTrue(nonIdenticalTrees(root1, root2), "Item.clone() should be a deep copy.");
    }

    private static Item parseQuery(String query, String filter, Query.Type type) {
        Parser parser = ParserFactory.newInstance(type, new ParserEnvironment());
        return parser.parse(new Parsable().setQuery(query).setFilter(filter));
    }

    // Control two equal trees does not have a "is" relationship for
    // any element
    private boolean nonIdenticalTrees(Item root1, Item root2) {
        if (root1 instanceof CompositeItem) {
            boolean nonID = root1 != root2;
            Iterator<?> i1 = ((CompositeItem) root1).getItemIterator();
            Iterator<?> i2 = ((CompositeItem) root2).getItemIterator();

            while (i1.hasNext() && nonID) {
                nonID &= nonIdenticalTrees((Item) i1.next(), (Item) i2.next());
            }
            return nonID;

        } else {
            return root1 != root2;
        }
    }

}