aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/tests/documenttypetestcase.cpp
blob: df6d8aa900f47720be4d884c7b009215b2a5738c (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/document/base/testdocrepo.h>
#include <vespa/document/datatype/documenttype.h>
#include <vespa/document/fieldvalue/fieldvalues.h>
#include <vespa/document/repo/configbuilder.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/vespalib/testkit/test_kit.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using document::config_builder::Struct;
using document::config_builder::Wset;
using document::config_builder::Array;
using document::config_builder::Map;
using namespace ::testing;


namespace document {

TEST(DocumentTypeTest, testSetGet)
{
  DocumentType docType("doctypetestdoc", 0);

  docType.addField(Field("stringattr", 3, *DataType::STRING));
  docType.addField(Field("nalle", 0, *DataType::INT));

  const Field& fetch1 = docType.getField("stringattr");
  const Field& fetch2 = docType.getField("stringattr");

  EXPECT_TRUE(fetch1 == fetch2);
  EXPECT_TRUE(fetch1.getName() == "stringattr");

  const Field& fetch3 = docType.getField(3);

  EXPECT_TRUE(fetch1 == fetch3);

  const Field& fetch4 = docType.getField(0);

  EXPECT_TRUE(fetch4 != fetch1);
}

void
categorizeFields(const Field::Set& fields,
                 std::vector<const Field*>& headers)
{
    for (const Field * field : fields)
    {
        headers.push_back(field);
    }
}

TEST(DocumentTypeTest, testInheritanceConfig)
{
    DocumentTypeRepo
        repo(readDocumenttypesConfig(TEST_PATH("data/inheritancetest.cfg")));
    {
        const DocumentType* type(repo.getDocumentType("music"));
        EXPECT_TRUE(type != NULL);
    }

    {
        const DocumentType* type(repo.getDocumentType("books"));
        EXPECT_TRUE(type != NULL);
    }
}

TEST(DocumentTypeTest, testHeaderContent)
{
    DocumentTypeRepo
        repo(readDocumenttypesConfig(TEST_PATH("data/doctypesconfigtest.cfg")));

    const DocumentType* type(repo.getDocumentType("derived"));

    Field::Set fields = type->getFieldsType().getFieldSet();

    std::vector<const Field*> headers;
    categorizeFields(fields, headers);

    EXPECT_TRUE(headers.size() == 6);
    EXPECT_TRUE(headers[0]->getName() == "field1");
    EXPECT_TRUE(headers[1]->getName() == "field2");
    EXPECT_TRUE(headers[2]->getName() == "field3");
    EXPECT_TRUE(headers[3]->getName() == "field4");
    EXPECT_TRUE(headers[4]->getName() == "field5");
    EXPECT_TRUE(headers[5]->getName() == "fieldarray1");
}

TEST(DocumentTypeTest, testMultipleInheritance)
{
    config_builder::DocumenttypesConfigBuilderHelper builder;
    builder.document(42, "test1", Struct("test1.header"),
                     Struct("test1.body")
                     .addField("stringattr", DataType::T_STRING)
                     .addField("nalle", DataType::T_INT));
    builder.document(43, "test2", Struct("test2.header"),
                     Struct("test2.body")
                     .addField("stringattr", DataType::T_STRING)
                     .addField("tmp", DataType::T_STRING)
                     .addField("tall", DataType::T_INT));
    builder.document(44, "test3",
                     Struct("test3.header"), Struct("test3.body"))
        .inherit(42).inherit(43);
    DocumentTypeRepo repo(builder.config());
    const DocumentType* docType3(repo.getDocumentType("test3"));

    EXPECT_TRUE(docType3->hasField("stringattr"));
    EXPECT_TRUE(docType3->hasField("nalle"));
    EXPECT_TRUE(docType3->hasField("tmp"));
    EXPECT_TRUE(docType3->hasField("tall"));

    Document doc(repo, *docType3, DocumentId("id:ns:test3::1"));

    IntFieldValue intVal(3);
    doc.setValue(doc.getField("nalle"), intVal);

    StringFieldValue stringVal("tmp");
    doc.setValue(doc.getField("tmp"), stringVal);

    EXPECT_TRUE(doc.getValue(doc.getField("nalle"))->getAsInt()==3);
    EXPECT_EQ(vespalib::string("tmp"),
              doc.getValue(doc.getField("tmp"))->getAsString());
}

namespace {

bool containsField(const DocumentType::FieldSet &fieldSet, const vespalib::string &field) {
    return fieldSet.getFields().find(field) != fieldSet.getFields().end();
}

}

TEST(DocumentTypeTest, testFieldSetCanContainFieldsNotInDocType)
{
    DocumentType docType("test1");
    docType.addField(Field("stringattr", 3, *DataType::STRING));
    docType.addField(Field("nalle", 0, *DataType::INT));
    {
        DocumentType::FieldSet::Fields tmp;
        tmp.insert("nalle");
        tmp.insert("nulle");
        docType.addFieldSet("a", tmp);
    }
    auto fieldSet = docType.getFieldSet("a");
    EXPECT_EQ((size_t)2, fieldSet->getFields().size());
    EXPECT_TRUE(containsField(*fieldSet, "nalle"));
    EXPECT_TRUE(containsField(*fieldSet, "nulle"));
}

TEST(DocumentTypeTest, testInheritance)
{
        // Inheritance of conflicting but equal datatype ok
    DocumentType docType("test1");
    docType.addField(Field("stringattr", 3, *DataType::STRING));
    docType.addField(Field("nalle", 0, *DataType::INT));

    DocumentType docType2("test2");
    docType2.addField(Field("stringattr", 3, *DataType::STRING));
    docType2.addField(Field("tmp", 5, *DataType::STRING));
    docType2.addField(Field("tall", 10, *DataType::INT));

    docType.inherit(docType2);
    EXPECT_TRUE(docType.hasField("stringattr"));
    EXPECT_TRUE(docType.hasField("nalle"));
    EXPECT_TRUE(docType.hasField("tmp"));
    EXPECT_TRUE(docType.hasField("tall"));

    DocumentType docType3("test3");
    docType3.addField(Field("stringattr", 3, *DataType::RAW));
    docType3.addField(Field("tall", 10, *DataType::INT));

    try{
        docType2.inherit(docType3);
        // FAIL() << "Supposed to fail";
    } catch (std::exception& e) {
        EXPECT_EQ(std::string("foo"), std::string(e.what()));
    }

    try{
        docType.inherit(docType3);
        // FAIL() << "Supposed to fail";
    } catch (std::exception& e) {
        EXPECT_EQ(std::string("foo"), std::string(e.what()));
    }

    DocumentType docType4("test4");
    docType4.inherit(docType3);

    EXPECT_TRUE(docType4.hasField("stringattr"));
    EXPECT_TRUE(docType4.hasField("tall"));

    try{
        docType3.inherit(docType4);
        FAIL() << "Accepted cyclic inheritance";
    } catch (std::exception& e) {
        EXPECT_THAT(e.what(), HasSubstr("Cannot add cyclic dependencies"));
    }

    DocumentType docType5("test5");
    docType5.addField(Field("stringattr", 20, *DataType::RAW));

    try{
        docType4.inherit(docType5);
        // FAIL() << "Supposed to fail";
    } catch (std::exception& e) {
        EXPECT_EQ(std::string("foo"), std::string(e.what()));
    }
}

TEST(DocumentTypeTest,testOutputOperator)
{
    DocumentType docType("test1");
    std::ostringstream ost;
    ost << docType;
    std::string expected("DocumentType(test1)");
    EXPECT_EQ(expected, ost.str());
}

} // document