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_Distributed]" 20const STORE_NAME = "distributed_rdb.db" 21var rdbStore = undefined; 22 23describe('rdbStoreDistributedTest', function () { 24 beforeAll(async function (done) { 25 console.info(TAG + 'beforeAll') 26 const config = { 27 "name": STORE_NAME, 28 } 29 try { 30 rdbStore = await dataRdb.getRdbStore(config, 1); 31 console.log(TAG + "create rdb store success") 32 expect(rdbStore).assertEqual(rdbStore) 33 let sqlStatement = "CREATE TABLE IF NOT EXISTS employee (" + 34 "id INTEGER PRIMARY KEY AUTOINCREMENT," + 35 "name TEXT NOT NULL," + 36 "age INTEGER)" 37 try { 38 await rdbStore.executeSql(sqlStatement, null) 39 console.log(TAG + "create table employee success") 40 } catch (err) { 41 console.log(TAG + "create table employee failed") 42 expect(null).assertFail() 43 } 44 45 sqlStatement = "CREATE TABLE IF NOT EXISTS product (" + 46 "id INTEGER PRIMARY KEY AUTOINCREMENT," + 47 "name TEXT NOT NULL," + 48 "price REAL," + 49 "vendor INTEGER," + 50 "describe TEXT)" 51 try { 52 await rdbStore.executeSql(sqlStatement, null) 53 console.log(TAG + "create table product success") 54 done() 55 } catch (err) { 56 console.log(TAG + "create table product failed") 57 expect(null).assertFail() 58 } 59 } catch (err) { 60 console.log(TAG + "create rdb store failed") 61 expect(null).assertFail() 62 } 63 done() 64 }) 65 66 beforeEach(async function () { 67 console.info(TAG + 'beforeEach') 68 }) 69 70 afterEach(async function () { 71 console.info(TAG + 'afterEach') 72 }) 73 74 afterAll(async function () { 75 console.info(TAG + 'afterAll') 76 rdbStore = null 77 await dataRdb.deleteRdbStore(STORE_NAME); 78 }) 79 80 console.log(TAG + "*************Unit Test Begin*************"); 81 82 /** 83 * @tc.name set_distributed_table_none_table 84 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_002 85 * @tc.desc rdb set distributed table using none table as argment 86 */ 87 it('testRdbStoreDistributed0002', 0, async function (done) { 88 console.log(TAG + "************* testRdbStoreDistributed002 start *************"); 89 try { 90 await rdbStore.setDistributedTables([]) 91 console.log(TAG + "set none to be distributed table success"); 92 expect(rdbStore).assertEqual(rdbStore) 93 } catch (err) { 94 console.log(TAG + "set none to be distributed table failed"); 95 expect(null).assertFail(); 96 } 97 done() 98 console.log(TAG + "************* testRdbStoreDistributed002 end *************"); 99 }) 100 101 /** 102 * @tc.name set distributed table using one table name 103 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_003 104 * @tc.desc set distributed table using one table name 105 */ 106 it('testRdbStoreDistributed0003', 0, async function (done) { 107 console.log(TAG + "************* testRdbStoreDistributed003 start *************"); 108 try { 109 await rdbStore.setDistributedTables(['employee']) 110 console.log(TAG + "set employee to be distributed table success"); 111 expect(rdbStore).assertEqual(rdbStore) 112 } catch (err) { 113 console.log(TAG + "set employee to be distributed table failed"); 114 expect(null).assertFail(); 115 } 116 done() 117 console.log(TAG + "************* testRdbStoreDistributed003 end *************"); 118 }) 119 120 /** 121 * @tc.name set distributed table using two table name 122 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_004 123 * @tc.desc set distributed table using two table name 124 */ 125 it('testRdbStoreDistributed0004', 0, async function (done) { 126 console.log(TAG + "************* testRdbStoreDistributed004 start *************"); 127 try { 128 await rdbStore.setDistributedTables(['employee', 'product']) 129 console.log(TAG + "set employee and product to be distributed table success"); 130 expect(rdbStore).assertEqual(rdbStore) 131 } catch (err) { 132 console.log(TAG + "set employee and product to be distributed table failed"); 133 expect(null).assertFail(); 134 } 135 done() 136 console.log(TAG + "************* testRdbStoreDistributed004 end *************"); 137 }) 138 139 /** 140 * @tc.name insert record after setting distributed table 141 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_005 142 * @tc.desc insert record after setting distributed table 143 */ 144 it('testRdbStoreDistributed0005', 0, async function (done) { 145 console.log(TAG + "************* testRdbStoreDistributed005 start *************"); 146 const record = { 147 "name": "Jim", 148 "age": 20, 149 } 150 try { 151 let rowId = await rdbStore.insert("employee", record) 152 console.log(TAG + "insert one record success " + rowId) 153 expect(1).assertEqual(rowId) 154 } catch (err) { 155 console.log(TAG + "insert one record failed"); 156 expect(null).assertFail(); 157 } 158 done() 159 console.log(TAG + "************* testRdbStoreDistributed005 end *************"); 160 }) 161 162 /** 163 * @tc.name update record after setting distributed table 164 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_006 165 * @tc.desc update record after setting distributed table 166 */ 167 it('testRdbStoreDistributed0006', 0, async function (done) { 168 console.log(TAG + "************* testRdbStoreDistributed006 start *************"); 169 const record = { 170 "name": "Jim", 171 "age": 30, 172 } 173 try { 174 let predicate = new dataRdb.RdbPredicates("employee"); 175 predicate.equalTo("id", 1); 176 try { 177 let rowId = await rdbStore.update(record, predicate); 178 console.log(TAG + "update one record success " + rowId) 179 expect(1).assertEqual(rowId) 180 } catch (err) { 181 console.log(TAG + "update one record failed"); 182 expect(null).assertFail(); 183 } 184 } catch (err) { 185 console.log(TAG + "construct predicate failed"); 186 expect(null).assertFail(); 187 } 188 done() 189 console.log(TAG + "************* testRdbStoreDistributed006 end *************"); 190 }) 191 192 /** 193 * @tc.name query record after setting distributed table 194 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_007 195 * @tc.desc query record after setting distributed table 196 */ 197 it('testRdbStoreDistributed0007', 0, async function (done) { 198 console.log(TAG + "************* testRdbStoreDistributed0007 start *************"); 199 try { 200 let predicates = new dataRdb.RdbPredicates("employee") 201 let resultSet = await rdbStore.query(predicates) 202 try { 203 console.log(TAG + "product resultSet query done"); 204 expect(true).assertEqual(resultSet.goToFirstRow()) 205 const id = await resultSet.getLong(resultSet.getColumnIndex("id")) 206 const name = await resultSet.getString(resultSet.getColumnIndex("name")) 207 const age = await resultSet.getLong(resultSet.getColumnIndex("age")) 208 209 await expect(1).assertEqual(id); 210 await expect("Jim").assertEqual(name); 211 await expect(30).assertEqual(age); 212 resultSet.close(); 213 expect(true).assertEqual(resultSet.isClosed) 214 } catch (e) { 215 console.log(TAG + "result get value failed") 216 expect(null).assertFail(); 217 } 218 } catch (err) { 219 console.log("query failed"); 220 expect(null).assertFail(); 221 } 222 done(); 223 console.log(TAG + "************* testRdbStoreDistributed0007 end *************"); 224 }) 225 226 /** 227 * @tc.name delete record after setting distributed table 228 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_008 229 * @tc.desc delete record after setting distributed table 230 */ 231 it('testRdbStoreDistributed0008', 0, async function (done) { 232 console.log(TAG + "************* testRdbStoreDistributed0008 start *************"); 233 let predicates = new dataRdb.RdbPredicates("employee") 234 try { 235 let number = await rdbStore.delete(predicates) 236 console.log(TAG + "employee Delete done: " + number) 237 expect(1).assertEqual(number) 238 } catch (err) { 239 console.log(TAG + "delete record failed"); 240 expect(null).assertFail() 241 } 242 done(); 243 console.log(TAG + "************* testRdbStoreDistributed0008 end *************"); 244 }) 245 246 /** 247 * @tc.name predicates inDevice 248 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_009 249 * @tc.desc predicates inDevice 250 */ 251 it('testRdbStoreDistributed0009', 0, async function (done) { 252 console.log(TAG + "************* testRdbStoreDistributed0009 start *************"); 253 let predicates = new dataRdb.RdbPredicates("employee") 254 try { 255 predicates = predicates.inDevices(["1234567890"]); 256 console.log(TAG + "inDevices success"); 257 } catch (err) { 258 console.log(TAG + "inDevices failed"); 259 expect(null).assertFail(); 260 } 261 done(); 262 console.log(TAG + "************* testRdbStoreDistributed0009 end *************"); 263 }) 264 265 /** 266 * @tc.name predicates inAllDevices 267 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_010 268 * @tc.desc predicates inAllDevices 269 */ 270 it('testRdbStoreDistributed0010', 0, async function (done) { 271 console.log(TAG + "************* testRdbStoreDistributed0010 start *************"); 272 let predicates = new dataRdb.RdbPredicates("employee") 273 try { 274 predicates = predicates.inAllDevices(); 275 console.log(TAG + "inAllDevices success"); 276 expect(predicates).assertEqual(predicates); 277 } catch (err) { 278 console.log(TAG + "inAllDevices failed"); 279 expect(null).assertFail(); 280 } 281 done(); 282 console.log(TAG + "************* testRdbStoreDistributed0010 end *************"); 283 }) 284 285 /** 286 * @tc.name sync test 287 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_011 288 * @tc.desc sync test 289 */ 290 it('testRdbStoreDistributed0011', 0, async function (done) { 291 console.log(TAG + "************* testRdbStoreDistributed0011 start *************"); 292 let predicates = new dataRdb.RdbPredicates("employee") 293 predicates = predicates.inDevices(["12345678abcd"]); 294 rdbStore.sync(dataRdb.SyncMode.SYNC_MODE_PUSH, predicates); 295 console.log(TAG + "sync push success"); 296 expect(rdbStore).assertEqual(rdbStore); 297 rdbStore.sync(dataRdb.SyncMode.SYNC_MODE_PULL, predicates); 298 console.log(TAG + "sync pull success"); 299 expect(rdbStore).assertEqual(rdbStore); 300 done(); 301 console.log(TAG + "************* testRdbStoreDistributed0011 end *************"); 302 }) 303 304 /** 305 * @tc.name subscribe test 306 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_012 307 * @tc.desc subscribe test 308 */ 309 it('testRdbStoreDistributed0012', 0, async function (done) { 310 console.log(TAG + "************* testRdbStoreDistributed0012 start *************"); 311 rdbStore.on("dataChange", dataRdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, (device) => { 312 console.log(TAG + device + " dataChange"); 313 }); 314 console.log(TAG + "on dataChange success"); 315 expect(rdbStore).assertEqual(rdbStore); 316 done() 317 console.log(TAG + "************* testRdbStoreDistributed0012 end *************"); 318 }) 319 320 /** 321 * @tc.name subscribe test 322 * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_013 323 * @tc.desc subscribe test 324 */ 325 it('testRdbStoreDistributed0013', 0, async function (done) { 326 console.log(TAG + "************* testRdbStoreDistributed0013 start *************"); 327 rdbStore.off("dataChange", dataRdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, (device) => { 328 console.log(TAG + device + " dataChange"); 329 }); 330 console.log(TAG + "off dataChange success"); 331 expect(rdbStore).assertEqual(rdbStore); 332 done() 333 console.log(TAG + "************* testRdbStoreDistributed0013 end *************"); 334 }) 335 console.log(TAG + "*************Unit Test End*************"); 336}) 337