summaryrefslogtreecommitdiffstats
path: root/juniper/src/test/queryvisitor_test.cpp
blob: 18dda67f43f5ca9bea566fc8190ab02e8930f587 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <memory>
#include <vespa/fastos/fastos.h>
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/juniper/queryhandle.h>
#include <vespa/juniper/queryvisitor.h>
#include <vespa/vespalib/stllike/string.h>

using namespace juniper;

class MyQuery : public juniper::IQuery
{
private:
    vespalib::string _term;

public:
    MyQuery(const vespalib::string &term) : _term(term) {}

    virtual bool Traverse(IQueryVisitor* v) const override {
        v->VisitKeyword(nullptr, _term.c_str(), _term.size());
        return true;
    }
    virtual int Weight(const QueryItem*) const override {
        return 0;
    }
    virtual ItemCreator Creator(const QueryItem*) const override {
        return ItemCreator::CREA_ORIG;
    }
    virtual const char* Index(const QueryItem*, size_t*) const override {
        return "my_index";
    }
    virtual bool UsefulIndex(const QueryItem*) const override {
        return true;
    }
};

struct Fixture
{
    MyQuery query;
    QueryModifier modifier;
    QueryHandle handle;
    QueryVisitor visitor;
    Fixture(const vespalib::string &term)
        : query(term),
          modifier(),
          handle(query, "", modifier),
          visitor(query, &handle, modifier)
    {}
};

TEST_F("require that terms are picked up by the query visitor", Fixture("my_term"))
{
    auto query = std::unique_ptr<QueryExpr>(f.visitor.GetQuery());
    ASSERT_TRUE(query != nullptr);
    QueryNode *node = query->AsNode();
    ASSERT_TRUE(node != nullptr);
    EXPECT_EQUAL(1, node->_arity);
    QueryTerm *term = node->_children[0]->AsTerm();
    ASSERT_TRUE(term != nullptr);
    EXPECT_EQUAL("my_term", vespalib::string(term->term()));
}

TEST_F("require that empty terms are ignored by the query visitor", Fixture(""))
{
    QueryExpr *query = f.visitor.GetQuery();
    ASSERT_TRUE(query == nullptr);
}

TEST_MAIN()
{
    TEST_RUN_ALL();
}