• 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 */
15import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'
16import dataRdb from '@ohos.data.rdb';
17import featureAbility from '@ohos.ability.featureAbility';
18
19const TAG = "[RDB_JSKITS_TEST]"
20const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
21    "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)";
22
23const STORE_CONFIG = {
24    name: "CreateDeleteWithFAContextTest.db",
25}
26
27describe('rdbStoreCreateDeleteWithFAContextTest', function () {
28    beforeAll(function () {
29        console.info(TAG + 'beforeAll')
30    })
31
32    beforeEach(async function () {
33        console.info(TAG + 'beforeEach')
34    })
35
36    afterEach(async function () {
37        console.info(TAG + 'afterEach')
38        await dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db");
39    })
40
41    afterAll(async function () {
42        console.info(TAG + 'afterAll')
43    })
44
45    console.log(TAG + "*************Unit Test Begin*************");
46    /**
47     * @tc.name rdb delete test
48     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0010
49     * @tc.desc rdb delete test
50     */
51    it('testRdbStoreCreateDeleteWithFAContextTest0001', 0, async function (done) {
52        console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0001 start *************");
53        let context = featureAbility.getContext()
54        var rdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1);
55        rdbStore = null
56        await dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db");
57        done()
58        console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0001 end *************");
59    })
60
61    /**
62     * @tc.name rdb delete test
63     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0020
64     * @tc.desc rdb delete test
65     */
66    it('testRdbStoreCreateDeleteWithFAContextTest0002', 0, async function (done) {
67        console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0002 start *************");
68        let context = featureAbility.getContext()
69        var rdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1);
70        await rdbStore.executeSql(CREATE_TABLE_TEST, null);
71        await rdbStore.executeSql("DELETE FROM test");
72        var u8 = new Uint8Array([1, 2, 3])
73        {
74            const valueBucket = {
75                "name": "zhangsan",
76                "age": 18,
77                "salary": 100.5,
78                "blobType": u8,
79            }
80            await rdbStore.insert("test", valueBucket)
81        }
82        {
83            const valueBucket = {
84                "name": "lisi",
85                "age": 28,
86                "salary": 100.5,
87                "blobType": u8,
88            }
89            await rdbStore.insert("test", valueBucket)
90        }
91        {
92            const valueBucket = {
93                "name": "lisi",
94                "age": 38,
95                "salary": 100.5,
96                "blobType": u8,
97            }
98            await rdbStore.insert("test", valueBucket)
99        }
100        {
101            let predicates = await new dataRdb.RdbPredicates("test")
102            predicates.equalTo("name", "zhangsan")
103            let ret = await rdbStore.delete(predicates)
104            expect(1).assertEqual(ret)
105        }
106        await rdbStore.executeSql("DELETE FROM test");
107        rdbStore = null
108        await dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db");
109        done()
110        console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0002 end *************");
111    })
112
113    /**
114     * @tc.name rdb delete test
115     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0030
116     * @tc.desc rdb delete test
117     */
118    it('testRdbStoreCreateDeleteWithFAContextTest0003', 0, function (done) {
119        console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0003 start *************");
120        let context = featureAbility.getContext()
121        dataRdb.getRdbStore(context, STORE_CONFIG, 1, (err, rdbStore) => {
122            if (err) {
123                console.info("Get RdbStore failed, err: " + err)
124                return
125            }
126            console.log("Get RdbStore successfully.")
127            rdbStore.executeSql(CREATE_TABLE_TEST, null, (err) => {
128                if (err) {
129                    console.info("executeSql CREATE_TABLE_TEST failed, err: " + err)
130                    return
131                }
132                console.log("executeSql CREATE_TABLE_TEST successfully.")
133                const valueBucket = {
134                    "name": "zhangsan",
135                    "age": 18,
136                    "salary": 100.5,
137                    "blobType": new Uint8Array([1, 2, 3]),
138                }
139                rdbStore.insert("test", valueBucket, (err, rowId) => {
140                    if (err) {
141                        console.log("Insert is failed");
142                        return;
143                    }
144                    console.log("Insert is successful, rowId = " + rowId)
145                    let predicates = new dataRdb.RdbPredicates("test")
146                    predicates.equalTo("name", "zhangsan")
147                    rdbStore.delete(predicates, (err, rows) => {
148                        if (err) {
149                            console.info("Delete failed, err: " + err)
150                            expect(null).assertFail()
151                        }
152                        console.log("Delete rows: " + rows)
153                        expect(1).assertEqual(rows)
154                        dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db", (err) => {
155                            if (err) {
156                                console.info("Delete RdbStore failed, err: " + err)
157                                return
158                            }
159                            console.log("Delete RdbStore successfully.")
160                            rdbStore = null
161                            done()
162                            console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0003 end *************");
163                        });
164                    })
165                })
166            });
167        })
168    })
169
170    /**
171     * @tc.name rdb delete test
172     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Delete_0040
173     * @tc.desc rdb delete test
174     */
175    it('testRdbStoreCreateDeleteWithFAContextTest0004', 0, async function (done) {
176        console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0004 start *************");
177        let context = featureAbility.getContext()
178        dataRdb.getRdbStore(context, STORE_CONFIG, 1).then((rdbStore) => {
179            console.log("Get RdbStore successfully.")
180            rdbStore.executeSql(CREATE_TABLE_TEST, null).then(() => {
181                console.log("executeSql CREATE_TABLE_TEST successfully.")
182                const valueBucket = {
183                    "name": "zhangsan",
184                    "age": 18,
185                    "salary": 100.5,
186                    "blobType": new Uint8Array([1, 2, 3]),
187                }
188                rdbStore.insert("test", valueBucket).then((rowId) => {
189                    console.log("Insert is successful, rowId = " + rowId)
190                    let predicates = new dataRdb.RdbPredicates("test")
191                    predicates.equalTo("name", "zhangsan")
192                    rdbStore.delete(predicates).then((rows) => {
193                        console.log("Delete rows: " + rows)
194                        expect(1).assertEqual(rows)
195                        rdbStore = null
196                        dataRdb.deleteRdbStore(context, "CreateDeleteWithFAContextTest.db").then(() => {
197                            console.log("Delete RdbStore successfully.")
198                            done()
199                            console.log(TAG + "************* testRdbStoreCreateDeleteWithFAContextTest0004 end *************");
200                        }).catch((err) => {
201                            console.info("Delete RdbStore failed, err: " + err)
202                        })
203                    }).catch((err) => {
204                        console.info("Delete failed, err: " + err)
205                        expect(null).assertFail()
206                    })
207                }).catch((err) => {
208                    console.log("Insert is failed");
209                })
210            }).catch((err) => {
211                console.info("executeSql CREATE_TABLE_TEST failed, err: " + err)
212            })
213        }).catch((err) => {
214            console.info("Get RdbStore failed, err: " + err)
215        })
216    })
217
218    console.log(TAG + "*************Unit Test End*************");
219})