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 SchemaToJsonStringSucTest001
131    * @tc.desc Test Js Api Schema.ToJsonString() successfully
132    * @tc.type: FUNC
133    * @tc.require: issueNumber
134    */
135    it('SchemaToJsonStringSucTest001', 0, async function (done) {
136        try {
137            let name = new ddm.FieldNode('name');
138            name.type = ddm.ValueType.STRING;
139            name.nullable = true;
140            name.default = 'zhangsan';
141
142            let schema = new ddm.Schema();
143            schema.root.appendChild(name);
144            schema.indexes = ['$.name'];
145            schema.mode = 1; // STRICT
146            schema.skip = 0;
147            options.kvStoreType = ddm.KVStoreType.SINGLE_VERSION;
148            options.schema = schema;
149            await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => {
150                console.info('SchemaToJsonStringSucTest001 getKVStore success' + JSON.stringify(options));
151                kvStore = store;
152                expect(store != null).assertTrue();
153                await kvStore.put("test_key_1", '{"name":"lisi"}');
154                await kvStore.put("test_key_2", '{"name":"wangwu"}');
155                await kvStore.put("test_key_3", '{}');
156                console.info('SchemaToJsonStringSucTest001 Put success');
157            });
158            console.info('SchemaToJsonStringSucTest001 start Query ...');
159            var query = new ddm.Query();
160            query.prefixKey('test_key_');
161            query.equalTo("$.name", 'zhangsan');
162            await kvStore.getEntries(query).then((entries) => {
163                console.info('SchemaToJsonStringSucTest001 get success : ' + JSON.stringify(entries));
164                expect(entries.length == 1).assertTrue();
165            }).catch((err) => {
166                console.info('SchemaToJsonStringSucTest001 get fail ' + err);
167                expect(null).assertFail();
168            });
169        } catch (e) {
170            console.info("SchemaToJsonStringSucTest001 fail on exception: " + e);
171            expect(null).assertFail();
172        }
173        done();
174    })
175
176    /**
177     * @tc.name SchemaToJsonStringSucTest002
178     * @tc.desc  Test Js Api Schema.ToJsonString() successfully
179     * @tc.type: FUNC
180     * @tc.require: issueNumber
181     */
182    it('SchemaToJsonStringSucTest002', 0, async function (done) {
183        try {
184            let name = new ddm.FieldNode('name');
185            name.type = ddm.ValueType.FLOAT;
186            name.nullable = true;
187            name.default = '1.23';
188
189            let schema = new ddm.Schema();
190            schema.root.appendChild(name);
191            schema.indexes = ['$.name'];
192            schema.mode = 1; // STRICT
193            schema.skip = 0;
194            options.kvStoreType = ddm.KVStoreType.SINGLE_VERSION;
195            options.schema = schema;
196            await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => {
197                console.info('SchemaToJsonStringSucTest002 getKVStore success' + JSON.stringify(options));
198                kvStore = store;
199                expect(store != null).assertTrue();
200                await kvStore.put("test_key_1", '{"name":3.14}');
201                await kvStore.put("test_key_2", '{"name":1.25}');
202                await kvStore.put("test_key_3", '{}');
203                console.info('SchemaToJsonStringSucTest002 Put success');
204            });
205            console.info('SchemaToJsonStringSucTest002 start Query ...');
206            var query = new ddm.Query();
207            query.prefixKey('test_key_');
208            query.equalTo("$.name", 1.23);
209            await kvStore.getEntries(query).then((entries) => {
210                console.info('SchemaToJsonStringSucTest002 get success : ' + JSON.stringify(entries));
211                expect(entries.length == 1).assertTrue();
212            }).catch((err) => {
213                console.info('SchemaToJsonStringSucTest002 get fail ' + err);
214                expect(null).assertFail();
215            });
216        } catch (e) {
217            console.info("SchemaToJsonStringSucTest002 fail on exception: " + e);
218            expect(null).assertFail();
219        }
220        done();
221    })
222
223    /**
224     * @tc.name SchemaToJsonStringSucTest003
225     * @tc.desc  Test Js Api Schema.ToJsonString() successfully
226     * @tc.type: FUNC
227     * @tc.require: issueNumber
228     */
229    it('SchemaToJsonStringSucTest003', 0, async function (done) {
230        try {
231            let name = new ddm.FieldNode('name');
232            name.type = ddm.ValueType.DOUBLE;
233            name.nullable = true;
234            name.default = '3.1415';
235
236            let schema = new ddm.Schema();
237            schema.root.appendChild(name);
238            schema.indexes = ['$.name'];
239            schema.mode = 1; // STRICT
240            schema.skip = 0;
241            options.kvStoreType = ddm.KVStoreType.SINGLE_VERSION;
242            options.schema = schema;
243            await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => {
244                console.info('SchemaToJsonStringSucTest003 getKVStore success' + JSON.stringify(options));
245                kvStore = store;
246                expect(store != null).assertTrue();
247                await kvStore.put("test_key_1", '{"name":3.1415}');
248                await kvStore.put("test_key_2", '{"name":1.2523}');
249                await kvStore.put("test_key_3", '{}');
250                console.info('SchemaToJsonStringSucTest003 Put success');
251            });
252            console.info('SchemaToJsonStringSucTest003 start Query ...');
253            var query = new ddm.Query();
254            query.prefixKey('test_key_');
255            query.equalTo("$.name", 3.1415);
256            await kvStore.getEntries(query).then((entries) => {
257                console.info('SchemaToJsonStringSucTest003 get success : ' + JSON.stringify(entries));
258                expect(entries.length == 2).assertTrue();
259            }).catch((err) => {
260                console.info('SchemaToJsonStringSucTest003 get fail ' + err);
261                expect(null).assertFail();
262            });
263        } catch (e) {
264            console.info("SchemaToJsonStringSucTest003 fail on exception: " + e);
265            expect(null).assertFail();
266        }
267        done();
268    })
269
270    /**
271     * @tc.name SchemaToJsonStringSucTest004
272     * @tc.desc  Test Js Api Schema.ToJsonString() successfully
273     * @tc.type: FUNC
274     * @tc.require: issueNumber
275     */
276    it('SchemaToJsonStringSucTest004', 0, async function (done) {
277        try {
278            let name = new ddm.FieldNode('name');
279            name.type = ddm.ValueType.BOOLEAN;
280            name.nullable = true;
281            name.default = 'true';
282
283            let schema = new ddm.Schema();
284            schema.root.appendChild(name);
285            schema.indexes = ['$.name'];
286            schema.mode = 1; // STRICT
287            schema.skip = 0;
288            options.kvStoreType = ddm.KVStoreType.SINGLE_VERSION;
289            options.schema = schema;
290            await kvManager.getKVStore(TEST_STORE_ID, options).then(async (store) => {
291                console.info('SchemaToJsonStringSucTest004 getKVStore success' + JSON.stringify(options));
292                kvStore = store;
293                expect(store != null).assertTrue();
294                await kvStore.put("test_key_1", '{"name":true}');
295                await kvStore.put("test_key_2", '{"name":false}');
296                await kvStore.put("test_key_3", '{}');
297                console.info('SchemaToJsonStringSucTest004 Put success');
298            });
299            console.info('SchemaToJsonStringSucTest004 start Query ...');
300            var query = new ddm.Query();
301            query.prefixKey('test_key_');
302            query.equalTo("$.name", true);
303            await kvStore.getEntries(query).then((entries) => {
304                console.info('SchemaToJsonStringSucTest004 get success : ' + JSON.stringify(entries));
305                expect(entries.length == 2).assertTrue();
306            }).catch((err) => {
307                console.info('SchemaToJsonStringSucTest004 get fail ' + err);
308                expect(null).assertFail();
309            });
310        } catch (e) {
311            console.info("SchemaToJsonStringSucTest004 fail on exception: " + e);
312            expect(null).assertFail();
313        }
314        done();
315    })
316
317    /**
318     * @tc.name SchemaRootTest
319     * @tc.desc  Test Js Api Schema.root successfully
320     * @tc.type: FUNC
321     * @tc.require: issueNumber
322     */
323    it('SchemaRootTest', 0, async function (done) {
324        try {
325            let english = new ddm.FieldNode('english');
326            english.type = ddm.ValueType.STRING;
327
328            let schema = new ddm.Schema();
329            expect(schema.root instanceof ddm.FieldNode).assertTrue();
330        } catch (e) {
331            console.info("schema fail on exception: " + e);
332            expect(null).assertFail();
333        }
334        done();
335    })
336
337    /**
338     * @tc.name SchemaIndexesSucTest
339     * @tc.desc  Test Js Api Schema.indexes successfully
340     * @tc.type: FUNC
341     * @tc.require: issueNumber
342     */
343    it('SchemaIndexesSucTest', 0, async function (done) {
344        try {
345
346            let schema = new ddm.Schema();
347            schema.indexes = ['$.english.first', '$.english.second'];
348            expect(schema.indexes[0] === '$.english.first' && schema.indexes[1] === '$.english.second').assertTrue();
349        } catch (e) {
350            console.info("schema fail on exception: " + e);
351            expect(null).assertFail();
352        }
353        done();
354    })
355
356    /**
357     * @tc.name SchemaMode1Test
358     * @tc.desc  Test Js Api Schema.mode with mode 1
359     * @tc.type: FUNC
360     * @tc.require: issueNumber
361     */
362    it('SchemaMode1Test', 0, async function (done) {
363        try {
364
365            let schema = new ddm.Schema();
366            schema.mode = 1;
367            console.info("schema mode = " + schema.mode)
368            expect(schema.mode === 1).assertTrue();
369        } catch (e) {
370            console.info("schema fail on exception: " + e);
371            expect(null).assertFail();
372        }
373        done();
374    })
375
376    /**
377     * @tc.name SchemaMode0Test
378     * @tc.desc  Test Js Api Schema.mode with mode 0
379     * @tc.type: FUNC
380     * @tc.require: issueNumber
381     */
382    it('SchemaMode0Test', 0, async function (done) {
383        try {
384
385            let schema = new ddm.Schema();
386            schema.mode = 0;
387            console.info("schema mode = " + schema.mode)
388            expect(schema.mode === 0).assertTrue();
389        } catch (e) {
390            console.info("schema fail on exception: " + e);
391            expect(null).assertFail();
392        }
393        done();
394    })
395
396    /**
397     * @tc.name SchemaSkipSucTest
398     * @tc.desc  Test Js Api Schema.skip successfully
399     * @tc.type: FUNC
400     * @tc.require: issueNumber
401     */
402    it('SchemaSkipSucTest', 0, async function (done) {
403        try {
404
405            let schema = new ddm.Schema();
406            schema.skip = 0;
407            expect(schema.skip === 0).assertTrue();
408        } catch (e) {
409            console.info("schema fail on exception: " + e);
410            expect(null).assertFail();
411        }
412        done();
413    })
414})
415