summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/matching/constant_value_repo/constant_value_repo_test.cpp
blob: acc16f418727d88d4f0a927fc3d31853b8d23d1a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/searchcore/proton/matching/constant_value_repo.h>
#include <vespa/searchcore/proton/matching/error_constant_value.h>
#include <vespa/eval/eval/value_cache/constant_value.h>

using namespace proton::matching;
using namespace vespalib::eval;

class DoubleConstantValue : public ConstantValue {
private:
    DoubleValue _value;
    ValueType _type;

public:
    DoubleConstantValue(double value_) : _value(value_), _type(ValueType::double_type()) {}
    virtual const ValueType &type() const override { return _type; }
    virtual const Value &value() const override { return _value; }
};

class MyConstantValueFactory : public ConstantValueFactory {
private:
    using Key = std::pair<vespalib::string, vespalib::string>;
    using Map = std::map<Key, double>;
    Map _map;

public:
    MyConstantValueFactory() : _map() {}
    void add(const vespalib::string &path, const vespalib::string &type, double value) {
        _map.insert(std::make_pair(std::make_pair(path, type), value));
    }
    virtual ConstantValue::UP create(const vespalib::string &path, const vespalib::string &type) const override {
        auto itr = _map.find(std::make_pair(path, type));
        if (itr != _map.end()) {
            return std::make_unique<DoubleConstantValue>(itr->second);
        }
        return std::make_unique<ErrorConstantValue>();
    }
};

struct Fixture {
    MyConstantValueFactory factory;
    ConstantValueRepo repo;
    Fixture()
        : factory(), repo(factory)
    {
        factory.add("path_1", "double", 3);
        factory.add("path_2", "double", 5);
        repo.reconfigure(RankingConstants({{"foo", "double", "path_1"},
                                           {"bar", "double", "path_3"}}));
    }
};

TEST_F("require that constant value can be retrieved from repo", Fixture)
{
    EXPECT_EQUAL(3, f.repo.getConstant("foo")->value().as_double());
}

TEST_F("require that non-existing constant value in repo returns error value", Fixture)
{
    EXPECT_TRUE(f.repo.getConstant("none")->value().is_error());
}

TEST_F("require that non-existing constant value in factory returns error value", Fixture)
{
    EXPECT_TRUE(f.repo.getConstant("bar")->value().is_error());
}

TEST_F("require that reconfigure replaces existing constant values in repo", Fixture)
{
    f.repo.reconfigure(RankingConstants({{"bar", "double", "path_3"},
                                         {"baz", "double", "path_2"}}));
    f.factory.add("path_3", "double", 7);
    EXPECT_TRUE(f.repo.getConstant("foo")->value().is_error());
    EXPECT_EQUAL(7, f.repo.getConstant("bar")->value().as_double());
    EXPECT_EQUAL(5, f.repo.getConstant("baz")->value().as_double());
}

TEST_MAIN() { TEST_RUN_ALL(); }