aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/matching/index_environment/index_environment_test.cpp
blob: ff86e0a5ff1d86b228e71469b4239c512fcbe0a8 (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
// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/vespalib/testkit/test_kit.h>

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

using namespace proton::matching;
using search::fef::FieldInfo;
using search::fef::FieldType;
using search::fef::Properties;
using search::index::Schema;
using search::index::schema::CollectionType;
using search::index::schema::DataType;
using vespalib::eval::ConstantValue;
using SIAF = Schema::ImportedAttributeField;

struct MyConstantValueRepo : public IConstantValueRepo {
    virtual ConstantValue::UP getConstant(const vespalib::string &) const override {
        return ConstantValue::UP();
    }
};

Schema::UP
buildSchema()
{
    Schema::UP result = std::make_unique<Schema>();
    result->addImportedAttributeField(SIAF("imported_a", DataType::INT32, CollectionType::SINGLE));
    result->addImportedAttributeField(SIAF("imported_b", DataType::STRING, CollectionType::ARRAY));
    return result;
}

Schema::UP
buildEmptySchema()
{
    return std::make_unique<Schema>();
}

struct Fixture {
    MyConstantValueRepo repo;
    Schema::UP schema;
    IndexEnvironment env;
    Fixture(Schema::UP schema_)
        : repo(),
          schema(std::move(schema_)),
          env(*schema, Properties(), repo)
    {
    }
    const FieldInfo *assertField(size_t idx,
                                 const vespalib::string &name,
                                 DataType dataType,
                                 CollectionType collectionType) {
        const FieldInfo *field = env.getField(idx);
        ASSERT_TRUE(field != nullptr);
        EXPECT_EQUAL(field, env.getFieldByName(name));
        EXPECT_EQUAL(name, field->name());
        EXPECT_EQUAL(dataType, field->get_data_type());
        EXPECT_TRUE(collectionType == field->collection());
        EXPECT_EQUAL(idx, field->id());
        return field;
    }
    void assertHiddenAttributeField(size_t idx,
                                    const vespalib::string &name,
                                    DataType dataType,
                                    CollectionType collectionType) {
        const FieldInfo *field = assertField(idx, name, dataType, collectionType);
        EXPECT_FALSE(field->hasAttribute());
        EXPECT_TRUE(field->type() == FieldType::HIDDEN_ATTRIBUTE);
        EXPECT_TRUE(field->isFilter());
    }
    void assertAttributeField(size_t idx,
                              const vespalib::string &name,
                              DataType dataType,
                              CollectionType collectionType) {
        const FieldInfo *field = assertField(idx, name, dataType, collectionType);
        EXPECT_TRUE(field->hasAttribute());
        EXPECT_TRUE(field->type() == FieldType::ATTRIBUTE);
        EXPECT_FALSE(field->isFilter());
    }
};

TEST_F("require that document meta store is always extracted in index environment", Fixture(buildEmptySchema()))
{
    ASSERT_EQUAL(1u, f.env.getNumFields());
    TEST_DO(f.assertHiddenAttributeField(0, "[documentmetastore]", DataType::RAW, CollectionType::SINGLE));
}

TEST_F("require that imported attribute fields are extracted in index environment", Fixture(buildSchema()))
{
    ASSERT_EQUAL(3u, f.env.getNumFields());
    TEST_DO(f.assertAttributeField(0, "imported_a", DataType::INT32, CollectionType::SINGLE));
    TEST_DO(f.assertAttributeField(1, "imported_b", DataType::STRING, CollectionType::ARRAY));
    EXPECT_EQUAL("[documentmetastore]", f.env.getField(2)->name());
}

TEST_MAIN() { TEST_RUN_ALL(); }