RelationalStoreBackupRestoreWithFAContextJsunit.test.js 17.1 KB
Newer Older
L
liangzhenyu123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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.
 */
J
jiyong_sd 已提交
15
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
Y
yanglifeng1217 已提交
16 17
import data_Rdb from '@ohos.data.relationalStore'
import ability_featureAbility from '@ohos.ability.featureAbility'
L
liangzhenyu123 已提交
18 19
import fileio from '@ohos.fileio'

Y
yanglifeng1217 已提交
20
const TAG = "[RelationalStore_JSKITS_TEST]"
L
liangzhenyu123 已提交
21 22
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)"
L
liangzhenyu123 已提交
23
const DATABASE_DIR = "/data/storage/el2/database/entry/rdb/"
L
liangzhenyu123 已提交
24
var RdbStore
Y
yanglifeng1217 已提交
25
var context = ability_featureAbility.getContext()
L
liangzhenyu123 已提交
26 27
const STORE_CONFIG = {
    name: "BackupResotreTest.db",
Y
yanglifeng1217 已提交
28
    securityLevel: data_Rdb.SecurityLevel.S1
L
liangzhenyu123 已提交
29 30 31 32
}
const DATABASE_BACKUP_NAME = "Backup.db"

async function CreatRdbStore(context, STORE_CONFIG) {
Y
yanglifeng1217 已提交
33
    let RdbStore = await data_Rdb.getRdbStore(context, STORE_CONFIG)
L
liangzhenyu123 已提交
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
    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) => {
L
liangzhenyu123 已提交
72
            console.info(TAG + "Backup error: " + err)
L
liangzhenyu123 已提交
73 74 75
            expect(true).assertTrue()
        })
        await promiseRestore
L
liangzhenyu123 已提交
76 77
    } catch(errInfo){
        console.info(TAG + "BackupTest error: " + errInfo)
L
liangzhenyu123 已提交
78 79 80 81 82 83 84 85 86 87 88 89
        expect(true).assertTrue()
    }

    RdbStore = null
}

async function ReStoreTest(restoreName) {
    try {
        let promiseRestore = RdbStore.restore(restoreName)
        promiseRestore.then(() => {
            expect(false).assertTrue()
        }).catch((err) => {
L
liangzhenyu123 已提交
90
            console.info(TAG + "Restore error: " + err)
L
liangzhenyu123 已提交
91 92 93
            expect(true).assertTrue()
        })
        await promiseRestore
L
liangzhenyu123 已提交
94 95
    } catch(errInfo) {
        console.info(TAG + "ReStoreTest error: " + errInfo)
L
liangzhenyu123 已提交
96 97 98 99 100 101
        expect(true).assertTrue()
    }

    RdbStore = null
}

Y
yanglifeng1217 已提交
102 103
export default function relationalStoreBackupRestorePromiseTest() {
describe('relationalStoreBackupRestorePromiseTest', function () {
L
liangzhenyu123 已提交
104 105 106 107 108 109 110 111 112 113 114
        beforeAll(async function () {
            console.info(TAG + 'beforeAll')
        })

        beforeEach(async function () {
            console.info(TAG + 'beforeEach')
            RdbStore = await CreatRdbStore(context, STORE_CONFIG)
        })

        afterEach(async function () {
            console.info(TAG + 'afterEach')
Y
yanglifeng1217 已提交
115 116 117
            await data_Rdb.deleteRdbStore(context, STORE_CONFIG.name)
            await data_Rdb.deleteRdbStore(context, DATABASE_BACKUP_NAME)
            await data_Rdb.deleteRdbStore(context, "BackupTest003.db")
L
liangzhenyu123 已提交
118 119 120 121 122 123 124 125
        })

        afterAll(async function () {
            console.info(TAG + 'afterAll')
        })

        console.info(TAG + "*************Unit Test Begin*************")

L
liangzhenyu123 已提交
126
        /**
Y
yanglifeng1217 已提交
127 128 129
         * @tc.name RelationalStore Backup Restore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0010
         * @tc.desc RelationalStore backup and restore function test
L
liangzhenyu123 已提交
130 131 132 133
         */
        it('RdbBackupRestoreTest_0010', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0010 start *************")

Y
yanglifeng1217 已提交
134 135
            // RDB backup function test
                await RdbStore.backup(DATABASE_BACKUP_NAME)
L
liangzhenyu123 已提交
136 137 138 139
                try {
                    fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
                    fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name)
                } catch (err) {
L
liangzhenyu123 已提交
140
                    console.info("RdbBackupRestoreTest_0010 backup success")
L
liangzhenyu123 已提交
141 142
                    expect(false).assertTrue()
                }
Y
yanglifeng1217 已提交
143
                // RDB before restored, delete data
Y
yanglifeng1217 已提交
144
                let deleteData = new data_Rdb.RdbPredicates("test")
L
liangzhenyu123 已提交
145 146
                deleteData.equalTo("name", "zhangsan")
                await RdbStore.delete(deleteData)
L
liangzhenyu123 已提交
147

Y
yanglifeng1217 已提交
148 149
                // RDB restore function test
                await RdbStore.restore(DATABASE_BACKUP_NAME)
L
liangzhenyu123 已提交
150 151 152 153 154 155
                    try {
                        fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
                        expect(false).assertTrue()
                    } catch (err) {
                        console.info("RdbBackupRestoreTest_0010 restore success")
                    }
L
liangzhenyu123 已提交
156

L
liangzhenyu123 已提交
157 158 159 160 161
                    try {
                        fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name)
                    } catch (err) {
                        expect(false).assertTrue()
                    }
Y
yanglifeng1217 已提交
162
                    // RDB after restored, data query test
Y
yanglifeng1217 已提交
163
                    let predicates = new data_Rdb.RdbPredicates("test")
L
liangzhenyu123 已提交
164
                    predicates.equalTo("name", "zhangsan")
Y
yanglifeng1217 已提交
165
                  let resultSet =  await RdbStore.query(predicates)
L
liangzhenyu123 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                        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()
                        }
Y
yanglifeng1217 已提交
184
                        resultSet.close()
L
liangzhenyu123 已提交
185
                        RdbStore = null
Y
yanglifeng1217 已提交
186

L
liangzhenyu123 已提交
187 188
                        done()
                        console.info(TAG + "************* RdbBackupRestoreTest_0010 end *************")
L
liangzhenyu123 已提交
189
        })
L
liangzhenyu123 已提交
190 191

        /**
Y
yanglifeng1217 已提交
192 193 194
         * @tc.name RelationalStore Backup test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0020
         * @tc.desc RelationalStore backup function test
L
liangzhenyu123 已提交
195 196
         */
        it('RdbBackupRestoreTest_0020', 0, async function (done) {
L
liangzhenyu123 已提交
197
            console.info(TAG + "************* RdbBackupRestoreTest_0020 start *************")
Y
yanglifeng1217 已提交
198
            // RelationalStore backup function test, backup file name empty
L
liangzhenyu123 已提交
199 200
            BackupTest("")

Y
yanglifeng1217 已提交
201
            // RelationalStore backup function test, backup file name already exists
L
liangzhenyu123 已提交
202 203 204
            BackupTest(STORE_CONFIG.name)

            done()
L
liangzhenyu123 已提交
205
            console.info(TAG + "************* RdbBackupRestoreTest_0020 end *************")
L
liangzhenyu123 已提交
206 207 208
        })

        /**
Y
yanglifeng1217 已提交
209 210 211
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0030
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
212 213
         */
        it('RdbBackupRestoreTest_0030', 0, async function (done) {
L
liangzhenyu123 已提交
214
            console.info(TAG + "************* RdbBackupRestoreTest_0030 start *************")
L
liangzhenyu123 已提交
215 216 217
            let backupName = "BackupTest003.db"
            await RdbStore.backup(backupName)

Y
yanglifeng1217 已提交
218
            // RelationalStore restore function test, backup file name empty
L
liangzhenyu123 已提交
219 220
            ReStoreTest("")

Y
yanglifeng1217 已提交
221
            // RelationalStore restore function test, backup file is specified to database name
L
liangzhenyu123 已提交
222 223 224
            ReStoreTest(STORE_CONFIG.name)

            done()
L
liangzhenyu123 已提交
225
            console.info(TAG + "************* RdbBackupRestoreTest_0030 end *************")
L
liangzhenyu123 已提交
226 227 228
        })

        /**
Y
yanglifeng1217 已提交
229 230 231
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0040
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
232 233
         */
        it('RdbBackupRestoreTest_0040', 0, async function (done) {
L
liangzhenyu123 已提交
234
            console.info(TAG + "************* RdbBackupRestoreTest_0040 start *************")
L
liangzhenyu123 已提交
235 236
            let dbName = "notExistName.db"

Y
yanglifeng1217 已提交
237
            // RelationalStore restore function test, backup file does not exists
L
liangzhenyu123 已提交
238 239 240 241 242 243 244 245
            try {
                fileio.accessSync(DATABASE_DIR + dbName)
                expect(false).assertTrue()
            } catch {
                ReStoreTest(dbName)
            }

            done()
L
liangzhenyu123 已提交
246
            console.info(TAG + "************* RdbBackupRestoreTest_0040 end *************")
L
liangzhenyu123 已提交
247 248
        })

L
liangzhenyu123 已提交
249
        /**
Y
yanglifeng1217 已提交
250 251 252
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0050
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
253 254 255 256
         */
         it('RdbBackupRestoreTest_0050', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0050 start *************")

Y
yanglifeng1217 已提交
257
            // RelationalStore restore function test, backup file
L
liangzhenyu123 已提交
258 259 260 261 262 263 264 265
            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(() => {
Y
yanglifeng1217 已提交
266
                data_Rdb.deleteRdbStore(context, DATABASE_BACKUP_NAME).then(() => {
L
liangzhenyu123 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
                    try{
                        fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
                    }catch(err){
                        console.info(TAG + 'error2  ' + err)
                    }
                })
            }).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()
                    }
Y
yanglifeng1217 已提交
282
                    done();
L
liangzhenyu123 已提交
283 284 285 286 287 288
                })
            })
            console.info(TAG + "************* RdbBackupRestoreTest_0050 end *************")
        })

        /**
Y
yanglifeng1217 已提交
289 290 291
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0060
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
292 293 294 295
         */
         it('RdbBackupRestoreTest_0060', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0060 start *************")
            // Backup file is specified to database name
L
liangzhenyu123 已提交
296 297 298
            RdbStore.backup(STORE_CONFIG.name).then(() => {
                console.info(TAG + 'Backup database finish');
                expect(false).assertTrue() ;
L
liangzhenyu123 已提交
299
            }).catch((err) => {
L
liangzhenyu123 已提交
300
                console.info(TAG + "Backup database error");
Y
yanglifeng1217 已提交
301
                expect(err != null).assertTrue() ;
L
liangzhenyu123 已提交
302
                done();
L
liangzhenyu123 已提交
303 304 305
            })
        })
        /**
Y
yanglifeng1217 已提交
306 307 308
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0070
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
309 310 311 312
         */
         it('RdbBackupRestoreTest_0070', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0070 start *************")
            await RdbStore.backup(DATABASE_BACKUP_NAME)
Y
yanglifeng1217 已提交
313
            await data_Rdb.deleteRdbStore(context, DATABASE_BACKUP_NAME)
L
liangzhenyu123 已提交
314 315 316
            try{
                fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)
            }catch(err){
Y
yanglifeng1217 已提交
317
                console.info(TAG + 'deleteRdbStore done')
L
liangzhenyu123 已提交
318
            }
L
liangzhenyu123 已提交
319
            await RdbStore.restore(DATABASE_BACKUP_NAME).catch((err) => {
Y
yanglifeng1217 已提交
320 321
                console.info(TAG + 'Restore fail: ' + err.code);
                expect(err != null).assertTrue();
L
liangzhenyu123 已提交
322 323 324 325 326 327
            })
            done();
            console.info(TAG + "************* RdbBackupRestoreTest_0070 end *************")
        })

        /**
Y
yanglifeng1217 已提交
328 329 330
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0080
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
331 332 333 334 335 336 337 338 339
         */
        it('RdbBackupRestoreTest_0080', 0, async function (done) {
        console.info(TAG + "************* RdbBackupRestoreTest_0080 start *************")
        BackupTest()
        done();
        console.info(TAG + "************* RdbBackupRestoreTest_0080 end *************")
        })

        /**
Y
yanglifeng1217 已提交
340 341 342
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0090
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
343 344 345 346 347 348 349 350 351
         */
         it('RdbBackupRestoreTest_0090', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0090 start *************")
            BackupTest([DATABASE_BACKUP_NAME])
            done();
            console.info(TAG + "************* RdbBackupRestoreTest_0090 end *************")
        })

        /**
Y
yanglifeng1217 已提交
352 353 354
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0100
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
355 356 357 358
         */
         it('RdbBackupRestoreTest_0100', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0100 start *************")
            await RdbStore.backup(DATABASE_BACKUP_NAME)
Y
yanglifeng1217 已提交
359
            await ReStoreTest([DATABASE_BACKUP_NAME])
L
liangzhenyu123 已提交
360 361 362 363 364
            done();
            console.info(TAG + "************* RdbBackupRestoreTest_0100 end *************")
        })

        /**
Y
yanglifeng1217 已提交
365 366 367
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0110
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
368 369 370 371
         */
         it('RdbBackupRestoreTest_0110', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0110 start *************")
            await RdbStore.backup(DATABASE_BACKUP_NAME)
Y
yanglifeng1217 已提交
372
            await ReStoreTest()
L
liangzhenyu123 已提交
373 374 375 376 377
            done();
            console.info(TAG + "************* RdbBackupRestoreTest_0110 end *************")
        })

        /**
Y
yanglifeng1217 已提交
378 379 380
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0120
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
381 382 383 384
         */
         it('RdbBackupRestoreTest_0120', 0, async function (done) {
            console.info(TAG + "************* RdbBackupRestoreTest_0120 start *************")
            await RdbStore.backup(DATABASE_BACKUP_NAME)
Y
yanglifeng1217 已提交
385
            await BackupTest(DATABASE_BACKUP_NAME)
L
liangzhenyu123 已提交
386 387 388 389 390
            done();
            console.info(TAG + "************* RdbBackupRestoreTest_0120 end *************")
        })

        /**
Y
yanglifeng1217 已提交
391 392 393
         * @tc.name RelationalStore BackupRestore test
         * @tc.number SUB_DDM_RelationalStore_JS_RdbBackupRestoreTest_0130
         * @tc.desc RelationalStore restore function test
L
liangzhenyu123 已提交
394 395 396 397 398
         */
         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)
Y
yanglifeng1217 已提交
399
            await ReStoreTest(DATABASE_BACKUP_NAME)
L
liangzhenyu123 已提交
400 401 402
            done();
            console.info(TAG + "************* RdbBackupRestoreTest_0130 end *************")
        })
L
liangzhenyu123 已提交
403
        console.info(TAG + "*************Unit Test End*************")
L
liangzhenyu123 已提交
404
    })
J
jiyong_sd 已提交
405
}