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