summaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/tests/searchvisitor/searchvisitor_test.cpp
blob: 216e02c5edd11a02658922e138dd21f18c9647d0 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/document/base/testdocrepo.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/query/tree/querybuilder.h>
#include <vespa/searchlib/query/tree/simplequery.h>
#include <vespa/searchlib/query/tree/stackdumpcreator.h>
#include <vespa/searchvisitor/searchenvironment.h>
#include <vespa/searchvisitor/searchvisitor.h>
#include <vespa/storage/frameworkimpl/component/storagecomponentregisterimpl.h>
#include <vespa/storageframework/defaultimplementation/clock/fakeclock.h>

#include <vespa/log/log.h>
LOG_SETUP("searchvisitor_test");

using namespace document;
using namespace search::query;
using namespace search;
using namespace storage;

namespace streaming {

class SearchVisitorTest : public vespalib::TestApp
{
private:
    framework::defaultimplementation::FakeClock _clock;
    StorageComponentRegisterImpl      _componentRegister;
    std::unique_ptr<StorageComponent> _component;
    SearchEnvironment                 _env;
    void testSearchVisitor();
    void testSearchEnvironment();
    void testCreateSearchVisitor(const vespalib::string & dir, const vdslib::Parameters & parameters);
    void testOnlyRequireWeakReadConsistency();

public:
    SearchVisitorTest();
    ~SearchVisitorTest() override;
    int Main() override;
};

SearchVisitorTest::SearchVisitorTest() :
    vespalib::TestApp(),
    _componentRegister(),
    _env("dir:" + TEST_PATH("cfg"))
{
    _componentRegister.setNodeInfo("mycluster", lib::NodeType::STORAGE, 1);
    _componentRegister.setClock(_clock);
    auto repo = std::make_shared<DocumentTypeRepo>(readDocumenttypesConfig(TEST_PATH("cfg/documenttypes.cfg")));
    _componentRegister.setDocumentTypeRepo(repo);
    _component = std::make_unique<StorageComponent>(_componentRegister, "storage");
}

SearchVisitorTest::~SearchVisitorTest() = default;

std::vector<spi::DocEntry::UP>
createDocuments(const vespalib::string & dir)
{
    (void) dir;
    std::vector<spi::DocEntry::UP> documents;
    spi::Timestamp ts;
    document::Document::UP doc(new document::Document());
    spi::DocEntry::UP e(new spi::DocEntry(ts, 0, std::move(doc)));
    documents.push_back(std::move(e));
    return documents;
}

void
SearchVisitorTest::testCreateSearchVisitor(const vespalib::string & dir, const vdslib::Parameters & params)
{
    SearchVisitorFactory sFactory(dir);
    VisitorFactory & factory(sFactory);
    std::unique_ptr<Visitor> sv(static_cast<SearchVisitor *>(factory.makeVisitor(*_component, _env, params)));
    document::BucketId bucketId;
    std::vector<spi::DocEntry::UP> documents(createDocuments(dir));
    Visitor::HitCounter hitCounter;
    sv->handleDocuments(bucketId, documents, hitCounter);
}

void
SearchVisitorTest::testSearchEnvironment()
{
    EXPECT_TRUE(_env.getVSMAdapter("simple") != nullptr);
    EXPECT_TRUE(_env.getRankManager("simple") != nullptr);
}

void
SearchVisitorTest::testSearchVisitor()
{
    vdslib::Parameters params;
    params.set("searchcluster", "aaa");
    params.set("queryflags", "0x40000");
    params.set("summarycount", "3");
    params.set("summaryclass", "petra");
    params.set("rankprofile", "default");

    QueryBuilder<SimpleQueryNodeTypes> builder;
    builder.addStringTerm("maptest", "sddocname", 0, Weight(0));
    Node::UP node = builder.build();
    vespalib::string stackDump = StackDumpCreator::create(*node);

    params.set("query", stackDump);
    testCreateSearchVisitor("dir:" + TEST_PATH("cfg"), params);
}

void
SearchVisitorTest::testOnlyRequireWeakReadConsistency()
{
    SearchVisitorFactory factory("dir:" + TEST_PATH("cfg"));
    VisitorFactory& factoryBase(factory);
    vdslib::Parameters params;
    std::unique_ptr<Visitor> sv(factoryBase.makeVisitor(*_component, _env, params));
    EXPECT_TRUE(sv->getRequiredReadConsistency() == spi::ReadConsistency::WEAK);
}

int
SearchVisitorTest::Main()
{
    TEST_INIT("searchvisitor_test");

    testSearchVisitor(); TEST_FLUSH();
    testSearchEnvironment(); TEST_FLUSH();
    testOnlyRequireWeakReadConsistency(); TEST_FLUSH();

    TEST_DONE();
}

} // namespace streaming

TEST_APPHOOK(::streaming::SearchVisitorTest)