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