diff --git a/distributeddatamgr/kvStorejstest/hap/src/main/js/test/KvManagerPromiseJsunit.test.js b/distributeddatamgr/kvStorejstest/hap/src/main/js/test/KvManagerPromiseJsunit.test.js index f51dd28bcf5f6aef89746a49bbfd91819313ef54..08ac3432ec975f3f5c8f69e400a04fd68a5b308b 100644 --- a/distributeddatamgr/kvStorejstest/hap/src/main/js/test/KvManagerPromiseJsunit.test.js +++ b/distributeddatamgr/kvStorejstest/hap/src/main/js/test/KvManagerPromiseJsunit.test.js @@ -14,6 +14,10 @@ */ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' import factory from '@ohos.data.distributedData'; +import abilityFeatureAbility from '@ohos.ability.featureAbility'; + +var context = abilityFeatureAbility.getContext(); +var contextApplication = context.getApplicationContext() const TEST_BUNDLE_NAME = 'ohos.acts.kvStore'; const TEST_STORE_ID = 'storeId'; @@ -863,6 +867,91 @@ describe('kvManagerPromiseTest', function () { } done(); }) + + /** + * @tc.number SUB_DISTRIBUTEDDATAMGR_KVMANAGER_CONTEXT_1000 + * @tc.name [JS-API9]KVManagerConfig.context + * @tc.desc Test Js Api createKVManager testcase 001 + */ + it('testcreateKVManager001', 0, async function (done) { + console.info('testcreateKVManager001'); + const config = { + bundleName: TEST_BUNDLE_NAME, + userInfo: { + userId: '0', + userType: factory.UserType.SAME_USER_ID + }, + context:context + } + try { + await factory.createKVManager(config).then((manager) => { + kvManager = manager; + expect(manager !=null).assertTrue(); + console.info('testcreateKVManager001 createKVManager success'); + kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => { + console.info("testcreateKVManager001 getKVStore success"); + await store.put("createKVManager","success").then(async () => { + await store.get("craeteKVManager","defaultValue").then((data) => { + expect("success").assertEqual(data) + console.info("testcreateKVManager001 put data success") + }) + }) + }).catch((err) => { + console.info("testcreateKVManager001 getKVStore err: " + JSON.stringify(err)); + expect(null).assertFail(); + }); + }).catch((err) => { + console.info('testcreateKVManager001 createKVManager err ' + err); + expect(null).assertFail() + }); + }catch (e) { + console.info('testcreateKVManager001 promise delete fail err' + err); + expect(null).assertFail(); + } + done(); + }) + + /** + * @tc.number SUB_DISTRIBUTEDDATAMGR_KVMANAGER_CONTEXTAPPLICATION_1000 + * @tc.name [JS-API9]KVManagerConfig.context + * @tc.desc Test Js Api createKVManager testcase 001 + */ + it('testcreateKVManager002', 0, async function (done) { + console.info('testcreateKVManager002'); + const config = { + bundleName: TEST_BUNDLE_NAME, + userInfo: { + userId: '0', + userType: factory.UserType.SAME_USER_ID + }, + context:contextApplication + } + try { + await factory.createKVManager(config).then((manager) => { + kvManager = manager; + console.info('testcreateKVManager002 createKVManager success'); + kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => { + console.info("testcreateKVManager002 getKVStore success"); + await store.put("createKVManager","success").then(async () => { + await store.get("craeteKVManager","defaultValue").then((data) => { + expect("success").assertEqual(data) + console.info("testcreateKVManager002 put data success") + }) + }) + }).catch((err) => { + console.info("testcreateKVManager002 getKVStore err: " + JSON.stringify(err)); + expect(null).assertFail(); + }); + }).catch((err) => { + console.info('testcreateKVManager002 createKVManager err ' + err); + expect(null).assertFail() + }); + }catch (e) { + console.info('testcreateKVManager002 promise delete fail err' + err); + expect(null).assertFail(); + } + done(); + }) }) } \ No newline at end of file diff --git a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/List.test.js b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/List.test.js index 2108d9c2ec073830a2cae472287f34d60da85eff..e9fe4f1bbca2b52df40d5df4b82e10952908a8f1 100644 --- a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/List.test.js +++ b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/List.test.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import rdbStoreBackupRestoreCallbackTest from './RdbstoreBackupRestoreCallbackJsunit.test.js' import rdbStoreBackupRestoreWithFAContextTest from './RdbstoreBackupRestoreWithFAContextJsunit.test.js' import rdbstoreChangeEncryptKeyTest from './RdbstoreChangeEncryptKeyJsunit.test.js' import rdbStoreDeleteTest from './RdbstoreDeleteJsunit.test.js' @@ -24,7 +25,9 @@ import rdbResultSetTest from './RdbStoreResultSetJsunit.test.js' import rdbstoreStoreExcuteSqlTest from './RdbstoreStoreExcuteSqlJsunit.test.js' import rdbstoreTransactionTest from './RdbstoreTransactionJsunit.test.js' import rdbStoreUpdateTest from './RdbstoreUpdateJsunit.test.js' +import rdbstoreQueryTest from './RdbstoreQuery.test.js' export default function testsuite() { +rdbStoreBackupRestoreCallbackTest() rdbStoreBackupRestoreWithFAContextTest() rdbstoreChangeEncryptKeyTest() rdbStoreDeleteTest() @@ -37,4 +40,5 @@ rdbResultSetTest() rdbstoreStoreExcuteSqlTest() rdbstoreTransactionTest() rdbStoreUpdateTest() +rdbstoreQueryTest() } diff --git a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreCallbackJsunit.test.js b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreCallbackJsunit.test.js new file mode 100644 index 0000000000000000000000000000000000000000..6c342b4d05bdfc9999671a5e123d4380b94f05b2 --- /dev/null +++ b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreCallbackJsunit.test.js @@ -0,0 +1,459 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' +import dataRdb from '@ohos.data.rdb' +import abilityFeatureAbility from '@ohos.ability.featureAbility' +import fileio from '@ohos.fileio' + +const TAG = "[RDB_JSKITS_TEST]" +const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS backupTest (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)" +const DATABASE_DIR = "/data/storage/el2/database/entry/rdb/" +var RdbStore +var context +const STORE_CONFIG = { + name: "BackupResotreTest.db", +} +const DATABASE_BACKUP_NAME = "Backup.db" + +async function CreatRdbStore(context, STORE_CONFIG) { + let RdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1) + await RdbStore.executeSql(CREATE_TABLE_TEST, null) + let u8 = new Uint8Array([1, 2, 3]) + { + const valueBucket = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + await RdbStore.insert("backupTest", valueBucket) + } + { + const valueBucket = { + "name": "lisi", + "age": 28, + "salary": 100.5, + "blobType": u8, + } + await RdbStore.insert("backupTest", valueBucket) + } + { + const valueBucket = { + "name": "wangwu", + "age": 38, + "salary": 90.0, + "blobType": u8, + } + await RdbStore.insert("backupTest", valueBucket) + } + return RdbStore +} + +function BackupCallbackTest(backupName) { + try { + RdbStore.backup(backupName, (err, data) => { + if(err != null){ + console.info(TAG + "Backup error: " + err) + expect(true).assertTrue() + }else{ + expect(false).assertTrue(); + } + }) + } catch(errInfo){ + console.info(TAG + "BackupCallbackTest error: " + errInfo) + expect(true).assertTrue() + } + + RdbStore = null +} + +function ReStoreCallbackTest(restoreName) { + try { + RdbStore.restore(restoreName, (err, data) => { + if(err != null){ + console.info(TAG + "Restore error: " + err) + expect(true).assertTrue() + }else{ + expect(false).assertTrue(); + } + }) + } catch(errInfo) { + console.info(TAG + "ReStoreCallbackTest error: " + errInfo) + expect(true).assertTrue() + } + + RdbStore = null +} + +export default function rdbStoreBackupRestoreCallbackTest() { + describe('rdbStoreBackupRestoreCallbackTest', function () { + + + beforeAll(async function () { + console.info(TAG + 'beforeAll') + }) + + beforeEach(async function () { + console.info(TAG + 'beforeEach') + context = abilityFeatureAbility.getContext() + RdbStore = await CreatRdbStore(context, STORE_CONFIG) + }) + + afterEach(async function () { + console.info(TAG + 'afterEach') + await dataRdb.deleteRdbStore(context, STORE_CONFIG.name) + await dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME) + await dataRdb.deleteRdbStore(context, "BackupTest003.db") + }) + + afterAll(async function () { + console.info(TAG + 'afterAll') + }) + + console.info(TAG + "*************Unit Test Begin*************") + + /** + * @tc.name RDB Backup Restore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0100 + * @tc.desc RDB backup and restore function test + */ + it('RdbBackupRestoreCallbackTest_0100', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0100 start *************") + + // RDB backup function test + await RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + }else{ + try { + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name) + } catch (err) { + expect(false).assertTrue() + } + } + }) + + // RDB before restored, delete data + let deleteData = new dataRdb.RdbPredicates("backupTest") + deleteData.equalTo("name", "zhangsan") + await RdbStore.delete(deleteData) + + // RDB restore function test + await RdbStore.restore(DATABASE_BACKUP_NAME, async (err, data) => { + if(err != null){ + expect(false).assertTrue() + }else{ + try { + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + expect(false).assertTrue() + } catch (err) { + expect(true).assertTrue() + } + + try { + fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name) + } catch (err) { + expect(false).assertTrue() + } + let predicates = new dataRdb.RdbPredicates("backupTest") + predicates.equalTo("name", "zhangsan") + let resultSet = await RdbStore.query(predicates) + try { + console.info(TAG + "After restore resultSet query done") + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + expect(1).assertEqual(id) + expect("zhangsan").assertEqual(name) + expect(1).assertEqual(blobType[0]) + } catch (err) { + console.info(TAG + 'RdbBackupRestoreTest_0010 accessSync err4: ' + err) + expect(false).assertTrue() + } + resultSet = null + RdbStore = null + } + }) + done() + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0100 end *************") + }) + + /** + * @tc.name RDB Backup test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0200 + * @tc.desc RDB backup function test + */ + it('RdbBackupRestoreCallbackTest_0200', 0, function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0200 start *************") + // RDB backup function test, backup file name empty + BackupCallbackTest("") + + // RDB backup function test, backup file name already exists + BackupCallbackTest(STORE_CONFIG.name) + + done() + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0200 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0300 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0300', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0300 start *************") + let backupName = "BackupTest003.db" + await RdbStore.backup(backupName) + + // RDB restore function test, backup file name empty + ReStoreCallbackTest("") + + // RDB restore function test, backup file is specified to database name + ReStoreCallbackTest(STORE_CONFIG.name) + + done() + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0300 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0400 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0400', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0400 start *************") + let dbName = "notExistName.db" + + // RDB restore function test, backup file does not exists + try { + fileio.accessSync(DATABASE_DIR + dbName) + expect(false).assertTrue() + } catch { + ReStoreCallbackTest(dbName) + } + + done() + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0400 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0500 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0500', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0500 start *************") + + // RDB restore function test, backup file + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue(0) + }else{ + try{ + console.info(TAG + 'Backup database success') + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + expect(false).assertTrue(); + } + } + }) + await dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME).then(() => { + try{ + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + console.info(TAG + 'error2 ' + err) + expect(true).assertTrue(); + } + }) + + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue(0) + }else{ + try{ + console.info(TAG + 'Backup database success') + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + expect(false).assertTrue(); + } + } + }) + done() + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0500 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0600 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0600', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0600 start *************") + + // Backup file is specified to database name + RdbStore.backup(STORE_CONFIG.name, (err, data) => { + if(err != null){ + expect(true).assertTrue() + }else{ + expect(false).assertTrue() + } + }) + + RdbStore.backup(STORE_CONFIG.name, (err, data) => { + if(err != null){ + expect(true).assertTrue() + }else{ + expect(false).assertTrue() + } + }) + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0600 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0700 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0700', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0700 start *************") + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + }else{ + expect(true).assertTrue() + } + }) + await dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME).then(() => { + try{ + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + expect(true).assertTrue(); + } + }) + + RdbStore.restore(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(true).assertTrue() + } + }) + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0700 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0800 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0800', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0800 start *************") + BackupCallbackTest() + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0800 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0900 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_0900', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0900 start *************") + BackupCallbackTest([DATABASE_BACKUP_NAME]) + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_0900 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_1000 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_1000', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1000 start *************") + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + } + }) + ReStoreCallbackTest([DATABASE_BACKUP_NAME]) + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1000 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_1100 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_1100', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1100 start *************") + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + } + }) + ReStoreCallbackTest() + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1100 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_1200 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_1200', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1200 start *************") + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + } + }) + BackupCallbackTest(DATABASE_BACKUP_NAME) + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1200 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_1300 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreCallbackTest_1300', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1300 start *************") + RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + }else{ + RdbStore.restore(DATABASE_BACKUP_NAME, (err, data) => { + if(err != null){ + expect(false).assertTrue() + }else{ + ReStoreCallbackTest(DATABASE_BACKUP_NAME) + } + }) + } + }) + done(); + console.info(TAG + "************* RdbBackupRestoreCallbackTest_1300 end *************") + }) + console.info(TAG + "*************Unit Test End*************") + }) +} \ No newline at end of file diff --git a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreWithFAContextJsunit.test.js b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreWithFAContextJsunit.test.js index 1a4c90017613fa6f96b19660cafd7cb33f2fdb79..5f88b4c6cccd6afd389d87644117ed75403e78dc 100644 --- a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreWithFAContextJsunit.test.js +++ b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreBackupRestoreWithFAContextJsunit.test.js @@ -20,7 +20,7 @@ import fileio from '@ohos.fileio' const TAG = "[RDB_JSKITS_TEST]" const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)" -const DATABASE_DIR = "/data/storage/el2/database/entry/db/" +const DATABASE_DIR = "/data/storage/el2/database/entry/rdb/" var RdbStore var context const STORE_CONFIG = { @@ -68,10 +68,12 @@ async function BackupTest(backupName) { promiseRestore.then(() => { expect(false).assertTrue() }).catch((err) => { + console.info(TAG + "Backup error: " + err) expect(true).assertTrue() }) await promiseRestore - } catch { + } catch(errInfo){ + console.info(TAG + "BackupTest error: " + errInfo) expect(true).assertTrue() } @@ -84,18 +86,20 @@ async function ReStoreTest(restoreName) { promiseRestore.then(() => { expect(false).assertTrue() }).catch((err) => { + console.info(TAG + "Restore error: " + err) expect(true).assertTrue() }) await promiseRestore - } catch { + } catch(errInfo) { + console.info(TAG + "ReStoreTest error: " + errInfo) expect(true).assertTrue() } RdbStore = null } -export default function rdbStoreBackupRestoreWithFAContextTest() { -describe('rdbStoreBackupRestoreWithFAContextTest', function () { +export default function rdbStoreBackupRestorePromiseTest() { +describe('rdbStoreBackupRestorePromiseTest', function () { beforeAll(async function () { console.info(TAG + 'beforeAll') }) @@ -119,70 +123,75 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { console.info(TAG + "*************Unit Test Begin*************") - // /** - // * @tc.name RDB Backup Restore test - // * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0010 - // * @tc.desc RDB backup and restore function test - // */ - // it('RdbBackupRestoreTest_0010', 0, async function (done) { - // await console.info(TAG + "************* RdbBackupRestoreTest_0010 start *************") - - // // RDB backup function test - // let promiseBackup = RdbStore.backup(DATABASE_BACKUP_NAME) - // promiseBackup.then(() => { - // try { - // fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) - // fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name) - // } catch (err) { - // expect(false).assertTrue() - // } - // }).catch((err) => { - // expect(false).assertTrue() - // }) - // await promiseBackup - - // // RDB restore function test - // let promiseRestore = RdbStore.restore(DATABASE_BACKUP_NAME) - // promiseRestore.then(() => { - // try { - // fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) - // expect(false).assertTrue() - // } catch (err) { - // expect(true).assertTrue() - // } - - // try { - // fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name) - // expect(true).assertTrue() - // } catch (err) { - // expect(false).assertTrue() - // } - // }).catch((err) => { - // expect(false).assertTrue() - // }) - // await promiseRestore - - // // RDB after restored, data query test - // let predicates = new dataRdb.RdbPredicates("test") - // predicates.equalTo("name", "zhangsan") - // let resultSet = await RdbStore.query(predicates) - // try { - // console.info(TAG + "After restore resultSet query done") - // expect(true).assertEqual(resultSet.goToFirstRow()) - // const id = resultSet.getLong(resultSet.getColumnIndex("id")) - // const name = resultSet.getString(resultSet.getColumnIndex("name")) - // const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) - // expect(1).assertEqual(id) - // expect("zhangsan").assertEqual(name) - // expect(1).assertEqual(blobType[0]) - // } catch (err) { - // expect(false).assertTrue() - // } - // resultSet = null - // RdbStore = null - // done() - // await console.info(TAG + "************* RdbBackupRestoreTest_0010 end *************") - // }) + /** + * @tc.name RDB Backup Restore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0010 + * @tc.desc RDB backup and restore function test + */ + it('RdbBackupRestoreTest_0010', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0010 start *************") + + // RDB backup function test + let promiseBackup = RdbStore.backup(DATABASE_BACKUP_NAME) + promiseBackup.then(() => { + try { + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name) + } catch (err) { + expect(false).assertTrue() + } + }).catch((err) => { + expect(false).assertTrue() + }) + await promiseBackup + + // RDB before restored, delete data + let deleteData = new dataRdb.RdbPredicates("test") + deleteData.equalTo("name", "zhangsan") + await RdbStore.delete(deleteData) + + // RDB restore function test + let promiseRestore = RdbStore.restore(DATABASE_BACKUP_NAME) + promiseRestore.then(() => { + try { + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + expect(false).assertTrue() + } catch (err) { + expect(true).assertTrue() + } + + try { + fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name) + } catch (err) { + expect(false).assertTrue() + } + }).catch((err) => { + expect(false).assertTrue() + }) + await promiseRestore + + // RDB after restored, data query test + let predicates = new dataRdb.RdbPredicates("test") + predicates.equalTo("name", "zhangsan") + let resultSet = await RdbStore.query(predicates) + try { + console.info(TAG + "After restore resultSet query done") + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + expect(1).assertEqual(id) + expect("zhangsan").assertEqual(name) + expect(1).assertEqual(blobType[0]) + } catch (err) { + console.info(TAG + 'RdbBackupRestoreTest_0010 accessSync err4: ' + err) + expect(false).assertTrue() + } + resultSet = null + RdbStore = null + done() + console.info(TAG + "************* RdbBackupRestoreTest_0010 end *************") + }) /** * @tc.name RDB Backup test @@ -190,7 +199,7 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { * @tc.desc RDB backup function test */ it('RdbBackupRestoreTest_0020', 0, async function (done) { - await console.info(TAG + "************* RdbBackupRestoreTest_0020 start *************") + console.info(TAG + "************* RdbBackupRestoreTest_0020 start *************") // RDB backup function test, backup file name empty BackupTest("") @@ -198,7 +207,7 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { BackupTest(STORE_CONFIG.name) done() - await console.info(TAG + "************* RdbBackupRestoreTest_0020 end *************") + console.info(TAG + "************* RdbBackupRestoreTest_0020 end *************") }) /** @@ -207,7 +216,7 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { * @tc.desc RDB restore function test */ it('RdbBackupRestoreTest_0030', 0, async function (done) { - await console.info(TAG + "************* RdbBackupRestoreTest_0030 start *************") + console.info(TAG + "************* RdbBackupRestoreTest_0030 start *************") let backupName = "BackupTest003.db" await RdbStore.backup(backupName) @@ -218,7 +227,7 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { ReStoreTest(STORE_CONFIG.name) done() - await console.info(TAG + "************* RdbBackupRestoreTest_0030 end *************") + console.info(TAG + "************* RdbBackupRestoreTest_0030 end *************") }) /** @@ -227,7 +236,7 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { * @tc.desc RDB restore function test */ it('RdbBackupRestoreTest_0040', 0, async function (done) { - await console.info(TAG + "************* RdbBackupRestoreTest_0040 start *************") + console.info(TAG + "************* RdbBackupRestoreTest_0040 start *************") let dbName = "notExistName.db" // RDB restore function test, backup file does not exists @@ -239,10 +248,173 @@ describe('rdbStoreBackupRestoreWithFAContextTest', function () { } done() - await console.info(TAG + "************* RdbBackupRestoreTest_0040 end *************") + console.info(TAG + "************* RdbBackupRestoreTest_0040 end *************") }) + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0050 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0050', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0050 start *************") + + // RDB restore function test, backup file + await RdbStore.backup(DATABASE_BACKUP_NAME).then(() => { + try{ + console.info(TAG + 'Backup database success') + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + expect(false).assertTrue(); + } + }).then(() => { + dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME).then(() => { + try{ + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + console.info(TAG + 'error2 ' + err) + expect(true).assertTrue(); + } + }) + }).then(() => { + RdbStore.backup(DATABASE_BACKUP_NAME).then(() => { + try{ + console.info(TAG + 'Backup database success') + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + console.info(TAG + 'error3 ' + err) + expect(false).assertTrue() + } + }) + }) + done() + console.info(TAG + "************* RdbBackupRestoreTest_0050 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0060 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0060', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0060 start *************") + + // Backup file is specified to database name + await RdbStore.backup(STORE_CONFIG.name).then(() => { + console.info(TAG + 'Backup database finish') + }).catch((err) => { + expect(true).assertTrue() + }) + + // Backup file is specified to database name again + await RdbStore.backup(STORE_CONFIG.name).then(() => { + console.info(TAG + 'Backup database finish') + }).catch((err) => { + expect(true).assertTrue(); + }) + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0060 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0070 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0070', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0070 start *************") + await RdbStore.backup(DATABASE_BACKUP_NAME) + await dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME).then(() => { + try{ + fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME) + }catch(err){ + expect(true).assertTrue(); + } + }) + await RdbStore.restore(DATABASE_BACKUP_NAME).catch((err) => { + console.info(TAG + 'Restore fail: ' + err) + expect(true).assertTrue(); + }) + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0070 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0080 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0080', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0080 start *************") + BackupTest() + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0080 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0090 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0090', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0090 start *************") + BackupTest([DATABASE_BACKUP_NAME]) + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0090 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0100 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0100', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0100 start *************") + await RdbStore.backup(DATABASE_BACKUP_NAME) + ReStoreTest([DATABASE_BACKUP_NAME]) + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0100 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0110 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0110', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0110 start *************") + await RdbStore.backup(DATABASE_BACKUP_NAME) + ReStoreTest() + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0110 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0120 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0120', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0120 start *************") + await RdbStore.backup(DATABASE_BACKUP_NAME) + BackupTest(DATABASE_BACKUP_NAME) + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0120 end *************") + }) + + /** + * @tc.name RDB BackupRestore test + * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0130 + * @tc.desc RDB restore function test + */ + it('RdbBackupRestoreTest_0130', 0, async function (done) { + console.info(TAG + "************* RdbBackupRestoreTest_0130 start *************") + await RdbStore.backup(DATABASE_BACKUP_NAME) + await RdbStore.restore(DATABASE_BACKUP_NAME) + ReStoreTest(DATABASE_BACKUP_NAME) + done(); + console.info(TAG + "************* RdbBackupRestoreTest_0130 end *************") + }) console.info(TAG + "*************Unit Test End*************") - } -) + }) } diff --git a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreInsertJsunit.test.js b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreInsertJsunit.test.js index ba393fb8d4fba370519eefd28e1de8eb161d0c91..ad08336449719db73ad54d1ccf5841fee13483db 100644 --- a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreInsertJsunit.test.js +++ b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreInsertJsunit.test.js @@ -18,19 +18,24 @@ import dataRdb from '@ohos.data.rdb'; const TAG = "[RDB_JSKITS_TEST]" const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; - +const CREATE_TABLE_NAME = "CREATE TABLE IF NOT EXISTS test" +const CREATE_TABLE = " (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; const STORE_CONFIG = { name: "InsertTest.db", } var rdbStore = undefined; +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} export default function rdbstoreInsertTest() { describe('rdbstoreInsertTest', function () { beforeAll(async function () { console.info(TAG + 'beforeAll') rdbStore = await dataRdb.getRdbStore(STORE_CONFIG, 1); - await rdbStore.executeSql(CREATE_TABLE_TEST, null); + await rdbStore.executeSql(CREATE_TABLE_TEST, null) + await rdbStore.executeSql(CREATE_TABLE_BATCHINSERT_TEST, null); }) beforeEach(async function () { @@ -45,7 +50,9 @@ describe('rdbstoreInsertTest', function () { afterAll(async function () { console.info(TAG + 'afterAll') rdbStore = null - await dataRdb.deleteRdbStore("InsertTest.db"); + await dataRdb.deleteRdbStore("InsertTest.db").then(() => { + sleep(2) + }); }) console.info(TAG + "*************Unit Test Begin*************"); @@ -195,5 +202,877 @@ describe('rdbstoreInsertTest', function () { done(); console.log(TAG + "************* testRdbStoreInsert0004 end *************"); }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0010 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0001', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertPromise0001 start *************"); + await rdbStore.executeSql(CREATE_TABLE_NAME + "1" + CREATE_TABLE) + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.batchInsert("test1", valueBuckets).then((number) => { + expect(3).assertEqual(number) + }).catch((err) =>{ + expect(false).assertTrue(); + }) + + let predicates = new dataRdb.RdbPredicates("test1"); + predicates.equalTo("name", "zhangsan") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(1).assertEqual(id); + expect("zhangsan").assertEqual(name) + expect(18).assertEqual(age) + expect(100.5).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + expect(false).assertEqual(resultSet.goToNextRow()) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + predicates = new dataRdb.RdbPredicates("test1"); + predicates.equalTo("name", "lisi") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(2).assertEqual(id); + expect("lisi").assertEqual(name) + expect(23).assertEqual(age) + expect(200).assertEqual(salary) + expect(false).assertEqual(resultSet.goToNextRow()) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + resultSet = null + done() + console.info(TAG + "************* testRdbStorebatchInsertPromise0001 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0020 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0002', 0, async function (done) { + await rdbStore.executeSql(CREATE_TABLE_NAME + "2" + CREATE_TABLE) + console.info(TAG + "************* testRdbStorebatchInsertPromise0002 start *************"); + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.batchInsert("test2", valueBuckets).then((number) => { + expect(3).assertEqual(number) + }).catch((err) =>{ + expect(false).assertTrue(); + }) + let predicates = new dataRdb.RdbPredicates("test2"); + predicates.equalTo("name", "lisi") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(2).assertEqual(id); + expect("lisi").assertEqual(name) + expect(23).assertEqual(age) + expect(200).assertEqual(salary) + await rdbstore.delete(predicates).then((number) => { + expect(1).assertEqual(number) + }).then(async () => { + resultSet = await rdbStore.query(predicates).catch((err) =>{ + expect(true).assertTrue(); + }) + }) + } catch (e) { + console.info("BatchInsert2 error " + e); + } + resultSet = null + done() + console.info(TAG + "************* testRdbStorebatchInsertPromise0002 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0030 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0003', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertPromise0003 start *************"); + await rdbStore.executeSql(CREATE_TABLE_NAME + "3" + CREATE_TABLE) + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.batchInsert("test3", valueBuckets).then((number) => { + expect(3).assertEqual(number) + }).catch((err) =>{ + expect(false).assertTrue(); + }) + + let predicates = new dataRdb.RdbPredicates("test3"); + predicates.equalTo("name", "zhangsan") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(1).assertEqual(id); + expect("zhangsan").assertEqual(name) + expect(18).assertEqual(age) + expect(100.5).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + predicates = new dataRdb.RdbPredicates("test3"); + predicates.equalTo("name", "lisi") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(2).assertEqual(id); + expect("lisi").assertEqual(name) + expect(23).assertEqual(age) + expect(200).assertEqual(salary) + const valueBucket4 = { + "name": "zhangmaowen", + "age": 25, + "salary": 500, + "blobType": u8, + } + await rdbstore.insert("test3",valueBucket4) + predicates = new dataRdb.RdbPredicates("test3"); + predicates.equalTo("name", "zhangmaowen") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(4).assertEqual(id); + expect("zhangmaowen").assertEqual(name) + expect(25).assertEqual(age) + expect(500).assertEqual(salary) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + } catch (e) { + console.info("BatchInsert1 error " + e); + } + resultSet = null + done(); + console.info(TAG + "************* testRdbStorebatchInsertPromise0003 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0040 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0004', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertPromise0004 start *************"); + await rdbStore.executeSql(CREATE_TABLE_NAME + "4" + CREATE_TABLE) + var u8 = new Uint8Array([1, 2, 3]) + var valueBuckets = new Array(100); + for(var i=0;i { + console.info(TAG + "Batch insert data end") + expect(100).assertEqual(number) + }).catch((err) =>{ + expect(false).assertTrue(); + }) + + let predicates = new dataRdb.RdbPredicates("test4"); + predicates.equalTo("name", "zhangsan55") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(56).assertEqual(id); + expect("zhangsan55").assertEqual(name) + expect(55).assertEqual(age) + expect(56).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + resultSet = null; + done() + console.info(TAG + "************* testRdbStorebatchInsertPromise0004 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0050 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0005', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertPromise0005 start *************"); + await rdbStore.executeSql(CREATE_TABLE_NAME + "5" + CREATE_TABLE) + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket3 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.batchInsert("test5", valueBuckets).then((number) => { + expect(3).assertEqual(number) + }).catch((err) =>{ + expect(false).assertTrue(); + }) + + let predicates = new dataRdb.RdbPredicates("test5"); + predicates.equalTo("name", "zhangsan") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + " name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect("zhangsan").assertEqual(name) + expect(18).assertEqual(age) + expect(100.5).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + expect(true).assertEqual(resultSet.goToNextRow()) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + resultSet = null; + done(); + console.info(TAG + "************* testRdbStorebatchInsertPromise0005 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0060 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0006', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertPromise0006 start *************"); + await rdbStore.executeSql(CREATE_TABLE_NAME + "6" + CREATE_TABLE) + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + try{ + await rdbStore.batchInsert("test6","valueBuckets").catch((err) =>{ + expect(true).assertTrue(); + }) + }catch(err){ + console.info(TAG + "Batch insert data error: " + err) + expect(true).assertTrue(); + } + done() + console.info(TAG + "************* testRdbStorebatchInsertPromise0006 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Promise_0070 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertPromise0007', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertPromise0007 start *************"); + await rdbStore.executeSql(CREATE_TABLE_NAME + "7" + CREATE_TABLE) + try{ + await rdbStore.batchInsert("test7").catch((err) =>{ + expect(true).assertTrue(); + }) + }catch(err){ + console.info(TAG + "Batch insert data error: " + err) + expect(true).assertTrue(); + } + done() + console.info(TAG + "************* testRdbStorebatchInsertPromise0007 end *************"); + }) + + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0100 + * @tc.desc rdb insert test + * zheg s + */ + it('testRdbStorebatchInsertCallback0001', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0001 start *************"); + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.executeSql(CREATE_TABLE_NAME + "callback1" + CREATE_TABLE).then(() => { + console.info(TAG + "Batch insert data start") + rdbStore.batchInsert("testcallback1", valueBuckets, async (err, data) => { + if(err != null){ + expect(false).assertTrue(); + }else{ + console.info(TAG + "Batch insert data end") + expect(3).assertEqual(data) + let predicates = new dataRdb.RdbPredicates("testcallback1"); + predicates.equalTo("name", "zhangsan") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(1).assertEqual(id); + expect("zhangsan").assertEqual(name) + expect(18).assertEqual(age) + expect(100.5).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + expect(false).assertEqual(resultSet.goToNextRow()) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + predicates = new dataRdb.RdbPredicates("testcallback1"); + predicates.equalTo("name", "lisi") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(2).assertEqual(id); + expect("lisi").assertEqual(name) + expect(23).assertEqual(age) + expect(200).assertEqual(salary) + expect(false).assertEqual(resultSet.goToNextRow()) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + } + }) + resultSet = null + }) + + done() + console.info(TAG + "************* testRdbStorebatchInsertCallback0001 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0200 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertCallback0002', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0002 start *************"); + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.executeSql(CREATE_TABLE_NAME + "Callback2" + CREATE_TABLE).then(async () => { + await rdbStore.batchInsert("testCallback2", valueBuckets, async (err, data) => { + if(err != null){ + expect(false).assertTrue(); + }else{ + expect(3).assertEqual(data) + let predicates = new dataRdb.RdbPredicates("testCallback2"); + predicates.equalTo("name", "lisi") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(2).assertEqual(id); + expect("lisi").assertEqual(name) + expect(23).assertEqual(age) + expect(200).assertEqual(salary) + await rdbstore.delete(predicates).then((number) => { + expect(1).assertEqual(number) + }).then(async () => { + resultSet = await rdbStore.query(predicates).catch((err) =>{ + expect(true).assertTrue(); + }) + }) + } catch (e) { + console.info("BatchInsert2 error " + e); + } + } + }) + }) + + resultSet = null + done() + console.info(TAG + "************* testRdbStorebatchInsertCallback0002 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0300 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertCallback0003', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0003 start *************"); + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.executeSql(CREATE_TABLE_NAME + "Callback3" + CREATE_TABLE).then(async () =>{ + await rdbStore.batchInsert("testCallback3", valueBuckets, async (err, data) => { + if(err != null){ + expect(false).assertTrue() + }else{ + expect(3).assertEqual(data) + let predicates = new dataRdb.RdbPredicates("testCallback3"); + predicates.equalTo("name", "zhangsan") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(1).assertEqual(id); + expect("zhangsan").assertEqual(name) + expect(18).assertEqual(age) + expect(100.5).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + predicates = new dataRdb.RdbPredicates("testCallback3"); + predicates.equalTo("name", "lisi") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(2).assertEqual(id); + expect("lisi").assertEqual(name) + expect(23).assertEqual(age) + expect(200).assertEqual(salary) + const valueBucket4 = { + "name": "zhangmaowen", + "age": 25, + "salary": 500, + "blobType": u8, + } + await rdbstore.insert("testCallback3",valueBucket4) + predicates = new dataRdb.RdbPredicates("testCallback3"); + predicates.equalTo("name", "zhangmaowen") + resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(4).assertEqual(id); + expect("zhangmaowen").assertEqual(name) + expect(25).assertEqual(age) + expect(500).assertEqual(salary) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + } catch (e) { + console.info("BatchInsert1 error " + e); + } + } + }) + }) + resultSet = null + done(); + console.info(TAG + "************* testRdbStorebatchInsertCallback0003 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0400 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertCallback0004', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0004 start *************"); + var u8 = new Uint8Array([1, 2, 3]) + var valueBuckets = new Array(100); + for(var i=0;i { + await rdbStore.batchInsert("testCallbak4", valueBuckets, async (err, data) => { + if(err != null){ + expect(false).assertTrue(); + }else{ + console.info(TAG + "Batch insert data end") + expect(100).assertEqual(data) + let predicates = new dataRdb.RdbPredicates("testCallbak4"); + predicates.equalTo("name", "zhangsan55") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + console.info(TAG + "id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect(56).assertEqual(id); + expect("zhangsan55").assertEqual(name) + expect(55).assertEqual(age) + expect(56).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + } + }) + }) + resultSet = null; + done() + console.info(TAG + "************* testRdbStorebatchInsertCallback0004 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0500 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertCallback0005', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0005 start *************"); + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket3 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.executeSql(CREATE_TABLE_NAME + "Callback5" + CREATE_TABLE).then(async () => { + await rdbStore.batchInsert("testCallback5", valueBuckets, async (err, number) => { + if(err != null){ + expect(false).assertTrue(); + }else{ + expect(3).assertEqual(number) + let predicates = new dataRdb.RdbPredicates("testCallback5"); + predicates.equalTo("name", "zhangsan") + let resultSet = await rdbStore.query(predicates) + try { + console.info(TAG + "resultSet query done"); + expect(true).assertEqual(resultSet.goToFirstRow()) + const name = resultSet.getString(resultSet.getColumnIndex("name")) + const age = resultSet.getLong(resultSet.getColumnIndex("age")) + const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) + const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) + console.info(TAG + " name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType); + expect("zhangsan").assertEqual(name) + expect(18).assertEqual(age) + expect(100.5).assertEqual(salary) + expect(1).assertEqual(blobType[0]) + expect(2).assertEqual(blobType[1]) + expect(3).assertEqual(blobType[2]) + expect(true).assertEqual(resultSet.goToNextRow()) + } catch (e) { + console.info("BatchInsert1 error " + e); + } + } + }) + }) + resultSet = null; + done(); + console.info(TAG + "************* testRdbStorebatchInsertCallback0005 end *************"); + }) + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0600 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertCallback0006', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0006 start *************"); + + var u8 = new Uint8Array([1, 2, 3]) + const valueBucket1 = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + const valueBucket2 = { + "name": "lisi", + "age": 23, + "salary": 200, + "blobType": u8, + } + const valueBucket3 = { + "name": "wangwu", + "age": 20, + "salary": 100.5, + "blobType": u8, + } + const valueBuckets = [valueBucket1, valueBucket2, valueBucket3] + await rdbStore.executeSql(CREATE_TABLE_NAME + "Callback6" + CREATE_TABLE).then(async () => { + try{ + await rdbStore.batchInsert("testCallback6", "valueBuckets", (err, data) => { + if(err != null){ + expect(true).assertTrue() + }else{ + expect(false).assertTrue() + } + }) + }catch(err){ + console.info(TAG + "Batch insert data error: " + err) + expect(true).assertTrue(); + } + }) + + done() + console.info(TAG + "************* testRdbStorebatchInsertCallback0006 end *************"); + }) + + + /** + * @tc.name rdb batchInsert test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_BatchInsert_Callback_0700 + * @tc.desc rdb insert test + */ + it('testRdbStorebatchInsertCallback0007', 0, async function (done) { + console.info(TAG + "************* testRdbStorebatchInsertCallback0007 start *************"); + try{ + await rdbStore.executeSql(CREATE_TABLE_NAME + "Callback7" + CREATE_TABLE).then(async () => { + await rdbstore.batchInsert("testCallback7", (err,data) => { + if(err != null){ + expect(true).assertTrue(); + } + }) + }) + }catch(err){ + console.info(TAG + "Batch insert data error: " + err) + expect(true).assertTrue(); + } + done() + console.info(TAG + "************* testRdbStorebatchInsertCallback0007 end *************"); + }) + console.info(TAG + "*************Unit Test End*************"); -})} +}) +} diff --git a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreQuery.test.js b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreQuery.test.js new file mode 100644 index 0000000000000000000000000000000000000000..d1833a2cf6cb03cf63c872d4044fe42dbf98cdce --- /dev/null +++ b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreQuery.test.js @@ -0,0 +1,349 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' +import dataRdb from '@ohos.data.rdb'; +import factory from '@ohos.data.distributedData' +import abilityFeatureAbility from '@ohos.ability.featureAbility' + +const TAG = "[RDB_JSKITS_TEST]" +const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)" +const DATABASE_DIR = "/data/storage/el2/database/entry/rdb/" +const STORE_CONFIG = { + name: "Query.db", +} +const TEST_BUNDLE_NAME="ohos.acts.relationalStorejstest" + +var kvManager = null; +var kvStore = null; +var localDeviceId = null; +var rdbStore +let context = abilityFeatureAbility.getContext() + +async function CreatRdbStore(context, STORE_CONFIG) { + let RdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1) + await RdbStore.executeSql(CREATE_TABLE_TEST, null) + let u8 = new Uint8Array([1, 2, 3]) + { + const valueBucket = { + "name": "zhangsan", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + await RdbStore.insert("test", valueBucket) + } + { + const valueBucket = { + "name": "lisi", + "age": 28, + "salary": 100.5, + "blobType": u8, + } + await RdbStore.insert("test", valueBucket) + } + { + const valueBucket = { + "name": "wangwu", + "age": 38, + "salary": 90.0, + "blobType": u8, + } + await RdbStore.insert("test", valueBucket) + } + return RdbStore +} + +async function getLocalDeviceId(){ + +} +export default function rdbstoreQueryTest(){ + describe("rdbstoreQueryTest", function() { + beforeAll(async function (done){ + const config = { + bundleName : TEST_BUNDLE_NAME, + userInfo : { + userId : '0', + userType : factory.UserType.SAME_USER_ID, + context:context + } + } + + const options = { + createIfMissing : true, + encrypt : false, + backup : false, + autoSync : true, + kvStoreType : factory.KVStoreType.DEVICE_COLLABORATION, + schema : '', + securityLevel : factory.SecurityLevel.S2, + } + + console.info('getLocalDeviceId config:'+ JSON.stringify(config)); + await factory.createKVManager(config).then((manager) => { + kvManager = manager; + console.info('getLocalDeviceId createKVManager success'); + }).catch((err) => { + console.info('getLocalDeviceId createKVManager err ' + err); + }); + await kvManager.getKVStore(TEST_STORE_ID, options).then((store) => { + kvStore = store; + console.info('getLocalDeviceId getKVStore for getDeviceId success'); + }).catch((err) => { + console.info('getLocalDeviceId getKVStore err ' + err); + }); + var getDeviceId = new Promise((resolve, reject) => { + kvStore.on('dataChange', 0, function (data) { + console.info('getLocalDeviceId on data change: ' + JSON.stringify(data)); + resolve(data.deviceId); + }); + kvStore.put("getDeviceId", "byPut").then((data) => { + console.info('getLocalDeviceId put success'); + expect(data == undefined).assertTrue(); + }); + setTimeout(() => { + reject(new Error('not resolved in 2 second, reject it.')) + }, 2000); + }); + await getDeviceId.then(function(deviceId) { + console.info('getLocalDeviceId getDeviceId ' + JSON.stringify(deviceId)); + localDeviceId = deviceId; + }).catch((error) => { + console.info('getLocalDeviceId can NOT getDeviceId, fail: ' + error); + expect(null).assertFail(); + }); + await kvManager.closeKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID, kvStore); + await kvManager.deleteKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID); + kvStore = null; + console.info('getLocalDeviceId end'); + getLocalDeviceId() + console.info(TAG + 'beforeAll') + done(); + }) + + beforeEach(async function () { + console.info(TAG + 'beforeEach') + + rdbStore = await CreatRdbStore(context, STORE_CONFIG) + }) + + afterEach(async function () { + console.info(TAG + 'afterEach') + await dataRdb.deleteRdbStore(context, STORE_CONFIG.name) + }) + + afterAll(async function () { + console.info(TAG + 'afterAll') + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Promise_0010 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryPromiseTest0010', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryPromiseTest0010 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + await rdbStore.remoteQuery(localDeviceId,"test",predicates,["name","age","salary"]).then( (resultSet) => { + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + }).catch((err) => { + console.info(TAG + "Remote query error: " + err) + expect(true).assertTrue(); + }) + + done(); + console.info(TAG + "RdbRemoteQueryPromiseTest0010 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Promise_0020 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryPromiseTest0020', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryPromiseTest0020 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery([localDeviceId],"test",predicates,["name","age","salary"]).then((resultSet) => { + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + }).catch((err) => { + console.info(TAG + "Remote query error" + err) + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryPromiseTest0020 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryPromiseTest0020 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Promise_0030 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryPromiseTest0030', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryPromiseTest0030 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery(localDeviceId,["test"],predicates,["name","age","salary"]).then((resultSet) => { + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + }).catch((err) => { + console.info(TAG + "Remote query error" + err) + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryPromiseTest0030 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryPromiseTest0030 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Promise_0040 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryPromiseTest0040', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryPromiseTest0040 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery(localDeviceId,"test",predicates,"age").then((resultSet) => { + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + }).catch((err) => { + console.info(TAG + "Remote query error" + err) + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryPromiseTest0040 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryPromiseTest0040 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Callback_0010 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryCallbackTest0010', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryCallbackTest0010 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery(localDeviceId, "test", predicates,["name","age","salary"], (err, data) => { + if(err != null){ + console.info(TAG + "Remote query error" + err) + }else{ + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + } + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryCallbackTest0010 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryCallbackTest0010 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Callback_0020 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryCallbackTest0020', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryCallbackTest0020 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery([localDeviceId], "test", predicates,["name","age","salary"], (err, data) => { + if(err != null){ + console.info(TAG + "Remote query error" + err) + }else{ + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + } + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryCallbackTest0020 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryCallbackTest0020 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Callback_0030 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryCallbackTest0030', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryCallbackTest0020 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery(localDeviceId, ["test"], predicates,["name","age","salary"], (err, data) => { + if(err != null){ + console.info(TAG + "Remote query error" + err) + }else{ + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + } + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryCallbackTest0030 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryCallbackTest0030 end") + }) + + /** + * @tc.name RDB remotequery test + * @tc.number SUB_DDM_RDB_JS_RemoteQueryTest_Callback_0040 + * @tc.desc RDB remotequery function test + */ + it('RdbRemoteQueryCallbackTest0040', 0, async function (done){ + console.info(TAG + "RdbRemoteQueryCallbackTest0040 start") + let predicates = new dataRdb.RdbPredicates("test"); + predicates.equalTo("name", "zhangsan") + try{ + await rdbStore.remoteQuery(localDeviceId, "test", predicates,"name", (err, data) => { + if(err != null){ + console.info(TAG + "Remote query error" + err) + }else{ + console.info(TAG + "Remote query success") + expect(false).assertTrue(); + } + }) + }catch(err){ + console.info(TAG + "RdbRemoteQueryCallbackTest0040 error:" + err) + } + + done(); + console.info(TAG + "RdbRemoteQueryCallbackTest0040 end") + }) + + }) +} \ No newline at end of file diff --git a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreRdbstoreJsunit.test.js b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreRdbstoreJsunit.test.js index 6d7e9c81d3bd0d60857f89d4bd0a6e06d94b4d62..53c88719c4d5be8305618b3e38fe1d8ac39916d0 100644 --- a/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreRdbstoreJsunit.test.js +++ b/distributeddatamgr/relationalStorejstest/hap/src/main/js/test/RdbstoreRdbstoreJsunit.test.js @@ -18,7 +18,7 @@ import dataRdb from '@ohos.data.rdb'; import abilityFeatureAbility from '@ohos.ability.featureAbility'; var context = abilityFeatureAbility.getContext(); - +var contextApplication = context.getApplicationContext() const TAG = "[RDB_JSKITS_TEST]" const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; @@ -183,7 +183,7 @@ describe('rdbStoreTest', function () { console.info(TAG + "************* testRdbStore0006 start *************"); let rdbstore = null; try{ - const STORE_CONFIG = { + const STORE_CONFIG= { name: "rdbstorecontext.db", } console.info(TAG + "first") @@ -210,23 +210,78 @@ describe('rdbStoreTest', function () { * @tc.number SUB_DDM_AppDataFWK_JSRDB_RdbStore_0070 * @tc.desc rdb store getRdbStore test */ - it('testRdbStore0007', 0, async function (done) { - console.info(TAG + "************* testRdbStore0007 start *************"); - let rdbstore = null; - dataRdb.getRdbStore(context,STORE_CONFIG, 1, (data,err) => { - if(err != undefined){ - expect(null).assertFail() - }else{ - rdbstore = data; - console.info(TAG + "getRdbStore success: " + rdbstore) - expect(rdbstore).assertNotNull(); - } - }); - await dataRdb.deleteRdbStore(STORE_CONFIG.name) - rdbstore = null; - done(); - console.info(TAG + "************* testRdbStore0007 end *************"); - }) + it('testRdbStore0007', 0, async function (done) { + console.info(TAG + "************* testRdbStore0007 start *************"); + let rdbstore = null; + try{ + const STORE_CONFIG = { + name: "rdbstorecontext.db", + } + console.info(TAG + "first") + await dataRdb.getRdbStore(contextApplication,STORE_CONFIG, 1).then((data) => { + console.info(TAG + "Get rdbstore success") + rdbstore = data; + expect(rdbstore != null).assertTrue(); + }).catch((errInfo) => { + console.info(TAG + "Get rdbstore fail " + errInfo) + expect(null).assertFail(); + }) + }catch(err){ + console.info(TAG + "Get rdbstore fail catch err: " + err) + expect(null).assertFail() + } + await dataRdb.deleteRdbStore(STORE_CONFIG.name) + rdbstore = null; + done(); + console.info(TAG + "************* testRdbStore0007 end *************"); + }) + + /** + * @tc.name rdb store deleteRdbStore test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_RdbStore_0080 + * @tc.desc rdb store deleteRdbStore test + */ + it('testRdbStore0008', 0, async function (done) { + console.info(TAG + "************* testRdbStore0008 start *************"); + let rdbstore = null; + dataRdb.getRdbStore(context,STORE_CONFIG, 1, (data,err) => { + if(err != undefined){ + expect(null).assertFail() + }else{ + rdbstore = data; + console.info(TAG + "getRdbStore success: " + rdbstore) + expect(rdbstore).assertNotNull(); + } + }); + await dataRdb.deleteRdbStore(context,STORE_CONFIG.name) + rdbstore = null; + done(); + console.info(TAG + "************* testRdbStore0008 end *************"); + }) + + /** + * @tc.name rdb store deleteRdbStore test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_RdbStore_0090 + * @tc.desc rdb store deleteRdbStore test + */ + it('testRdbStore0009', 0, async function(done){ + console.info(TAG + "************* testRdbStore0009 start *************"); + let rdbstore = null; + dataRdb.getRdbStore(contextApplication,STORE_CONFIG, 1, (data,err) => { + if(err != undefined){ + expect(null).assertFail() + }else{ + rdbstore = data; + console.info(TAG + "getRdbStore success: " + rdbstore) + expect(rdbstore).assertNotNull(); + } + }); + await dataRdb.deleteRdbStore(contextApplication,STORE_CONFIG.name) + rdbstore = null; + + done(); + console.info(TAG + "************* testRdbStore0009 end *************"); + }) console.info(TAG + "*************Unit Test End*************"); })}