1/* 2 * Copyright (C) 2021 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 */ 15 16import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 17import dataRdb from '@ohos.data.rdb'; 18 19const TAG = "[RDB_JSKITS_TEST]" 20const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY, " + 21 "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; 22 23const STORE_CONFIG = { 24 name: "TransactionInsertTest.db", 25} 26 27var rdbStore = undefined; 28 29describe('rdbStoreTransactionTest', function () { 30 beforeAll(async function () { 31 console.info(TAG + 'beforeAll') 32 rdbStore = await dataRdb.getRdbStore(STORE_CONFIG, 1); 33 await rdbStore.executeSql(CREATE_TABLE_TEST, null); 34 }) 35 36 beforeEach(async function () { 37 console.info(TAG + 'beforeEach') 38 39 }) 40 41 afterEach(async function () { 42 console.info(TAG + 'afterEach') 43 await rdbStore.executeSql("DELETE FROM test"); 44 }) 45 46 afterAll(async function () { 47 console.info(TAG + 'afterAll') 48 rdbStore = null 49 await dataRdb.deleteRdbStore("TransactionInsertTest.db"); 50 }) 51 52 console.log(TAG + "*************Unit Test Begin*************"); 53 54 /** 55 * @tc.name rdb transaction insert test 56 * @tc.number testRdbTransactionInsert0001 57 * @tc.desc rdb transaction insert & commit, the result comes out is 3 items; 58 */ 59 it('testRdbTransactionInsert0001', 0, async function (done) { 60 console.log(TAG + "************* testRdbStoreInsert0001 start *************"); 61 var u8 = new Uint8Array([1, 2, 3]) 62 try { 63 rdbStore.beginTransaction() 64 const valueBucket = { 65 "name": "lisi", 66 "age": 18, 67 "salary": 100.5, 68 "blobType": u8, 69 } 70 await rdbStore.insert("test", valueBucket) 71 72 rdbStore.commit() 73 74 let predicates = new dataRdb.RdbPredicates("test"); 75 let resultSet = await rdbStore.query(predicates) 76 console.log(TAG + "testRdbTransactionInsert0001 result count " + resultSet.rowCount) 77 expect(1).assertEqual(resultSet.rowCount) 78 resultSet.close() 79 } catch (e) { 80 console.log(TAG + e); 81 expect(null).assertFail() 82 console.log(TAG + "testRdbTransactionInsert0001 failed"); 83 } 84 done() 85 console.log(TAG + "************* testRdbTransactionInsert0001 end *************"); 86 }) 87 88 /** 89 * @tc.name rdb transaction insert test 90 * @tc.number testRdbTransactionInsert0001 91 * @tc.desc rdb transaction insert & commit, the result comes out is 3 items; 92 */ 93 it('testRdbTransactionInsert0002', 0, async function (done) { 94 console.log(TAG + "************* testRdbStoreInsert0002 start *************"); 95 var u8 = new Uint8Array([1, 2, 3]) 96 try { 97 rdbStore.beginTransaction() 98 const valueBucket = { 99 "name": "lisi", 100 "age": 18, 101 "salary": 100.5, 102 "blobType": u8, 103 } 104 await rdbStore.insert("test", valueBucket) 105 106 const valueBucket1 = { 107 "name": "zhangsan", 108 "age": 20, 109 "salary": 9.5, 110 "blobType": u8, 111 } 112 await rdbStore.insert("test", valueBucket1) 113 114 115 const valueBucket2 = { 116 "name": "wangwu", 117 "age": 16, 118 "salary": 99, 119 "blobType": u8, 120 } 121 await rdbStore.insert("test", valueBucket2) 122 123 rdbStore.commit() 124 125 let predicates = new dataRdb.RdbPredicates("test"); 126 let resultSet = await rdbStore.query(predicates) 127 expect(3).assertEqual(resultSet.rowCount) 128 resultSet.close() 129 } catch (e) { 130 expect(null).assertFail() 131 console.log(TAG + "testRdbTransactionInsert0002 failed"); 132 } 133 done() 134 console.log(TAG + "************* testRdbTransactionInsert0002 end *************"); 135 }) 136 137 138 /** 139 * @tc.name rdb transaction insert test 140 * @tc.number testRdbTransactionInsert0002 141 * @tc.desc while using transaction to insert values, querying the db, 142 * the result comes out is 0; 143 */ 144 it('testRdbTransactionInsert0003', 0, async function (done) { 145 console.log(TAG + "************* testRdbTransactionInsert0003 start *************"); 146 var u8 = new Uint8Array([1, 2, 3]) 147 try { 148 rdbStore.beginTransaction() 149 const valueBucket = { 150 "name": "lisi", 151 "age": 18, 152 "salary": 100.5, 153 "blobType": u8, 154 } 155 await rdbStore.insert("test", valueBucket) 156 157 const valueBucket1 = { 158 "name": "zhangsan", 159 "age": 20, 160 "salary": 9.5, 161 "blobType": u8, 162 } 163 await rdbStore.insert("test", valueBucket1) 164 165 let predicates = new dataRdb.RdbPredicates("test"); 166 let resultSet = await rdbStore.query(predicates) 167 expect(0).assertEqual(resultSet.rowCount) 168 resultSet.close() 169 const valueBucket2 = { 170 "name": "wangwu", 171 "age": 16, 172 "salary": 99, 173 "blobType": u8, 174 } 175 await rdbStore.insert("test", valueBucket2) 176 177 rdbStore.commit() 178 } catch (e) { 179 expect(null).assertFail() 180 console.log(TAG + "testRdbTransactionInsert0003 failed"); 181 } 182 done() 183 console.log(TAG + "************* testRdbTransactionInsert0003 end *************"); 184 }) 185 186 /** 187 * @tc.name rdb insert test 188 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Insert_0010 189 * @tc.desc the classical transaction scenario, when we insert or commit the value, 190 * db returns an exception, we need to catch exception and rollback. 191 */ 192 it('testRdbTransactionRollBack0001', 0, async function (done) { 193 console.log(TAG + "************* testRdbTransactionRollBack0001 start *************"); 194 var u8 = new Uint8Array([1, 2, 3]) 195 try { 196 rdbStore.beginTransaction() 197 const valueBucket = { 198 "id": 1, 199 "name": "lisi", 200 "age": 18, 201 "salary": 100.5, 202 "blobType": u8, 203 } 204 await rdbStore.insert("test", valueBucket) 205 await rdbStore.insert("test", valueBucket) 206 207 rdbStore.commit() 208 } catch (e) { 209 rdbStore.rollBack() 210 let predicates = new dataRdb.RdbPredicates("test"); 211 let resultSet = await rdbStore.query(predicates) 212 console.log(TAG + "testRdbTransactionRollBack0001 result count " + resultSet.rowCount); 213 expect(0).assertEqual(resultSet.rowCount) 214 resultSet.close() 215 } 216 done() 217 console.log(TAG + "************* testRdbTransactionRollBack0001 end *************"); 218 }) 219 220 /** 221 * @tc.name rdb insert test 222 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Insert_0010 223 * @tc.desc the classical transaction scenario, when we insert or commit the value, 224 * db returns an exception, we need to catch exception and rollback. 225 */ 226 it('testRdbTransactionMulti0003', 0, async function (done) { 227 console.log(TAG + "************* testRdbTransactionMulti0003 start *************"); 228 var u8 = new Uint8Array([1, 2, 3]) 229 try { 230 rdbStore.beginTransaction() 231 const valueBucket = { 232 "id": 1, 233 "name": "lisi", 234 "age": 18, 235 "salary": 100.5, 236 "blobType": u8, 237 } 238 await rdbStore.insert("test", valueBucket); 239 240 rdbStore.beginTransaction() 241 const valueBucket1 = { 242 "name": "zhangsan", 243 "age": 20, 244 "salary": 220.5, 245 "blobType": u8, 246 } 247 await rdbStore.insert("test", valueBucket1) 248 249 rdbStore.rollBack() 250 251 await rdbStore.insert("test", valueBucket) 252 rdbStore.commit() 253 254 let predicates = new dataRdb.RdbPredicates("test"); 255 let ret = await rdbStore.query(predicates) 256 expect(1).assertEqual(ret.rowCount) 257 ret.close() 258 } catch (e) { 259 rdbStore.rollBack() 260 console.log(TAG + "testRdbTransactionMulti0003 rollback ***** "); 261 } 262 done() 263 console.log(TAG + "************* testRdbTransactionMulti0003 end *************"); 264 }) 265 266 console.log(TAG + "*************Unit Test End*************"); 267 268})