• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "RdbCursorTest"
16 
17 #include "rdb_cursor.h"
18 
19 #include "gtest/gtest.h"
20 #include "log_print.h"
21 #include "result_set.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 using DBStatus = DistributedDB::DBStatus;
30 namespace OHOS::Test {
31 namespace DistributedRDBTest {
32 static constexpr int MAX_DATA_NUM = 100;
33 class MockResultSet : public DistributedDB::ResultSet {
34 public:
MockResultSet()35     MockResultSet() {}
~MockResultSet()36     virtual ~MockResultSet() {}
37 
Close()38     void Close() override {}
39 
GetCount() const40     int GetCount() const override
41     {
42         return MAX_DATA_NUM;
43     }
44 
MoveToFirst()45     bool MoveToFirst() override
46     {
47         return true;
48     }
49 
MoveToNext()50     bool MoveToNext() override
51     {
52         return true;
53     }
54 
MoveToPrevious()55     bool MoveToPrevious() override
56     {
57         return true;
58     }
59 
IsAfterLast() const60     bool IsAfterLast() const override
61     {
62         return true;
63     }
64 
GetPosition() const65     int GetPosition() const override
66     {
67         return MAX_DATA_NUM;
68     }
69 
MoveToLast()70     bool MoveToLast() override
71     {
72         return true;
73     }
74 
Move(int offset)75     bool Move(int offset) override
76     {
77         return true;
78     }
79 
MoveToPosition(int position)80     bool MoveToPosition(int position) override
81     {
82         return true;
83     }
84 
IsFirst() const85     bool IsFirst() const override
86     {
87         return true;
88     }
89 
IsLast() const90     bool IsLast() const override
91     {
92         return true;
93     }
94 
IsBeforeFirst() const95     bool IsBeforeFirst() const override
96     {
97         return true;
98     }
99 
IsClosed() const100     bool IsClosed() const override
101     {
102         return true;
103     }
104 
GetEntry(DistributedDB::Entry & entry) const105     DBStatus GetEntry(DistributedDB::Entry &entry) const override
106     {
107         return DBStatus::OK;
108     }
109 
GetColumnNames(std::vector<std::string> & columnNames) const110     void GetColumnNames(std::vector<std::string> &columnNames) const override
111     {
112         columnNames = { "age", "identifier", "name", "phoneNumber" };
113     }
114 
GetColumnType(int columnIndex,DistributedDB::ResultSet::ColumnType & columnType) const115     DBStatus GetColumnType(int columnIndex, DistributedDB::ResultSet::ColumnType &columnType) const override
116     {
117         if (columnIndex < 0) {
118             return DBStatus::DB_ERROR;
119         }
120         return DBStatus::OK;
121     }
122 
GetColumnIndex(const std::string & columnName,int & columnIndex) const123     DBStatus GetColumnIndex(const std::string &columnName, int &columnIndex) const override
124     {
125         if (columnName == "ERROR") {
126             return DBStatus::DB_ERROR;
127         }
128         return DBStatus::OK;
129     }
130 
GetColumnName(int columnIndex,std::string & columnName) const131     DBStatus GetColumnName(int columnIndex, std::string &columnName) const override
132     {
133         if (columnIndex < 0) {
134             return DBStatus::DB_ERROR;
135         }
136         return DBStatus::OK;
137     }
138 
Get(int columnIndex,std::vector<uint8_t> & value) const139     DBStatus Get(int columnIndex, std::vector<uint8_t> &value) const override
140     {
141         if (columnIndex < 0) {
142             return DBStatus::DB_ERROR;
143         }
144         return DBStatus::OK;
145     }
146 
Get(int columnIndex,std::string & value) const147     DBStatus Get(int columnIndex, std::string &value) const override
148     {
149         if (columnIndex < 0) {
150             return DBStatus::DB_ERROR;
151         }
152         return DBStatus::OK;
153     }
154 
Get(int columnIndex,int64_t & value) const155     DBStatus Get(int columnIndex, int64_t &value) const override
156     {
157         if (columnIndex < 0) {
158             return DBStatus::DB_ERROR;
159         }
160         return DBStatus::OK;
161     }
162 
Get(int columnIndex,double & value) const163     DBStatus Get(int columnIndex, double &value) const override
164     {
165         if (columnIndex < 0) {
166             return DBStatus::DB_ERROR;
167         }
168         return DBStatus::OK;
169     }
170 
IsColumnNull(int columnIndex,bool & isNull) const171     DBStatus IsColumnNull(int columnIndex, bool &isNull) const override
172     {
173         if (columnIndex < 0) {
174             return DBStatus::DB_ERROR;
175         }
176         return DBStatus::OK;
177     }
178 
GetRow(std::map<std::string,DistributedDB::VariantData> & data) const179     DBStatus GetRow(std::map<std::string, DistributedDB::VariantData> &data) const override
180     {
181         return DBStatus::OK;
182     }
183 };
184 
185 class RdbCursorTest : public testing::Test {
186 public:
SetUpTestCase(void)187     static void SetUpTestCase(void){};
TearDownTestCase(void)188     static void TearDownTestCase(void){};
SetUp()189     void SetUp(){};
TearDown()190     void TearDown(){};
191 
192 protected:
193     static std::shared_ptr<MockResultSet> resultSet;
194     static std::shared_ptr<RdbCursor> rdbCursor;
195 };
196 std::shared_ptr<MockResultSet> RdbCursorTest::resultSet = std::make_shared<MockResultSet>();
197 std::shared_ptr<RdbCursor> RdbCursorTest::rdbCursor = std::make_shared<RdbCursor>(std::move(resultSet));
198 
199 /**
200 * @tc.name: RdbCursorTest001
201 * @tc.desc: RdbCacheCursor function and error test.
202 * @tc.type: FUNC
203 * @tc.require:
204 * @tc.author: SQL
205 */
206 HWTEST_F(RdbCursorTest, RdbCursorTest001, TestSize.Level1)
207 {
208     EXPECT_NE(rdbCursor, nullptr);
209     std::vector<std::string> expectedNames = {"age", "identifier", "name", "phoneNumber"};
210     std::vector<std::string> names;
211     auto result = rdbCursor->GetColumnNames(names);
212     EXPECT_EQ(result, GeneralError::E_OK);
213     EXPECT_EQ(names, expectedNames);
214 
215     std::string colName = "colName";
216     auto err = rdbCursor->GetColumnName(1, colName);
217     EXPECT_EQ(err, GeneralError::E_OK);
218 
219     int32_t type = rdbCursor->GetColumnType(0);
220     EXPECT_EQ(type, TYPE_INDEX<std::monostate>);
221     type = rdbCursor->GetColumnType(-1);
222     EXPECT_EQ(type, TYPE_INDEX<std::monostate>);
223 
224     int32_t count = rdbCursor->GetCount();
225     EXPECT_EQ(count, MAX_DATA_NUM);
226 
227     err = rdbCursor->MoveToFirst();
228     EXPECT_EQ(err, GeneralError::E_OK);
229 
230     err = rdbCursor->MoveToNext();
231     EXPECT_EQ(err, GeneralError::E_OK);
232 
233     err = rdbCursor->MoveToPrev();
234     EXPECT_EQ(err, GeneralError::E_OK);
235 }
236 
237 /**
238 * @tc.name: RdbCursorTest002
239 * @tc.desc: RdbCacheCursor function and error test.
240 * @tc.type: FUNC
241 * @tc.require:
242 * @tc.author: SQL
243 */
244 HWTEST_F(RdbCursorTest, RdbCursorTest002, TestSize.Level1)
245 {
246     EXPECT_NE(rdbCursor, nullptr);
247     DistributedData::VBucket data;
248     auto result = rdbCursor->GetEntry(data);
249     EXPECT_EQ(result, GeneralError::E_OK);
250 
251     result = rdbCursor->GetRow(data);
252     EXPECT_EQ(result, GeneralError::E_OK);
253 
254     DistributedData::Value value;
255     result = rdbCursor->Get(1, value);
256     EXPECT_EQ(result, GeneralError::E_OK);
257     result = rdbCursor->Get(-1, value);
258     EXPECT_EQ(result, GeneralError::E_ERROR);
259 
260     std::string col = "col";
261     result = rdbCursor->Get(col, value);
262     EXPECT_EQ(result, GeneralError::E_ERROR);
263     result = rdbCursor->Get("ERROR", value);
264     EXPECT_EQ(result, GeneralError::E_ERROR);
265     bool ret = rdbCursor->IsEnd();
266     EXPECT_EQ(ret, true);
267 
268     result = rdbCursor->Close();
269     EXPECT_EQ(result, GeneralError::E_OK);
270 }
271 
272 /**
273 * @tc.name: Convert
274 * @tc.desc: Convert function test.
275 * @tc.type: FUNC
276 * @tc.require:
277 * @tc.author: SQL
278 */
279 HWTEST_F(RdbCursorTest, Convert, TestSize.Level1)
280 {
281     EXPECT_NE(rdbCursor, nullptr);
282     DistributedDB::ResultSet::ColumnType dbColumnType = DistributedDB::ResultSet::ColumnType::INT64;
283     int32_t result = rdbCursor->Convert(dbColumnType);
284     EXPECT_EQ(result, TYPE_INDEX<int64_t>);
285 
286     dbColumnType = DistributedDB::ResultSet::ColumnType::STRING;
287     result = rdbCursor->Convert(dbColumnType);
288     EXPECT_EQ(result, TYPE_INDEX<std::string>);
289 
290     dbColumnType = DistributedDB::ResultSet::ColumnType::BLOB;
291     result = rdbCursor->Convert(dbColumnType);
292     EXPECT_EQ(result, TYPE_INDEX<std::vector<uint8_t>>);
293 
294     dbColumnType = DistributedDB::ResultSet::ColumnType::DOUBLE;
295     result = rdbCursor->Convert(dbColumnType);
296     EXPECT_EQ(result, TYPE_INDEX<double>);
297 
298     dbColumnType = DistributedDB::ResultSet::ColumnType::NULL_VALUE;
299     result = rdbCursor->Convert(dbColumnType);
300     EXPECT_EQ(result, TYPE_INDEX<std::monostate>);
301 
302     dbColumnType = DistributedDB::ResultSet::ColumnType::INVALID_TYPE;
303     result = rdbCursor->Convert(dbColumnType);
304     EXPECT_EQ(result, TYPE_INDEX<std::monostate>);
305 
306     dbColumnType = static_cast<DistributedDB::ResultSet::ColumnType>(MAX_DATA_NUM);
307     result = rdbCursor->Convert(dbColumnType);
308     EXPECT_EQ(result, TYPE_INDEX<std::monostate>);
309 }
310 } // namespace DistributedRDBTest
311 } // namespace OHOS::Test