add_unit.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 IndexAddTest : 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. IndexAddTest() {
  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("add");
  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. auto status = index->vdb_->DropDB();
  44. EXPECT_EQ(0, status.GetCode());
  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 *IndexAddTest::index = nullptr;
  60. DocumentID IndexAddTest::document_id = 1;
  61. TEST_F(IndexAddTest, AddOneDocument) {
  62. ASSERT_EQ(documents.size(), 0);
  63. for (int i = 0; i < 10; i++)
  64. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "a", 1, 1, 2));
  65. bool ret =
  66. index->index_writer_->AddDocuments(table, documents, nullptr, nullptr);
  67. EXPECT_TRUE(ret);
  68. for (auto du : documents) {
  69. EXPECT_EQ(0, du->Status().GetCode());
  70. }
  71. }
  72. TEST_F(IndexAddTest, AddTwiceDocument) {
  73. ASSERT_EQ(documents.size(), 0);
  74. for (int i = 0; i < 10; i++)
  75. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "a", 1, 1, 2));
  76. bool ret =
  77. index->index_writer_->AddDocuments(table, documents, nullptr, nullptr);
  78. EXPECT_TRUE(ret);
  79. for (auto du : documents) {
  80. EXPECT_EQ(0, du->Status().GetCode());
  81. }
  82. ret = index->index_writer_->AddDocuments(table, documents, nullptr, nullptr);
  83. EXPECT_FALSE(ret);
  84. for (auto du : documents) {
  85. EXPECT_EQ(wwsearch::kDocumentExistStatus, du->Status().GetCode());
  86. }
  87. }
  88. } // namespace wwsearch