aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/attribute/imported_attributes_repo/imported_attributes_repo_test.cpp
blob: 59f40f2fc2a953d50003d6fa1a48c5f316904693 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("imported_attributes_repo_test");
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/searchcommon/attribute/basictype.h>
#include <vespa/searchcommon/attribute/iattributevector.h>
#include <vespa/searchcore/proton/attribute/imported_attributes_repo.h>
#include <vespa/searchlib/attribute/imported_attribute_vector.h>
#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h>
#include <vespa/searchlib/attribute/reference_attribute.h>
#include <algorithm>

using proton::ImportedAttributesRepo;
using search::AttributeVector;
using search::attribute::BasicType;
using search::attribute::Config;
using search::attribute::ImportedAttributeVector;
using search::attribute::ImportedAttributeVectorFactory;
using search::attribute::ReferenceAttribute;

ImportedAttributeVector::SP
createAttr(const vespalib::string &name)
{
    return ImportedAttributeVectorFactory::create(name,
                                                  ReferenceAttribute::SP(),
                                                  std::shared_ptr<search::IDocumentMetaStoreContext>(),
                                                  AttributeVector::SP(),
                                                  std::shared_ptr<const search::IDocumentMetaStoreContext>(),
                                                  false);
}

struct Fixture {
    ImportedAttributesRepo repo;
    Fixture() : repo() {}
    void add(ImportedAttributeVector::SP attr) {
        repo.add(attr->getName(), attr);
    }
    ImportedAttributeVector::SP get(const vespalib::string &name) const {
        return repo.get(name);
    }
};

TEST_F("require that attributes can be added and retrieved", Fixture)
{
    ImportedAttributeVector::SP fooAttr = createAttr("foo");
    ImportedAttributeVector::SP barAttr = createAttr("bar");
    f.add(fooAttr);
    f.add(barAttr);
    EXPECT_EQUAL(2u, f.repo.size());
    EXPECT_EQUAL(f.get("foo").get(), fooAttr.get());
    EXPECT_EQUAL(f.get("bar").get(), barAttr.get());
}

TEST_F("require that attribute can be replaced", Fixture)
{
    ImportedAttributeVector::SP attr1 = createAttr("foo");
    ImportedAttributeVector::SP attr2 = createAttr("foo");
    f.add(attr1);
    f.add(attr2);
    EXPECT_EQUAL(1u, f.repo.size());
    EXPECT_EQUAL(f.get("foo").get(), attr2.get());
}

TEST_F("require that not-found attribute returns nullptr", Fixture)
{
    ImportedAttributeVector *notFound = nullptr;
    EXPECT_EQUAL(f.get("not_found").get(), notFound);
}

TEST_F("require that all attributes can be retrieved", Fixture)
{
    f.add(createAttr("foo"));
    f.add(createAttr("bar"));
    std::vector<ImportedAttributeVector::SP> list;
    f.repo.getAll(list);
    EXPECT_EQUAL(2u, list.size());
    // Don't depend on internal (unspecified) ordering
    std::sort(list.begin(), list.end(), [](auto& lhs, auto& rhs){
        return lhs->getName() < rhs->getName();
    });
    EXPECT_EQUAL("bar", list[0]->getName());
    EXPECT_EQUAL("foo", list[1]->getName());
}

TEST_MAIN()
{
    TEST_RUN_ALL();
}