diff options
author | Tor Egge <Tor.Egge@online.no> | 2022-10-20 16:58:55 +0200 |
---|---|---|
committer | Tor Egge <Tor.Egge@online.no> | 2022-10-20 16:58:55 +0200 |
commit | e8d09ff331c6d490c0a373c8339f1c52256e91f2 (patch) | |
tree | bdba1775ce90aff68e41836c378f69d5ce90f2aa /searchlib/src/tests | |
parent | 4f9542e708798f2e1156418aac67add4a181fb5a (diff) |
Add schema builder, used by unit tests to make schema based on document type.
Diffstat (limited to 'searchlib/src/tests')
7 files changed, 198 insertions, 101 deletions
diff --git a/searchlib/src/tests/diskindex/fusion/fusion_test.cpp b/searchlib/src/tests/diskindex/fusion/fusion_test.cpp index 54d34c849a0..735dd7e3c14 100644 --- a/searchlib/src/tests/diskindex/fusion/fusion_test.cpp +++ b/searchlib/src/tests/diskindex/fusion/fusion_test.cpp @@ -14,6 +14,7 @@ #include <vespa/searchlib/fef/termfieldmatchdata.h> #include <vespa/searchlib/index/dummyfileheadercontext.h> #include <vespa/searchlib/test/doc_builder.h> +#include <vespa/searchlib/test/schema_builder.h> #include <vespa/searchlib/test/string_field_builder.h> #include <vespa/searchlib/index/schemautil.h> #include <vespa/searchlib/memoryindex/document_inverter.h> @@ -38,6 +39,7 @@ LOG_SETUP("fusion_test"); namespace search { using document::ArrayFieldValue; +using document::DataType; using document::Document; using document::StringFieldValue; using document::WeightedSetFieldValue; @@ -50,9 +52,9 @@ using memoryindex::FieldIndexCollection; using queryeval::SearchIterator; using search::common::FileHeaderContext; using search::index::schema::CollectionType; -using search::index::schema::DataType; using search::index::test::MockFieldLengthInspector; using search::test::DocBuilder; +using search::test::SchemaBuilder; using search::test::StringFieldBuilder; using vespalib::SequencedTaskExecutor; @@ -137,30 +139,10 @@ make_doc10(DocBuilder &b) return doc; } -Schema::IndexField -make_index_field(vespalib::stringref name, CollectionType collection_type, bool interleaved_features) -{ - Schema::IndexField index_field(name, DataType::STRING, collection_type); - index_field.set_interleaved_features(interleaved_features); - return index_field; -} - -Schema -make_schema(bool interleaved_features) -{ - Schema schema; - schema.addIndexField(make_index_field("f0", CollectionType::SINGLE, interleaved_features)); - schema.addIndexField(make_index_field("f1", CollectionType::SINGLE, interleaved_features)); - schema.addIndexField(make_index_field("f2", CollectionType::ARRAY, interleaved_features)); - schema.addIndexField(make_index_field("f3", CollectionType::WEIGHTEDSET, interleaved_features)); - return schema; -} - DocBuilder::AddFieldsType make_add_fields() { return [](auto& header) { using namespace document::config_builder; - using DataType = document::DataType; header.addField("f0", DataType::T_STRING) .addField("f1", DataType::T_STRING) .addField("f2", Array(DataType::T_STRING)) @@ -168,6 +150,13 @@ make_add_fields() }; } +Schema +make_schema(bool interleaved_features) +{ + DocBuilder db(make_add_fields()); + return *SchemaBuilder(db).add_all_indexes(interleaved_features).build(); +} + void assert_interleaved_features(DiskIndex &d, const vespalib::string &field, const vespalib::string &term, uint32_t doc_id, uint32_t exp_num_occs, uint32_t exp_field_length) { @@ -334,7 +323,7 @@ FusionTest::requireThatFusionIsWorking(const vespalib::string &prefix, bool dire iField.getDataType(), CollectionType::SINGLE)); } - schema3.addIndexField(Schema::IndexField("f4", DataType::STRING)); + schema3.addIndexField(Schema::IndexField("f4", search::index::schema::DataType::STRING)); schema.addFieldSet(Schema::FieldSet("nc0"). addField("f0").addField("f1")); schema2.addFieldSet(Schema::FieldSet("nc0"). diff --git a/searchlib/src/tests/memoryindex/document_inverter/document_inverter_test.cpp b/searchlib/src/tests/memoryindex/document_inverter/document_inverter_test.cpp index 85b8fc64304..4862218045f 100644 --- a/searchlib/src/tests/memoryindex/document_inverter/document_inverter_test.cpp +++ b/searchlib/src/tests/memoryindex/document_inverter/document_inverter_test.cpp @@ -7,6 +7,7 @@ #include <vespa/document/repo/configbuilder.h> #include <vespa/searchlib/index/field_length_calculator.h> #include <vespa/searchlib/test/doc_builder.h> +#include <vespa/searchlib/test/schema_builder.h> #include <vespa/searchlib/test/string_field_builder.h> #include <vespa/searchlib/memoryindex/document_inverter_context.h> #include <vespa/searchlib/memoryindex/field_index_remover.h> @@ -23,12 +24,12 @@ namespace search::memoryindex { +using document::DataType; using document::Document; using index::FieldLengthCalculator; using index::Schema; -using index::schema::CollectionType; -using index::schema::DataType; using search::test::DocBuilder; +using search::test::SchemaBuilder; using search::test::StringFieldBuilder; using vespalib::SequencedTaskExecutor; using vespalib::ISequencedTaskExecutor; @@ -39,7 +40,6 @@ DocBuilder::AddFieldsType make_add_fields() { return [](auto& header) { using namespace document::config_builder; - using DataType = document::DataType; header.addField("f0", DataType::T_STRING) .addField("f1", DataType::T_STRING) .addField("f2", Array(DataType::T_STRING)) @@ -105,8 +105,8 @@ VESPA_THREAD_STACK_TAG(invert_executor) VESPA_THREAD_STACK_TAG(push_executor) struct DocumentInverterTest : public ::testing::Test { - Schema _schema; DocBuilder _b; + Schema _schema; std::unique_ptr<ISequencedTaskExecutor> _invertThreads; std::unique_ptr<ISequencedTaskExecutor> _pushThreads; WordStore _word_store; @@ -117,18 +117,9 @@ struct DocumentInverterTest : public ::testing::Test { DocumentInverterContext _inv_context; DocumentInverter _inv; - static Schema makeSchema() { - Schema schema; - schema.addIndexField(Schema::IndexField("f0", DataType::STRING)); - schema.addIndexField(Schema::IndexField("f1", DataType::STRING)); - schema.addIndexField(Schema::IndexField("f2", DataType::STRING, CollectionType::ARRAY)); - schema.addIndexField(Schema::IndexField("f3", DataType::STRING, CollectionType::WEIGHTEDSET)); - return schema; - } - DocumentInverterTest() - : _schema(makeSchema()), - _b(make_add_fields()), + : _b(make_add_fields()), + _schema(*SchemaBuilder(_b).add_all_indexes().build()), _invertThreads(SequencedTaskExecutor::create(invert_executor, 1)), _pushThreads(SequencedTaskExecutor::create(push_executor, 1)), _word_store(), diff --git a/searchlib/src/tests/memoryindex/field_index/field_index_test.cpp b/searchlib/src/tests/memoryindex/field_index/field_index_test.cpp index b9e933ab3e8..6e0c9be0c97 100644 --- a/searchlib/src/tests/memoryindex/field_index/field_index_test.cpp +++ b/searchlib/src/tests/memoryindex/field_index/field_index_test.cpp @@ -21,6 +21,7 @@ #include <vespa/searchlib/memoryindex/posting_iterator.h> #include <vespa/searchlib/queryeval/iterators.h> #include <vespa/searchlib/test/doc_builder.h> +#include <vespa/searchlib/test/schema_builder.h> #include <vespa/searchlib/test/string_field_builder.h> #include <vespa/searchlib/test/index/mock_field_length_inspector.h> #include <vespa/searchlib/test/memoryindex/wrap_inserter.h> @@ -45,15 +46,16 @@ using namespace fef; using namespace index; using document::ArrayFieldValue; +using document::DataType; using document::Document; using document::StringFieldValue; using document::WeightedSetFieldValue; using queryeval::RankedSearchIteratorBase; using queryeval::SearchIterator; using search::index::schema::CollectionType; -using search::index::schema::DataType; using search::index::test::MockFieldLengthInspector; using search::test::DocBuilder; +using search::test::SchemaBuilder; using search::test::StringFieldBuilder; using vespalib::GenerationHandler; using vespalib::ISequencedTaskExecutor; @@ -510,17 +512,16 @@ myCompactFeatures(FieldIndexCollection &fieldIndexes, ISequencedTaskExecutor &pu } Schema -make_single_field_schema() +make_all_index_schema(DocBuilder::AddFieldsType add_fields) { - Schema result; - result.addIndexField(Schema::IndexField("f0", DataType::STRING)); - return result; + DocBuilder db(add_fields); + return *SchemaBuilder(db).add_all_indexes().build(); } DocBuilder::AddFieldsType make_single_add_fields() { - return [](auto& header) { header.addField("f0", document::DataType::T_STRING); }; + return [](auto& header) { header.addField("f0", DataType::T_STRING); }; } template <typename FieldIndexType> @@ -528,7 +529,7 @@ struct FieldIndexTest : public ::testing::Test { Schema schema; FieldIndexType idx; FieldIndexTest() - : schema(make_single_field_schema()), + : schema(make_all_index_schema(make_single_add_fields())), idx(schema, 0) { } @@ -713,22 +714,10 @@ TEST_F(FieldIndexInterleavedFeaturesTest, interleaved_features_are_capped) EXPECT_EQ(std::numeric_limits<uint16_t>::max(), entry.get_field_length()); } -Schema -make_multi_field_schema() -{ - Schema result; - result.addIndexField(Schema::IndexField("f0", DataType::STRING)); - result.addIndexField(Schema::IndexField("f1", DataType::STRING)); - result.addIndexField(Schema::IndexField("f2", DataType::STRING, CollectionType::ARRAY)); - result.addIndexField(Schema::IndexField("f3", DataType::STRING, CollectionType::WEIGHTEDSET)); - return result; -} - DocBuilder::AddFieldsType make_multi_field_add_fields() { return [](auto& header) { using namespace document::config_builder; - using DataType = document::DataType; header.addField("f0", DataType::T_STRING) .addField("f1", DataType::T_STRING) .addField("f2", Array(DataType::T_STRING)) @@ -740,7 +729,7 @@ struct FieldIndexCollectionTest : public ::testing::Test { Schema schema; FieldIndexCollection fic; FieldIndexCollectionTest() - : schema(make_multi_field_schema()), + : schema(make_all_index_schema(make_multi_field_add_fields())), fic(schema, MockFieldLengthInspector()) { } @@ -908,6 +897,7 @@ struct FieldIndexCollectionTypeTest : public ::testing::Test { } static Schema make_schema() { Schema result; + using DataType = search::index::schema::DataType; result.addIndexField(Schema::IndexField("normal", DataType::STRING)); Schema::IndexField interleaved("interleaved", DataType::STRING); interleaved.set_interleaved_features(true); @@ -935,18 +925,18 @@ VESPA_THREAD_STACK_TAG(push_executor) class InverterTest : public ::testing::Test { public: + DocBuilder _b; Schema _schema; FieldIndexCollection _fic; - DocBuilder _b; std::unique_ptr<ISequencedTaskExecutor> _invertThreads; std::unique_ptr<ISequencedTaskExecutor> _pushThreads; DocumentInverterContext _inv_context; DocumentInverter _inv; - InverterTest(const Schema& schema, DocBuilder::AddFieldsType add_fields) - : _schema(schema), + InverterTest(DocBuilder::AddFieldsType add_fields) + : _b(add_fields), + _schema(*SchemaBuilder(_b).add_all_indexes().build()), _fic(_schema, MockFieldLengthInspector()), - _b(add_fields), _invertThreads(SequencedTaskExecutor::create(invert_executor, 2)), _pushThreads(SequencedTaskExecutor::create(push_executor, 2)), _inv_context(_schema, *_invertThreads, *_pushThreads, _fic), @@ -968,7 +958,7 @@ public: class BasicInverterTest : public InverterTest { public: - BasicInverterTest() : InverterTest(make_multi_field_schema(), make_multi_field_add_fields()) {} + BasicInverterTest() : InverterTest(make_multi_field_add_fields()) {} }; TEST_F(BasicInverterTest, require_that_inversion_is_working) @@ -1162,29 +1152,19 @@ TEST_F(BasicInverterTest, require_that_inverter_handles_remove_via_document_remo EXPECT_TRUE(assertPostingList("[]", find("c", 1))); } -Schema -make_uri_schema() -{ - Schema result; - result.addUriIndexFields(Schema::IndexField("iu", DataType::STRING)); - result.addUriIndexFields(Schema::IndexField("iau", DataType::STRING, CollectionType::ARRAY)); - result.addUriIndexFields(Schema::IndexField("iwu", DataType::STRING, CollectionType::WEIGHTEDSET)); - return result; -} - DocBuilder::AddFieldsType make_uri_add_fields() { return [](auto& header) { using namespace document::config_builder; - header.addField("iu", document::DataType::T_URI) - .addField("iau", Array(document::DataType::T_URI)) - .addField("iwu", Wset(document::DataType::T_URI)); + header.addField("iu", DataType::T_URI) + .addField("iau", Array(DataType::T_URI)) + .addField("iwu", Wset(DataType::T_URI)); }; } class UriInverterTest : public InverterTest { public: - UriInverterTest() : InverterTest(make_uri_schema(), make_uri_add_fields()) {} + UriInverterTest() : InverterTest(make_uri_add_fields()) {} }; TEST_F(UriInverterTest, require_that_uri_indexing_is_working) @@ -1260,7 +1240,7 @@ TEST_F(UriInverterTest, require_that_uri_indexing_is_working) class CjkInverterTest : public InverterTest { public: - CjkInverterTest() : InverterTest(make_single_field_schema(), make_single_add_fields()) {} + CjkInverterTest() : InverterTest(make_single_add_fields()) {} }; TEST_F(CjkInverterTest, require_that_cjk_indexing_is_working) diff --git a/searchlib/src/tests/memoryindex/field_inverter/field_inverter_test.cpp b/searchlib/src/tests/memoryindex/field_inverter/field_inverter_test.cpp index db97846dc30..c75d581a055 100644 --- a/searchlib/src/tests/memoryindex/field_inverter/field_inverter_test.cpp +++ b/searchlib/src/tests/memoryindex/field_inverter/field_inverter_test.cpp @@ -8,6 +8,7 @@ #include <vespa/searchcommon/common/schema.h> #include <vespa/searchlib/index/field_length_calculator.h> #include <vespa/searchlib/test/doc_builder.h> +#include <vespa/searchlib/test/schema_builder.h> #include <vespa/searchlib/test/string_field_builder.h> #include <vespa/searchlib/memoryindex/field_index_remover.h> #include <vespa/searchlib/memoryindex/field_inverter.h> @@ -20,12 +21,13 @@ namespace search { using document::ArrayFieldValue; +using document::DataType; using document::Document; using document::WeightedSetFieldValue; using index::Schema; using index::schema::CollectionType; -using index::schema::DataType; using search::test::DocBuilder; +using search::test::SchemaBuilder; using search::test::StringFieldBuilder; using namespace index; @@ -140,8 +142,8 @@ makeCorruptDocument(DocBuilder &b, size_t wordOffset) } struct FieldInverterTest : public ::testing::Test { - Schema _schema; DocBuilder _b; + Schema _schema; WordStore _word_store; FieldIndexRemover _remover; test::OrderedFieldIndexInserterBackend _inserter_backend; @@ -149,20 +151,10 @@ struct FieldInverterTest : public ::testing::Test { std::vector<std::unique_ptr<IOrderedFieldIndexInserter>> _inserters; std::vector<std::unique_ptr<FieldInverter> > _inverters; - static Schema makeSchema() { - Schema schema; - schema.addIndexField(Schema::IndexField("f0", DataType::STRING)); - schema.addIndexField(Schema::IndexField("f1", DataType::STRING)); - schema.addIndexField(Schema::IndexField("f2", DataType::STRING, CollectionType::ARRAY)); - schema.addIndexField(Schema::IndexField("f3", DataType::STRING, CollectionType::WEIGHTEDSET)); - return schema; - } - static DocBuilder::AddFieldsType make_add_fields() { return [](auto& header) { using namespace document::config_builder; - using DataType = document::DataType; header.addField("f0", DataType::T_STRING) .addField("f1", DataType::T_STRING) .addField("f2", Array(DataType::T_STRING)) @@ -171,8 +163,8 @@ struct FieldInverterTest : public ::testing::Test { } FieldInverterTest() - : _schema(makeSchema()), - _b(make_add_fields()), + : _b(make_add_fields()), + _schema(*SchemaBuilder(_b).add_all_indexes().build()), _word_store(), _remover(_word_store), _inserter_backend(), diff --git a/searchlib/src/tests/memoryindex/memory_index/memory_index_test.cpp b/searchlib/src/tests/memoryindex/memory_index/memory_index_test.cpp index b5b8e0c7a7c..536c9314134 100644 --- a/searchlib/src/tests/memoryindex/memory_index/memory_index_test.cpp +++ b/searchlib/src/tests/memoryindex/memory_index/memory_index_test.cpp @@ -9,6 +9,7 @@ #include <vespa/searchlib/fef/termfieldmatchdata.h> #include <vespa/searchlib/index/i_field_length_inspector.h> #include <vespa/searchlib/test/doc_builder.h> +#include <vespa/searchlib/test/schema_builder.h> #include <vespa/searchlib/test/string_field_builder.h> #include <vespa/searchlib/memoryindex/memory_index.h> #include <vespa/searchlib/query/tree/simplequery.h> @@ -26,10 +27,10 @@ #include <vespa/log/log.h> LOG_SETUP("memory_index_test"); +using document::DataType; using document::Document; using document::FieldValue; using search::ScheduleTaskCallback; -using search::index::schema::DataType; using search::index::FieldLengthInfo; using search::index::IFieldLengthInspector; using vespalib::makeLambdaTask; @@ -37,6 +38,7 @@ using search::query::Node; using search::query::SimplePhrase; using search::query::SimpleStringTerm; using search::test::DocBuilder; +using search::test::SchemaBuilder; using search::test::StringFieldBuilder; using vespalib::ISequencedTaskExecutor; using vespalib::SequencedTaskExecutor; @@ -48,10 +50,10 @@ using namespace search::queryeval; //----------------------------------------------------------------------------- struct MySetup : public IFieldLengthInspector { - Schema schema; + std::vector<vespalib::string> fields; std::map<vespalib::string, FieldLengthInfo> field_lengths; MySetup &field(const std::string &name) { - schema.addIndexField(Schema::IndexField(name, DataType::STRING)); + fields.emplace_back(name); return *this; } MySetup& field_length(const vespalib::string& field_name, const FieldLengthInfo& info) { @@ -65,19 +67,23 @@ struct MySetup : public IFieldLengthInspector { } return FieldLengthInfo(); } + void add_fields(document::config_builder::Struct& header) const { - for (uint32_t i = 0; i < schema.getNumIndexFields(); ++i) { - auto& field = schema.getIndexField(i); - header.addField(field.getName(), document::DataType::T_STRING); + for (auto& field : fields) { + header.addField(field, DataType::T_STRING); } } + Schema make_all_index_schema() const { + DocBuilder db([this](auto& header) { add_fields(header); }); + return *SchemaBuilder(db).add_all_indexes().build(); + } + }; //----------------------------------------------------------------------------- struct Index { - Schema schema; vespalib::ThreadStackExecutor _executor; std::unique_ptr<ISequencedTaskExecutor> _invertThreads; std::unique_ptr<ISequencedTaskExecutor> _pushThreads; @@ -147,11 +153,10 @@ VESPA_THREAD_STACK_TAG(invert_executor) VESPA_THREAD_STACK_TAG(push_executor) Index::Index(const MySetup &setup) - : schema(setup.schema), - _executor(1, 128_Ki), + : _executor(1, 128_Ki), _invertThreads(SequencedTaskExecutor::create(invert_executor, 2)), _pushThreads(SequencedTaskExecutor::create(push_executor, 2)), - index(schema, setup, *_invertThreads, *_pushThreads), + index(setup.make_all_index_schema(), setup, *_invertThreads, *_pushThreads), builder([&setup](auto& header) { setup.add_fields(header); }), sfb(builder), builder_doc(), diff --git a/searchlib/src/tests/test/schema_builder/CMakeLists.txt b/searchlib/src/tests/test/schema_builder/CMakeLists.txt new file mode 100644 index 00000000000..6a23d9e283e --- /dev/null +++ b/searchlib/src/tests/test/schema_builder/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(searchlib_schema_builder_test_app TEST + SOURCES + schema_builder_test.cpp + DEPENDS + searchlib_test + GTest::GTest +) +vespa_add_test(NAME searchlib_schema_builder_test_app COMMAND searchlib_schema_builder_test_app) diff --git a/searchlib/src/tests/test/schema_builder/schema_builder_test.cpp b/searchlib/src/tests/test/schema_builder/schema_builder_test.cpp new file mode 100644 index 00000000000..32d1c3d6041 --- /dev/null +++ b/searchlib/src/tests/test/schema_builder/schema_builder_test.cpp @@ -0,0 +1,131 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/searchlib/test/schema_builder.h> +#include <vespa/document/datatype/datatype.h> +#include <vespa/document/repo/configbuilder.h> +#include <vespa/searchcommon/common/schema.h> +#include <vespa/searchlib/test/doc_builder.h> +#include <vespa/vespalib/gtest/gtest.h> + +using search::index::Schema; +using search::index::schema::CollectionType; +using search::test::DocBuilder; +using search::test::SchemaBuilder; + +namespace { + +auto add_all_fields = [](auto& header) + { using namespace document::config_builder; + using document::DataType; + header.addField("int8", DataType::T_BYTE) + .addField("int16", DataType::T_SHORT) + .addField("int32", DataType::T_INT) + .addField("int64", DataType::T_LONG) + .addField("bool", DataType::T_BOOL) + .addField("float", DataType::T_FLOAT) + .addField("double", DataType::T_DOUBLE) + .addField("string", DataType::T_STRING) + .addField("url", DataType::T_URI) + .addTensorField("tensor", "tensor(x{},y{})") + .addField("int32_array", Array(DataType::T_INT)) + .addField("int32_wset", Wset(DataType::T_INT)) + .addField("url_array", Array(DataType::T_URI)) + .addField("url_wset", Wset(DataType::T_URI)); + }; + +} + +class SchemaBuilderTest : public testing::Test +{ +protected: + std::unique_ptr<Schema> schema; + + SchemaBuilderTest(); + ~SchemaBuilderTest(); + void assert_index(vespalib::stringref name, search::index::schema::DataType exp_dt, CollectionType exp_ct); + void assert_all_indexes(); + void assert_attribute(vespalib::stringref name, search::index::schema::DataType exp_dt, CollectionType exp_ct, const vespalib::string exp_tensor_spec = ""); + void assert_all_attributes(); +}; + +SchemaBuilderTest::SchemaBuilderTest() + : schema() +{ +} + +SchemaBuilderTest::~SchemaBuilderTest() = default; + +void +SchemaBuilderTest::assert_index(vespalib::stringref name, search::index::schema::DataType exp_dt, CollectionType exp_ct) +{ + auto field_id = schema->getIndexFieldId(name); + ASSERT_NE(Schema::UNKNOWN_FIELD_ID, field_id); + auto& field = schema->getIndexField(field_id); + EXPECT_EQ(exp_dt, field.getDataType()); + EXPECT_EQ(exp_ct, field.getCollectionType()); +} + +void +SchemaBuilderTest::assert_all_indexes() +{ + using DataType = search::index::schema::DataType; + EXPECT_EQ(25u, schema->getNumIndexFields()); // string and url*, 1 + 3 * 8 + assert_index("string", DataType::STRING, CollectionType::SINGLE); + assert_index("url", DataType::STRING, CollectionType::SINGLE); + assert_index("url.scheme", DataType::STRING, CollectionType::SINGLE); + assert_index("url_array", DataType::STRING, CollectionType::ARRAY); + assert_index("url_wset", DataType::STRING, CollectionType::WEIGHTEDSET); +} + +void +SchemaBuilderTest::assert_attribute(vespalib::stringref name, search::index::schema::DataType exp_dt, CollectionType exp_ct, const vespalib::string exp_tensor_spec) +{ + auto field_id = schema->getAttributeFieldId(name); + ASSERT_NE(Schema::UNKNOWN_FIELD_ID, field_id); + auto& field = schema->getAttributeField(field_id); + EXPECT_EQ(exp_dt, field.getDataType()); + EXPECT_EQ(exp_ct, field.getCollectionType()); + EXPECT_EQ(exp_tensor_spec, field.get_tensor_spec()); +} + +void +SchemaBuilderTest::assert_all_attributes() +{ + using DataType = search::index::schema::DataType; + EXPECT_EQ(11u, schema->getNumAttributeFields()); // all but url*, 14 - 3 + assert_attribute("int8", DataType::INT8, CollectionType::SINGLE); + assert_attribute("int16", DataType::INT16, CollectionType::SINGLE); + assert_attribute("int32", DataType::INT32, CollectionType::SINGLE); + assert_attribute("int64", DataType::INT64, CollectionType::SINGLE); + assert_attribute("bool", DataType::BOOL, CollectionType::SINGLE); + assert_attribute("float", DataType::FLOAT, CollectionType::SINGLE); + assert_attribute("double", DataType::DOUBLE, CollectionType::SINGLE); + assert_attribute("string", DataType::STRING, CollectionType::SINGLE); + assert_attribute("tensor", DataType::TENSOR, CollectionType::SINGLE, "tensor(x{},y{})"); + assert_attribute("int32_array", DataType::INT32, CollectionType::ARRAY); + assert_attribute("int32_wset", DataType::INT32, CollectionType::WEIGHTEDSET); +} + +TEST_F(SchemaBuilderTest, all_fields) +{ + DocBuilder db(add_all_fields); + schema = SchemaBuilder(db).add_indexes({"string", "url", "url_array", "url_wset"}).add_attributes({"int8", "int16", "int32", "int64", "bool", "float", "double", "string", "tensor", "int32_array", "int32_wset"}).build(); + assert_all_indexes(); + assert_all_attributes(); +} + +TEST_F(SchemaBuilderTest, all_indexes_auto) +{ + DocBuilder db(add_all_fields); + schema = SchemaBuilder(db).add_all_indexes().build(); + assert_all_indexes(); +} + +TEST_F(SchemaBuilderTest, all_attributes_auto) +{ + DocBuilder db(add_all_fields); + schema = SchemaBuilder(db).add_all_attributes().build(); + assert_all_attributes(); +} + +GTEST_MAIN_RUN_ALL_TESTS() |