1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sys_adapter_dbstore_test.h"
17 #include <vector>
18
19 namespace OHOS {
20 namespace HiviewDFX {
SetUpTestCase()21 void SysAdapterDbstoreTest::SetUpTestCase() {}
22
TearDownTestCase()23 void SysAdapterDbstoreTest::TearDownTestCase() {}
24
SetUp()25 void SysAdapterDbstoreTest::SetUp()
26 {
27 storeMgr_ = std::make_unique<StoreManager>();
28 }
29
TearDown()30 void SysAdapterDbstoreTest::TearDown() {}
31
32 /**
33 * @tc.name: StoreManagerTest001
34 * @tc.desc: test GetDocStore
35 * @tc.type: FUNC
36 * @tc.require: issues/I658Z7
37 */
38 HWTEST_F(SysAdapterDbstoreTest, StoreManagerTest001, testing::ext::TestSize.Level3)
39 {
40 Option option;
41 option.db = "/data/test/test.db";
42 option.flag = Option::NO_TRIM_ON_CLOSE;
43 auto dbPtr = storeMgr_->GetDocStore(option);
44 ASSERT_TRUE(dbPtr != nullptr);
45 Entry entry;
46 entry.id = 0;
47 entry.value = "test1";
48 ASSERT_TRUE(dbPtr->Put(entry)!= 0);
49 Entry entry1;
50 entry1.id = 0;
51 entry1.value = R"~({"domain_":"demo","name_":"StoreManagerTest001","type_":1,"tz_":8,
52 "time_":1620271291200,"pid_":6527,"tid_":6527,"traceid_":"f0ed5160bb2df4b","spanid_":"10","pspanid_":"20",
53 "trace_flag_":4,"keyBool":1,"keyChar":97})~";
54 ASSERT_TRUE(dbPtr->Put(entry1) == 0);
55 std::vector<Entry> entrys;
56 Entry entry2;
57 entry2.id = 0;
58 entry2.value = R"~({"domain_":"demo","name_":"StoreManagerTest001","type_":1,"tz_":8,
59 "time_":1620271291200,"pid_":6527,"tid_":6527,"traceid_":"f0ed5160bb2df4b","spanid_":"10","pspanid_":"20",
60 "trace_flag_":4,"keyBool":1,"keyChar":97})~";
61 entrys.push_back(entry2);
62 ASSERT_TRUE(dbPtr->PutBatch(entrys) == 0);
63 std::cout << "num:" << dbPtr->GetNum() << std::endl;
64 ASSERT_TRUE(dbPtr->GetNum() == 2);
65 Option option2;
66 option2.db = "/data/test/test.db";
67 option2.flag = Option::RDONLY;
68 auto dbPtr2 = storeMgr_->GetDocStore(option2);
69 ASSERT_TRUE(dbPtr2 != nullptr);
70 Option option3;
71 option3.db = "/data/test/test.db";
72 option3.flag = Option::TRUNC;
73 auto dbPtr3 = storeMgr_->GetDocStore(option3);
74 ASSERT_TRUE(dbPtr3 != nullptr);
75 Option option4;
76 option4.db = "/data/test/test1.db";
77 Option option5;
78 option5.db = "/data/test/test.db";
79 std::string bakupfile = "/data/test/back_up_test.db";
80 std::string bakupfile1 = "/data/test/back_up_test.db1";
81 ASSERT_TRUE(storeMgr_->CloseDocStore(option4) != 0);
82 ASSERT_TRUE(storeMgr_->OnlineBackupDocStore(option4, bakupfile1) != 0);
83 storeMgr_->OnlineBackupDocStore(option5, bakupfile);
84 ASSERT_TRUE(storeMgr_->DeleteDocStore(option4) != 0);
85 ASSERT_TRUE(storeMgr_->DeleteDocStore(option5) == 0);
86 }
87 } // namespace HiviewDFX
88 } // namespace OHOS