1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 16import data_relationalStore from '@ohos.data.relationalStore' 17import ability_featureAbility from '@ohos.ability.featureAbility' 18import fileio from '@ohos.file.fs' 19 20const TAG = "[RELATIONAL_STORE_JSKITS_TEST]" 21const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " 22 + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)" 23const DATABASE_DIR = "/data/storage/el2/database/entry/rdb/" 24var rdbStore 25var context = ability_featureAbility.getContext() 26const STORE_CONFIG = { 27 name: "BackupResotreTest.db", 28 encrypt: true, 29 securityLevel: data_relationalStore.SecurityLevel.S1, 30} 31const DATABASE_BACKUP_NAME = "Backup.db" 32 33async function CreatRdbStore(STORE_CONFIG) { 34 let rdbStore = await data_relationalStore.getRdbStore(context, STORE_CONFIG) 35 await rdbStore.executeSql(CREATE_TABLE_TEST, null) 36 let u8 = new Uint8Array([1, 2, 3]) 37 { 38 const valueBucket = { 39 "name": "zhangsan", 40 "age": 18, 41 "salary": 100.5, 42 "blobType": u8, 43 } 44 await rdbStore.insert("test", valueBucket) 45 } 46 { 47 const valueBucket = { 48 "name": "lisi", 49 "age": 28, 50 "salary": 100.5, 51 "blobType": u8, 52 } 53 await rdbStore.insert("test", valueBucket) 54 } 55 { 56 const valueBucket = { 57 "name": "wangwu", 58 "age": 38, 59 "salary": 90.0, 60 "blobType": u8, 61 } 62 await rdbStore.insert("test", valueBucket) 63 } 64 return rdbStore 65} 66 67async function BackupTest(backupName) { 68 try { 69 let promiseRestore = rdbStore.backup(backupName) 70 promiseRestore.then(() => { 71 expect(false).assertTrue() 72 }).catch((err) => { 73 expect(true).assertTrue() 74 }) 75 await promiseRestore 76 } catch { 77 expect(true).assertTrue() 78 } 79 80 rdbStore = null 81} 82 83async function RestoreTest(restoreName) { 84 try { 85 let promiseRestore = rdbStore.restore(restoreName) 86 promiseRestore.then(() => { 87 expect(false).assertTrue() 88 }).catch((err) => { 89 expect(true).assertTrue() 90 }) 91 await promiseRestore 92 } catch { 93 expect(true).assertTrue() 94 } 95 96 rdbStore = null 97} 98 99describe('rdbStoreBackupRestoreWithFAContextTest', function () { 100 beforeAll(async function () { 101 console.info(TAG + 'beforeAll') 102 }) 103 104 beforeEach(async function () { 105 console.info(TAG + 'beforeEach') 106 rdbStore = await CreatRdbStore(STORE_CONFIG) 107 }) 108 109 afterEach(async function () { 110 console.info(TAG + 'afterEach') 111 rdbStore = null 112 await data_relationalStore.deleteRdbStore(context, STORE_CONFIG.name) 113 await data_relationalStore.deleteRdbStore(context, DATABASE_BACKUP_NAME) 114 }) 115 116 afterAll(async function () { 117 console.info(TAG + 'afterAll') 118 }) 119 120 console.log(TAG + "*************Unit Test Begin*************") 121 122 /** 123 * @tc.name RDB Backup Restore test 124 * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0010 125 * @tc.desc RDB backup and restore function test 126 */ 127 it('RdbBackupRestoreTest_0010', 0, async function (done) { 128 await console.log(TAG + "************* RdbBackupRestoreTest_0010 start *************") 129 130 try { 131 await rdbStore.backup(DATABASE_BACKUP_NAME) 132 // expect(true).assertEqual(fileio.accessSync(DATABASE_DIR + DATABASE_BACKUP_NAME)) 133 // expect(true).assertEqual(fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name)) 134 135 await rdbStore.restore(DATABASE_BACKUP_NAME) 136 // expect(true).assertEqual(fileio.accessSync(DATABASE_DIR + STORE_CONFIG.name)) 137 } catch (err) { 138 expect().assertFail() 139 } 140 141 // RDB after restored, data query test 142 let predicates = new data_relationalStore.RdbPredicates("test") 143 predicates.equalTo("name", "zhangsan") 144 let resultSet = await rdbStore.query(predicates) 145 try { 146 console.log(TAG + "After restore resultSet query done") 147 expect(true).assertEqual(resultSet.goToFirstRow()) 148 const id = resultSet.getLong(resultSet.getColumnIndex("id")) 149 const name = resultSet.getString(resultSet.getColumnIndex("name")) 150 const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) 151 expect(1).assertEqual(id) 152 expect("zhangsan").assertEqual(name) 153 expect(1).assertEqual(blobType[0]) 154 } catch (err) { 155 expect(false).assertTrue() 156 } 157 resultSet.close() 158 resultSet = null 159 done() 160 await console.log(TAG + "************* RdbBackupRestoreTest_0010 end *************") 161 }) 162 163 /** 164 * @tc.name RDB Backup test 165 * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0020 166 * @tc.desc RDB backup function test 167 */ 168 it('RdbBackupRestoreTest_0020', 0, async function (done) { 169 await console.log(TAG + "************* RdbBackupRestoreTest_0020 start *************") 170 // RDB backup function test, backup file name empty 171 BackupTest("") 172 173 // RDB backup function test, backup file name already exists 174 BackupTest(STORE_CONFIG.name) 175 176 done() 177 await console.log(TAG + "************* RdbBackupRestoreTest_0020 end *************") 178 }) 179 180 /** 181 * @tc.name RDB BackupRestore test 182 * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0030 183 * @tc.desc RDB restore function test 184 */ 185 it('RdbBackupRestoreTest_0030', 0, async function (done) { 186 await console.log(TAG + "************* RdbBackupRestoreTest_0030 start *************") 187 await rdbStore.backup(DATABASE_BACKUP_NAME) 188 189 // RDB restore function test, backup file name empty 190 RestoreTest("") 191 192 // RDB restore function test, backup file is specified to database name 193 RestoreTest(STORE_CONFIG.name) 194 195 done() 196 await console.log(TAG + "************* RdbBackupRestoreTest_0030 end *************") 197 }) 198 199 /** 200 * @tc.name RDB BackupRestore test 201 * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0040 202 * @tc.desc RDB restore function test 203 */ 204 it('RdbBackupRestoreTest_0040', 0, async function (done) { 205 await console.log(TAG + "************* RdbBackupRestoreTest_0040 start *************") 206 let dbName = "notExistName.db" 207 208 // RDB restore function test, backup file does not exists 209 try { 210 expect(false).assertEqual(fileio.accessSync(DATABASE_DIR + dbName)) 211 } catch (errCode) { 212 expect(13900002).assertEqual(errCode.code) 213 } 214 RestoreTest(dbName) 215 done() 216 await console.log(TAG + "************* RdbBackupRestoreTest_0040 end *************") 217 }) 218 219 /** 220 * @tc.name RDB BackupRestore test 221 * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0050 222 * @tc.desc RDB backup function test 223 */ 224 it('RdbBackupRestoreBackupTest_0050', 0, async function (done) { 225 console.log(TAG + "************* RdbBackupRestoreBackupTest_0050 start *************") 226 227 const STORE_NAME = "AfterCloseTest.db"; 228 const rdbStore = await data_relationalStore.getRdbStore( 229 context, 230 { 231 name: STORE_NAME, 232 securityLevel: data_relationalStore.SecurityLevel.S1 233 } 234 ) 235 try { 236 await rdbStore.close(); 237 console.info(`${TAG} close succeeded`); 238 } catch (err) { 239 expect(null).assertFail(); 240 console.error(`${TAG} close failed, code is ${err.code},message is ${err.message}`); 241 } 242 243 try { 244 let dbName = "QueryTest_bak.db" 245 await rdbStore.backup(dbName) 246 } catch (err) { 247 console.log("catch err: failed, err: code=" + err.code + " message=" + err.message) 248 expect("14800014").assertEqual(err.code) 249 } 250 251 await data_relationalStore.deleteRdbStore(context, STORE_NAME); 252 done(); 253 console.log(TAG + "************* RdbBackupRestoreBackupTest_0050 end *************") 254 }) 255 /** 256 * @tc.name RDB BackupRestore test 257 * @tc.number SUB_DDM_RDB_JS_RdbBackupRestoreTest_0060 258 * @tc.desc RDB restore function test 259 */ 260 it('RdbBackupRestoreBackupTest_0060', 0, async function (done) { 261 console.log(TAG + "************* RdbBackupRestoreBackupTest_0060 start *************") 262 263 const STORE_NAME = "AfterCloseTest.db"; 264 const rdbStore = await data_relationalStore.getRdbStore( 265 context, 266 { 267 name: STORE_NAME, 268 securityLevel: data_relationalStore.SecurityLevel.S1 269 } 270 ) 271 272 let dbName = "notExistName.db" 273 BackupTest(dbName); 274 275 try { 276 await rdbStore.close(); 277 console.info(`${TAG} close succeeded`); 278 } catch (err) { 279 expect(null).assertFail(); 280 console.error(`${TAG} close failed, code is ${err.code},message is ${err.message}`); 281 } 282 283 try { 284 await rdbStore.restore(dbName) 285 } catch (err) { 286 console.log("catch err: failed, err: code=" + err.code + " message=" + err.message) 287 expect("14800014").assertEqual(err.code) 288 } 289 290 await data_relationalStore.deleteRdbStore(context, STORE_NAME); 291 done(); 292 console.log(TAG + "************* RdbBackupRestoreBackupTest_0060 end *************") 293 }) 294 295 console.log(TAG + "*************Unit Test End*************") 296 } 297) 298