diff options
author | Tor Egge <Tor.Egge@online.no> | 2024-02-15 16:30:49 +0100 |
---|---|---|
committer | Tor Egge <Tor.Egge@online.no> | 2024-02-15 16:32:24 +0100 |
commit | 296732b5d66e1383010b334cae4e9cd958c1baa9 (patch) | |
tree | 7d1e0f2a0b525e1a7db7940354cceeaaa0589d63 /searchlib/src/tests/attribute | |
parent | ebba2fa2e5d4a8e59430c2d5ae563e99e10912f5 (diff) |
Add predicate attribute unit test.
Diffstat (limited to 'searchlib/src/tests/attribute')
-rw-r--r-- | searchlib/src/tests/attribute/predicate_attribute/CMakeLists.txt | 9 | ||||
-rw-r--r-- | searchlib/src/tests/attribute/predicate_attribute/predicate_attribute_test.cpp | 121 |
2 files changed, 130 insertions, 0 deletions
diff --git a/searchlib/src/tests/attribute/predicate_attribute/CMakeLists.txt b/searchlib/src/tests/attribute/predicate_attribute/CMakeLists.txt new file mode 100644 index 00000000000..eb2c99a5d81 --- /dev/null +++ b/searchlib/src/tests/attribute/predicate_attribute/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(searchlib_predicate_attribute_test_app TEST + SOURCES + predicate_attribute_test.cpp + DEPENDS + searchlib + GTest::gtest +) +vespa_add_test(NAME searchlib_predicate_attribute_test_app COMMAND searchlib_predicate_attribute_test_app) diff --git a/searchlib/src/tests/attribute/predicate_attribute/predicate_attribute_test.cpp b/searchlib/src/tests/attribute/predicate_attribute/predicate_attribute_test.cpp new file mode 100644 index 00000000000..077c5a7b455 --- /dev/null +++ b/searchlib/src/tests/attribute/predicate_attribute/predicate_attribute_test.cpp @@ -0,0 +1,121 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/document/fieldvalue/predicatefieldvalue.h> +#include <vespa/document/predicate/predicate_slime_builder.h> +#include <vespa/searchcommon/attribute/config.h> +#include <vespa/searchlib/attribute/attributefactory.h> +#include <vespa/searchlib/attribute/attributevector.h> +#include <vespa/searchlib/attribute/predicate_attribute.h> +#include <vespa/vespalib/gtest/gtest.h> +#include <filesystem> +#include <memory> +#include <sstream> + +using document::FieldValue; +using document::PredicateSlimeBuilder; +using document::PredicateFieldValue; +using search::AttributeFactory; +using search::AttributeVector; +using search::PredicateAttribute; +using search::attribute::Config; +using search::attribute::BasicType; + +namespace { + +std::filesystem::path tmp_dir("tmp"); +std::string attr_name("test"); + +void remove_tmp_dir() { + std::filesystem::remove_all(tmp_dir); +} + +void make_tmp_dir() { + std::filesystem::create_directories(tmp_dir); +} + +const Config predicate(BasicType::Type::PREDICATE); + +Config get_predicate_with_arity(uint32_t arity) +{ + Config ret(predicate); + search::attribute::PredicateParams predicateParams; + predicateParams.setArity(arity); + ret.setPredicateParams(predicateParams); + return ret; +} + +std::shared_ptr<AttributeVector> +make_attribute(vespalib::stringref name, const Config& cfg, bool setup) +{ + auto attribute = AttributeFactory:: + createAttribute(name, cfg); + if (attribute && setup) { + attribute->addReservedDoc(); + } + return attribute; +} + +std::string +fv_as_string(const FieldValue& val) +{ + std::ostringstream os; + val.print(os, false, ""); + return os.str(); +} + +} + +class PredicateAttributeTest : public ::testing::Test +{ +protected: + PredicateAttributeTest(); + ~PredicateAttributeTest(); + void SetUp() override; + void TearDown() override; +}; + +PredicateAttributeTest::PredicateAttributeTest() + : ::testing::Test() +{ +} + +PredicateAttributeTest::~PredicateAttributeTest() +{ +} + +void +PredicateAttributeTest::SetUp() +{ + make_tmp_dir(); +} + +void +PredicateAttributeTest::TearDown() +{ + remove_tmp_dir(); +} + +TEST_F(PredicateAttributeTest, save_and_load_predicate_attribute) +{ + auto cfg = get_predicate_with_arity(2); + auto attr = make_attribute(attr_name, cfg, true); + auto& pattr = dynamic_cast<PredicateAttribute&>(*attr); + PredicateSlimeBuilder builder; + builder.neg().feature("foo").value("bar").value("baz"); + PredicateFieldValue val(builder.build()); + EXPECT_EQ("'foo' not in ['bar','baz']\n", fv_as_string(val)); + attr->addDocs(10); + pattr.updateValue(1, val); + attr->commit(); + EXPECT_TRUE(attr->isLoaded()); + std::filesystem::path file_name(tmp_dir); + file_name.append(attr_name); + attr->save(file_name.native()); + auto attr2 = make_attribute(file_name.native(), cfg, false); + EXPECT_FALSE(attr2->isLoaded()); + EXPECT_TRUE(attr2->load()); + EXPECT_TRUE(attr2->isLoaded()); + EXPECT_EQ(11, attr2->getCommittedDocIdLimit()); +} + +GTEST_MAIN_RUN_ALL_TESTS() |