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 */ 15 16import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 17import ddm from '@ohos.data.distributedKVStore'; 18import abilityFeatureAbility from '@ohos.ability.featureAbility' 19 20var context = abilityFeatureAbility.getContext(); 21const TEST_BUNDLE_NAME = 'com.example.myapplication'; 22const TEST_STORE_ID = 'storeId'; 23 24var kvManager = null; 25var kvStore = null; 26 27describe('schemaTest', function () { 28 const config = { 29 bundleName: TEST_BUNDLE_NAME, 30 context: context, 31 } 32 33 var options = { 34 createIfMissing: true, 35 encrypt: false, 36 backup: false, 37 autoSync: true, 38 kvStoreType: ddm.KVStoreType.SINGLE_VERSION, 39 schema: {}, 40 securityLevel: ddm.SecurityLevel.S1, 41 } 42 43 beforeAll(async function (done) { 44 try { 45 kvManager = ddm.createKVManager(config); 46 console.info("beforeAll: createKVManager (single) with " + JSON.stringify(options)); 47 } catch (e) { 48 console.info("fail on exception: " + e); 49 expect(null).assertFail(); 50 } 51 done(); 52 }) 53 54 afterAll(async function (done) { 55 console.info('afterAll'); 56 kvManager = null; 57 kvStore = null; 58 done(); 59 }) 60 61 beforeEach(async function (done) { 62 console.info('beforeEach testcase will update options:' + JSON.stringify(options)); 63 done(); 64 }) 65 66 afterEach(async function (done) { 67 console.info('afterEach'); 68 await kvManager.closeKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID, kvStore).then(async () => { 69 console.info('afterEach closeKVStore success'); 70 await kvManager.deleteKVStore(TEST_BUNDLE_NAME, TEST_STORE_ID).then(() => { 71 console.info('afterEach deleteKVStore success'); 72 }).catch((err) => { 73 console.info('afterEach deleteKVStore err ' + err); 74 }); 75 }).catch((err) => { 76 console.info('afterEach closeKVStore err ' + err); 77 }); 78 kvStore = null; 79 done(); 80 }) 81 82 /** 83 * @tc.name SchemaToJsonStringSucTest 84 * @tc.desc Test Js Api Schema.ToJsonString() successfully 85 * @tc.type: FUNC 86 * @tc.require: issueNumber 87 */ 88 it('SchemaToJsonStringSucTest', 0, async function (done) { 89 try { 90 let name = new ddm.FieldNode('name'); 91 name.type = ddm.ValueType.INTEGER; 92 name.nullable = false; 93 name.default = 0; 94 95 let schema = new ddm.Schema(); 96 schema.root.appendChild(name); 97 schema.indexes = ['$.name']; 98 schema.mode = 1; // STRICT 99 schema.skip = 0; 100 options.kvStoreType = ddm.KVStoreType.SINGLE_VERSION; 101 options.schema = schema; 102 await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => { 103 console.info('SchemaToJsonStringSucTest getKVStore success' + JSON.stringify(options)); 104 kvStore = store; 105 expect(store != null).assertTrue(); 106 await kvStore.put("test_key_1", '{"name":1}'); 107 await kvStore.put("test_key_2", '{"name":2}'); 108 await kvStore.put("test_key_3", '{"name":3}'); 109 console.info('SchemaToJsonStringSucTest Put success'); 110 }); 111 console.info('SchemaToJsonStringSucTest start Query ...'); 112 var query = new ddm.Query(); 113 query.prefixKey('test_key_'); 114 query.notEqualTo("$.name", 3); 115 await kvStore.getEntries(query).then((entries) => { 116 console.info('SchemaToJsonStringSucTest get success : ' + JSON.stringify(entries)); 117 expect(entries.length == 2).assertTrue(); 118 }).catch((err) => { 119 console.info('SchemaToJsonStringSucTest get fail ' + err); 120 expect(null).assertFail(); 121 }); 122 } catch (e) { 123 console.info("SchemaToJsonStringSucTest fail on exception: " + e); 124 expect(null).assertFail(); 125 } 126 done(); 127 }) 128 129 /** 130 * @tc.name SchemaRootTest 131 * @tc.desc Test Js Api Schema.root successfully 132 * @tc.type: FUNC 133 * @tc.require: issueNumber 134 */ 135 it('SchemaRootTest', 0, async function (done) { 136 try { 137 let english = new ddm.FieldNode('english'); 138 english.type = ddm.ValueType.STRING; 139 140 let schema = new ddm.Schema(); 141 expect(schema.root instanceof ddm.FieldNode).assertTrue(); 142 } catch (e) { 143 console.info("schema fail on exception: " + e); 144 expect(null).assertFail(); 145 } 146 done(); 147 }) 148 149 /** 150 * @tc.name SchemaIndexesSucTest 151 * @tc.desc Test Js Api Schema.indexes successfully 152 * @tc.type: FUNC 153 * @tc.require: issueNumber 154 */ 155 it('SchemaIndexesSucTest', 0, async function (done) { 156 try { 157 158 let schema = new ddm.Schema(); 159 schema.indexes = ['$.english.first', '$.english.second']; 160 expect(schema.indexes[0] === '$.english.first' && schema.indexes[1] === '$.english.second').assertTrue(); 161 } catch (e) { 162 console.info("schema fail on exception: " + e); 163 expect(null).assertFail(); 164 } 165 done(); 166 }) 167 168 /** 169 * @tc.name SchemaMode1Test 170 * @tc.desc Test Js Api Schema.mode with mode 1 171 * @tc.type: FUNC 172 * @tc.require: issueNumber 173 */ 174 it('SchemaMode1Test', 0, async function (done) { 175 try { 176 177 let schema = new ddm.Schema(); 178 schema.mode = 1; 179 console.info("schema mode = " + schema.mode) 180 expect(schema.mode === 1).assertTrue(); 181 } catch (e) { 182 console.info("schema fail on exception: " + e); 183 expect(null).assertFail(); 184 } 185 done(); 186 }) 187 188 /** 189 * @tc.name SchemaMode0Test 190 * @tc.desc Test Js Api Schema.mode with mode 0 191 * @tc.type: FUNC 192 * @tc.require: issueNumber 193 */ 194 it('SchemaMode0Test', 0, async function (done) { 195 try { 196 197 let schema = new ddm.Schema(); 198 schema.mode = 0; 199 console.info("schema mode = " + schema.mode) 200 expect(schema.mode === 0).assertTrue(); 201 } catch (e) { 202 console.info("schema fail on exception: " + e); 203 expect(null).assertFail(); 204 } 205 done(); 206 }) 207 208 /** 209 * @tc.name SchemaSkipSucTest 210 * @tc.desc Test Js Api Schema.skip successfully 211 * @tc.type: FUNC 212 * @tc.require: issueNumber 213 */ 214 it('SchemaSkipSucTest', 0, async function (done) { 215 try { 216 217 let schema = new ddm.Schema(); 218 schema.skip = 0; 219 expect(schema.skip === 0).assertTrue(); 220 } catch (e) { 221 console.info("schema fail on exception: " + e); 222 expect(null).assertFail(); 223 } 224 done(); 225 }) 226}) 227