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 #include <iostream>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string>
19 #include <string.h>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <fcntl.h>
23 #include <securec.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include "napi/native_api.h"
28 #include "common/common.h"
29 #include "relational_store.h"
30 #include "relational_store_error_code.h"
31 #include "oh_cursor.h"
32 #include "oh_predicates.h"
33 #include "oh_value_object.h"
34 #include "oh_values_bucket.h"
35
36 OH_Rdb_Store *cursorTestRdbStore_ = NULL;
37
38 char *RDB_TEST_PATH = NULL;
39 char RDB_STORE_NAME[] = "rdb_store_cursor_test.db";
40 char BUNDLE_NAME[] = "com.acts.rdb.napitest";
41 char MODULE_NAME[] = "com.acts.rdb.napitest";
42
43 static OH_Rdb_Config config_;
InitRdbConfig()44 static void InitRdbConfig()
45 {
46 config_.dataBaseDir = RDB_TEST_PATH;
47 config_.storeName = RDB_STORE_NAME;
48 config_.bundleName = BUNDLE_NAME;
49 config_.moduleName = MODULE_NAME;
50 config_.securityLevel = OH_Rdb_SecurityLevel::S1;
51 config_.isEncrypt = false;
52 config_.area = Rdb_SecurityArea::RDB_SECURITY_AREA_EL1;
53 config_.selfSize = sizeof(OH_Rdb_Config);
54 }
55
RdbFilePath(napi_env env,napi_callback_info info)56 static napi_value RdbFilePath(napi_env env, napi_callback_info info) {
57 int errCode = 0;
58 size_t argc = 1;
59 napi_value args[1] = {nullptr};
60 napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
61
62 size_t bufferSize = 0;
63 napi_get_value_string_latin1(env, args[0], nullptr, 0, &bufferSize);
64
65 char *buffer = (char*)malloc((bufferSize) + 1);
66 napi_get_value_string_utf8(env, args[0], buffer, bufferSize+1, &bufferSize);
67
68 RDB_TEST_PATH = (char*)malloc((bufferSize) + 1);
69 sprintf(RDB_TEST_PATH, "%s", buffer);
70
71 napi_value returnCode;
72 napi_create_double(env, errCode, &returnCode);
73 return returnCode;
74 }
75
SetAsset(Data_Asset * asset,int index)76 static void SetAsset(Data_Asset *asset, int index)
77 {
78 OH_Data_Asset_SetName(asset, "name");
79 OH_Data_Asset_SetUri(asset,"uri");
80 OH_Data_Asset_SetPath(asset, "path");
81 OH_Data_Asset_SetCreateTime(asset, index);
82 OH_Data_Asset_SetModifyTime(asset, index);
83 OH_Data_Asset_SetSize(asset, index);
84 OH_Data_Asset_SetStatus(asset, Data_AssetStatus::ASSET_NORMAL);
85 }
86
CreateAssetTable()87 static void CreateAssetTable()
88 {
89 char createTableSql[] = "CREATE TABLE IF NOT EXISTS asset_table (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 "
90 "asset, data2 assets );";
91 OH_Rdb_Execute(cursorTestRdbStore_, createTableSql);
92 char table[] = "asset_table";
93 int assetsCount = 2;
94 int curRow = 1;
95 OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
96 Data_Asset *asset1 = OH_Data_Asset_CreateOne();
97 SetAsset(asset1, 1);
98 Data_Asset *asset2 = OH_Data_Asset_CreateOne();
99 SetAsset(asset2, 2);
100
101 valueBucket->putInt64(valueBucket, "id", curRow);
102 OH_VBucket_PutAsset(valueBucket, "data1", asset1);
103 Data_Asset **assets1 = OH_Data_Asset_CreateMultiple(assetsCount);
104 SetAsset(assets1[0], 1);
105 SetAsset(assets1[1], 2);
106 OH_VBucket_PutAssets(valueBucket, "data2", assets1, assetsCount);
107 OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
108 curRow++;
109
110 valueBucket->clear(valueBucket);
111 valueBucket->putInt64(valueBucket, "id", curRow);
112 OH_VBucket_PutAsset(valueBucket, "data1", asset2);
113 Data_Asset **assets2 = OH_Data_Asset_CreateMultiple(assetsCount);
114 SetAsset(assets2[0], 1);
115 SetAsset(assets2[1], 3);
116 OH_VBucket_PutAssets(valueBucket, "data2", assets2, assetsCount);
117 OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
118
119 OH_Data_Asset_DestroyMultiple(assets1, assetsCount);
120 OH_Data_Asset_DestroyMultiple(assets2, assetsCount);
121 OH_Data_Asset_DestroyOne(asset1);
122 OH_Data_Asset_DestroyOne(asset2);
123 valueBucket->destroy(valueBucket);
124 }
125
CursorSetUpTestCase(napi_env env,napi_callback_info info)126 static napi_value CursorSetUpTestCase(napi_env env, napi_callback_info info) {
127 InitRdbConfig();
128 mkdir(config_.dataBaseDir, 0770);
129
130 int errCode = 0;
131 char table[] = "test";
132 cursorTestRdbStore_ = OH_Rdb_GetOrOpen(&config_, &errCode);
133 NAPI_ASSERT(env, errCode == 0, "OH_Rdb_GetOrOpen is fail.");
134 NAPI_ASSERT(env, cursorTestRdbStore_ != NULL, "OH_Rdb_GetOrOpen config is fail.");
135
136 char createTableSql[] = "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 TEXT, data2 INTEGER, "
137 "data3 FLOAT, data4 BLOB, data5 TEXT);";
138 errCode = OH_Rdb_Execute(cursorTestRdbStore_, createTableSql);
139
140 OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
141 valueBucket->putInt64(valueBucket, "id", 1);
142 valueBucket->putText(valueBucket, "data1", "zhangSan");
143 valueBucket->putInt64(valueBucket, "data2", 12800);
144 valueBucket->putReal(valueBucket, "data3", 100.1);
145 uint8_t arr[] = {1, 2, 3, 4, 5};
146 int len = sizeof(arr) / sizeof(arr[0]);
147 valueBucket->putBlob(valueBucket, "data4", arr, len);
148 valueBucket->putText(valueBucket, "data5", "ABCDEFG");
149 errCode = OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
150 NAPI_ASSERT(env, errCode == 1, "OH_Rdb_Insert 1 is fail.");
151
152 valueBucket->clear(valueBucket);
153 valueBucket->putInt64(valueBucket, "id", 2);
154 valueBucket->putText(valueBucket, "data1", "liSi");
155 valueBucket->putInt64(valueBucket, "data2", 13800);
156 valueBucket->putReal(valueBucket, "data3", 200.1);
157 valueBucket->putText(valueBucket, "data5", "ABCDEFGH");
158 errCode = OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
159 NAPI_ASSERT(env, errCode == 2, "OH_Rdb_Insert 2 is fail.");
160
161 valueBucket->clear(valueBucket);
162 valueBucket->putInt64(valueBucket, "id", 3);
163 valueBucket->putText(valueBucket, "data1", "wangWu");
164 valueBucket->putInt64(valueBucket, "data2", 14800);
165 valueBucket->putReal(valueBucket, "data3", 300.1);
166 valueBucket->putText(valueBucket, "data5", "ABCDEFGHI");
167 errCode = OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
168 NAPI_ASSERT(env, errCode == 3, "OH_Rdb_Insert 3 is fail.");
169
170 errCode = valueBucket->destroy(valueBucket);
171
172 napi_value returnCode;
173 napi_create_double(env, errCode, &returnCode);
174 return returnCode;
175 }
176
CursorTearDownTestCase(napi_env env,napi_callback_info info)177 static napi_value CursorTearDownTestCase(napi_env env, napi_callback_info info) {
178 int errCode = 0;
179 char dropTableSql[] = "DROP TABLE IF EXISTS test";
180 OH_Rdb_Execute(cursorTestRdbStore_, dropTableSql);
181 OH_Rdb_CloseStore(cursorTestRdbStore_);
182 errCode = OH_Rdb_DeleteStore(&config_);
183 napi_value returnCode;
184 napi_create_double(env, errCode, &returnCode);
185 return returnCode;
186 }
187
188 /**
189 * @tc.name: SUB_DDM_RDB_CURSOR_0100
190 * @tc.desc: napi test RDB cursor for GetColumnType.
191 * @tc.type: FUNC
192 */
SUB_DDM_RDB_CURSOR_0100(napi_env env,napi_callback_info info)193 static napi_value SUB_DDM_RDB_CURSOR_0100(napi_env env, napi_callback_info info) {
194
195 int errCode = 0;
196 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
197
198 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
199 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
200
201 cursor->goToNextRow(cursor);
202
203 OH_ColumnType type;
204 errCode = cursor->getColumnType(cursor, 0, &type);
205 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_INT64, "getColumnType 0 is fail.");
206
207 errCode = cursor->getColumnType(cursor, 1, &type);;
208 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_TEXT, "getColumnType 1 is fail.");
209
210 errCode = cursor->getColumnType(cursor, 2, &type);
211 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_INT64, "getColumnType 2 is fail.");
212
213 errCode = cursor->getColumnType(cursor, 3, &type);
214 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_REAL, "getColumnType 3 is fail.");
215
216 errCode = cursor->getColumnType(cursor, 4, &type);
217 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_BLOB, "getColumnType 4 is fail.");
218
219 errCode = cursor->getColumnType(cursor, 5, &type);
220 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_TEXT, "getColumnType 5 is fail.");
221
222 predicates->destroy(predicates);
223 errCode = cursor->destroy(cursor);
224
225 napi_value returnCode;
226 napi_create_double(env, errCode, &returnCode);
227 return returnCode;
228 }
229
230
231 /**
232 * @tc.name: SUB_DDM_RDB_CURSOR_0200
233 * @tc.desc: napi test RDB cursor for GetColumnIndex.
234 * @tc.type: FUNC
235 */
SUB_DDM_RDB_CURSOR_0200(napi_env env,napi_callback_info info)236 static napi_value SUB_DDM_RDB_CURSOR_0200(napi_env env, napi_callback_info info) {
237 int errCode = 0;
238 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
239
240 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
241 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
242
243 int columnIndex;
244 errCode = cursor->getColumnIndex(cursor, "data1", &columnIndex);
245 NAPI_ASSERT(env, columnIndex == 1, "getColumnIndex 1 is fail.");
246
247 errCode = cursor->getColumnIndex(cursor, "data2", &columnIndex);
248 NAPI_ASSERT(env, columnIndex == 2, "getColumnIndex 2 is fail.");
249
250 errCode = cursor->getColumnIndex(cursor, "data3", &columnIndex);
251 NAPI_ASSERT(env, columnIndex == 3, "getColumnIndex 3 is fail.");
252
253 errCode = cursor->getColumnIndex(cursor, "data4", &columnIndex);
254 NAPI_ASSERT(env, columnIndex == 4, "getColumnIndex 4 is fail.");
255
256 errCode = cursor->getColumnIndex(cursor, "data5", &columnIndex);
257 NAPI_ASSERT(env, columnIndex == 5, "getColumnIndex 5 is fail.");
258
259 predicates->destroy(predicates);
260 errCode = cursor->destroy(cursor);
261
262 napi_value returnCode;
263 napi_create_double(env, errCode, &returnCode);
264 return returnCode;
265 }
266 /**
267 * @tc.name: SUB_DDM_RDB_CURSOR_0300
268 * @tc.desc: napi test RDB cursor for GetColumnName.
269 * @tc.type: FUNC
270 */
SUB_DDM_RDB_CURSOR_0300(napi_env env,napi_callback_info info)271 static napi_value SUB_DDM_RDB_CURSOR_0300(napi_env env, napi_callback_info info) {
272 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
273
274 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
275 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
276
277 char name[10];
278 int errCode = cursor->getColumnName(cursor, 1, name, 10);
279 NAPI_ASSERT(env, strncmp(name, "data1", 5) == 0, "getColumnName data1 is fail.");
280
281 errCode = cursor->getColumnName(cursor, 2, name, 6);
282 NAPI_ASSERT(env, strncmp(name, "data2", 5) == 0, "getColumnName data2 is fail.");
283
284 errCode = cursor->getColumnName(cursor, 3, name, 6);
285 NAPI_ASSERT(env, strncmp(name, "data3", 5) == 0, "getColumnName data3 is fail.");
286
287 errCode = cursor->getColumnName(cursor, 4, name, 6);
288 NAPI_ASSERT(env, strncmp(name, "data4", 5) == 0, "getColumnName data4 is fail.");
289
290 errCode = cursor->getColumnName(cursor, 5, name, 6);
291 NAPI_ASSERT(env, strncmp(name, "data5", 5) == 0, "getColumnName data5 is fail.");
292
293 predicates->destroy(predicates);
294 errCode = cursor->destroy(cursor);
295
296 napi_value returnCode;
297 napi_create_double(env, errCode, &returnCode);
298 return returnCode;
299 }
300
301
302 /**
303 * @tc.name: SUB_DDM_RDB_CURSOR_0400
304 * @tc.desc: napi test RDB cursor for Getxxx.
305 * @tc.type: FUNC
306 */
SUB_DDM_RDB_CURSOR_0400(napi_env env,napi_callback_info info)307 static napi_value SUB_DDM_RDB_CURSOR_0400(napi_env env, napi_callback_info info) {
308 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
309
310 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
311 int len1 = sizeof(columnNames) / sizeof(columnNames[0]);
312 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len1);
313 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
314
315 int errCode = 0;
316 int rowCount = 0;
317 cursor->getRowCount(cursor, &rowCount);
318 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
319
320 cursor->goToNextRow(cursor);
321
322 int columnCount = 0;
323 cursor->getColumnCount(cursor, &columnCount);
324 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
325
326 size_t size = 0;
327 cursor->getSize(cursor, 0, &size);
328 char data1Value[size + 1];
329 cursor->getText(cursor, 0, data1Value, size + 1);
330 NAPI_ASSERT(env, strncmp(data1Value, "zhangSan", size) == 0, "getText is fail.");
331
332 int64_t data2Value;
333 cursor->getInt64(cursor, 1, &data2Value);
334 NAPI_ASSERT(env, data2Value == 12800, "getInt64 is fail.");
335
336 double data3Value;
337 cursor->getReal(cursor, 2, &data3Value);
338 NAPI_ASSERT(env, data3Value == 100.1, "getReal is fail.");
339
340 cursor->getSize(cursor, 3, &size);
341 unsigned char data4Value[size];
342 cursor->getBlob(cursor, 3, data4Value, size);
343 NAPI_ASSERT(env, data4Value[0] == 1, "getBlob 0 is fail.");
344 NAPI_ASSERT(env, data4Value[1] == 2, "getBlob 1 is fail.");
345
346 cursor->goToNextRow(cursor);
347
348 cursor->getSize(cursor, 0, &size);
349 char data1Value_1[size + 1];
350 cursor->getText(cursor, 0, data1Value_1, size + 1);
351 NAPI_ASSERT(env, strncmp(data1Value_1, "liSi", size) == 0, "getText is fail.");
352
353 cursor->getInt64(cursor, 1, &data2Value);
354 NAPI_ASSERT(env, data2Value == 13800, "getInt64 is fail.");
355
356 cursor->getReal(cursor, 2, &data3Value);
357 NAPI_ASSERT(env, data3Value == 200.1, "getReal is fail.");
358
359 bool isNull = false;
360 cursor->isNull(cursor, 3, &isNull);
361 NAPI_ASSERT(env, isNull == true, "isNull is fail.");
362
363 predicates->destroy(predicates);
364 errCode = cursor->destroy(cursor);
365
366 napi_value returnCode;
367 napi_create_double(env, errCode, &returnCode);
368 return returnCode;
369 }
370 /**
371 * @tc.name: SUB_DDM_RDB_CURSOR_0500
372 * @tc.desc: napi test RDB cursor for GetColumnIndex.
373 * @tc.type: FUNC
374 */
SUB_DDM_RDB_CURSOR_0500(napi_env env,napi_callback_info info)375 static napi_value SUB_DDM_RDB_CURSOR_0500(napi_env env, napi_callback_info info) {
376 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
377
378 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
379 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
380
381 int columnIndex;
382 int errCode = cursor->getColumnIndex(cursor, "data", &columnIndex);
383 NAPI_ASSERT(env, columnIndex == -1, "getColumnIndex is fail.");
384
385 errCode = predicates->destroy(predicates);
386
387 napi_value returnCode;
388 napi_create_double(env, errCode, &returnCode);
389 return returnCode;
390 }
391
392 /**
393 * @tc.name: SUB_DDM_RDB_CURSOR_0600
394 * @tc.desc: napi test RDB cursor for GetColumnIndex.
395 * @tc.type: FUNC
396 */
SUB_DDM_RDB_CURSOR_0600(napi_env env,napi_callback_info info)397 static napi_value SUB_DDM_RDB_CURSOR_0600(napi_env env, napi_callback_info info) {
398 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
399
400 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
401 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
402
403 int columnIndex;
404 char *name = NULL;
405 int errCode = cursor->getColumnIndex(cursor, name, &columnIndex);
406 NAPI_ASSERT(env, errCode == 14800001, "getColumnIndex is fail.");
407
408 errCode = predicates->destroy(predicates);
409
410 napi_value returnCode;
411 napi_create_double(env, errCode, &returnCode);
412 return returnCode;
413 }
414
415 /**
416 * @tc.name: SUB_DDM_RDB_CURSOR_0700
417 * @tc.desc: napi test RDB cursor for GetColumnType.
418 * @tc.type: FUNC
419 */
SUB_DDM_RDB_CURSOR_0700(napi_env env,napi_callback_info info)420 static napi_value SUB_DDM_RDB_CURSOR_0700(napi_env env, napi_callback_info info) {
421 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
422
423 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
424 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
425
426 cursor->goToNextRow(cursor);
427
428 OH_ColumnType type;
429 int errCode = cursor->getColumnType(cursor, 6, &type);
430 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_NULL, "getColumnType is fail.");
431
432 predicates->destroy(predicates);
433 errCode = cursor->destroy(cursor);
434
435 napi_value returnCode;
436 napi_create_double(env, errCode, &returnCode);
437 return returnCode;
438 }
439
440 /**
441 * @tc.name: SUB_DDM_RDB_CURSOR_0800
442 * @tc.desc: napi test RDB cursor for GetColumnName.
443 * @tc.type: FUNC
444 */
SUB_DDM_RDB_CURSOR_0800(napi_env env,napi_callback_info info)445 static napi_value SUB_DDM_RDB_CURSOR_0800(napi_env env, napi_callback_info info) {
446 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
447
448 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
449 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
450
451 char name[10] = "";
452 int errCode = cursor->getColumnName(cursor, 6, name, 10);
453 NAPI_ASSERT(env, strncmp(name, "", 10) == 0, "getColumnName is fail.");
454 predicates->destroy(predicates);
455 errCode = cursor->destroy(cursor);
456
457 napi_value returnCode;
458 napi_create_double(env, errCode, &returnCode);
459 return returnCode;
460 }
461
462 /**
463 * @tc.name: SUB_DDM_RDB_CURSOR_0900
464 * @tc.desc: napi test RDB cursor for getSize.
465 * @tc.type: FUNC
466 */
SUB_DDM_RDB_CURSOR_0900(napi_env env,napi_callback_info info)467 static napi_value SUB_DDM_RDB_CURSOR_0900(napi_env env, napi_callback_info info) {
468 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
469
470 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
471 int len1 = sizeof(columnNames) / sizeof(columnNames[0]);
472 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len1);
473 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
474
475 int errCode = 0;
476 int rowCount = 0;
477 cursor->getRowCount(cursor, &rowCount);
478 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
479
480 cursor->goToNextRow(cursor);
481
482 int columnCount = 0;
483 cursor->getColumnCount(cursor, &columnCount);
484 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
485
486 size_t size = 0;
487 cursor->getSize(cursor, 6, &size);
488 NAPI_ASSERT(env, (int)size == 0, "getSize is fail.");
489
490 predicates->destroy(predicates);
491 errCode = cursor->destroy(cursor);
492
493 napi_value returnCode;
494 napi_create_double(env, errCode, &returnCode);
495 return returnCode;
496 }
497
498 /**
499 * @tc.name: SUB_DDM_RDB_CURSOR_1000
500 * @tc.desc: napi test RDB cursor for getText.
501 * @tc.type: FUNC
502 */
SUB_DDM_RDB_CURSOR_1000(napi_env env,napi_callback_info info)503 static napi_value SUB_DDM_RDB_CURSOR_1000(napi_env env, napi_callback_info info) {
504 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
505
506 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
507 int len1 = sizeof(columnNames) / sizeof(columnNames[0]);
508 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len1);
509 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
510
511 int rowCount = 0;
512 cursor->getRowCount(cursor, &rowCount);
513 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
514
515 cursor->goToNextRow(cursor);
516
517 int columnCount = 0;
518 cursor->getColumnCount(cursor, &columnCount);
519 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
520
521 size_t size = 0;
522 cursor->getSize(cursor, 0, &size);
523 char data1Value[size + 1];
524 cursor->getText(cursor, 6, data1Value, size + 1);
525 NAPI_ASSERT(env, strncmp(data1Value, "", size) == 0, "getText is fail.");
526
527 predicates->destroy(predicates);
528 int errCode = cursor->destroy(cursor);
529
530 napi_value returnCode;
531 napi_create_double(env, errCode, &returnCode);
532 return returnCode;
533 }
534
535 /**
536 * @tc.name: SUB_DDM_RDB_CURSOR_1100
537 * @tc.desc: napi test RDB cursor for getInt64.
538 * @tc.type: FUNC
539 */
SUB_DDM_RDB_CURSOR_1100(napi_env env,napi_callback_info info)540 static napi_value SUB_DDM_RDB_CURSOR_1100(napi_env env, napi_callback_info info) {
541 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
542
543 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
544 int len1 = sizeof(columnNames) / sizeof(columnNames[0]);
545 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len1);
546 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
547
548 int rowCount = 0;
549 cursor->getRowCount(cursor, &rowCount);
550 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
551
552 cursor->goToNextRow(cursor);
553
554 int columnCount = 0;
555 cursor->getColumnCount(cursor, &columnCount);
556 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
557
558 int64_t data2Value = 0;
559 int errCode = cursor->getInt64(cursor, 6, &data2Value);
560 NAPI_ASSERT(env, data2Value == 0, "getInt64 is fail.");
561
562 predicates->destroy(predicates);
563 errCode = cursor->destroy(cursor);
564
565 napi_value returnCode;
566 napi_create_double(env, errCode, &returnCode);
567 return returnCode;
568 }
569
570 /**
571 * @tc.name: SUB_DDM_RDB_CURSOR_1200
572 * @tc.desc: napi test RDB cursor for getReal.
573 * @tc.type: FUNC
574 */
SUB_DDM_RDB_CURSOR_1200(napi_env env,napi_callback_info info)575 static napi_value SUB_DDM_RDB_CURSOR_1200(napi_env env, napi_callback_info info) {
576 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
577
578 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
579 int len = sizeof(columnNames) / sizeof(columnNames[0]);
580 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len);
581 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
582
583 int rowCount = 0;
584 cursor->getRowCount(cursor, &rowCount);
585 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
586
587 cursor->goToNextRow(cursor);
588
589 int columnCount = 0;
590 cursor->getColumnCount(cursor, &columnCount);
591 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
592
593 double data3Value = 0;
594 cursor->getReal(cursor, 6, &data3Value);
595 NAPI_ASSERT(env, data3Value == 0, "getReal is fail.");
596
597 predicates->destroy(predicates);
598 int errCode = cursor->destroy(cursor);
599
600 napi_value returnCode;
601 napi_create_double(env, errCode, &returnCode);
602 return returnCode;
603 }
604
605 /**
606 * @tc.name: SUB_DDM_RDB_CURSOR_1300
607 * @tc.desc: getBlob
608 * @tc.type: FUNC
609 */
SUB_DDM_RDB_CURSOR_1300(napi_env env,napi_callback_info info)610 static napi_value SUB_DDM_RDB_CURSOR_1300(napi_env env, napi_callback_info info) {
611 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
612
613 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
614 int len1 = sizeof(columnNames) / sizeof(columnNames[0]);
615 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len1);
616 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query config is fail.");
617
618 int rowCount = 0;
619 cursor->getRowCount(cursor, &rowCount);
620 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
621
622 cursor->goToNextRow(cursor);
623
624 int columnCount = 0;
625 cursor->getColumnCount(cursor, &columnCount);
626 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
627
628 size_t size = 0;
629 cursor->getSize(cursor, 3, &size);
630 unsigned char data4Value[] = "string";
631 int errCode = cursor->getBlob(cursor, 6, data4Value, size);
632 NAPI_ASSERT(env, strncmp((char*)data4Value, "string", size) == 0, "getBlob is fail.");
633
634 predicates->destroy(predicates);
635 errCode = cursor->destroy(cursor);
636
637 napi_value returnCode;
638 napi_create_double(env, errCode, &returnCode);
639 return returnCode;
640 }
641
642 /**
643 * @tc.name: SUB_DDM_RDB_CURSOR_1400
644 * @tc.desc: isNull
645 * @tc.type: FUNC
646 */
SUB_DDM_RDB_CURSOR_1400(napi_env env,napi_callback_info info)647 static napi_value SUB_DDM_RDB_CURSOR_1400(napi_env env, napi_callback_info info) {
648 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
649
650 const char *columnNames[] = {"data1", "data2", "data3", "data4"};
651 int len1 = sizeof(columnNames) / sizeof(columnNames[0]);
652 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, columnNames, len1);
653 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
654
655 int rowCount = 0;
656 cursor->getRowCount(cursor, &rowCount);
657 NAPI_ASSERT(env, rowCount == 3, "getRowCount is fail.");
658
659 cursor->goToNextRow(cursor);
660
661 int columnCount = 0;
662 cursor->getColumnCount(cursor, &columnCount);
663 NAPI_ASSERT(env, columnCount == 4, "getColumnCount is fail.");
664
665 bool isNull = false;
666 int errCode = cursor->isNull(cursor, 6, &isNull);
667 NAPI_ASSERT(env, isNull == false, "isNull is fail.");
668
669 predicates->destroy(predicates);
670 errCode = cursor->destroy(cursor);
671
672 napi_value returnCode;
673 napi_create_double(env, errCode, &returnCode);
674 return returnCode;
675 }
676
677 /**
678 * @tc.name: SUB_DDM_RDB_CURSOR_1500
679 * @tc.desc: getRowCount
680 * @tc.type: FUNC
681 */
SUB_DDM_RDB_CURSOR_1500(napi_env env,napi_callback_info info)682 static napi_value SUB_DDM_RDB_CURSOR_1500(napi_env env, napi_callback_info info) {
683
684 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
685 OH_VObject *valueObject = OH_Rdb_CreateValueObject();
686 const char *data1Value = "zhangSan";
687 valueObject->putText(valueObject, data1Value);
688 predicates->equalTo(predicates, "data1", valueObject);
689
690 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
691 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
692
693 int rowCount = 0;
694 cursor->getRowCount(cursor, &rowCount);
695 NAPI_ASSERT(env, rowCount == 1, "getRowCount is fail.");
696
697 predicates->destroy(predicates);
698 int errCode = cursor->destroy(cursor);
699
700 napi_value returnCode;
701 napi_create_double(env, errCode, &returnCode);
702 return returnCode;
703 }
704
705 /**
706 * @tc.name: SUB_DDM_RDB_CURSOR_1600
707 * @tc.desc: getRowCount
708 * @tc.type: FUNC
709 */
SUB_DDM_RDB_CURSOR_1600(napi_env env,napi_callback_info info)710 static napi_value SUB_DDM_RDB_CURSOR_1600(napi_env env, napi_callback_info info) {
711 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
712 OH_VObject *valueObject = OH_Rdb_CreateValueObject();
713 const char *data1Value = "name";
714 valueObject->putText(valueObject, data1Value);
715 predicates->equalTo(predicates, "data1", valueObject);
716
717 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
718 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
719
720 int rowCount = -1;
721 cursor->getRowCount(cursor, &rowCount);
722 NAPI_ASSERT(env, rowCount == 0, "getRowCount is fail.");
723
724 predicates->destroy(predicates);
725 int errCode = cursor->destroy(cursor);
726
727 napi_value returnCode;
728 napi_create_double(env, errCode, &returnCode);
729 return returnCode;
730 }
731
732 /**
733 * @tc.name: SUB_DDM_RDB_CURSOR_1700
734 * @tc.desc: getRowCount
735 * @tc.type: FUNC
736 */
SUB_DDM_RDB_CURSOR_1700(napi_env env,napi_callback_info info)737 static napi_value SUB_DDM_RDB_CURSOR_1700(napi_env env, napi_callback_info info) {
738
739 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
740 OH_VObject *valueObject1 = OH_Rdb_CreateValueObject();
741 const char *data1Value1 = "zhangSan";
742 valueObject1->putText(valueObject1, data1Value1);
743 predicates->equalTo(predicates, "data1", valueObject1);
744 OH_VObject *valueObject2 = OH_Rdb_CreateValueObject();
745 const char *data1Value2 = "name";
746 valueObject2->putText(valueObject2, data1Value2);
747 predicates->equalTo(predicates, "data1", valueObject2);
748
749 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
750 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
751
752 int rowCount = -1;
753 cursor->getRowCount(cursor, &rowCount);
754 NAPI_ASSERT(env, rowCount == 0, "getRowCount is fail.");
755
756 predicates->destroy(predicates);
757 int errCode = cursor->destroy(cursor);
758
759 napi_value returnCode;
760 napi_create_double(env, errCode, &returnCode);
761 return returnCode;
762 }
763
764 /**
765 * @tc.name: SUB_DDM_RDB_CURSOR_1800
766 * @tc.desc:
767 * @tc.type: FUNC
768 */
SUB_DDM_RDB_CURSOR_1800(napi_env env,napi_callback_info info)769 static napi_value SUB_DDM_RDB_CURSOR_1800(napi_env env, napi_callback_info info) {
770 OH_Predicates *predicates = OH_Rdb_CreatePredicates("test");
771 OH_VObject *valueObject = OH_Rdb_CreateValueObject();
772 const char *data1Value = "zhangSan1";
773 valueObject->putText(valueObject, data1Value);
774 predicates->equalTo(predicates, "data1", valueObject);
775
776 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
777 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
778
779 int rowCount = -1;
780 cursor->getRowCount(cursor, &rowCount);
781 NAPI_ASSERT(env, rowCount == 0, "getRowCount is fail.");
782
783 int errCode = cursor->goToNextRow(cursor);
784 NAPI_ASSERT(env, errCode == 14800026, "goToNextRow is fail.");
785
786 predicates->destroy(predicates);
787 errCode = cursor->destroy(cursor);
788
789 napi_value returnCode;
790 napi_create_double(env, errCode, &returnCode);
791 return returnCode;
792 }
793
794 /**
795 * @tc.name: SUB_DDM_RDB_CURSOR_1900
796 * @tc.desc: Normal testCase of cursor for OH_Data_Asset_CreateOne, OH_Data_Asset_SetName.
797 * OH_Data_Asset_SetUri,OH_Data_Asset_SetPath
798 * @tc.type: FUNC
799 */
SUB_DDM_RDB_CURSOR_1900(napi_env env,napi_callback_info info)800 static napi_value SUB_DDM_RDB_CURSOR_1900(napi_env env, napi_callback_info info) {
801 Data_Asset *asset1 = OH_Data_Asset_CreateOne();
802 int errCode = OH_Data_Asset_SetName(asset1, "name1");
803 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetName is fail.");
804 errCode = OH_Data_Asset_SetUri(asset1, "uri1");
805 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetUri is fail.");
806 errCode = OH_Data_Asset_SetPath(asset1, "path1");
807 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetPath is fail.");
808 errCode = OH_Data_Asset_SetCreateTime(asset1, 1);
809 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetCreateTime is fail.");
810 errCode = OH_Data_Asset_SetModifyTime(asset1, 1);
811 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetModifyTime is fail.");
812 errCode = OH_Data_Asset_SetSize(asset1, 1);
813 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetSize is fail.");
814 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_NORMAL);
815 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetStatus is fail.");
816
817 napi_value returnCode;
818 napi_create_double(env, errCode, &returnCode);
819 return returnCode;
820
821 }
822
823 /**
824 * @tc.name: SUB_DDM_RDB_CURSOR_2000
825 * @tc.desc: Normal testCase of cursor for OH_Data_Asset_SetStatus
826 * @tc.type: FUNC
827 */
SUB_DDM_RDB_CURSOR_2000(napi_env env,napi_callback_info info)828 static napi_value SUB_DDM_RDB_CURSOR_2000(napi_env env, napi_callback_info info) {
829 Data_Asset *asset1 = OH_Data_Asset_CreateOne();
830 int errCode = OH_Data_Asset_SetName(asset1, "name1");
831 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Data_Asset_SetName is fail.");
832 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_NULL);
833 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_NULL is fail.");
834 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_NORMAL);
835 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_NORMAL is fail.");
836 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_INSERT);
837 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_INSERT is fail.");
838 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_UPDATE);
839 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_UPDATE is fail.");
840 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_DELETE);
841 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_DELETE is fail.");
842 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_ABNORMAL);
843 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_ABNORMAL is fail.");
844 errCode = OH_Data_Asset_SetStatus(asset1, Data_AssetStatus::ASSET_DOWNLOADING);
845 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "ASSET_DOWNLOADING is fail.");
846
847 napi_value returnCode;
848 napi_create_double(env, errCode, &returnCode);
849 return returnCode;
850 }
851
852 /**
853 * @tc.name: SUB_DDM_RDB_CURSOR_2100
854 * @tc.desc: Normal testCase of cursor for OH_Data_Asset_CreateMultiple
855 * @tc.type: FUNC
856 */
SUB_DDM_RDB_CURSOR_2100(napi_env env,napi_callback_info info)857 static napi_value SUB_DDM_RDB_CURSOR_2100(napi_env env, napi_callback_info info) {
858 char createTableSql[] = "CREATE TABLE IF NOT EXISTS asset_table (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 "
859 "asset, data2 assets );";
860 int errCode = OH_Rdb_Execute(cursorTestRdbStore_, createTableSql);
861 NAPI_ASSERT(env, errCode == OH_Rdb_ErrCode::RDB_OK, "OH_Rdb_Execute is fail.");
862 char table[] = "asset_table";
863 int assetsCount = 2;
864 int curRow = 1;
865 OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
866 Data_Asset *asset1 = OH_Data_Asset_CreateOne();
867 SetAsset(asset1, 1);
868 Data_Asset *asset2 = OH_Data_Asset_CreateOne();
869 SetAsset(asset2, 2);
870 valueBucket->putInt64(valueBucket, "id", curRow);
871 OH_VBucket_PutAsset(valueBucket, "data1", asset1);
872 Data_Asset **assets1 = OH_Data_Asset_CreateMultiple(assetsCount);
873 SetAsset(assets1[0], 1);
874 SetAsset(assets1[1], 2);
875 errCode = OH_VBucket_PutAssets(valueBucket, "data2", assets1, assetsCount);
876 int rowID = OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
877 NAPI_ASSERT(env, rowID == curRow, "OH_Rdb_Insert is fail.");
878 curRow++;
879
880 valueBucket->clear(valueBucket);
881 valueBucket->putInt64(valueBucket, "id", curRow);
882 OH_VBucket_PutAsset(valueBucket, "data1", asset2);
883 Data_Asset **assets2 = OH_Data_Asset_CreateMultiple(assetsCount);
884 SetAsset(assets2[0], 1);
885 SetAsset(assets2[1], 3);
886 errCode = OH_VBucket_PutAssets(valueBucket, "data2", assets2, assetsCount);
887 rowID = OH_Rdb_Insert(cursorTestRdbStore_, table, valueBucket);
888 NAPI_ASSERT(env, rowID == curRow, "OH_Rdb_Insert is fail.");
889
890 OH_Data_Asset_DestroyMultiple(assets1, assetsCount);
891 OH_Data_Asset_DestroyMultiple(assets2, assetsCount);
892 OH_Data_Asset_DestroyOne(asset1);
893 OH_Data_Asset_DestroyOne(asset2);
894 valueBucket->destroy(valueBucket);
895 napi_value returnCode;
896 napi_create_double(env, errCode, &returnCode);
897 return returnCode;
898 }
899
900 /**
901 * @tc.name: SUB_DDM_RDB_CURSOR_2200
902 * @tc.desc: Normal testCase of cursor for getAsset.
903 * @tc.type: FUNC
904 */
SUB_DDM_RDB_CURSOR_2200(napi_env env,napi_callback_info info)905 static napi_value SUB_DDM_RDB_CURSOR_2200(napi_env env, napi_callback_info info) {
906 CreateAssetTable();
907 int errCode = 0;
908 OH_Predicates *predicates = OH_Rdb_CreatePredicates("asset_table");
909 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
910 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
911 cursor->goToNextRow(cursor);
912 OH_ColumnType type;
913 errCode = cursor->getColumnType(cursor, 0, &type);
914 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_INT64, "getColumnType is fail.");
915 int64_t id;
916 errCode = cursor->getInt64(cursor, 0, &id);
917 NAPI_ASSERT(env, id == 1, "getInt64 is fail.");
918 errCode = cursor->getColumnType(cursor, 1, &type);
919 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_ASSET, "getColumnType is fail.");
920
921 Data_Asset *asset = OH_Data_Asset_CreateOne();
922 errCode = cursor->getAsset(cursor, 1, asset);
923 NAPI_ASSERT(env, asset != NULL, "OH_Data_Asset_CreateOne is fail.");
924
925 char name[10] = "";
926 size_t nameLength = 10;
927 errCode = OH_Data_Asset_GetName(asset, name, &nameLength);
928 NAPI_ASSERT(env, strncmp(name, "name", nameLength) == 0, "OH_Data_Asset_GetName is fail.");
929 char uri[10] = "";
930 size_t uriLength = 10;
931 errCode = OH_Data_Asset_GetUri(asset, uri, &uriLength);
932 NAPI_ASSERT(env, strncmp(uri, "uri", uriLength) == 0, "OH_Data_Asset_GetUri is fail.");
933 char path[10] = "";
934 size_t pathLength = 10;
935 errCode = OH_Data_Asset_GetPath(asset, path, &pathLength);
936 NAPI_ASSERT(env, strncmp(path, "path", pathLength) == 0, "OH_Data_Asset_GetPath is fail.");
937 int64_t createTime = 0;
938 errCode = OH_Data_Asset_GetCreateTime(asset, &createTime);
939 NAPI_ASSERT(env, createTime == 1, "getInt64 is fail.");
940 int64_t modifyTime = 0;
941 errCode = OH_Data_Asset_GetModifyTime(asset, &modifyTime);
942 NAPI_ASSERT(env, modifyTime == 1, "OH_Data_Asset_GetModifyTime is fail.");
943 size_t size = 0;
944 errCode = OH_Data_Asset_GetSize(asset, &size);
945 NAPI_ASSERT(env, size == 1, "OH_Data_Asset_GetSize is fail.");
946 Data_AssetStatus status = Data_AssetStatus::ASSET_NULL;
947 errCode = OH_Data_Asset_GetStatus(asset, &status);
948 NAPI_ASSERT(env, status == Data_AssetStatus::ASSET_INSERT, "OH_Data_Asset_GetStatus is fail.");
949
950 predicates->destroy(predicates);
951 OH_Data_Asset_DestroyOne(asset);
952 errCode = cursor->destroy(cursor);
953 napi_value returnCode;
954 napi_create_double(env, errCode, &returnCode);
955 return returnCode;
956 }
957
958 /**
959 * @tc.name: SUB_DDM_RDB_CURSOR_2300
960 * @tc.desc: Normal testCase of cursor for getAssets.
961 * @tc.type: FUNC
962 */
SUB_DDM_RDB_CURSOR_2300(napi_env env,napi_callback_info info)963 static napi_value SUB_DDM_RDB_CURSOR_2300(napi_env env, napi_callback_info info) {
964 CreateAssetTable();
965 int errCode = 0;
966 OH_Predicates *predicates = OH_Rdb_CreatePredicates("asset_table");
967 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
968 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
969 cursor->goToNextRow(cursor);
970 OH_ColumnType type;
971 errCode = cursor->getColumnType(cursor, 0, &type);
972 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_INT64, "getColumnType is fail.");
973 int64_t id;
974 errCode = cursor->getInt64(cursor, 0, &id);
975 NAPI_ASSERT(env, id == 1, "getInt64 is fail.");
976 errCode = cursor->getColumnType(cursor, 2, &type);
977 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_ASSETS, "getColumnType is fail.");
978 uint32_t assetCount = 0;
979 errCode = cursor->getAssets(cursor, 2, nullptr, &assetCount);
980 NAPI_ASSERT(env, assetCount == 2, "getAssets is fail.");
981 Data_Asset **assets = OH_Data_Asset_CreateMultiple(assetCount);
982 errCode = cursor->getAssets(cursor, 2, assets, &assetCount);
983 NAPI_ASSERT(env, assetCount == 2, "getAssets is fail.");
984
985 Data_Asset *asset = assets[1];
986 NAPI_ASSERT(env, asset != NULL, "Assets is fail.");
987
988 char name[10] = "";
989 size_t nameLength = 10;
990 errCode = OH_Data_Asset_GetName(asset, name, &nameLength);
991 NAPI_ASSERT(env, strncmp(name, "name", nameLength) == 0, "OH_Data_Asset_GetName is fail.");
992 char uri[10] = "";
993 size_t uriLength = 10;
994 errCode = OH_Data_Asset_GetUri(asset, uri, &uriLength);
995 NAPI_ASSERT(env, strncmp(uri, "uri", uriLength) == 0, "OH_Data_Asset_GetUri is fail.");
996 char path[10] = "";
997 size_t pathLength = 10;
998 errCode = OH_Data_Asset_GetPath(asset, path, &pathLength);
999 NAPI_ASSERT(env, strncmp(path, "path", pathLength) == 0, "OH_Data_Asset_GetPath is fail.");
1000 int64_t createTime = 0;
1001 errCode = OH_Data_Asset_GetCreateTime(asset, &createTime);
1002 NAPI_ASSERT(env, createTime == 2, "OH_Data_Asset_GetCreateTime is fail.");
1003 int64_t modifyTime = 0;
1004 errCode = OH_Data_Asset_GetModifyTime(asset, &modifyTime);
1005 NAPI_ASSERT(env, modifyTime == 2, "OH_Data_Asset_GetModifyTime is fail.");
1006 size_t size = 0;
1007 errCode = OH_Data_Asset_GetSize(asset, &size);
1008 NAPI_ASSERT(env, size == 2, "OH_Data_Asset_GetSize is fail.");
1009 Data_AssetStatus status = Data_AssetStatus::ASSET_NULL;
1010 errCode = OH_Data_Asset_GetStatus(asset, &status);
1011 NAPI_ASSERT(env, status == Data_AssetStatus::ASSET_INSERT, "OH_Data_Asset_GetStatus is fail.");
1012
1013 predicates->destroy(predicates);
1014 OH_Data_Asset_DestroyMultiple(assets, assetCount);
1015 errCode = cursor->destroy(cursor);
1016 napi_value returnCode;
1017 napi_create_double(env, errCode, &returnCode);
1018 return returnCode;
1019 }
1020
1021
1022 /**
1023 * @tc.name: SUB_DDM_RDB_CURSOR_2400
1024 * @tc.desc: Normal testCase of cursor for getAssets err.
1025 * @tc.type: FUNC
1026 */
SUB_DDM_RDB_CURSOR_2400(napi_env env,napi_callback_info info)1027 static napi_value SUB_DDM_RDB_CURSOR_2400(napi_env env, napi_callback_info info) {
1028 CreateAssetTable();
1029 int errCode = 0;
1030 OH_Predicates *predicates = OH_Rdb_CreatePredicates("asset_table");
1031 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
1032 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
1033 cursor->goToNextRow(cursor);
1034 OH_ColumnType type;
1035 errCode = cursor->getColumnType(cursor, 0, &type);
1036 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_INT64, "getColumnType is fail.");
1037 int64_t id;
1038 errCode = cursor->getInt64(cursor, 0, &id);
1039 NAPI_ASSERT(env, id == 1, "getInt64 is fail.");
1040 errCode = cursor->getColumnType(cursor, 2, &type);
1041 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_ASSETS, "getColumnType is fail.");
1042 uint32_t assetCount = 0;
1043 errCode = cursor->getAssets(cursor, 2, nullptr, &assetCount);
1044 NAPI_ASSERT(env, assetCount == 2, "getAssets is fail.");
1045 Data_Asset **assets = OH_Data_Asset_CreateMultiple(assetCount);
1046 errCode = cursor->getAssets(nullptr, 2, assets, &assetCount);
1047 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "getAssets is fail.");
1048
1049 predicates->destroy(predicates);
1050 errCode = cursor->destroy(cursor);
1051 napi_value returnCode;
1052 napi_create_double(env, errCode, &returnCode);
1053 return returnCode;
1054 }
1055
1056
1057 /**
1058 * @tc.name: SUB_DDM_RDB_CURSOR_2500
1059 * @tc.desc: Normal testCase of cursor for getAssets err.
1060 * @tc.type: FUNC
1061 */
SUB_DDM_RDB_CURSOR_2500(napi_env env,napi_callback_info info)1062 static napi_value SUB_DDM_RDB_CURSOR_2500(napi_env env, napi_callback_info info) {
1063 CreateAssetTable();
1064 int errCode = 0;
1065 OH_Predicates *predicates = OH_Rdb_CreatePredicates("asset_table");
1066 OH_Cursor *cursor = OH_Rdb_Query(cursorTestRdbStore_, predicates, NULL, 0);
1067 NAPI_ASSERT(env, cursor != NULL, "OH_Rdb_Query is fail.");
1068 cursor->goToNextRow(cursor);
1069 OH_ColumnType type;
1070 errCode = cursor->getColumnType(cursor, 0, &type);
1071 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_INT64, "getColumnType is fail.");
1072 int64_t id;
1073 errCode = cursor->getInt64(cursor, 0, &id);
1074 NAPI_ASSERT(env, id == 1, "getInt64 is fail.");
1075 errCode = cursor->getColumnType(cursor, 2, &type);
1076 NAPI_ASSERT(env, type == OH_ColumnType::TYPE_ASSETS, "getColumnType is fail.");
1077 uint32_t assetCount = 0;
1078 errCode = cursor->getAssets(cursor, 2, nullptr, &assetCount);
1079 NAPI_ASSERT(env, assetCount == 2, "getAssets is fail.");
1080 Data_Asset **assets = OH_Data_Asset_CreateMultiple(assetCount);
1081 errCode = cursor->getAssets(cursor, 2, assets, &assetCount);
1082 NAPI_ASSERT(env, assetCount == 2, "getAssets is fail.");
1083
1084 Data_Asset *asset = assets[1];
1085 NAPI_ASSERT(env, asset != NULL, "Assets is fail.");
1086
1087 char name[10] = "";
1088 size_t nameLength = 10;
1089 errCode = OH_Data_Asset_GetName(nullptr, name, &nameLength);
1090 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetName is fail.");
1091 char uri[10] = "";
1092 size_t uriLength = 10;
1093 errCode = OH_Data_Asset_GetUri(nullptr, uri, &uriLength);
1094 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetUri is fail.");
1095 char path[10] = "";
1096 size_t pathLength = 10;
1097 errCode = OH_Data_Asset_GetPath(nullptr, path, &pathLength);
1098 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetPath is fail.");
1099 int64_t createTime = 0;
1100 errCode = OH_Data_Asset_GetCreateTime(nullptr, &createTime);
1101 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetCreateTime is fail.");
1102 int64_t modifyTime = 0;
1103 errCode = OH_Data_Asset_GetModifyTime(nullptr, &modifyTime);
1104 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetModifyTime is fail.");
1105 size_t size = 0;
1106 errCode = OH_Data_Asset_GetSize(nullptr, &size);
1107 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetSize is fail.");
1108 Data_AssetStatus status = Data_AssetStatus::ASSET_NULL;
1109 errCode = OH_Data_Asset_GetStatus(nullptr, &status);
1110 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1111
1112 predicates->destroy(predicates);
1113 OH_Data_Asset_DestroyMultiple(assets, assetCount);
1114 errCode = cursor->destroy(cursor);
1115 napi_value returnCode;
1116 napi_create_double(env, errCode, &returnCode);
1117 return returnCode;
1118 }
1119
1120 /**
1121 * @tc.name: SUB_DDM_RDB_CURSOR_2600
1122 * @tc.desc: Normal testCase of cursor for OH_Data_Asset_SetName err.
1123 * @tc.type: FUNC
1124 */
SUB_DDM_RDB_CURSOR_2600(napi_env env,napi_callback_info info)1125 static napi_value SUB_DDM_RDB_CURSOR_2600(napi_env env, napi_callback_info info) {
1126 int errCode = OH_Data_Asset_SetName(nullptr, "name1");
1127 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1128 errCode = OH_Data_Asset_SetUri(nullptr, "uri1");
1129 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1130 errCode = OH_Data_Asset_SetPath(nullptr, "path1");
1131 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1132 errCode = OH_Data_Asset_SetCreateTime(nullptr, 1);
1133 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1134 errCode = OH_Data_Asset_SetModifyTime(nullptr, 1);
1135 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1136 errCode = OH_Data_Asset_SetSize(nullptr, 1);
1137 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1138 errCode = OH_Data_Asset_SetStatus(nullptr, Data_AssetStatus::ASSET_NORMAL);
1139 NAPI_ASSERT(env, errCode == RDB_E_INVALID_ARGS, "OH_Data_Asset_GetStatus is fail.");
1140 errCode = 0;
1141 napi_value returnCode;
1142 napi_create_double(env, errCode, &returnCode);
1143 return returnCode;
1144
1145 }
1146
1147 EXTERN_C_START
Init(napi_env env,napi_value exports)1148 static napi_value Init(napi_env env, napi_value exports) {
1149 napi_property_descriptor desc[] = {
1150 {"RdbFilePath", nullptr, RdbFilePath, nullptr, nullptr, nullptr, napi_default, nullptr},
1151 {"CursorSetUpTestCase", nullptr, CursorSetUpTestCase, nullptr, nullptr, nullptr, napi_default, nullptr},
1152 {"CursorTearDownTestCase", nullptr, CursorTearDownTestCase, nullptr, nullptr, nullptr, napi_default, nullptr},
1153 {"SUB_DDM_RDB_CURSOR_0100", nullptr, SUB_DDM_RDB_CURSOR_0100, nullptr, nullptr, nullptr, napi_default, nullptr},
1154 {"SUB_DDM_RDB_CURSOR_0200", nullptr, SUB_DDM_RDB_CURSOR_0200, nullptr, nullptr, nullptr, napi_default, nullptr},
1155 {"SUB_DDM_RDB_CURSOR_0300", nullptr, SUB_DDM_RDB_CURSOR_0300, nullptr, nullptr, nullptr, napi_default, nullptr},
1156 {"SUB_DDM_RDB_CURSOR_0400", nullptr, SUB_DDM_RDB_CURSOR_0400, nullptr, nullptr, nullptr, napi_default, nullptr},
1157 {"SUB_DDM_RDB_CURSOR_0500", nullptr, SUB_DDM_RDB_CURSOR_0500, nullptr, nullptr, nullptr, napi_default, nullptr},
1158 {"SUB_DDM_RDB_CURSOR_0600", nullptr, SUB_DDM_RDB_CURSOR_0600, nullptr, nullptr, nullptr, napi_default, nullptr},
1159 {"SUB_DDM_RDB_CURSOR_0700", nullptr, SUB_DDM_RDB_CURSOR_0700, nullptr, nullptr, nullptr, napi_default, nullptr},
1160 {"SUB_DDM_RDB_CURSOR_0800", nullptr, SUB_DDM_RDB_CURSOR_0800, nullptr, nullptr, nullptr, napi_default, nullptr},
1161 {"SUB_DDM_RDB_CURSOR_0900", nullptr, SUB_DDM_RDB_CURSOR_0900, nullptr, nullptr, nullptr, napi_default, nullptr},
1162 {"SUB_DDM_RDB_CURSOR_1000", nullptr, SUB_DDM_RDB_CURSOR_1000, nullptr, nullptr, nullptr, napi_default, nullptr},
1163 {"SUB_DDM_RDB_CURSOR_1100", nullptr, SUB_DDM_RDB_CURSOR_1100, nullptr, nullptr, nullptr, napi_default, nullptr},
1164 {"SUB_DDM_RDB_CURSOR_1200", nullptr, SUB_DDM_RDB_CURSOR_1200, nullptr, nullptr, nullptr, napi_default, nullptr},
1165 {"SUB_DDM_RDB_CURSOR_1300", nullptr, SUB_DDM_RDB_CURSOR_1300, nullptr, nullptr, nullptr, napi_default, nullptr},
1166 {"SUB_DDM_RDB_CURSOR_1400", nullptr, SUB_DDM_RDB_CURSOR_1400, nullptr, nullptr, nullptr, napi_default, nullptr},
1167 {"SUB_DDM_RDB_CURSOR_1500", nullptr, SUB_DDM_RDB_CURSOR_1500, nullptr, nullptr, nullptr, napi_default, nullptr},
1168 {"SUB_DDM_RDB_CURSOR_1600", nullptr, SUB_DDM_RDB_CURSOR_1600, nullptr, nullptr, nullptr, napi_default, nullptr},
1169 {"SUB_DDM_RDB_CURSOR_1700", nullptr, SUB_DDM_RDB_CURSOR_1700, nullptr, nullptr, nullptr, napi_default, nullptr},
1170 {"SUB_DDM_RDB_CURSOR_1800", nullptr, SUB_DDM_RDB_CURSOR_1800, nullptr, nullptr, nullptr, napi_default, nullptr},
1171 {"SUB_DDM_RDB_CURSOR_1900", nullptr, SUB_DDM_RDB_CURSOR_1900, nullptr, nullptr, nullptr, napi_default, nullptr},
1172 {"SUB_DDM_RDB_CURSOR_2000", nullptr, SUB_DDM_RDB_CURSOR_2000, nullptr, nullptr, nullptr, napi_default, nullptr},
1173 {"SUB_DDM_RDB_CURSOR_2100", nullptr, SUB_DDM_RDB_CURSOR_2100, nullptr, nullptr, nullptr, napi_default, nullptr},
1174 {"SUB_DDM_RDB_CURSOR_2200", nullptr, SUB_DDM_RDB_CURSOR_2200, nullptr, nullptr, nullptr, napi_default, nullptr},
1175 {"SUB_DDM_RDB_CURSOR_2300", nullptr, SUB_DDM_RDB_CURSOR_2300, nullptr, nullptr, nullptr, napi_default, nullptr},
1176 {"SUB_DDM_RDB_CURSOR_2400", nullptr, SUB_DDM_RDB_CURSOR_2400, nullptr, nullptr, nullptr, napi_default, nullptr},
1177 {"SUB_DDM_RDB_CURSOR_2500", nullptr, SUB_DDM_RDB_CURSOR_2500, nullptr, nullptr, nullptr, napi_default, nullptr},
1178 {"SUB_DDM_RDB_CURSOR_2600", nullptr, SUB_DDM_RDB_CURSOR_2600, nullptr, nullptr, nullptr, napi_default, nullptr},
1179 };
1180
1181
1182 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1183 return exports;
1184 }
1185 EXTERN_C_END
1186
1187
1188 static napi_module demoModule = {
1189 .nm_version = 1,
1190 .nm_flags = 0,
1191 .nm_filename = nullptr,
1192 .nm_register_func = Init,
1193 .nm_modname = "cursor",
1194 .nm_priv = ((void *)0),
1195 .reserved = {0},
1196 };
1197
RegisterEntryModule(void)1198 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) {
1199 napi_module_register(&demoModule);
1200 }
1201