• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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]"
20const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)";
21
22const STORE_CONFIG = {
23    name: "TransactionInsertTest.db",
24}
25
26var rdbStore = undefined;
27
28describe('rdbStoreInsertTest', function () {
29    beforeAll(async function () {
30        console.info(TAG + 'beforeAll')
31        rdbStore = await dataRdb.getRdbStore(STORE_CONFIG, 1);
32        await rdbStore.executeSql(CREATE_TABLE_TEST, null);
33    })
34
35    beforeEach(async function () {
36        console.info(TAG + 'beforeEach')
37
38    })
39
40    afterEach(async function () {
41        console.info(TAG + 'afterEach')
42        await rdbStore.executeSql("DELETE FROM test");
43    })
44
45    afterAll(async function () {
46        console.info(TAG + 'afterAll')
47        rdbStore = null
48        await dataRdb.deleteRdbStore("TransactionInsertTest.db");
49    })
50
51    console.log(TAG + "*************Unit Test Begin*************");
52
53    /**
54     * @tc.name rdb transaction insert test
55     * @tc.number testRdbTransactionInsert0001
56     * @tc.desc rdb transaction insert & commit, the result comes out is 3 items;
57     */
58    it('testRdbTransactionInsert0001', 0, async function (done) {
59        console.log(TAG + "************* testRdbStoreInsert0001 start *************");
60        var u8 = new Uint8Array([1, 2, 3])
61        try {
62            rdbStore.beginTransaction()
63            const valueBucket = {
64                "name": "lisi",
65                "age": 18,
66                "salary": 100.5,
67                "blobType": u8,
68            }
69            await rdbStore.insert("test", valueBucket)
70
71            rdbStore.commit()
72
73            let predicates = new dataRdb.RdbPredicates("test");
74            let resultSet = await rdbStore.query(predicates)
75            console.log(TAG + "testRdbTransactionInsert0001 result count " + resultSet.rowCount)
76            expect(1).assertEqual(resultSet.rowCount)
77            resultSet.close()
78        } catch (e) {
79            console.log(TAG + e);
80            expect(null).assertFail()
81            console.log(TAG + "testRdbTransactionInsert0001 failed");
82        }
83        done()
84        console.log(TAG + "************* testRdbTransactionInsert0001 end *************");
85    })
86
87    /**
88     * @tc.name rdb transaction insert test
89     * @tc.number testRdbTransactionInsert0001
90     * @tc.desc rdb transaction insert & commit, the result comes out is 3 items;
91     */
92    it('testRdbTransactionInsert0002', 0, async function (done) {
93        console.log(TAG + "************* testRdbStoreInsert0002 start *************");
94        var u8 = new Uint8Array([1, 2, 3])
95        try {
96            rdbStore.beginTransaction()
97            const valueBucket = {
98                "name": "lisi",
99                "age": 18,
100                "salary": 100.5,
101                "blobType": u8,
102            }
103            await rdbStore.insert("test", valueBucket)
104
105            const valueBucket1 = {
106                "name": "zhangsan",
107                "age": 20,
108                "salary": 9.5,
109                "blobType": u8,
110            }
111            await rdbStore.insert("test", valueBucket1)
112
113
114            const valueBucket2 = {
115                "name": "wangwu",
116                "age": 16,
117                "salary": 99,
118                "blobType": u8,
119            }
120            await rdbStore.insert("test", valueBucket2)
121
122            rdbStore.commit()
123
124            let predicates = new dataRdb.RdbPredicates("test");
125            let resultSet = await rdbStore.query(predicates)
126            expect(3).assertEqual(resultSet.rowCount)
127            resultSet.close()
128        } catch (e) {
129            expect(null).assertFail()
130            console.log(TAG + "testRdbTransactionInsert0002 failed");
131        }
132        done()
133        console.log(TAG + "************* testRdbTransactionInsert0002 end *************");
134    })
135
136
137    /**
138     * @tc.name rdb transaction insert test
139     * @tc.number testRdbTransactionInsert0002
140     * @tc.desc while using transaction to insert values, querying the db,
141     *     the result comes out is 0;
142     */
143    it('testRdbTransactionInsert0003', 0, async function (done) {
144        console.log(TAG + "************* testRdbTransactionInsert0003 start *************");
145        var u8 = new Uint8Array([1, 2, 3])
146        try {
147            rdbStore.beginTransaction()
148            const valueBucket = {
149                "name": "lisi",
150                "age": 18,
151                "salary": 100.5,
152                "blobType": u8,
153            }
154            await rdbStore.insert("test", valueBucket)
155
156            const valueBucket1 = {
157                "name": "zhangsan",
158                "age": 20,
159                "salary": 9.5,
160                "blobType": u8,
161            }
162            await rdbStore.insert("test", valueBucket1)
163
164            let predicates = new dataRdb.RdbPredicates("test");
165            let resultSet = await rdbStore.query(predicates)
166            expect(0).assertEqual(resultSet.rowCount)
167            resultSet.close()
168            const valueBucket2 = {
169                "name": "wangwu",
170                "age": 16,
171                "salary": 99,
172                "blobType": u8,
173            }
174            await rdbStore.insert("test", valueBucket2)
175
176            rdbStore.commit()
177        } catch (e) {
178            expect(null).assertFail()
179            console.log(TAG + "testRdbTransactionInsert0003 failed");
180        }
181        done()
182        console.log(TAG + "************* testRdbTransactionInsert0003 end *************");
183    })
184
185    /**
186     * @tc.name rdb insert test
187     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Insert_0010
188     * @tc.desc the classical transaction scenario, when we insert or commit the value,
189     *     db returns an exception, we need to catch exception and rollback.
190     */
191    it('testRdbTransactionRollBack0001', 0, async function (done) {
192        console.log(TAG + "************* testRdbTransactionRollBack0001 start *************");
193        var u8 = new Uint8Array([1, 2, 3])
194        try {
195            rdbStore.beginTransaction()
196            const valueBucket = {
197                "id": 1,
198                "name": "lisi",
199                "age": 18,
200                "salary": 100.5,
201                "blobType": u8,
202            }
203            await rdbStore.insert("test", valueBucket)
204            await rdbStore.insert("test", valueBucket)
205
206            rdbStore.commit()
207        } catch (e) {
208            rdbStore.rollBack()
209            let predicates = new dataRdb.RdbPredicates("test");
210            let resultSet = await rdbStore.query(predicates)
211            console.log(TAG + "testRdbTransactionRollBack0001 result count " + resultSet.rowCount);
212            expect(0).assertEqual(resultSet.rowCount)
213            resultSet.close()
214        }
215        done()
216        console.log(TAG + "************* testRdbTransactionRollBack0001 end *************");
217    })
218
219    /**
220     * @tc.name rdb insert test
221     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Insert_0010
222     * @tc.desc the classical transaction scenario, when we insert or commit the value,
223     *     db returns an exception, we need to catch exception and rollback.
224     */
225    it('testRdbTransactionMulti0001', 0, async function (done) {
226        console.log(TAG + "************* testRdbTransactionMulti0001 start *************");
227        var u8 = new Uint8Array([1, 2, 3])
228        try {
229            rdbStore.beginTransaction()
230            const valueBucket = {
231                "id": 1,
232                "name": "lisi",
233                "age": 18,
234                "salary": 100.5,
235                "blobType": u8,
236            }
237            await rdbStore.insert("test", valueBucket)
238
239            rdbStore.beginTransaction()
240            const valueBucket1 = {
241                "name": "zhangsan",
242                "age": 20,
243                "salary": 220.5,
244                "blobType": u8,
245            }
246            let num = rdbStore.insert("test", valueBucket1)
247            num.then(async (ret) => {
248                console.log(TAG + "testRdbTransactionMulti0001 * insert result " + ret);
249                expect(2).assertEqual(ret)
250            })
251
252            rdbStore.commit()
253
254            let predicates = new dataRdb.RdbPredicates("test");
255            num =  rdbStore.query(predicates)
256            num.then(async (ret) => {
257                expect(2).assertEqual(ret.rowCount)
258                ret.close()
259            })
260        } catch (e) {
261            console.log(TAG + "testRdbTransactionMulti0001 fail ***** ");
262        }
263        done()
264        console.log(TAG + "************* testRdbTransactionMulti0001 end *************");
265    })
266
267    /**
268     * @tc.name rdb insert test
269     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Insert_0010
270     * @tc.desc the classical transaction scenario, when we insert or commit the value,
271     *     db returns an exception, we need to catch exception and rollback.
272     */
273    it('testRdbTransactionMulti0002', 0, async function (done) {
274        console.log(TAG + "************* testRdbTransactionMulti0002 start *************");
275        var u8 = new Uint8Array([1, 2, 3])
276        try {
277            rdbStore.beginTransaction()
278            const valueBucket = {
279                "id": 1,
280                "name": "lisi",
281                "age": 18,
282                "salary": 100.5,
283                "blobType": u8,
284            }
285            await rdbStore.insert("test", valueBucket, function (err, ret){
286
287            });
288
289            rdbStore.beginTransaction()
290            const valueBucket1 = {
291                "name": "zhangsan",
292                "age": 20,
293                "salary": 220.5,
294                "blobType": u8,
295            }
296            let num = rdbStore.insert("test", valueBucket1)
297            num.then(async (ret) => {
298                console.log(TAG + "testRdbTransactionMulti0002 * insert result " + ret);
299                expect(2).assertEqual(ret)
300                ret.close()
301            })
302
303            rdbStore.rollBack()
304
305            rdbStore.commit()
306
307            let predicates = new dataRdb.RdbPredicates("test");
308            num =  rdbStore.query(predicates)
309            num.then(async (ret) => {
310                console.log(TAG + "testRdbTransactionMulti0002 * final query " + ret.rowCount);
311                expect(1).assertEqual(ret.rowCount)
312                ret.close()
313            })
314        } catch (e) {
315            console.log(TAG + "testRdbTransactionMulti0002 fail ***** ");
316        }
317        done()
318        console.log(TAG + "************* testRdbTransactionMulti0002 end *************");
319    })
320
321    /**
322     * @tc.name rdb insert test
323     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Insert_0010
324     * @tc.desc the classical transaction scenario, when we insert or commit the value,
325     *     db returns an exception, we need to catch exception and rollback.
326     */
327    it('testRdbTransactionMulti0003', 0, async function (done) {
328        console.log(TAG + "************* testRdbTransactionMulti0003 start *************");
329        var u8 = new Uint8Array([1, 2, 3])
330        try {
331            rdbStore.beginTransaction()
332            const valueBucket = {
333                "id": 1,
334                "name": "lisi",
335                "age": 18,
336                "salary": 100.5,
337                "blobType": u8,
338            }
339            await rdbStore.insert("test", valueBucket, function (err, ret){
340
341            });
342
343            rdbStore.beginTransaction()
344            const valueBucket1 = {
345                "name": "zhangsan",
346                "age": 20,
347                "salary": 220.5,
348                "blobType": u8,
349            }
350            let num = await rdbStore.insert("test", valueBucket1)
351
352            rdbStore.rollBack()
353
354            await rdbStore.insert("test", valueBucket)
355            rdbStore.commit()
356
357            let predicates = new dataRdb.RdbPredicates("test");
358            num =  rdbStore.query(predicates)
359            num.then(async (ret) => {
360                console.log(TAG + "testRdbTransactionMulti0003 * final query " + ret.rowCount);
361                expect(1).assertEqual(ret.rowCount)
362                ret.close()
363            })
364        } catch (e) {
365            rdbStore.rollBack()
366            console.log(TAG + "testRdbTransactionMulti0003 rollback ***** ");
367        }
368        done()
369        console.log(TAG + "************* testRdbTransactionMulti0003 end *************");
370    })
371
372    console.log(TAG + "*************Unit Test End*************");
373
374})