1
0

replace_unit.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #include <gtest/gtest.h>
  19. #include "include/index_wrapper.h"
  20. #include "unittest_util.h"
  21. namespace wwsearch {
  22. class IndexReplaceTest : public ::testing::Test {
  23. public:
  24. static DefaultIndexWrapper *index;
  25. static uint64_t document_id;
  26. wwsearch::TableID table;
  27. std::vector<DocumentUpdater *> documents;
  28. std::list<DocumentID> match_documentsid;
  29. public:
  30. IndexReplaceTest() {
  31. table.business_type = 1;
  32. table.partition_set = 1;
  33. }
  34. static void SetUpTestCase() {
  35. index = new DefaultIndexWrapper();
  36. index->DBParams().path = std::string("/tmp/unit_") + std::string("replace");
  37. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  38. : wwsearch::kSearchLogLevelError);
  39. auto status = index->Open(g_use_rocksdb, g_use_compression);
  40. ASSERT_TRUE(status.GetCode() == 0);
  41. }
  42. static void TearDownTestCase() {
  43. if (index != nullptr) {
  44. index->vdb_->DropDB();
  45. delete index;
  46. index = nullptr;
  47. }
  48. }
  49. virtual void SetUp() override { table.partition_set++; }
  50. virtual void TearDown() override {
  51. for (auto du : documents) {
  52. delete du;
  53. }
  54. documents.clear();
  55. }
  56. uint64_t GetDocumentID() { return document_id++; }
  57. private:
  58. };
  59. DefaultIndexWrapper *IndexReplaceTest::index = nullptr;
  60. DocumentID IndexReplaceTest::document_id = 1;
  61. TEST_F(IndexReplaceTest, AddAndReplaceDocument) {
  62. DocumentID document_id = GetDocumentID();
  63. auto du = TestUtil::NewDocument(document_id, "abc", 1, 1, 2);
  64. {
  65. documents.push_back(du);
  66. bool ret =
  67. index->index_writer_->AddDocuments(table, documents, nullptr, nullptr);
  68. EXPECT_TRUE(ret); // document not exist
  69. }
  70. du = TestUtil::NewDocument(document_id, "efg", 1, 1, 2, 5);
  71. {
  72. for (auto document : documents) delete document;
  73. documents.clear();
  74. documents.push_back(du);
  75. bool ret = index->index_writer_->ReplaceDocuments(table, documents, nullptr,
  76. nullptr);
  77. EXPECT_TRUE(ret);
  78. }
  79. wwsearch::Searcher searcher(&index->Config());
  80. {
  81. match_documentsid.clear();
  82. wwsearch::BooleanQuery query1(1, "abc");
  83. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  84. match_documentsid);
  85. EXPECT_EQ(0, status.GetCode());
  86. EXPECT_EQ(0, match_documentsid.size());
  87. }
  88. {
  89. match_documentsid.clear();
  90. wwsearch::BooleanQuery query1(6, "efg");
  91. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  92. match_documentsid);
  93. EXPECT_EQ(0, status.GetCode());
  94. EXPECT_EQ(1, match_documentsid.size());
  95. }
  96. }
  97. } // namespace wwsearch