1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "CacheCursorTest"
16 #include <gtest/gtest.h>
17 #include "log_print.h"
18 #include "cache_cursor.h"
19 #include "store/general_value.h"
20 using namespace testing::ext;
21 using namespace OHOS::DistributedData;
22 using namespace OHOS::DistributedRdb;
23
24 namespace OHOS::Test {
25 namespace DistributedDataTest {
26 static constexpr int MAX_DATA_NUM = 100;
27 static constexpr int AGE = 25;
28 static constexpr const char *NAME = "tony";
29 static constexpr const char *PHONENUMBER = "10086";
30 class CacheCursorTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33 static void TearDownTestCase(void);
34 void SetUp();
35 void TearDown();
36 static std::shared_ptr<CacheCursor> GetCursor();
37 protected:
38 CacheCursorTest();
39 void InitData();
40 static std::shared_ptr<CacheCursor> cursor_;
41 };
42
43 std::shared_ptr<CacheCursor> CacheCursorTest::cursor_ = nullptr;
44
CacheCursorTest()45 CacheCursorTest::CacheCursorTest() { }
46
GetCursor()47 std::shared_ptr<CacheCursor> CacheCursorTest::GetCursor()
48 {
49 return cursor_;
50 }
51
SetUpTestCase(void)52 void CacheCursorTest::SetUpTestCase(void)
53 {
54 std::vector<VBucket> records;
55 for (int i = 0; i < MAX_DATA_NUM; i++) {
56 VBucket record;
57 record["identifier"] = i;
58 record["name"] = NAME;
59 record["age"] = AGE;
60 record["phoneNumber"] = PHONENUMBER;
61 records.push_back(record);
62 }
63 cursor_ = std::make_shared<CacheCursor>(std::move(records));
64 }
65
TearDownTestCase()66 void CacheCursorTest::TearDownTestCase() {}
67
SetUp()68 void CacheCursorTest::SetUp() {}
69
TearDown()70 void CacheCursorTest::TearDown() {}
71
72 HWTEST_F(CacheCursorTest, CacheCursorTest_001, TestSize.Level0)
73 {
74 auto cursor = CacheCursorTest::GetCursor();
75 ASSERT_NE(cursor, nullptr);
76 std::vector<std::string> expectedNames {"age", "identifier", "name", "phoneNumber"};
77 std::vector<std::string> names;
78 auto err = cursor->GetColumnNames(names);
79 EXPECT_EQ(names, expectedNames);
80 std::string colName;
81 err = cursor->GetColumnName(4, colName);
82 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
83
84 int type = cursor->GetColumnType(0);
85 EXPECT_EQ(type, 1);
86 type = cursor->GetColumnType(4);
87 EXPECT_EQ(type, -1);
88
89 int count = cursor->GetCount();
90 EXPECT_EQ(count, 100);
91
92 err = cursor->MoveToFirst();
93 EXPECT_EQ(err, GeneralError::E_OK);
94
95 err = cursor->MoveToNext();
96 EXPECT_EQ(err, GeneralError::E_OK);
97
98 for (int i = 2; i < count; i++) {
99 err = cursor->MoveToNext();
100 }
101 EXPECT_EQ(err, GeneralError::E_OK);
102 err = cursor->MoveToNext();
103 EXPECT_EQ(err, GeneralError::E_ERROR);
104
105 err = cursor->MoveToPrev();
106 EXPECT_EQ(err, GeneralError::E_NOT_SUPPORT);
107 }
108
109 HWTEST_F(CacheCursorTest, CacheCursorTest_002, TestSize.Level0)
110 {
111 auto cursor = CacheCursorTest::GetCursor();
112 ASSERT_NE(cursor, nullptr);
113 auto err = cursor->MoveToFirst();
114
115 DistributedData::VBucket data;
116 err = cursor->GetEntry(data);
117 EXPECT_EQ(err, GeneralError::E_OK);
118
119 int64_t identifier = *std::get_if<int64_t>(&data["identifier"]);
120 EXPECT_EQ(identifier, 0);
121 std::string name = *std::get_if<std::string>(&data["name"]);
122 EXPECT_EQ(name, "Tony");
123 int64_t age = *std::get_if<int64_t>(&data["age"]);
124 EXPECT_EQ(age, 25);
125 std::string phoneNumber = *std::get_if<std::string>(&data["phoneNumber"]);
126 EXPECT_EQ(phoneNumber, "10086");
127
128 while (err == GeneralError::E_OK) {
129 cursor->GetRow(data);
130 err = cursor->MoveToNext();
131 }
132
133 identifier = *std::get_if<int64_t>(&data["identifier"]);
134 EXPECT_EQ(identifier, 99);
135
136 DistributedData::Value value;
137 err = cursor->Get(0, value);
138 age = *std::get_if<int64_t>(&value);
139 EXPECT_EQ(age, 25);
140
141 err = cursor->Get("name", value);
142 name = *std::get_if<std::string>(&value);
143 EXPECT_EQ(name, "Tony");
144
145 bool ret = cursor->IsEnd();
146 EXPECT_EQ(ret, false);
147
148 err = cursor->Close();
149 EXPECT_EQ(err, GeneralError::E_OK);
150 }
151 }
152 }