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 */ 15import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 16import dataRdb from '@ohos.data.rdb'; 17import featureAbility from '@ohos.ability.featureAbility'; 18 19const TAG = "[RDB_JSKITS_TEST]" 20const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 21 "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; 22 23const STORE_CONFIG = { 24 name: "CreateDeleteWithFAContextTest.db", 25} 26 27describe('rdbStoreCreateDeleteWithFAContextTest', function () { 28 beforeAll(function () { 29 console.info(TAG + 'beforeAll') 30 }) 31 32 beforeEach(async function () { 33 console.info(TAG + 'beforeEach') 34 }) 35 36 afterEach(async function () { 37 console.info(TAG + 'afterEach') 38 await dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db"); 39 }) 40 41 afterAll(async function () { 42 console.info(TAG + 'afterAll') 43 }) 44 45 console.log(TAG + "*************Unit Test Begin*************"); 46 /** 47 * @tc.name rdb delete test 48 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0010 49 * @tc.desc rdb delete test 50 */ 51 it('testRdbStoreCreateDeleteWithFAContextTest0001', 0, async function (done) { 52 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0001 start *************"); 53 let context = featureAbility.getContext() 54 var rdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1); 55 rdbStore = null 56 await dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db"); 57 done() 58 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0001 end *************"); 59 }) 60 61 /** 62 * @tc.name rdb delete test 63 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0020 64 * @tc.desc rdb delete test 65 */ 66 it('testRdbStoreCreateDeleteWithFAContextTest0002', 0, async function (done) { 67 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0002 start *************"); 68 let context = featureAbility.getContext() 69 var rdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1); 70 await rdbStore.executeSql(CREATE_TABLE_TEST, null); 71 await rdbStore.executeSql("DELETE FROM test"); 72 var u8 = new Uint8Array([1, 2, 3]) 73 { 74 const valueBucket = { 75 "name": "zhangsan", 76 "age": 18, 77 "salary": 100.5, 78 "blobType": u8, 79 } 80 await rdbStore.insert("test", valueBucket) 81 } 82 { 83 const valueBucket = { 84 "name": "lisi", 85 "age": 28, 86 "salary": 100.5, 87 "blobType": u8, 88 } 89 await rdbStore.insert("test", valueBucket) 90 } 91 { 92 const valueBucket = { 93 "name": "lisi", 94 "age": 38, 95 "salary": 100.5, 96 "blobType": u8, 97 } 98 await rdbStore.insert("test", valueBucket) 99 } 100 { 101 let predicates = await new dataRdb.RdbPredicates("test") 102 predicates.equalTo("name", "zhangsan") 103 let deletePromise = rdbStore.delete(predicates) 104 deletePromise.then(async (ret) => { 105 await expect(1).assertEqual(ret) 106 await console.log(TAG + "Delete done: " + ret) 107 }).catch((err) => { 108 expect(null).assertFail() 109 }) 110 await deletePromise 111 } 112 await rdbStore.executeSql("DELETE FROM test"); 113 rdbStore = null 114 await dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db"); 115 done() 116 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0002 end *************"); 117 }) 118 119 /** 120 * @tc.name rdb delete test 121 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0030 122 * @tc.desc rdb delete test 123 */ 124 it('testRdbStoreCreateDeleteWithFAContextTest0003', 0, function (done) { 125 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0003 start *************"); 126 let context = featureAbility.getContext() 127 dataRdb.getRdbStore(context, STORE_CONFIG, 1, (err, rdbStore) => { 128 if (err) { 129 console.info("Get RdbStore failed, err: " + err) 130 return 131 } 132 console.log("Get RdbStore successfully.") 133 rdbStore.executeSql(CREATE_TABLE_TEST, null, (err) => { 134 if (err) { 135 console.info("executeSql CREATE_TABLE_TEST failed, err: " + err) 136 return 137 } 138 console.log("executeSql CREATE_TABLE_TEST successfully.") 139 const valueBucket = { 140 "name": "zhangsan", 141 "age": 18, 142 "salary": 100.5, 143 "blobType": new Uint8Array([1, 2, 3]), 144 } 145 rdbStore.insert("test", valueBucket, (err, rowId) => { 146 if (err) { 147 console.log("Insert is failed"); 148 return; 149 } 150 console.log("Insert is successful, rowId = " + rowId) 151 let predicates = new dataRdb.RdbPredicates("test") 152 predicates.equalTo("name", "zhangsan") 153 rdbStore.delete(predicates, (err, rows) => { 154 if (err) { 155 console.info("Delete failed, err: " + err) 156 expect(null).assertFail() 157 } 158 console.log("Delete rows: " + rows) 159 expect(1).assertEqual(rows) 160 dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db", (err) => { 161 if (err) { 162 console.info("Delete RdbStore failed, err: " + err) 163 return 164 } 165 console.log("Delete RdbStore successfully.") 166 rdbStore = null 167 done() 168 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0003 end *************"); 169 }); 170 }) 171 }) 172 }); 173 }) 174 }) 175 176 /** 177 * @tc.name rdb delete test 178 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0040 179 * @tc.desc rdb delete test 180 */ 181 it('testRdbStoreCreateDeleteWithFAContextTest0004', 0, async function (done) { 182 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0004 start *************"); 183 let context = featureAbility.getContext() 184 dataRdb.getRdbStore(context, STORE_CONFIG, 1).then((rdbStore) => { 185 console.log("Get RdbStore successfully.") 186 rdbStore.executeSql(CREATE_TABLE_TEST, null).then(() => { 187 console.log("executeSql CREATE_TABLE_TEST successfully.") 188 const valueBucket = { 189 "name": "zhangsan", 190 "age": 18, 191 "salary": 100.5, 192 "blobType": new Uint8Array([1, 2, 3]), 193 } 194 rdbStore.insert("test", valueBucket).then((rowId) => { 195 console.log("Insert is successful, rowId = " + rowId) 196 let predicates = new dataRdb.RdbPredicates("test") 197 predicates.equalTo("name", "zhangsan") 198 rdbStore.delete(predicates).then((rows) => { 199 console.log("Delete rows: " + rows) 200 expect(1).assertEqual(rows) 201 rdbStore = null 202 dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db").then(() => { 203 console.log("Delete RdbStore successfully.") 204 done() 205 console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0004 end *************"); 206 }).catch((err) => { 207 console.info("Delete RdbStore failed, err: " + err) 208 }) 209 }).catch((err) => { 210 console.info("Delete failed, err: " + err) 211 expect(null).assertFail() 212 }) 213 }).catch((err) => { 214 console.log("Insert is failed"); 215 }) 216 }).catch((err) => { 217 console.info("executeSql CREATE_TABLE_TEST failed, err: " + err) 218 }) 219 }).catch((err) => { 220 console.info("Get RdbStore failed, err: " + err) 221 }) 222 }) 223 224 console.log(TAG + "*************Unit Test End*************"); 225})