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