aboutsummaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/tests/document/document_test.cpp
blob: acebd9ed4a43b0732b1475a65a5fe12972fa8554 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/document/fieldvalue/fieldvalues.h>
#include <vespa/document/datatype/documenttype.h>
#include <vespa/vsm/common/storagedocument.h>
#include <vespa/vespalib/stllike/asciistream.h>

using namespace document;

namespace vsm {

class DocumentTest : public vespalib::TestApp
{
private:
    void testStorageDocument();
    void testStringFieldIdTMap();
public:
    int Main() override;
};

void
DocumentTest::testStorageDocument()
{
    DocumentType dt("testdoc", 0);

    Field fa("a", 0, *DataType::STRING);
    Field fb("b", 1, *DataType::STRING);
    dt.addField(fa);
    dt.addField(fb);

    auto doc = document::Document::make_without_repo(dt, DocumentId());
    doc->setValue(fa, StringFieldValue("foo"));
    doc->setValue(fb, StringFieldValue("bar"));

    SharedFieldPathMap fpmap(new FieldPathMapT());
    fpmap->emplace_back();
    dt.buildFieldPath(fpmap->back(),"a");
    fpmap->emplace_back();
    dt.buildFieldPath(fpmap->back(), "b");
    fpmap->emplace_back();
    ASSERT_TRUE((*fpmap)[0].size() == 1);
    ASSERT_TRUE((*fpmap)[1].size() == 1);
    ASSERT_TRUE((*fpmap)[2].size() == 0);

    StorageDocument sdoc(std::move(doc), fpmap, 3);
    ASSERT_TRUE(sdoc.valid());

    EXPECT_EQUAL(std::string("foo"), sdoc.getField(0)->getAsString());
    EXPECT_EQUAL(std::string("bar"), sdoc.getField(1)->getAsString());
    EXPECT_TRUE(sdoc.getField(2) == nullptr);
    // test caching
    EXPECT_EQUAL(std::string("foo"), sdoc.getField(0)->getAsString());
    EXPECT_EQUAL(std::string("bar"), sdoc.getField(1)->getAsString());
    EXPECT_TRUE(sdoc.getField(2) == nullptr);

    // set new values
    EXPECT_TRUE(sdoc.setField(0, FieldValue::UP(new StringFieldValue("baz"))));
    EXPECT_EQUAL(std::string("baz"), sdoc.getField(0)->getAsString());
    EXPECT_EQUAL(std::string("bar"), sdoc.getField(1)->getAsString());
    EXPECT_TRUE(sdoc.getField(2) == nullptr);
    EXPECT_TRUE(sdoc.setField(1, FieldValue::UP(new StringFieldValue("qux"))));
    EXPECT_EQUAL(std::string("baz"), sdoc.getField(0)->getAsString());
    EXPECT_EQUAL(std::string("qux"), sdoc.getField(1)->getAsString());
    EXPECT_TRUE(sdoc.getField(2) == nullptr);
    EXPECT_TRUE(sdoc.setField(2, FieldValue::UP(new StringFieldValue("quux"))));
    EXPECT_EQUAL(std::string("baz"), sdoc.getField(0)->getAsString());
    EXPECT_EQUAL(std::string("qux"), sdoc.getField(1)->getAsString());
    EXPECT_EQUAL(std::string("quux"), sdoc.getField(2)->getAsString());

    EXPECT_TRUE(!sdoc.setField(3, FieldValue::UP(new StringFieldValue("thud"))));

    SharedFieldPathMap fim;
    StorageDocument s2(std::make_unique<document::Document>(), fim, 0);
    EXPECT_EQUAL(IdString().toString(), s2.docDoc().getId().toString());
}

void DocumentTest::testStringFieldIdTMap()
{
    StringFieldIdTMap m;
    EXPECT_EQUAL(0u, m.highestFieldNo());
    EXPECT_TRUE(StringFieldIdTMap::npos == m.fieldNo("unknown"));
    m.add("f1");
    EXPECT_EQUAL(0u, m.fieldNo("f1"));
    EXPECT_EQUAL(1u, m.highestFieldNo());
    m.add("f1");
    EXPECT_EQUAL(0u, m.fieldNo("f1"));
    EXPECT_EQUAL(1u, m.highestFieldNo());
    m.add("f2");
    EXPECT_EQUAL(1u, m.fieldNo("f2"));
    EXPECT_EQUAL(2u, m.highestFieldNo());
    m.add("f3", 7);
    EXPECT_EQUAL(7u, m.fieldNo("f3"));
    EXPECT_EQUAL(8u, m.highestFieldNo());
    m.add("f3");
    EXPECT_EQUAL(7u, m.fieldNo("f3"));
    EXPECT_EQUAL(8u, m.highestFieldNo());
    m.add("f2", 13);
    EXPECT_EQUAL(13u, m.fieldNo("f2"));
    EXPECT_EQUAL(14u, m.highestFieldNo());
    m.add("f4");
    EXPECT_EQUAL(3u, m.fieldNo("f4"));
    EXPECT_EQUAL(14u, m.highestFieldNo());
    {
        vespalib::asciistream os;
        StringFieldIdTMap t;
        t.add("b");
        t.add("a");
        os << t;
        EXPECT_EQUAL(vespalib::string("a = 1\nb = 0\n"), os.str());
    }
    
}

int
DocumentTest::Main()
{
    TEST_INIT("document_test");

    testStorageDocument();
    testStringFieldIdTMap();

    TEST_DONE();
}

}

TEST_APPHOOK(vsm::DocumentTest);