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, " +
20    "name TEXT, " + "age INTEGER, " + "salary REAL, " + "adddate DATE)";
21const STORE_CONFIG = {
22    name: "PredicatesComplexFiledJsunit.db",
23}
24var rdbStore = undefined;
25
26describe('rdbStorePredicatesComplexFiledTest', function () {
27    beforeAll(async function () {
28        console.info(TAG + 'beforeAll')
29        rdbStore = await dataRdb.getRdbStore(STORE_CONFIG, 1);
30        await generateTable();
31    })
32
33    beforeEach(async function () {
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("PredicatesComplexFiledJsunit.db");
45    })
46
47    async function generateTable() {
48        console.info(TAG + 'generateTable')
49        await rdbStore.executeSql(CREATE_TABLE_TEST);
50        const valueBucket1 = { id: 1, name: "ZhangSan", age: 20, salary: 100.51, adddate: '2022-09-01' }
51        await rdbStore.insert("test", valueBucket1)
52        const valueBucket2 = { id: 2, name: "LiSi", age: 21, salary: 120.61, adddate: '2022-09-01' }
53        await rdbStore.insert("test", valueBucket2)
54        const valueBucket3 = { id: 3, name: "WangWu", age: 22, salary: 130.71, adddate: '2022-09-02' }
55        await rdbStore.insert("test", valueBucket3)
56        const valueBucket4 = { id: 4, name: "SunLiu", age: 23, salary: 160.81, adddate: '2022-09-02' }
57        await rdbStore.insert("test", valueBucket4)
58        const valueBucket5 = { id: 5, name: "MaQi", age: 24, salary: 170.91, adddate: '2022-09-02' }
59        await rdbStore.insert("test", valueBucket5)
60        console.info(TAG + 'generateTable end')
61    }
62
63    /**
64     * @tc.name resultSet Update test
65     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Predicates_ComplexFiled_0001
66     * @tc.desc resultSet Update test
67     */
68    it('testRdbPredicatesComplexFiled0001', 0, async function (done) {
69        console.log(TAG + "************* testRdbPredicatesComplexFiled0001 start *************");
70
71        let predicates = await new dataRdb.RdbPredicates("test")
72        predicates.groupBy(["DATE(test.adddate)"]).orderByAsc("COUNT(*)")
73        let resultSet = await rdbStore.query(predicates, ["COUNT(*) AS 'num count'", "DATE(test.adddate) as birthday"])
74        expect(true).assertEqual(resultSet.goToFirstRow())
75        let count = await resultSet.getLong(resultSet.getColumnIndex("num count"))
76        let birthday = await resultSet.getString(resultSet.getColumnIndex("birthday"))
77        expect(2).assertEqual(count);
78        await expect("2022-09-01").assertEqual(birthday)
79        expect(true).assertEqual(resultSet.goToNextRow())
80        count = await resultSet.getLong(resultSet.getColumnIndex("num count"))
81        birthday = await resultSet.getString(resultSet.getColumnIndex("birthday"))
82        expect(3).assertEqual(count);
83        await expect("2022-09-02").assertEqual(birthday)
84        expect(false).assertEqual(resultSet.goToNextRow())
85        resultSet.close();
86        done();
87        console.log(TAG + "************* testRdbPredicatesComplexFiled0001 end   *************");
88    })
89
90    /**
91     * @tc.name resultSet Update test
92     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Predicates_ComplexFiled_0002
93     * @tc.desc resultSet Update test
94     */
95    it('testRdbPredicatesComplexFiled0002', 0, async function (done) {
96        console.log(TAG + "************* testRdbPredicatesComplexFiled0002 start *************");
97
98        let predicates = await new dataRdb.RdbPredicates("test")
99        predicates.groupBy(["DATE(test.adddate)"]).orderByDesc("COUNT(*)")
100        let resultSet = await rdbStore.query(predicates, ["COUNT(*) AS numcount", "DATE(test.adddate) as birthday"])
101        expect(true).assertEqual(resultSet.goToFirstRow())
102        let count = await resultSet.getLong(resultSet.getColumnIndex("numcount"))
103        let birthday = await resultSet.getString(resultSet.getColumnIndex("birthday"))
104        expect(3).assertEqual(count);
105        await expect("2022-09-02").assertEqual(birthday)
106        expect(true).assertEqual(resultSet.goToNextRow())
107        count = await resultSet.getLong(resultSet.getColumnIndex("numcount"))
108        birthday = await resultSet.getString(resultSet.getColumnIndex("birthday"))
109        expect(2).assertEqual(count);
110        await expect("2022-09-01").assertEqual(birthday)
111        expect(false).assertEqual(resultSet.goToNextRow())
112        resultSet.close();
113        done();
114        console.log(TAG + "************* testRdbPredicatesComplexFiled0002 end   *************");
115    })
116
117    console.log(TAG + "*************Unit Test End*************");
118})
119