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 <gtest/gtest.h>
17 #include <string>
18
19 #include "datashare_predicates.h"
20 #include "rdb_logger.h"
21 #include "rdb_errno.h"
22 #include "rdb_helper.h"
23 #include "rdb_open_callback.h"
24 #include "rdb_utils.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::NativeRdb;
28 using namespace OHOS::DataShare;
29 using namespace OHOS::RdbDataShareAdapter;
30
31 class RdbDataShareAdapterTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34
35 static void TearDownTestCase(void);
36
37 void SetUp();
38
39 void TearDown();
40
41 void GenerateDefaultTable();
42
43 void GenerateDefaultEmptyTable();
44
45 static const std::string DATABASE_NAME;
46 static std::shared_ptr<RdbStore> store;
47 static const int E_SQLITE_ERROR;
48 static const int E_INVALID_COLUMN_TYPE;
49 static const size_t DEFAULT_BLOCK_SIZE;
50 static const std::string RDB_ADAPTER_TEST_PATH;
51 };
52
53 const std::string RdbDataShareAdapterTest::RDB_ADAPTER_TEST_PATH = "/data/test/";
54 const std::string RdbDataShareAdapterTest::DATABASE_NAME = RDB_ADAPTER_TEST_PATH + "rdbDataShareAdapter_test.db";
55 const int RdbDataShareAdapterTest::E_SQLITE_ERROR = -1; // errno SQLITE_ERROR
56 const int RdbDataShareAdapterTest::E_INVALID_COLUMN_TYPE = 1009; // errno SQLITE_NULL
57 const size_t RdbDataShareAdapterTest::DEFAULT_BLOCK_SIZE = 2 * 1024 * 1024;
58 std::shared_ptr<RdbStore> RdbDataShareAdapterTest::store = nullptr;
59
60 class RdbStepSharedResultSetOpenCallback : public RdbOpenCallback {
61 public:
62 int OnCreate(RdbStore &rdbStore) override;
63
64 int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override;
65
66 static const std::string CREATE_TABLE_TEST;
67 };
68
OnCreate(RdbStore & store)69 int RdbStepSharedResultSetOpenCallback::OnCreate(RdbStore &store)
70 {
71 return OHOS::NativeRdb::E_OK;
72 }
73
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)74 int RdbStepSharedResultSetOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
75 {
76 return OHOS::NativeRdb::E_OK;
77 }
78
SetUpTestCase(void)79 void RdbDataShareAdapterTest::SetUpTestCase(void)
80 {
81 int errCode = OHOS::NativeRdb::E_OK;
82 RdbStoreConfig config(RdbDataShareAdapterTest::DATABASE_NAME);
83 RdbStepSharedResultSetOpenCallback helper;
84 RdbDataShareAdapterTest::store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
85 EXPECT_NE(RdbDataShareAdapterTest::store, nullptr);
86 EXPECT_EQ(errCode, OHOS::NativeRdb::E_OK);
87 }
88
TearDownTestCase(void)89 void RdbDataShareAdapterTest::TearDownTestCase(void)
90 {
91 RdbHelper::DeleteRdbStore(RdbDataShareAdapterTest::DATABASE_NAME);
92 }
93
SetUp(void)94 void RdbDataShareAdapterTest::SetUp(void)
95 {
96 store->ExecuteSql("DROP TABLE IF EXISTS test");
97 }
98
TearDown(void)99 void RdbDataShareAdapterTest::TearDown(void)
100 {
101 RdbHelper::ClearCache();
102 }
103
GenerateDefaultTable()104 void RdbDataShareAdapterTest::GenerateDefaultTable()
105 {
106 std::string createTableSql = std::string("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 TEXT, ") +
107 std::string("data2 INTEGER, data3 FLOAT, data4 BLOB);");
108 store->ExecuteSql(createTableSql);
109
110 std::string insertSql = "INSERT INTO test (data1, data2, data3, data4) VALUES (?, ?, ?, ?);";
111
112 /* insert first entry data */
113 uint8_t uValue = 66;
114 std::vector<uint8_t> typeBlob;
115 typeBlob.push_back(uValue);
116 store->ExecuteSql(insertSql, std::vector<ValueObject> {
117 ValueObject(std::string("hello")), ValueObject((int)10),
118 ValueObject((double)1.0), ValueObject((std::vector<uint8_t>)typeBlob)
119 });
120
121 /* insert second entry data */
122 typeBlob.clear();
123 store->ExecuteSql(insertSql, std::vector<ValueObject> {
124 ValueObject(std::string("2")), ValueObject((int)-5), ValueObject((double)2.5),
125 ValueObject() // set double value 2.5
126 });
127
128 /* insert third entry data */
129 store->ExecuteSql(insertSql, std::vector<ValueObject> {
130 ValueObject(std::string("hello world")), ValueObject((int)3), ValueObject((double)1.8),
131 ValueObject(std::vector<uint8_t> { 4, 5, 6 }) // set int value 3, double 1.8
132 });
133
134 /* insert four entry data */
135 store->ExecuteSql(insertSql, std::vector<ValueObject> {
136 ValueObject(std::string("new world")), ValueObject((int)5),
137 ValueObject((double)5.8), ValueObject() // set int value 5, double 5.8
138 });
139 }
140
GenerateDefaultEmptyTable()141 void RdbDataShareAdapterTest::GenerateDefaultEmptyTable()
142 {
143 std::string createTableSql = std::string("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 TEXT, ") +
144 std::string("data2 INTEGER, data3 FLOAT, data4 BLOB);");
145 store->ExecuteSql(createTableSql);
146 }
147
148 /* *
149 * @tc.name: Rdb_DataShare_Adapter_001
150 * @tc.desc: test RdbDataShareAdapter
151 * @tc.type: FUNC
152 */
153 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_001, TestSize.Level1)
154 {
155 GenerateDefaultTable();
156
157 std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet = store->QueryByStep("SELECT * FROM test");
158 EXPECT_NE(resultSet, nullptr);
159 int rowCount;
160 resultSet.get()->GetRowCount(rowCount);
161 LOG_INFO("result row count: %{public}d", rowCount);
162 EXPECT_NE(rowCount, 0);
163 auto bridge = OHOS::RdbDataShareAdapter::RdbUtils::ToResultSetBridge(resultSet);
164 EXPECT_NE(bridge, nullptr);
165 }
166
167 /* *
168 * @tc.name: Rdb_DataShare_Adapter_002
169 * @tc.desc: normal testcase of RdbDataShareAdapter
170 * @tc.type: FUNC
171 */
172 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_002, TestSize.Level1)
173 {
174 GenerateDefaultTable();
175
176 std::string table = "test";
177 std::string column = "data1";
178 std::string value = "hello";
179 DataSharePredicates predicates;
180 predicates.EqualTo(column, value);
181 std::vector<std::string> columns;
182 std::shared_ptr<AbsSharedResultSet> allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
183 int rowCount;
184 allDataTypes.get()->GetRowCount(rowCount);
185 EXPECT_EQ(rowCount, 1);
186 }
187
188 /* *
189 * @tc.name: Rdb_DataShare_Adapter_003
190 * @tc.desc: normal testcase of RdbDataShareAdapter
191 * @tc.type: FUNC
192 */
193 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_003, TestSize.Level1)
194 {
195 GenerateDefaultTable();
196
197 std::string table = "test";
198 OHOS::DataShare::DataSharePredicates predicates;
199 predicates.GreaterThan("data2", -5);
200 std::vector<std::string> columns;
201 std::shared_ptr<AbsSharedResultSet> allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
202 int rdbRowCount;
203 allDataTypes.get()->GetRowCount(rdbRowCount);
204 EXPECT_EQ(rdbRowCount, 3);
205
206 allDataTypes->GoToFirstRow();
207
208 std::string strValue;
209 allDataTypes->GetString(1, strValue);
210 EXPECT_EQ("hello", strValue);
211
212 int intValue;
213 allDataTypes->GetInt(2, intValue);
214 EXPECT_EQ(intValue, 10);
215
216 std::vector<uint8_t> blobValue;
217 uint8_t blobData = 66;
218 allDataTypes->GetBlob(4, blobValue);
219 EXPECT_EQ(blobData, blobValue[0]);
220
221 allDataTypes->GoToNextRow();
222 allDataTypes->GetBlob(4, blobValue);
223 EXPECT_EQ(3, static_cast<int>(blobValue.size()));
224 blobData = 5;
225 EXPECT_EQ(blobData, blobValue[1]);
226 }
227
228 /* *
229 * @tc.name: Rdb_DataShare_Adapter_004
230 * @tc.desc: normal testcase of RdbDataShareAdapter
231 * @tc.type: FUNC
232 */
233 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_004, TestSize.Level1)
234 {
235 GenerateDefaultTable();
236
237 std::string table = "test";
238 OHOS::DataShare::DataSharePredicates predicates;
239 predicates.SetWhereClause("`data2` > ?");
240 predicates.SetWhereArgs(std::vector<std::string> { "-5" });
241 predicates.SetOrder("data3");
242 std::vector<std::string> columns;
243 std::shared_ptr<AbsSharedResultSet> allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
244 int rowCount;
245 allDataTypes.get()->GetRowCount(rowCount);
246 EXPECT_EQ(rowCount, 3);
247
248 allDataTypes->GoToFirstRow();
249
250 std::string strValue;
251 allDataTypes->GetString(1, strValue);
252 EXPECT_EQ("hello", strValue);
253
254 int intValue;
255 allDataTypes->GetInt(2, intValue);
256 EXPECT_EQ(intValue, 10);
257
258 std::vector<uint8_t> blobValue;
259 allDataTypes->GetBlob(4, blobValue);
260 EXPECT_EQ(1, static_cast<int>(blobValue.size()));
261
262 allDataTypes->GoToNextRow();
263
264 allDataTypes->GetString(1, strValue);
265 EXPECT_EQ("hello world", strValue);
266
267 allDataTypes->GetInt(2, intValue);
268 EXPECT_EQ(intValue, 3);
269
270 allDataTypes->GetBlob(4, blobValue);
271 EXPECT_EQ(3, static_cast<int>(blobValue.size()));
272 uint8_t blobData = 5;
273 EXPECT_EQ(blobData, blobValue[1]);
274 }
275
276 /* *
277 * @tc.name: Rdb_DataShare_Adapter_005
278 * @tc.desc: normal testcase of RdbDataShareAdapter
279 * @tc.type: FUNC
280 */
281 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_005, TestSize.Level1)
282 {
283 GenerateDefaultTable();
284 DataShareValuesBucket values;
285 int64_t id;
286 int changedRows;
287 values.Put("data1", std::string("tulip"));
288 values.Put("data2", 100);
289 values.Put("data3", 50.5);
290 values.Put("data4", std::vector<uint8_t> { 20, 21, 22 });
291
292 int ret = store->Insert(id, "test", RdbUtils::ToValuesBucket(values));
293 EXPECT_EQ(ret, OHOS::NativeRdb::E_OK);
294 EXPECT_EQ(5, id);
295
296 std::string table = "test";
297 OHOS::DataShare::DataSharePredicates predicates;
298 std::string value = "tulip";
299 predicates.EqualTo("data1", value);
300 std::vector<std::string> columns;
301 std::shared_ptr<AbsSharedResultSet> allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
302 int rowCount;
303 allDataTypes.get()->GetRowCount(rowCount);
304 EXPECT_EQ(rowCount, 1);
305
306 allDataTypes.get()->Close();
307
308 values.Clear();
309 values.Put("data3", 300.5);
310 values.Put("data4", std::vector<uint8_t> { 17, 18, 19 });
311 ret = store->Update(
312 changedRows, "test", RdbUtils::ToValuesBucket(values), "data1 = ?", std::vector<std::string> { "tulip" });
313 EXPECT_EQ(ret, OHOS::NativeRdb::E_OK);
314 EXPECT_EQ(1, changedRows);
315
316 allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
317 allDataTypes->GoToFirstRow();
318
319 double doubleVal;
320 allDataTypes->GetDouble(3, doubleVal);
321 EXPECT_EQ(300.5, doubleVal);
322
323 int deletedRows;
324 ret = store->Delete(deletedRows, RdbUtils::ToPredicates(predicates, table));
325 EXPECT_EQ(ret, OHOS::NativeRdb::E_OK);
326 EXPECT_EQ(1, deletedRows);
327 }
328
329 /* *
330 * @tc.name: Rdb_DataShare_Adapter_006
331 * @tc.desc: normal testcase of RdbDataShareAdapter
332 * @tc.type: FUNC
333 */
334 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_006, TestSize.Level1)
335 {
336 std::string createTableSql = std::string("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,") +
337 std::string("age INTEGER);");
338 store->ExecuteSql(createTableSql);
339
340 DataShareValuesBucket values;
341 int64_t id;
342 values.Put("name", std::string("lisi"));
343 int64_t ageValue = 0x80001000;
344 values.Put("age", ageValue);
345
346 ValuesBucket valuesBucket = RdbUtils::ToValuesBucket(values);
347 ValueObject valueObject;
348 valuesBucket.GetObject("age", valueObject);
349 int64_t res;
350 int ret = valueObject.GetLong(res);
351 LOG_INFO("Rdb_DataShare_Adapter_006 res is: %{public}" PRId64, res);
352 EXPECT_EQ(ret, OHOS::NativeRdb::E_OK);
353 EXPECT_EQ(res, ageValue);
354
355 int ret1 = store->Insert(id, "test", valuesBucket);
356 EXPECT_EQ(ret1, OHOS::NativeRdb::E_OK);
357 EXPECT_EQ(1, id);
358
359 std::string table = "test";
360 OHOS::DataShare::DataSharePredicates predicates;
361 std::string value = "lisi";
362 predicates.EqualTo("name", value);
363 std::vector<std::string> columns;
364 std::shared_ptr<AbsSharedResultSet> allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
365 allDataTypes->GoToFirstRow();
366 int64_t intValue;
367 allDataTypes->GetLong(2, intValue);
368 EXPECT_EQ(intValue, ageValue);
369 }
370
371 /* *
372 * @tc.name: Rdb_DataShare_Adapter_007
373 * @tc.desc: normal testcase of RdbDataShareAdapter
374 * @tc.type: FUNC
375 */
376 HWTEST_F(RdbDataShareAdapterTest, Rdb_DataShare_Adapter_007, TestSize.Level1)
377 {
378 GenerateDefaultTable();
379
380 std::string table = "test";
381 OHOS::DataShare::DataSharePredicates predicates;
382 predicates.Limit(1, 2);
383 std::vector<std::string> columns;
384 std::shared_ptr<AbsSharedResultSet> allDataTypes = store->Query(RdbUtils::ToPredicates(predicates, table), columns);
385 int rdbRowCount;
386 allDataTypes.get()->GetRowCount(rdbRowCount);
387 EXPECT_EQ(rdbRowCount, 1);
388
389 allDataTypes->GoToFirstRow();
390
391 std::string strValue;
392 allDataTypes->GetString(1, strValue);
393 EXPECT_EQ("hello world", strValue);
394
395 int intValue;
396 allDataTypes->GetInt(2, intValue);
397 EXPECT_EQ(intValue, 3);
398
399 double doubleValue;
400 allDataTypes->GetDouble(3, doubleValue);
401 EXPECT_EQ(doubleValue, 1.8);
402
403 std::vector<uint8_t> blobValue;
404 uint8_t blobData1 = 4;
405 uint8_t blobData2 = 5;
406 uint8_t blobData3 = 6;
407 allDataTypes->GetBlob(4, blobValue);
408 EXPECT_EQ(3, static_cast<int>(blobValue.size()));
409 EXPECT_EQ(blobData1, blobValue[0]);
410 EXPECT_EQ(blobData2, blobValue[1]);
411 EXPECT_EQ(blobData3, blobValue[2]);
412 }