未验证 提交 7c75f7e1 编写于 作者: O openharmony_ci 提交者: Gitee

!8303 【Distributeddatamgr】【OpenHarmony-3.2-Release】用例优化

Merge pull request !8303 from lina123456/OpenHarmony-3.2-Release
......@@ -23,11 +23,8 @@ 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'
import rdbStoreEncryptionTest from './RdbstoreEncryptionJsunit.test.js'
import rdbStorePredicatesComplexFiledTest from './RdbstorePredicatesComplexFiledJsunit.test.js'
import rdbStoreBackupRestoreWithFAContextTest from './RdbstoreBackupRestoreWithFAContextJsunit.test.js'
import rdbStoreBackupRestoreCallbackTest from './RdbstoreBackupRestoreCallbackJsunit.test.js'
export default function testsuite() {
rdbStoreDeleteTest()
rdbStoreDistributedTest()
......@@ -39,9 +36,6 @@ export default function testsuite() {
rdbstoreStoreExcuteSqlTest()
rdbstoreTransactionTest()
rdbStoreUpdateTest()
rdbstoreQueryTest()
rdbStoreEncryptionTest()
rdbStorePredicatesComplexFiledTest()
rdbStoreBackupRestoreWithFAContextTest()
rdbStoreBackupRestoreCallbackTest()
}
/*
* 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')
try {
await dataRdb.deleteRdbStore(context, STORE_CONFIG.name)
await dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME)
await dataRdb.deleteRdbStore(context, "BackupTest003.db")
} catch (err) {
console.info(TAG + "deleteRdbStore err" + JSON.stringify(err))
}
})
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,async (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")
RdbStore.delete(deleteData).then(()=> {
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) {
console.info(TAG + "rdb restore1 done")
}
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()
}else{
try{
console.info(TAG + 'Backup database success')
fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
}catch(err){
expect(false).assertTrue();
}
dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME).then(() => {
try{
fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
}catch(err){
console.info(TAG + 'error2 ' + err)
}
RdbStore.backup(DATABASE_BACKUP_NAME, (err, data) => {
if(err != null){
console.info(`${TAG} Backup database second failed`)
}else{
try{
console.info(TAG + 'Backup database second 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) => {
expect(err != null).assertTrue()
console.info(TAG + "RdbBackupRestoreCallbackTest_0600 backup1 done")
RdbStore.backup(STORE_CONFIG.name, (err, data) => {
expect(err != null).assertTrue()
console.info(TAG + "RdbBackupRestoreCallbackTest_0600 backup2 done")
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 *************")
let DATABASE_BACKUP_TEST_NAME = "BackupTest.db"
RdbStore.backup(DATABASE_BACKUP_TEST_NAME, async (err, data) => {
expect(err == null).assertTrue()
console.info(TAG + "RdbBackupRestoreCallbackTest_0700 backup done")
dataRdb.deleteRdbStore(context, DATABASE_BACKUP_TEST_NAME, () => {
try {
fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_TEST_NAME)
} catch (err) {
console.info(TAG + "RdbBackupRestoreCallbackTest_0700 deleteRdbStore done")
}
RdbStore.restore(DATABASE_BACKUP_TEST_NAME, (err, data) => {
expect(err != null).assertTrue()
console.info(TAG + "RdbBackupRestoreCallbackTest_0700 restore done")
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
/*
* 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 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/"
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("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 BackupTest(backupName) {
try {
let promiseRestore = RdbStore.backup(backupName)
promiseRestore.then(() => {
expect(false).assertTrue()
}).catch((err) => {
console.info(TAG + "Backup error: " + err)
expect(true).assertTrue()
})
await promiseRestore
} catch (errInfo) {
console.info(TAG + "BackupTest error: " + errInfo)
expect(true).assertTrue()
}
RdbStore = null
}
async function ReStoreTest(restoreName) {
try {
let promiseRestore = RdbStore.restore(restoreName)
promiseRestore.then(() => {
expect(false).assertTrue()
}).catch((err) => {
console.info(TAG + "Restore error: " + err)
expect(true).assertTrue()
})
await promiseRestore
} catch (errInfo) {
console.info(TAG + "ReStoreTest error: " + errInfo)
expect(true).assertTrue()
}
RdbStore = null
}
export default function rdbStoreBackupRestorePromiseTest() {
describe('rdbStoreBackupRestorePromiseTest', 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')
try {
await dataRdb.deleteRdbStore(context, STORE_CONFIG.name)
await dataRdb.deleteRdbStore(context, DATABASE_BACKUP_NAME)
await dataRdb.deleteRdbStore(context, "BackupTest003.db")
} catch (err) {
console.info(TAG + "deleteRdbStore err" + JSON.stringify(err))
}
})
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_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
await RdbStore.backup(DATABASE_BACKUP_NAME)
try {
fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name)
} catch (err) {
console.info("RdbBackupRestoreTest_0010 backup success")
expect(false).assertTrue()
}
// RDB before restored, delete data
let deleteData = new dataRdb.RdbPredicates("test")
deleteData.equalTo("name", "zhangsan")
await RdbStore.delete(deleteData)
// RDB restore function test
await RdbStore.restore(DATABASE_BACKUP_NAME)
try {
fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
expect(false).assertTrue()
} catch (err) {
console.info("RdbBackupRestoreTest_0010 restore success")
}
try {
fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name)
} catch (err) {
console.info("RdbBackupRestoreTest_0010 restore success2")
expect(false).assertTrue()
}
// 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")
resultSet.goToFirstRow();
expect(true).assertEqual(resultSet.goToFirstRow())
console.info("RdbBackupRestoreTest_0010 goto first row success")
const id = resultSet.getLong(resultSet.getColumnIndex("id"))
console.info("RdbBackupRestoreTest_0010 get dolumnindex id success")
const name = resultSet.getString(resultSet.getColumnIndex("name"))
console.info("RdbBackupRestoreTest_0010 get dolumnindex name success")
const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType"))
console.info(`RdbBackupRestoreTest_0010 id is ${id},name id ${name},blobType is ${blobType[0]}`)
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.close()
RdbStore = null
done()
console.info(TAG + "************* RdbBackupRestoreTest_0010 end *************")
})
/**
* @tc.name RDB Backup test
* @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0020
* @tc.desc RDB backup function test
*/
it('RdbBackupRestoreTest_0020', 0, async function (done) {
console.info(TAG + "************* RdbBackupRestoreTest_0020 start *************")
// RDB backup function test, backup file name empty
BackupTest("")
// RDB backup function test, backup file name already exists
BackupTest(STORE_CONFIG.name)
done()
console.info(TAG + "************* RdbBackupRestoreTest_0020 end *************")
})
/**
* @tc.name RDB BackupRestore test
* @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0030
* @tc.desc RDB restore function test
*/
it('RdbBackupRestoreTest_0030', 0, async function (done) {
console.info(TAG + "************* RdbBackupRestoreTest_0030 start *************")
let backupName = "BackupTest003.db"
await RdbStore.backup(backupName)
// RDB restore function test, backup file name empty
ReStoreTest("")
// RDB restore function test, backup file is specified to database name
ReStoreTest(STORE_CONFIG.name)
done()
console.info(TAG + "************* RdbBackupRestoreTest_0030 end *************")
})
/**
* @tc.name RDB BackupRestore test
* @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0040
* @tc.desc RDB restore function test
*/
it('RdbBackupRestoreTest_0040', 0, async function (done) {
console.info(TAG + "************* RdbBackupRestoreTest_0040 start *************")
let dbName = "notExistName.db"
// RDB restore function test, backup file does not exists
try {
fileio.accessSync(DATABASE_DIR + dbName)
expect(false).assertTrue()
} catch {
ReStoreTest(dbName)
}
done()
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 + 'RdbBackupRestoreTest_0050 deleteRdbStore done ')
}
})
}).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
RdbStore.backup(STORE_CONFIG.name).then(() => {
console.info(TAG + 'RdbBackupRestoreTest_0060 Backup database err');
expect(false).assertTrue();
}).catch((err) => {
console.info(TAG + "RdbBackupRestoreTest_0060 Backup database done");
expect(err != null).assertTrue();
done();
})
})
/**
* @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)
try {
fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
} catch (err) {
console.info(TAG + "RdbBackupRestoreTest_0070 deleteRdbStore done");
}
await RdbStore.restore(DATABASE_BACKUP_NAME).catch((err) => {
console.info(TAG + 'Restore ')
expect(err != null).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)
await 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)
await 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)
await 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)
await ReStoreTest(DATABASE_BACKUP_NAME)
done();
console.info(TAG + "************* RdbBackupRestoreTest_0130 end *************")
})
console.info(TAG + "*************Unit Test End*************")
})
}
/*
* Copyright (C) 2021-2023 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 STORE_CONFIG = {
name: "Query.db",
}
const TEST_BUNDLE_NAME="ohos.acts.rdbjstest"
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 errInfo = undefined
let predicates = new dataRdb.RdbPredicates("test");
predicates.equalTo("name", "zhangsan")
try{
rdbStore.remoteQuery(localDeviceId,"test",predicates,["name","age","salary"])
}catch(err){
errInfo = err
}
expect(errInfo.code).assertEqual("401")
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册