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