1/* 2 * Copyright (C) 2021-2022 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, Level, Size, TestType } from "@ohos/hypium"; 16import data_Rdb from '@ohos.data.relationalStore'; 17import factory from '@ohos.data.distributedData'; 18import ability_featureAbility from '@ohos.ability.featureAbility'; 19 20const TAG = "[RelationalStore_JSKITS_TEST]" 21const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " 22+ "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)" 23const STORE_CONFIG = { 24 name: "Query.db", 25 securityLevel: data_Rdb.SecurityLevel.S1 26} 27const TEST_BUNDLE_NAME="ohos.acts.relationalstorejstest" 28const TEST_STORE_ID="storeId"; 29 30var kvManager = null 31var kvStore = null 32var localDeviceId = null 33var rdbStore 34var context = ability_featureAbility.getContext() 35 36async function CreatRdbStore(context, STORE_CONFIG) { 37 let RdbStore = await data_Rdb.getRdbStore(context, STORE_CONFIG) 38 await RdbStore.executeSql(CREATE_TABLE_TEST, null) 39 let u8 = new Uint8Array([1, 2, 3]) 40 { 41 const valueBucket = { 42 "name": "zhangsan", 43 "age": 18, 44 "salary": 100.5, 45 "blobType": u8, 46 } 47 await RdbStore.insert("test", valueBucket) 48 } 49 { 50 const valueBucket = { 51 "name": "lisi", 52 "age": 28, 53 "salary": 100.5, 54 "blobType": u8, 55 } 56 await RdbStore.insert("test", valueBucket) 57 } 58 { 59 const valueBucket = { 60 "name": "wangwu", 61 "age": 38, 62 "salary": 90.0, 63 "blobType": u8, 64 } 65 await RdbStore.insert("test", valueBucket) 66 } 67 68} 69 70async function getLocalDeviceId(){ 71 72} 73export default function relationalStoreQueryTest(){ 74 describe("relationalStoreQueryTest", function() { 75 beforeAll(async function (done){ 76 const config = { 77 bundleName : TEST_BUNDLE_NAME, 78 userInfo : { 79 userId : '0', 80 userType : factory.UserType.SAME_USER_ID, 81 context:context 82 } 83 } 84 85 const options = { 86 createIfMissing : true, 87 encrypt : false, 88 backup : false, 89 autoSync : true, 90 kvStoreType : factory.KVStoreType.DEVICE_COLLABORATION, 91 schema : '', 92 securityLevel : factory.SecurityLevel.S2, 93 } 94 95 console.info('getLocalDeviceId config:'+ JSON.stringify(config)); 96 await factory.createKVManager(config).then((manager) => { 97 kvManager = manager; 98 console.info('getLocalDeviceId createKVManager success'); 99 }).catch((err) => { 100 console.info('getLocalDeviceId createKVManager err ' + err); 101 }); 102 await kvManager.getKVStore(TEST_STORE_ID, options).then((store) => { 103 kvStore = store; 104 console.info('getLocalDeviceId getKVStore for getDeviceId success'); 105 }).catch((err) => { 106 console.info('getLocalDeviceId getKVStore err ' + err); 107 }); 108 var getDeviceId = new Promise((resolve, reject) => { 109 kvStore.on('dataChange', 0, function (data) { 110 console.info('getLocalDeviceId on data change: ' + JSON.stringify(data)); 111 resolve(data.deviceId); 112 }); 113 kvStore.put("getDeviceId", "byPut").then((data) => { 114 console.info('getLocalDeviceId put success'); 115 expect(data == undefined).assertTrue(); 116 }); 117 setTimeout(() => { 118 reject(new Error('not resolved in 2 second, reject it.')) 119 }, 2000); 120 }); 121 await getDeviceId.then(function(deviceId) { 122 console.info('getLocalDeviceId getDeviceId ' + JSON.stringify(deviceId)); 123 localDeviceId = deviceId; 124 }).catch((error) => { 125 console.info('getLocalDeviceId can NOT getDeviceId, fail: ' + error); 126 expect(null).assertFail(); 127 }); 128 await kvManager.closeKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID, kvStore); 129 await kvManager.deleteKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID); 130 kvStore = null; 131 console.info('getLocalDeviceId end'); 132 getLocalDeviceId() 133 console.info(TAG + 'beforeAll') 134 done(); 135 }) 136 137 beforeEach(async function () { 138 console.info(TAG + 'beforeEach') 139 140 rdbStore = await CreatRdbStore(context, STORE_CONFIG) 141 }) 142 143 afterEach(async function () { 144 console.info(TAG + 'afterEach') 145 await data_Rdb.deleteRdbStore(context, STORE_CONFIG.name) 146 }) 147 148 afterAll(async function () { 149 console.info(TAG + 'afterAll') 150 }) 151 152 /** 153 * @tc.name RdbRemoteQueryPromiseTest0010 154 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0100 155 * @tc.desc RelationalStore remotequery function test 156 * @tc.size : MediumTest 157 * @tc.type : Function 158 * @tc.level : Level 2 159 */ 160 161 it('RdbRemoteQueryPromiseTest0010', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 162 console.info(TAG + "RdbRemoteQueryPromiseTest0010 start") 163 let predicates = new data_Rdb.RdbPredicates("test"); 164 predicates.equalTo("name", "zhangsan") 165 try{ 166 await rdbStore.remoteQuery(localDeviceId,"test",predicates,["name","age","salary"]).then((resultSet) => { 167 console.info(TAG + "Remote query success") 168 expect().assertFail(); 169 }).catch((err) => { 170 console.info(TAG + "Remote query error" + err) 171 expect().assertFail(); 172 }) 173 }catch(err){ 174 console.info(TAG + "RdbRemoteQueryPromiseTest0010 error:" + err) 175 expect(err.message!==null).assertTrue(); 176 done(); 177 } 178 179 180 console.info(TAG + "RdbRemoteQueryPromiseTest0010 end") 181 }) 182 /** 183 * @tc.name RdbRemoteQueryPromiseTest0020 184 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0200 185 * @tc.desc RelationalStore remotequery function test 186 * @tc.size : MediumTest 187 * @tc.type : Function 188 * @tc.level : Level 2 189 */ 190 it('RdbRemoteQueryPromiseTest0020', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 191 console.info(TAG + "RdbRemoteQueryPromiseTest0020 start") 192 let predicates = new data_Rdb.RdbPredicates("test"); 193 predicates.equalTo("name", "zhangsan") 194 try{ 195 await rdbStore.remoteQuery([localDeviceId],"test",predicates,["name","age","salary"]).then((resultSet) => { 196 console.info(TAG + "Remote query success") 197 expect(false).assertTrue(); 198 }).catch((err) => { 199 console.info(TAG + "Remote query error" + err) 200 }) 201 }catch(err){ 202 console.info(TAG + "RdbRemoteQueryPromiseTest0020 error:" + err) 203 } 204 205 done(); 206 console.info(TAG + "RdbRemoteQueryPromiseTest0020 end") 207 }) 208 209 /** 210 * @tc.name RdbRemoteQueryPromiseTest0030 211 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0300 212 * @tc.desc RelationalStore remotequery function test 213 * @tc.size : MediumTest 214 * @tc.type : Function 215 * @tc.level : Level 2 216 */ 217 it('RdbRemoteQueryPromiseTest0030', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 218 console.info(TAG + "RdbRemoteQueryPromiseTest0030 start") 219 let predicates = new data_Rdb.RdbPredicates("test"); 220 predicates.equalTo("name", "zhangsan") 221 try{ 222 await rdbStore.remoteQuery(localDeviceId,["test"],predicates,["name","age","salary"]).then((resultSet) => { 223 console.info(TAG + "Remote query success") 224 expect(false).assertTrue(); 225 }).catch((err) => { 226 console.info(TAG + "Remote query error" + err) 227 }) 228 }catch(err){ 229 console.info(TAG + "RdbRemoteQueryPromiseTest0030 error:" + err) 230 } 231 232 done(); 233 console.info(TAG + "RdbRemoteQueryPromiseTest0030 end") 234 }) 235 236 /** 237 * @tc.name RdbRemoteQueryPromiseTest0040 238 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0400 239 * @tc.desc RelationalStore remotequery function test 240 * @tc.size : MediumTest 241 * @tc.type : Function 242 * @tc.level : Level 2 243 */ 244 it('RdbRemoteQueryPromiseTest0040', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 245 console.info(TAG + "RdbRemoteQueryPromiseTest0040 start") 246 let predicates = new data_Rdb.RdbPredicates("test"); 247 predicates.equalTo("name", "zhangsan") 248 try{ 249 await rdbStore.remoteQuery(localDeviceId,"test",predicates,"age").then((resultSet) => { 250 console.info(TAG + "Remote query success") 251 expect(false).assertTrue(); 252 }).catch((err) => { 253 console.info(TAG + "Remote query error" + err) 254 }) 255 }catch(err){ 256 console.info(TAG + "RdbRemoteQueryPromiseTest0040 error:" + err) 257 } 258 259 done(); 260 console.info(TAG + "RdbRemoteQueryPromiseTest0040 end") 261 }) 262 263 /** 264 * @tc.name RdbRemoteQueryCallbackTest0010 265 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0500 266 * @tc.desc RelationalStore remotequery function test 267 * @tc.size : MediumTest 268 * @tc.type : Function 269 * @tc.level : Level 2 270 */ 271 it('RdbRemoteQueryCallbackTest0010', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 272 console.info(TAG + "RdbRemoteQueryCallbackTest0010 start") 273 let predicates = new data_Rdb.RdbPredicates("test"); 274 predicates.equalTo("name", "zhangsan") 275 try{ 276 await rdbStore.remoteQuery(localDeviceId, "test", predicates,["name","age","salary"], (err, data) => { 277 if(err != null){ 278 console.info(TAG + "Remote query error" + err) 279 }else{ 280 console.info(TAG + "Remote query success") 281 expect(false).assertTrue(); 282 } 283 }) 284 }catch(err){ 285 console.info(TAG + "RdbRemoteQueryCallbackTest0010 error:" + err) 286 } 287 288 done(); 289 console.info(TAG + "RdbRemoteQueryCallbackTest0010 end") 290 }) 291 292 /** 293 * @tc.name RdbRemoteQueryCallbackTest0020 294 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0600 295 * @tc.desc RelationalStore remotequery function test 296 * @tc.size : MediumTest 297 * @tc.type : Function 298 * @tc.level : Level 2 299 */ 300 it('RdbRemoteQueryCallbackTest0020', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 301 console.info(TAG + "RdbRemoteQueryCallbackTest0020 start") 302 let predicates = new data_Rdb.RdbPredicates("test"); 303 predicates.equalTo("name", "zhangsan") 304 try{ 305 await rdbStore.remoteQuery([localDeviceId], "test", predicates,["name","age","salary"], (err, data) => { 306 if(err != null){ 307 console.info(TAG + "Remote query error" + err) 308 }else{ 309 console.info(TAG + "Remote query success") 310 expect(false).assertTrue(); 311 } 312 }) 313 }catch(err){ 314 console.info(TAG + "RdbRemoteQueryCallbackTest0020 error:" + err) 315 } 316 317 done(); 318 console.info(TAG + "RdbRemoteQueryCallbackTest0020 end") 319 }) 320 321 /** 322 * @tc.name RdbRemoteQueryCallbackTest0030 323 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0700 324 * @tc.desc RelationalStore remotequery function test 325 * @tc.size : MediumTest 326 * @tc.type : Function 327 * @tc.level : Level 2 328 */ 329 it('RdbRemoteQueryCallbackTest0030', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 330 console.info(TAG + "RdbRemoteQueryCallbackTest0020 start") 331 let predicates = new data_Rdb.RdbPredicates("test"); 332 predicates.equalTo("name", "zhangsan") 333 try{ 334 await rdbStore.remoteQuery(localDeviceId, ["test"], predicates,["name","age","salary"], (err, data) => { 335 if(err != null){ 336 console.info(TAG + "Remote query error" + err) 337 }else{ 338 console.info(TAG + "Remote query success") 339 expect(false).assertTrue(); 340 } 341 }) 342 }catch(err){ 343 console.info(TAG + "RdbRemoteQueryCallbackTest0030 error:" + err) 344 } 345 346 done(); 347 console.info(TAG + "RdbRemoteQueryCallbackTest0030 end") 348 }) 349 350 /** 351 * @tc.name RdbRemoteQueryCallbackTest0040 352 * @tc.number SUB_DistributedData_RelationalStore_SDK_QueryJsAPITest_0800 353 * @tc.desc RelationalStore remotequery function test 354 * @tc.size : MediumTest 355 * @tc.type : Function 356 * @tc.level : Level 2 357 */ 358 it('RdbRemoteQueryCallbackTest0040', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL2, async function (done){ 359 console.info(TAG + "RdbRemoteQueryCallbackTest0040 start") 360 let predicates = new data_Rdb.RdbPredicates("test"); 361 predicates.equalTo("name", "zhangsan") 362 try{ 363 await rdbStore.remoteQuery(localDeviceId, "test", predicates,"name", (err, data) => { 364 if(err != null){ 365 console.info(TAG + "Remote query error" + err) 366 }else{ 367 console.info(TAG + "Remote query success") 368 expect(false).assertTrue(); 369 } 370 }) 371 }catch(err){ 372 console.info(TAG + "RdbRemoteQueryCallbackTest0040 error:" + err) 373 } 374 375 done(); 376 console.info(TAG + "RdbRemoteQueryCallbackTest0040 end") 377 }) 378 379 }) 380}