delete_unit.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 IndexDeleteTest : 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. public:
  29. IndexDeleteTest() {
  30. table.business_type = 1;
  31. table.partition_set = 1;
  32. }
  33. static void SetUpTestCase() {
  34. index = new DefaultIndexWrapper();
  35. index->DBParams().path = std::string("/tmp/unit_") + std::string("delete");
  36. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  37. : wwsearch::kSearchLogLevelError);
  38. auto status = index->Open(g_use_rocksdb, g_use_compression);
  39. ASSERT_TRUE(status.GetCode() == 0);
  40. }
  41. static void TearDownTestCase() {
  42. if (index != nullptr) {
  43. index->vdb_->DropDB();
  44. delete index;
  45. index = nullptr;
  46. }
  47. }
  48. virtual void SetUp() override { table.partition_set++; }
  49. virtual void TearDown() override {
  50. for (auto du : documents) {
  51. delete du;
  52. }
  53. documents.clear();
  54. }
  55. uint64_t GetDocumentID() { return document_id++; }
  56. private:
  57. };
  58. DefaultIndexWrapper *IndexDeleteTest::index = nullptr;
  59. DocumentID IndexDeleteTest::document_id = 1;
  60. TEST_F(IndexDeleteTest, AddOneDocument) {
  61. for (int i = 0; i < 10; i++)
  62. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "a", 1, 1, 2));
  63. bool ret =
  64. index->index_writer_->DeleteDocuments(table, documents, nullptr, nullptr);
  65. EXPECT_TRUE(ret);
  66. for (auto du : documents) {
  67. EXPECT_EQ(wwsearch::kDocumentNotExistStatus, du->Status().GetCode());
  68. }
  69. }
  70. TEST_F(IndexDeleteTest, AddTwiceDocument) {
  71. for (int i = 0; i < 10; i++)
  72. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "a", 1, 1, 2));
  73. bool ret =
  74. index->index_writer_->AddDocuments(table, documents, nullptr, nullptr);
  75. EXPECT_TRUE(ret);
  76. for (auto du : documents) {
  77. EXPECT_EQ(0, du->Status().GetCode());
  78. }
  79. ret =
  80. index->index_writer_->DeleteDocuments(table, documents, nullptr, nullptr);
  81. EXPECT_TRUE(ret);
  82. for (auto du : documents) {
  83. EXPECT_EQ(0, du->Status().GetCode());
  84. }
  85. }
  86. } // namespace wwsearch