1/* 2 * Copyright (c) 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 } from 'deccjsunit/index' 16import ddm from '@ohos.data.distributedData'; 17 18const TEST_BUNDLE_NAME = 'com.example.myapplication'; 19const TEST_STORE_ID = 'storeId'; 20 21var kvManager = null; 22var kvStore = null; 23 24function putBatchString(len, prefix) { 25 let entries = []; 26 for (var i = 0; i < len; i++) { 27 entries.push({ 28 key : prefix + i, 29 value : { 30 type : ddm.ValueType.STRING, 31 value : '{"english":{"first":"leda' + i + '", "second":"yang"}}' 32 } 33 }); 34 } 35 return entries; 36} 37 38async function testPutAndGet(kvManager, options) { 39 try { 40 await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => { 41 console.info('testPutAndGet getKVStore success' + JSON.stringify(options)); 42 kvStore = store; 43 expect(store != null).assertTrue(); 44 }).catch((err) => { 45 console.info('testPutAndGet getKVStore fail ' + err); 46 expect(null).assertFail(); 47 }); 48 var canGet = new Promise((resolve, reject) => { 49 kvStore.on('dataChange', 0, function (data) { 50 console.info('testPutAndGet resolve on data change: ' + JSON.stringify(data)); 51 resolve(data.deviceId); 52 }); 53 let entries = putBatchString(10, 'test_key_'); 54 kvStore.putBatch(entries).then((data) => { 55 console.info('testPutAndGet put success'); 56 expect(data == undefined).assertTrue(); 57 }); 58 setTimeout(() => { 59 reject(new Error('not resolved in 2 second, reject it.')) 60 }, 2000); 61 }); 62 await canGet.then(async function(deviceId) { 63 var query = new ddm.Query(); 64 query.prefixKey('test_key_'); 65 query.like('$.english.first', 'led%'); 66 if (options.kvStoreType == ddm.KVStoreType.DEVICE_COLLABORATION) { 67 console.info('testPutAndGet deviceId = ' + deviceId); 68 query.deviceId(deviceId); 69 } 70 await kvStore.getEntries(query).then((entries) => { 71 console.info('testPutAndGet get success : ' + JSON.stringify(entries)); 72 expect(entries.length == 10).assertTrue(); 73 }).catch((err) => { 74 console.info('testPutAndGet get fail ' + err); 75 expect(null).assertFail(); 76 }); 77 }).catch((error) => { 78 console.info('testPutAndGet canGet fail: ' + error); 79 expect(null).assertFail(); 80 }); 81 } catch (e) { 82 console.info('testPutAndGet get exception: ' + e); 83 } 84} 85 86describe('schemaTest', function() { 87 const config = { 88 bundleName : TEST_BUNDLE_NAME, 89 userInfo : { 90 userId : '0', 91 userType : ddm.UserType.SAME_USER_ID 92 } 93 } 94 95 var options = { 96 createIfMissing : true, 97 encrypt : false, 98 backup : false, 99 autoSync : true, 100 kvStoreType : ddm.KVStoreType.SINGLE_VERSION, 101 schema : {}, 102 securityLevel : ddm.SecurityLevel.NO_LEVEL, 103 } 104 105 beforeAll(async function (done) { 106 try { 107 console.info("beforeAll: createKVManager (single) with " + JSON.stringify(options)); 108 await ddm.createKVManager(config).then((manager) => { 109 kvManager = manager; 110 console.info('beforeAll createKVManager success'); 111 }).catch((err) => { 112 console.info('beforeAll createKVManager err ' + err); 113 }); 114 } catch (e) { 115 console.info("fail on exception: " + e); 116 expect(null).assertFail(); 117 } 118 done(); 119 }) 120 121 afterAll(async function (done) { 122 console.info('afterAll'); 123 kvManager = null; 124 kvStore = null; 125 done(); 126 }) 127 128 beforeEach(async function (done) { 129 console.info('beforeEach testcase will update options:' + JSON.stringify(options)); 130 done(); 131 }) 132 133 afterEach(async function (done) { 134 console.info('afterEach'); 135 await kvManager.closeKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID, kvStore).then(async () => { 136 console.info('afterEach closeKVStore success'); 137 await kvManager.deleteKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID).then(() => { 138 console.info('afterEach deleteKVStore success'); 139 }).catch((err) => { 140 console.info('afterEach deleteKVStore err ' + err); 141 }); 142 }).catch((err) => { 143 console.info('afterEach closeKVStore err ' + err); 144 }); 145 kvStore = null; 146 done(); 147 }) 148 149 /** 150 * @tc.name SchemaToJsonStringTest001 151 * @tc.desc Test Js Api Schema.ToJsonString() testcase 001 152 * @tc.type: FUNC 153 * @tc.require: issueNumber 154 */ 155 it('SchemaToJsonStringTest001', 0, async function(done) { 156 try { 157 let first = new ddm.FieldNode('first'); 158 first.type = ddm.ValueType.STRING; 159 first.nullable = false; 160 first.default = 'first name'; 161 162 let second = new ddm.FieldNode('second'); 163 second.type = ddm.ValueType.STRING; 164 second.nullable = false; 165 second.default = 'second name'; 166 167 let english = new ddm.FieldNode('english'); 168 english.type = ddm.ValueType.STRING; 169 english.appendChild(first); 170 english.appendChild(second); 171 172 let schema = new ddm.Schema(); 173 schema.root.appendChild(english); 174 schema.indexes = ['$.english.first', '$.english.second']; 175 } catch (e) { 176 console.info("schema fail on exception: " + e); 177 expect(null).assertFail(); 178 } 179 done(); 180 }) 181 182 /** 183 * @tc.name SchemaToJsonStringTest002 184 * @tc.desc Test Js Api Schema.ToJsonString() testcase 002 185 * @tc.type: FUNC 186 * @tc.require: issueNumber 187 */ 188 it('SchemaToJsonStringTest002', 0, async function(done) { 189 try { 190 let first = new ddm.FieldNode('first'); 191 first.type = ddm.ValueType.STRING; 192 first.nullable = false; 193 first.default = 'first name'; 194 195 let second = new ddm.FieldNode('second'); 196 second.type = ddm.ValueType.STRING; 197 second.nullable = false; 198 second.default = 'second name'; 199 200 let english = new ddm.FieldNode('english'); 201 english.type = ddm.ValueType.STRING; 202 english.appendChild(first); 203 english.appendChild(second); 204 205 let schema = new ddm.Schema(); 206 schema.root.appendChild(english); 207 schema.indexes = ['$.english.first', '$.english.second']; 208 options.kvStoreType = ddm.KVStoreType.DEVICE_COLLABORATION; 209 options.schema = schema; 210 await testPutAndGet(kvManager, options); 211 console.info("schematestPutAndGet done"); 212 } catch (e) { 213 console.info("schema fail on exception: " + e); 214 expect(null).assertFail(); 215 } 216 done(); 217 }) 218 219 /** 220 * @tc.name SchemaToJsonStringTest003 221 * @tc.desc Test Js Api Schema.ToJsonString() testcase 003 222 * @tc.type: FUNC 223 * @tc.require: issueNumber 224 */ 225 it('SchemaToJsonStringTest003', 0, async function(done) { 226 try { 227 let name = new ddm.FieldNode('name'); 228 name.type = ddm.ValueType.INTEGER; 229 name.nullable = false; 230 name.default = 0; 231 232 let schema = new ddm.Schema(); 233 schema.root.appendChild(name); 234 schema.indexes = ['$.name']; 235 schema.mode = 1; // STRICT 236 options.kvStoreType = ddm.KVStoreType.SINGLE_VERSION; 237 options.schema = schema; 238 await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => { 239 console.info('SchemaToJsonStringTest003 getKVStore success' + JSON.stringify(options)); 240 kvStore = store; 241 expect(store != null).assertTrue(); 242 await kvStore.put("test_key_1", '{"name":1}'); 243 await kvStore.put("test_key_2", '{"name":2}'); 244 await kvStore.put("test_key_3", '{"name":3}'); 245 console.info('SchemaToJsonStringTest003 Put success'); 246 }); 247 console.info('SchemaToJsonStringTest003 start Query ...'); 248 var query = new ddm.Query(); 249 query.prefixKey('test_key_'); 250 query.notEqualTo("$.name", 3); 251 await kvStore.getEntries(query).then((entries) => { 252 console.info('SchemaToJsonStringTest003 get success : ' + JSON.stringify(entries)); 253 expect(entries.length == 2).assertTrue(); 254 }).catch((err) => { 255 console.info('SchemaToJsonStringTest003 get fail ' + err); 256 expect(null).assertFail(); 257 }); 258 } catch (e) { 259 console.info("SchemaToJsonStringTest003 fail on exception: " + e); 260 expect(null).assertFail(); 261 } 262 done(); 263 }) 264 265 /** 266 * @tc.name SchemaToJsonStringTest004 267 * @tc.desc Test Js Api Schema.ToJsonString() testcase 004 268 * @tc.type: FUNC 269 * @tc.require: issueNumber 270 */ 271 it('SchemaToJsonStringTest004', 0, async function(done) { 272 try { 273 let english = new ddm.FieldNode('english'); 274 english.type = ddm.ValueType.STRING; 275 276 let schema = new ddm.Schema(); 277 schema.root.appendChild(english); 278 schema.indexes = []; 279 expect(null).assertFail(); 280 } catch (e) { 281 console.info("schema exception is: " + e); 282 } 283 done(); 284 }) 285 286 /** 287 * @tc.name SchemarootTest001 288 * @tc.desc Test Js Api Schema.root testcase 001 289 * @tc.type: FUNC 290 * @tc.require: issueNumber 291 */ 292 it('SchemarootTest001', 0, async function(done) { 293 try { 294 let english = new ddm.FieldNode('english'); 295 english.type = ddm.ValueType.STRING; 296 297 let schema = new ddm.Schema(); 298 expect(schema.root instanceof ddm.FieldNode).assertTrue(); 299 } catch (e) { 300 console.info("schema fail on exception: " + e); 301 expect(null).assertFail(); 302 } 303 done(); 304 }) 305 306 /** 307 * @tc.name SchemaindexesTest001 308 * @tc.desc Test Js Api Schema.indexes testcase 001 309 * @tc.type: FUNC 310 * @tc.require: issueNumber 311 */ 312 it('SchemaindexesTest001', 0, async function(done) { 313 try { 314 315 let schema = new ddm.Schema(); 316 schema.indexes = ['$.english.first', '$.english.second']; 317 expect(schema.indexes[0] === '$.english.first' && schema.indexes[1] === '$.english.second').assertTrue(); 318 } catch (e) { 319 console.info("schema fail on exception: " + e); 320 expect(null).assertFail(); 321 } 322 done(); 323 }) 324 325 /** 326 * @tc.name SchemamodeTest001 327 * @tc.desc Test Js Api Schema.mode testcase 001 328 * @tc.type: FUNC 329 * @tc.require: issueNumber 330 */ 331 it('SchemamodeTest001', 0, async function(done) { 332 try { 333 334 let schema = new ddm.Schema(); 335 schema.mode = 1; 336 console.info("schema mode = "+schema.mode) 337 expect(schema.mode === 1).assertTrue(); 338 } catch (e) { 339 console.info("schema fail on exception: " + e); 340 expect(null).assertFail(); 341 } 342 done(); 343 }) 344 345 /** 346 * @tc.name SchemamodeTest002 347 * @tc.desc Test Js Api Schema.mode testcase 002 348 * @tc.type: FUNC 349 * @tc.require: issueNumber 350 */ 351 it('SchemamodeTest002', 0, async function(done) { 352 try { 353 354 let schema = new ddm.Schema(); 355 schema.mode = 0; 356 console.info("schema mode = "+schema.mode) 357 expect(schema.mode === 0).assertTrue(); 358 } catch (e) { 359 console.info("schema fail on exception: " + e); 360 expect(null).assertFail(); 361 } 362 done(); 363 }) 364 365 /** 366 * @tc.name SchemaskipTest001 367 * @tc.desc Test Js Api Schema.skip testcase 001 368 * @tc.type: FUNC 369 * @tc.require: issueNumber 370 */ 371 it('SchemaskipTest001', 0, async function(done) { 372 try { 373 374 let schema = new ddm.Schema(); 375 schema.skip = 0; 376 expect(schema.skip === 0).assertTrue(); 377 } catch (e) { 378 console.info("schema fail on exception: " + e); 379 expect(null).assertFail(); 380 } 381 done(); 382 }) 383}) 384