RdbstoreBackupRestoreCallbackJsunit.test.js 18.8 KB
Newer Older
L
liangzhenyu123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*
 * 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
L
liangzhenyu123 已提交
137
            await RdbStore.backup(DATABASE_BACKUP_NAME,async (err, data) => {
L
liangzhenyu123 已提交
138 139 140 141 142 143 144 145 146 147 148
                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()
                    }
                }
            
L
liangzhenyu123 已提交
149 150 151 152 153 154 155 156 157 158 159 160
                // 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) {
Y
yanglifeng1217 已提交
161
                                console.info(TAG + "rdb restore1 done")
L
liangzhenyu123 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
                            }
            
                            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 *************")
                    })
                })            
L
liangzhenyu123 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
            })
        })
    
        /**
         * @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){
L
liangzhenyu123 已提交
264
                    expect(false).assertTrue()
L
liangzhenyu123 已提交
265 266 267 268 269 270 271
                }else{
                    try{
                        console.info(TAG + 'Backup database success')
                        fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
                    }catch(err){
                        expect(false).assertTrue();
                    }
L
liangzhenyu123 已提交
272 273 274 275 276 277 278 279
                    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){
Y
yanglifeng1217 已提交
280
                                console.info(`${TAG} Backup database second failed`)
L
liangzhenyu123 已提交
281 282
                            }else{
                                try{
L
liangzhenyu123 已提交
283
                                    console.info(TAG + 'Backup database second success')
L
liangzhenyu123 已提交
284 285 286 287 288
                                    fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
                                }catch(err){
                                    expect(false).assertTrue();
                                }
                            }
Y
yanglifeng1217 已提交
289 290
                            done();
                            console.info(TAG + "************* RdbBackupRestoreCallbackTest_0500 end *************")
L
liangzhenyu123 已提交
291 292
                        })
                    })
L
liangzhenyu123 已提交
293 294 295 296 297 298 299 300 301
                }
            })
        })
    
        /**
         * @tc.name RDB BackupRestore test
         * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0600
         * @tc.desc RDB restore function test
         */
Y
yanglifeng1217 已提交
302
         it('RdbBackupRestoreCallbackTest_0600', 0, async function (done) {
L
liangzhenyu123 已提交
303
            console.info(TAG + "************* RdbBackupRestoreCallbackTest_0600 start *************")
Y
yanglifeng1217 已提交
304

L
liangzhenyu123 已提交
305 306
            // Backup file is specified to database name
            RdbStore.backup(STORE_CONFIG.name, (err, data) => {
Y
yanglifeng1217 已提交
307 308
                expect(err != null).assertTrue()
                console.info(TAG + "RdbBackupRestoreCallbackTest_0600 backup1 done")
L
liangzhenyu123 已提交
309

Y
yanglifeng1217 已提交
310 311 312 313 314 315
                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 *************")
                })
L
liangzhenyu123 已提交
316 317 318 319 320 321 322 323
            })
        })
    
        /**
         * @tc.name RDB BackupRestore test
         * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreCallbackTest_0700
         * @tc.desc RDB restore function test
         */
Y
yanglifeng1217 已提交
324
        it('RdbBackupRestoreCallbackTest_0700', 0, async function (done) {
L
liangzhenyu123 已提交
325
            console.info(TAG + "************* RdbBackupRestoreCallbackTest_0700 start *************")
L
liangzhenyu123 已提交
326
            let DATABASE_BACKUP_TEST_NAME = "BackupTest.db"
Y
yanglifeng1217 已提交
327 328 329
            RdbStore.backup(DATABASE_BACKUP_TEST_NAME, async (err, data) => {
                expect(err == null).assertTrue()
                console.info(TAG + "RdbBackupRestoreCallbackTest_0700 backup done")
L
liangzhenyu123 已提交
330

Y
yanglifeng1217 已提交
331 332 333 334 335 336 337 338 339 340 341 342
                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();
                    })
                })
L
liangzhenyu123 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            })
            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()
                }
Y
yanglifeng1217 已提交
382 383
                ReStoreCallbackTest([DATABASE_BACKUP_NAME])
                 done();
L
liangzhenyu123 已提交
384
            })
Y
yanglifeng1217 已提交
385 386
            
           
L
liangzhenyu123 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400
            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()
                }
Y
yanglifeng1217 已提交
401 402
                   ReStoreCallbackTest()
                   done();
L
liangzhenyu123 已提交
403
            })
Y
yanglifeng1217 已提交
404 405
         
            
L
liangzhenyu123 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
            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();
Y
yanglifeng1217 已提交
422 423
            })
    
L
liangzhenyu123 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
            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)
                        }
                    })
                }
Y
yanglifeng1217 已提交
446
                done();
Y
yanglifeng1217 已提交
447
                console.info(TAG + "************* RdbBackupRestoreCallbackTest_1300 end *************")
L
liangzhenyu123 已提交
448
            })
Y
yanglifeng1217 已提交
449
            
L
liangzhenyu123 已提交
450 451 452 453
        })
        console.info(TAG + "*************Unit Test End*************")
    })
}