summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/tests/docsummary/positionsdfw_test.cpp
blob: 0de84096e821f0c8bf57b68507b97fa05c4fe75f (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Unit tests for positionsdfw.

#include <vespa/searchlib/attribute/extendableattributes.h>
#include <vespa/searchlib/attribute/iattributemanager.h>
#include <vespa/searchlib/common/matching_elements.h>
#include <vespa/searchsummary/docsummary/docsum_field_writer.h>
#include <vespa/searchsummary/docsummary/positionsdfw.h>
#include <vespa/searchsummary/docsummary/idocsumenvironment.h>
#include <vespa/searchsummary/docsummary/docsumstate.h>
#include <vespa/searchsummary/test/slime_value.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/juniper/rpinterface.h>

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

using search::IAttributeManager;
using search::MatchingElements;
using search::SingleInt64ExtAttribute;
using search::attribute::IAttributeContext;
using search::attribute::IAttributeVector;
using search::attribute::IAttributeFunctor;
using vespalib::string;
using std::vector;

namespace search::docsummary {

namespace {

class Test : public vespalib::TestApp {
    void requireThat2DPositionFieldIsWritten();

public:
    int Main() override;
};

int
Test::Main()
{
    TEST_INIT("positionsdfw_test");

    TEST_DO(requireThat2DPositionFieldIsWritten());

    TEST_DONE();
}

struct MyEnvironment : IDocsumEnvironment {
    IAttributeManager *attribute_man;

    MyEnvironment() : attribute_man(0) {}

    const IAttributeManager *getAttributeManager() const override { return attribute_man; }
    string lookupIndex(const string &s) const override { return s; }
    juniper::Juniper *getJuniper() override { return 0; }
};

class MyAttributeContext : public IAttributeContext {
    const IAttributeVector &_attr;
public:
    MyAttributeContext(const IAttributeVector &attr) : _attr(attr) {}
     const IAttributeVector *getAttribute(const string &) const override {
        return &_attr;
    }
    const IAttributeVector *getAttributeStableEnum(const string &) const override {
        LOG_ABORT("MyAttributeContext::getAttributeStableEnum should not be reached");
    }
    void getAttributeList(vector<const IAttributeVector *> &) const override {
        LOG_ABORT("MyAttributeContext::getAttributeList should not be reached");
    }

    void
    asyncForAttribute(const vespalib::string &, std::unique_ptr<IAttributeFunctor>) const override {
        LOG_ABORT("MyAttributeContext::asyncForAttribute should not be reached");
    }
};

class MyAttributeManager : public IAttributeManager {
    const IAttributeVector &_attr;
public:

    MyAttributeManager(const IAttributeVector &attr) : _attr(attr) {}
    AttributeGuard::UP getAttribute(const string &) const override {
        LOG_ABORT("should not be reached");
    }
    std::unique_ptr<attribute::AttributeReadGuard> getAttributeReadGuard(const string &, bool) const override {
        LOG_ABORT("should not be reached");
    }
    void getAttributeList(vector<AttributeGuard> &) const override {
        LOG_ABORT("should not be reached");
    }

    void
    asyncForAttribute(const vespalib::string &, std::unique_ptr<IAttributeFunctor>) const override {
        LOG_ABORT("should not be reached");
    }

    IAttributeContext::UP createContext() const override {
        return std::make_unique<MyAttributeContext>(_attr);
    }

    std::shared_ptr<attribute::ReadableAttributeVector> readable_attribute_vector(const string&) const override {
        LOG_ABORT("should not be reached");
    }
};

struct MyGetDocsumsStateCallback : GetDocsumsStateCallback {
    virtual void FillSummaryFeatures(GetDocsumsState&) override {}
    virtual void FillRankFeatures(GetDocsumsState&) override {}
    std::unique_ptr<MatchingElements> fill_matching_elements(const MatchingElementsFields &) override { abort(); }
};

template <typename AttrType>
void checkWritePositionField(AttrType &attr,
                             uint32_t doc_id, const vespalib::string &expect_json) {
    for (AttributeVector::DocId i = 0; i < doc_id + 1; ) {
        attr.addDoc(i);
        if (i == 007) {
            attr.add((int64_t) -1);
        } else if (i == 0x42) {
            attr.add(0xAAAAaaaaAAAAaaaa);
        } else if (i == 0x17) {
            attr.add(0x5555aaaa5555aaab);
        } else if (i == 42) {
            attr.add(0x8000000000000000);
        } else {
            attr.add(i); // value = docid
        }
    }

    MyAttributeManager attribute_man(attr);
    PositionsDFW::UP writer = PositionsDFW::create(attr.getName().c_str(), &attribute_man, false);
    ASSERT_TRUE(writer.get());
    ResType res_type = RES_JSONSTRING;
    MyGetDocsumsStateCallback callback;
    GetDocsumsState state(callback);
    state._attributes.push_back(&attr);

    vespalib::Slime target;
    vespalib::slime::SlimeInserter inserter(target);
    writer->insertField(doc_id, &state, res_type, inserter);

    test::SlimeValue expected(expect_json);
    EXPECT_EQUAL(expected.slime, target);
}

void Test::requireThat2DPositionFieldIsWritten() {
    SingleInt64ExtAttribute attr("foo");
    checkWritePositionField(attr, 0x3e, "{x:6,y:7,latlong:'N0.000007;E0.000006'}");
    checkWritePositionField(attr,  007, "{x:-1,y:-1,latlong:'S0.000001;W0.000001'}");
    checkWritePositionField(attr, 0x42, "{x:0,y:-1,latlong:'S0.000001;E0.000000'}");
    checkWritePositionField(attr, 0x17, "{x:-16711935,y:16711935,latlong:'N16.711935;W16.711935'}");
    checkWritePositionField(attr,   42, "null");
}

}  // namespace
}

TEST_APPHOOK(search::docsummary::Test);