1 /*
2 * Copyright (c) 2024 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
17 #include "cache_cursor.h"
18
19 #include "gtest/gtest.h"
20 #include "log_print.h"
21 #include "store/cursor.h"
22 #include "store/general_value.h"
23
24 using namespace OHOS;
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::DistributedRdb;
28 using namespace OHOS::DistributedData;
29 namespace OHOS::Test {
30 namespace DistributedRDBTest {
31 static constexpr int MAX_DATA_NUM = 100;
32 static constexpr int AGE = 25;
33 static constexpr const char *NAME = "tony";
34 static constexpr const char *PHONENUMBER = "10086";
35 class CacheCursorTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
TearDownTestCase(void)38 static void TearDownTestCase(void){};
SetUp()39 void SetUp(){};
TearDown()40 void TearDown(){};
41 static std::shared_ptr<CacheCursor> GetCursor();
42
43 protected:
44 CacheCursorTest();
45 static std::shared_ptr<CacheCursor> cursor_;
46 };
47
48 std::shared_ptr<CacheCursor> CacheCursorTest::cursor_ = nullptr;
49
CacheCursorTest()50 CacheCursorTest::CacheCursorTest() {}
51
GetCursor()52 std::shared_ptr<CacheCursor> CacheCursorTest::GetCursor()
53 {
54 return cursor_;
55 }
56
SetUpTestCase(void)57 void CacheCursorTest::SetUpTestCase(void)
58 {
59 std::vector<VBucket> records;
60 for (int i = 0; i < MAX_DATA_NUM; i++) {
61 VBucket record;
62 record["identifier"] = i;
63 record["name"] = NAME;
64 record["age"] = AGE;
65 record["phoneNumber"] = PHONENUMBER;
66 records.push_back(record);
67 }
68 cursor_ = std::make_shared<CacheCursor>(std::move(records));
69 };
70
71 /**
72 * @tc.name: CacheCursorTest001
73 * @tc.desc: RdbCacheCursor function test.
74 * @tc.type: FUNC
75 * @tc.require:
76 * @tc.author: SQL
77 */
HWTEST_F(CacheCursorTest, CacheCursorTest001, TestSize.Level1)78 HWTEST_F(CacheCursorTest, CacheCursorTest001, TestSize.Level1)
79 {
80 auto cursor = CacheCursorTest::GetCursor();
81 EXPECT_NE(cursor, nullptr);
82 std::vector<std::string> expectedNames = { "age", "identifier", "name", "phoneNumber" };
83 std::vector<std::string> names;
84 auto result = cursor->GetColumnNames(names);
85 EXPECT_EQ(result, GeneralError::E_OK);
86 EXPECT_EQ(names, expectedNames);
87
88 std::string colName;
89 auto err = cursor->GetColumnName(4, colName);
90 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
91 err = cursor->GetColumnName(-1, colName);
92 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
93 err = cursor->GetColumnName(1, colName);
94 EXPECT_EQ(err, GeneralError::E_OK);
95
96 int type = cursor->GetColumnType(0);
97 EXPECT_EQ(type, 1);
98 type = cursor->GetColumnType(4);
99 EXPECT_EQ(type, -1);
100 type = cursor->GetColumnType(-1);
101 EXPECT_EQ(type, -1);
102
103 int count = cursor->GetCount();
104 EXPECT_EQ(count, MAX_DATA_NUM);
105
106 err = cursor->MoveToFirst();
107 EXPECT_EQ(err, GeneralError::E_OK);
108
109 err = cursor->MoveToNext();
110 EXPECT_EQ(err, GeneralError::E_OK);
111 for (int i = 2; i < count; i++) {
112 err = cursor->MoveToNext();
113 }
114 err = cursor->MoveToNext();
115 EXPECT_EQ(err, GeneralError::E_ERROR);
116
117 err = cursor->MoveToPrev();
118 EXPECT_EQ(err, GeneralError::E_NOT_SUPPORT);
119 }
120
121 /**
122 * @tc.name: UnCacheCursorTest001
123 * @tc.desc: RdbCacheCursor function error test.
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author: SQL
127 */
HWTEST_F(CacheCursorTest, UnCacheCursorTest001, TestSize.Level1)128 HWTEST_F(CacheCursorTest, UnCacheCursorTest001, TestSize.Level1)
129 {
130 auto cursor = CacheCursorTest::GetCursor();
131 EXPECT_NE(cursor, nullptr);
132 std::vector<std::string> expectedNames = { "age", "identifier", "name", "phoneNumber" };
133 std::vector<std::string> names;
134 auto result = cursor->GetColumnNames(names);
135 EXPECT_EQ(result, GeneralError::E_OK);
136 EXPECT_EQ(names, expectedNames);
137
138 std::string colName;
139 auto err = cursor->GetColumnName(4, colName);
140 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
141 err = cursor->GetColumnName(-1, colName);
142 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
143 err = cursor->GetColumnName(1, colName);
144 EXPECT_EQ(err, GeneralError::E_OK);
145
146 int type = cursor->GetColumnType(0);
147 EXPECT_EQ(type, 1);
148 type = cursor->GetColumnType(4);
149 EXPECT_EQ(type, -1);
150 type = cursor->GetColumnType(-1);
151 EXPECT_EQ(type, -1);
152 }
153
154 /**
155 * @tc.name: CacheCursorTest002
156 * @tc.desc: RdbCacheCursor function test.
157 * @tc.type: FUNC
158 * @tc.require:
159 * @tc.author: SQL
160 */
HWTEST_F(CacheCursorTest, CacheCursorTest002, TestSize.Level0)161 HWTEST_F(CacheCursorTest, CacheCursorTest002, TestSize.Level0)
162 {
163 auto cursor = CacheCursorTest::GetCursor();
164 EXPECT_NE(cursor, nullptr);
165 auto err = cursor->MoveToFirst();
166
167 DistributedData::VBucket data;
168 err = cursor->GetEntry(data);
169 EXPECT_EQ(err, GeneralError::E_OK);
170
171 int64_t identifier = *std::get_if<int64_t>(&data["identifier"]);
172 EXPECT_EQ(identifier, 0);
173 std::string name = *std::get_if<std::string>(&data["name"]);
174 EXPECT_EQ(name, "tony");
175 int64_t age = *std::get_if<int64_t>(&data["age"]);
176 EXPECT_EQ(age, 25);
177 std::string phoneNumber = *std::get_if<std::string>(&data["phoneNumber"]);
178 EXPECT_EQ(phoneNumber, "10086");
179
180 while (err == GeneralError::E_OK) {
181 cursor->GetRow(data);
182 err = cursor->MoveToNext();
183 }
184
185 identifier = *std::get_if<int64_t>(&data["identifier"]);
186 EXPECT_EQ(identifier, 99);
187
188 DistributedData::Value value;
189 err = cursor->Get(0, value);
190 age = *std::get_if<int64_t>(&value);
191 EXPECT_EQ(age, 25);
192
193 err = cursor->Get("name", value);
194 name = *std::get_if<std::string>(&value);
195 EXPECT_EQ(name, "tony");
196
197 bool ret = cursor->IsEnd();
198 EXPECT_EQ(ret, false);
199
200 err = cursor->Close();
201 EXPECT_EQ(err, GeneralError::E_OK);
202 }
203
204 /**
205 * @tc.name: UnCacheCursorTest002
206 * @tc.desc: RdbCacheCursor function error test.
207 * @tc.type: FUNC
208 * @tc.require:
209 * @tc.author: SQL
210 */
HWTEST_F(CacheCursorTest, UnCacheCursorTest002, TestSize.Level0)211 HWTEST_F(CacheCursorTest, UnCacheCursorTest002, TestSize.Level0)
212 {
213 auto cursor = CacheCursorTest::GetCursor();
214 EXPECT_NE(cursor, nullptr);
215 auto err = cursor->MoveToFirst();
216
217 DistributedData::VBucket data;
218 err = cursor->GetEntry(data);
219 EXPECT_EQ(err, GeneralError::E_OK);
220
221 int64_t identifier = *std::get_if<int64_t>(&data["identifier"]);
222 EXPECT_EQ(identifier, 0);
223 std::string name = *std::get_if<std::string>(&data["name"]);
224 EXPECT_EQ(name, "tony");
225 int64_t age = *std::get_if<int64_t>(&data["age"]);
226 EXPECT_EQ(age, 25);
227 std::string phoneNumber = *std::get_if<std::string>(&data["phoneNumber"]);
228 EXPECT_EQ(phoneNumber, "10086");
229
230 while (err == GeneralError::E_OK) {
231 cursor->GetRow(data);
232 err = cursor->MoveToNext();
233 }
234
235 identifier = *std::get_if<int64_t>(&data["identifier"]);
236 EXPECT_EQ(identifier, 99);
237
238 DistributedData::Value value;
239 err = cursor->Get(-1, value);
240 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
241 err = cursor->Get(4, value);
242 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
243 err = cursor->Get(0, value);
244 age = *std::get_if<int64_t>(&value);
245 EXPECT_EQ(age, 25);
246
247 err = cursor->Get("name", value);
248 name = *std::get_if<std::string>(&value);
249 EXPECT_EQ(name, "tony");
250
251 err = cursor->Get("err", value);
252 EXPECT_EQ(err, GeneralError::E_INVALID_ARGS);
253
254 bool ret = cursor->IsEnd();
255 EXPECT_EQ(ret, false);
256
257 err = cursor->Close();
258 EXPECT_EQ(err, GeneralError::E_OK);
259 }
260 } // namespace DistributedRDBTest
261 } // namespace OHOS::Test