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