• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 dataRdb from '@ohos.data.rdb';
17
18const TAG = "[RDB_JSKITS_TEST]"
19const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT UNIQUE, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)";
20const STORE_CONFIG = {
21    name: "UpdataTest.db",
22}
23var rdbStore = undefined;
24
25describe('rdbStoreUpdateTest', function () {
26    beforeAll(async function () {
27        console.info(TAG + 'beforeAll')
28        rdbStore = await dataRdb.getRdbStore(STORE_CONFIG, 1);
29        await rdbStore.executeSql(CREATE_TABLE_TEST, null);
30    })
31
32    beforeEach(async function () {
33        await rdbStore.executeSql("DELETE FROM test");
34        console.info(TAG + 'beforeEach')
35    })
36
37    afterEach(function () {
38        console.info(TAG + 'afterEach')
39    })
40
41    afterAll(async function () {
42        console.info(TAG + 'afterAll')
43        rdbStore = null
44        await dataRdb.deleteRdbStore("UpdataTest.db");
45    })
46
47    /**
48     * @tc.name resultSet Update test
49     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0001
50     * @tc.desc resultSet Update test
51     */
52    it('testRdbStoreUpdate0001', 0, async function (done) {
53        console.log(TAG + "************* testRdbStoreUpdate0001 start *************");
54        var u8 = new Uint8Array([1, 2, 3])
55        {
56            const valueBucket = {
57                "name": "zhangsan",
58                "age": 18,
59                "salary": 100.5,
60                "blobType": u8,
61            }
62            let insertPromise = rdbStore.insert("test", valueBucket)
63            insertPromise.then(async (ret) => {
64                expect(1).assertEqual(ret);
65                await console.log(TAG + "update done: " + ret);
66            }).catch((err) => {
67                expect(null).assertFail();
68            })
69            await insertPromise
70        }
71        {
72            var u8 = new Uint8Array([4, 5, 6])
73            const valueBucket = {
74                "name": "lisi",
75                "age": 20,
76                "salary": 200.5,
77                "blobType": u8,
78            }
79            let predicates = await new dataRdb.RdbPredicates("test")
80            await predicates.equalTo("id", "1")
81            let updatePromise = rdbStore.update(valueBucket, predicates)
82            updatePromise.then(async (ret) => {
83                await expect(1).assertEqual(ret);
84                await console.log(TAG + "update done: " + ret);
85                {
86                    let predicates = await new dataRdb.RdbPredicates("test")
87                    let resultSet = await rdbStore.query(predicates)
88
89                    expect(true).assertEqual(resultSet.goToFirstRow())
90                    const id = await resultSet.getLong(resultSet.getColumnIndex("id"))
91                    const name = await resultSet.getString(resultSet.getColumnIndex("name"))
92                    const age = await resultSet.getLong(resultSet.getColumnIndex("age"))
93                    const salary = await resultSet.getDouble(resultSet.getColumnIndex("salary"))
94                    const blobType = await resultSet.getBlob(resultSet.getColumnIndex("blobType"))
95
96                    await expect(1).assertEqual(id);
97                    await expect("lisi").assertEqual(name);
98                    await expect(20).assertEqual(age);
99                    await expect(200.5).assertEqual(salary);
100                    await expect(4).assertEqual(blobType[0]);
101                    await expect(5).assertEqual(blobType[1]);
102                    await expect(6).assertEqual(blobType[2]);
103                    console.log(TAG + "{id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType);
104                    await expect(false).assertEqual(resultSet.goToNextRow())
105                    resultSet = null
106                }
107
108            }).catch((err) => {
109                console.log(TAG + "update error");
110                expect(null).assertFail();
111            })
112            // await updatePromise
113        }
114
115        done();
116        console.log(TAG + "************* testRdbStoreUpdate0001 end   *************");
117    })
118
119    /**
120     * @tc.name resultSet Update test
121     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0002
122     * @tc.desc resultSet Update test
123     */
124    it('testRdbStoreUpdate0002', 0, async function (done) {
125        console.log(TAG + "************* testRdbStoreUpdate0002 start *************");
126        {
127            var u8 = new Uint8Array([1, 2, 3])
128            const valueBucket = {
129                "name": "zhangsan",
130                "age": 18,
131                "salary": 100.5,
132                "blobType": u8,
133            }
134            try {
135                let predicates = new dataRdb.RdbPredicates("")
136                let updatePromise = rdbStore.update(valueBucket, predicates)
137                updatePromise.then(async (ret) => {
138                    await console.log(TAG + "update done: " + ret);
139                    expect(null).assertFail();
140                }).catch((err) => {
141                    console.log(TAG + "update with null table name");
142                })
143            } catch (err) {
144                console.log(
145                    "catch err: update with null table name failed, err: code=" + err.code + " message=" + err.message)
146                expect("401").assertEqual(err.code)
147            }
148            try {
149                const emptyBucket = {};
150                let predicates = await new dataRdb.RdbPredicates("test")
151                let updatePromise = rdbStore.update(emptyBucket, predicates)
152                updatePromise.then(async (ret) => {
153                    await console.log(TAG + "update done: " + ret);
154                    expect(null).assertFail();
155                }).catch((err) => {
156                    console.log(TAG + "update with wrong valueBucket");
157                })
158            } catch (err) {
159                console.log("catch err: update with wrong valueBucket failed, err: code=" + err.code
160                            + " message=" + err.message)
161                expect("401").assertEqual(err.code)
162            }
163            try {
164                let predicates = await new dataRdb.RdbPredicates("test")
165                await predicates.equalTo("aaa", "null")
166                let updatePromise = rdbStore.update(valueBucket, predicates)
167                updatePromise.then(async (ret) => {
168                    await console.log(TAG + "update done: " + ret);
169                    expect(null).assertFail();
170                }).catch((err) => {
171                    console.log(TAG + "update with wrong condition");
172                })
173            } catch (err) {
174                console.log("catch err: update with wrong condition failed, err: code=" + err.code
175                            + " message=" + err.message)
176            }
177        }
178        done();
179        console.log(TAG + "************* testRdbStoreUpdate0002 end   *************");
180    })
181
182    /**
183     * @tc.name resultSet Update test
184     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0003
185     * @tc.desc resultSet Update test
186     */
187    it('testRdbStoreUpdate0003', 0, async function (done) {
188        console.log(TAG + "************* testRdbStoreUpdate0003 start *************");
189        {
190            var u8 = new Uint8Array([1, 2, 3])
191            const valueBucket = {
192                "name": "zhangsan",
193                "age": 18,
194                "salary": 100.5,
195                "blobType": u8,
196                "wrongColumn": 100.5,
197            }
198            {
199                let predicates = new dataRdb.RdbPredicates("wrongTable")
200                let updatePromise = rdbStore.update(valueBucket, predicates)
201                updatePromise.then(async (ret) => {
202                    await console.log(TAG + "update done: " + ret);
203                    expect(null).assertFail();
204                }).catch((err) => {
205                    console.log(TAG + "update with wrong table name");
206                })
207                // await updatePromise
208            }
209            {
210                let predicates = await new dataRdb.RdbPredicates("test")
211                let updatePromise = rdbStore.update(valueBucket, predicates)
212                updatePromise.then(async (ret) => {
213                    await console.log(TAG + "update done: " + ret);
214                    expect(null).assertFail();
215                }).catch((err) => {
216                    console.log(TAG + "update with wrong column name");
217                })
218                // await updatePromise
219            }
220        }
221        done();
222        console.log(TAG + "************* testRdbStoreUpdate0003 end   *************");
223    })
224
225    /**
226     * @tc.name resultSet Update test
227     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0004
228     * @tc.desc resultSet Update test
229     */
230    it('testRdbStoreUpdate0004', 0, async function (done) {
231        console.log(TAG + "************* testRdbStoreUpdate0004 start *************");
232        {
233            var u8 = new Uint8Array([1, 2, 3])
234            const valueBucket = {
235                "name": "zhangsan",
236                "age": 18,
237                "salary": 100.5,
238                "blobType": u8,
239            }
240            {
241                let predicates = await new dataRdb.RdbPredicates("test")
242                await predicates.equalTo("aaa", "null")
243                let updatePromise = rdbStore.update(valueBucket, predicates)
244                updatePromise.then(async (ret) => {
245                    await console.log(TAG + "update done: " + ret);
246                    expect(null).assertFail();
247                }).catch((err) => {
248                    console.log(TAG + "update with wrong condition");
249                })
250                // await updatePromise
251            }
252            {
253                const emptyBucket = {};
254                let predicates = await new dataRdb.RdbPredicates("test")
255                await predicates.equalTo("name", "zhangsan")
256                await predicates.equalTo("age", 18)
257                await predicates.equalTo("null", 100.5)
258                let updatePromise = rdbStore.update(valueBucket, predicates)
259                updatePromise.then(async (ret) => {
260                    await console.log(TAG + "update done: " + ret);
261                    expect(null).assertFail();
262                }).catch((err) => {
263                    console.log(TAG + "update with wrong condition");
264                })
265            }
266        }
267        done();
268        console.log(TAG + "************* testRdbStoreUpdate0004 end   *************");
269    })
270
271    /**
272     * @tc.name resultSet Update Extra long character test
273     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0005
274     * @tc.desc resultSet Update Extra long character test
275     */
276    it('testRdbStoreUpdate0005', 0, async function (done) {
277        console.log(TAG + "************* testRdbStoreUpdate0005 start *************");
278        var u8 = new Uint8Array([1, 2, 3])
279        {
280            const valueBucket = {
281                "name": "xiaoming",
282                "age": 18,
283                "salary": 100.5,
284                "blobType": u8,
285            }
286            await rdbStore.insert("test", valueBucket)
287        }
288        {
289            var u8 = new Uint8Array([4, 5, 6])
290            var nameStr = "abcd" + "e".repeat(2000) + "./&*$!@()"
291            const valueBucket = {
292                "name": nameStr,
293                "age": 20,
294                "salary": 200.5,
295                "blobType": u8,
296            }
297            let predicates = await new dataRdb.RdbPredicates("test")
298            await predicates.equalTo("name", "xiaoming")
299            let updatePromise = rdbStore.update(valueBucket, predicates)
300            updatePromise.then(async (ret) => {
301                await expect(1).assertEqual(ret);
302                await console.log(TAG + "update done: " + ret);
303                {
304                    let predicates = await new dataRdb.RdbPredicates("test")
305                    predicates.equalTo("age", 20)
306                    let resultSet = await rdbStore.query(predicates)
307                    expect(true).assertEqual(resultSet.goToFirstRow())
308                    const name = await resultSet.getString(resultSet.getColumnIndex("name"))
309                    await expect(nameStr).assertEqual(name);
310                    console.log(TAG + "{id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType);
311                    resultSet = null
312                }
313
314            }).catch((err) => {
315                console.log(TAG + "update error");
316                expect(null).assertFail();
317            })
318        }
319
320        done();
321        console.log(TAG + "************* testRdbStoreUpdate0005 end   *************");
322    })
323
324    /**
325     * @tc.name resultSet Update Extra long character test
326     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0006
327     * @tc.desc resultSet Update Extra long character test
328     */
329    it('testRdbStoreUpdate0006', 0, async function (done) {
330        console.log(TAG + "************* testRdbStoreUpdate0006 start *************");
331        var u8 = new Uint8Array([1, 2, 3])
332        {
333            const valueBucket = {
334                "name": "xiaohua",
335                "age": 18,
336                "salary": 100.5,
337                "blobType": u8,
338            }
339            await rdbStore.insert("test", valueBucket)
340        }
341        {
342            var u8 = new Uint8Array([4, 5, 6])
343            var nameStr = "橘子是水果" + "e".repeat(2000)
344            const valueBucket = {
345                "name": nameStr,
346                "age": 19,
347                "salary": 200.5,
348                "blobType": u8,
349            }
350            let predicates = await new dataRdb.RdbPredicates("test")
351            await predicates.equalTo("name", "xiaohua")
352            let updatePromise = rdbStore.update(valueBucket, predicates)
353            updatePromise.then(async (ret) => {
354                await expect(1).assertEqual(ret);
355                await console.log(TAG + "update done: " + ret);
356                {
357                    let predicates = await new dataRdb.RdbPredicates("test")
358                    predicates.equalTo("age", 19)
359                    let resultSet = await rdbStore.query(predicates)
360                    expect(true).assertEqual(resultSet.goToFirstRow())
361                    const name = await resultSet.getString(resultSet.getColumnIndex("name"))
362                    await expect(nameStr).assertEqual(name);
363                    console.log(TAG + "{id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType);
364                    resultSet = null
365                }
366
367            }).catch((err) => {
368                console.log(TAG + "update error");
369                expect(null).assertFail();
370            })
371        }
372
373        done();
374        console.log(TAG + "************* testRdbStoreUpdate0006 end   *************");
375    })
376
377    /**
378     * @tc.name resultSet Update Extra long character test
379     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Update_0007
380     * @tc.desc resultSet Update Extra long character test
381     */
382    it('testRdbStoreUpdate0007', 0, async function (done) {
383        console.log(TAG + "************* testRdbStoreUpdate0007 start *************");
384        var u8 = new Uint8Array([1, 2, 3])
385        {
386            const valueBucket = {
387                "name": "xiaocan",
388                "age": 18,
389                "salary": 100.5,
390                "blobType": u8,
391            }
392            await rdbStore.insert("test", valueBucket)
393        }
394        {
395            var u8 = new Uint8Array([4, 5, 6])
396            var nameStr = "菠萝是水果" + "e".repeat(2000) + "好吃又不贵"
397            const valueBucket = {
398                "name": nameStr,
399                "age": 21,
400                "salary": 200.5,
401                "blobType": u8,
402            }
403            let predicates = await new dataRdb.RdbPredicates("test")
404            await predicates.equalTo("name", "xiaocan")
405            let updatePromise = rdbStore.update(valueBucket, predicates)
406            updatePromise.then(async (ret) => {
407                await expect(1).assertEqual(ret);
408                await console.log(TAG + "update done: " + ret);
409                {
410                    let predicates = await new dataRdb.RdbPredicates("test")
411                    predicates.equalTo("age", 21)
412                    let resultSet = await rdbStore.query(predicates)
413                    expect(true).assertEqual(resultSet.goToFirstRow())
414                    const name = await resultSet.getString(resultSet.getColumnIndex("name"))
415                    await expect(nameStr).assertEqual(name);
416                    console.log(TAG + "{id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", blobType=" + blobType);
417                    resultSet = null
418                }
419
420            }).catch((err) => {
421                console.log(TAG + "update error");
422                expect(null).assertFail();
423            })
424        }
425
426        done();
427        console.log(TAG + "************* testRdbStoreUpdate0007 end   *************");
428    })
429    console.log(TAG + "*************Unit Test End*************");
430})
431