RdbStoreDistributedJsunit.test.js 15.8 KB
Newer Older
Y
yanglei1217 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
import dataRdb from '@ohos.data.rdb';

const TAG = "[RDB_JSKITS_TEST_Distributed]"
const STORE_NAME = "distributed_rdb.db"
var rdbStore = undefined;

describe('rdbStoreDistributedTest', function () {
    beforeAll(async function () {
        console.info(TAG + 'beforeAll')
    })

    beforeEach(async function () {
        console.info(TAG + 'beforeEach')
    })

    afterEach(async function () {
        console.info(TAG + 'afterEach')
    })

    afterAll(async function () {
        console.info(TAG + 'afterAll')
        await dataRdb.deleteRdbStore(STORE_NAME);
    })

L
liangzhenyu123 已提交
41
    console.info(TAG + "*************Unit Test Begin*************");
Y
yanglei1217 已提交
42 43 44 45 46 47 48

    /**
     * @tc.name rdb open test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_001
     * @tc.desc rdb open test
     */
    it('testRdbStoreDistributed0001', 0, async function (done) {
L
liangzhenyu123 已提交
49
        console.info(TAG + "************* testRdbStoreDistributed001 start *************");
Y
yanglei1217 已提交
50 51 52 53 54
        const config = {
            "name": STORE_NAME,
        }
        try {
            rdbStore = await dataRdb.getRdbStore(config, 1);
L
liangzhenyu123 已提交
55
            console.info(TAG + "create rdb store success")
Y
yanglei1217 已提交
56 57 58 59 60 61 62
            expect(rdbStore).assertEqual(rdbStore)
            let sqlStatement = "CREATE TABLE IF NOT EXISTS employee (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT NOT NULL," +
                "age INTEGER)"
            try {
                await rdbStore.executeSql(sqlStatement, null)
L
liangzhenyu123 已提交
63
                console.info(TAG + "create table employee success")
Y
yanglei1217 已提交
64
            } catch (err) {
L
liangzhenyu123 已提交
65
                console.info(TAG + "create table employee failed")
Y
yanglei1217 已提交
66 67 68 69 70 71 72 73 74 75 76
                expect(null).assertFail()
            }

            sqlStatement = "CREATE TABLE IF NOT EXISTS product (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT NOT NULL," +
                "price REAL," +
                "vendor INTEGER," +
                "describe TEXT)"
            try {
                await rdbStore.executeSql(sqlStatement, null)
L
liangzhenyu123 已提交
77
                console.info(TAG + "create table product success")
Y
yanglei1217 已提交
78
            } catch (err) {
L
liangzhenyu123 已提交
79
                console.info(TAG + "create table product failed")
Y
yanglei1217 已提交
80 81 82
                expect(null).assertFail()
            }
        } catch (err) {
L
liangzhenyu123 已提交
83
            console.info(TAG + "create rdb store failed")
Y
yanglei1217 已提交
84 85 86
            expect(null).assertFail()
        }
        done()
L
liangzhenyu123 已提交
87
        console.info(TAG + "************* testRdbStoreDistributed001 end *************");
Y
yanglei1217 已提交
88 89 90 91 92 93 94 95
    })

    /**
     * @tc.name set_distributed_table_none_table
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_002
     * @tc.desc rdb set distributed table using none table as argment
     */
    it('testRdbStoreDistributed0002', 0, async function (done) {
L
liangzhenyu123 已提交
96
        console.info(TAG + "************* testRdbStoreDistributed002 start *************");
Y
yanglei1217 已提交
97 98
        try {
            await rdbStore.setDistributedTables([])
L
liangzhenyu123 已提交
99
            console.info(TAG + "set none to be distributed table success");
Y
yanglei1217 已提交
100 101
            expect(rdbStore).assertEqual(rdbStore)
        } catch (err) {
L
liangzhenyu123 已提交
102
            console.info(TAG + "set none to be distributed table failed");
Y
yanglei1217 已提交
103 104 105
            expect(null).assertFail();
        }
        done()
L
liangzhenyu123 已提交
106
        console.info(TAG + "************* testRdbStoreDistributed002 end *************");
Y
yanglei1217 已提交
107 108 109 110 111 112 113 114
    })

    /**
     * @tc.name set distributed table using one table name
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_003
     * @tc.desc set distributed table using one table name
     */
    it('testRdbStoreDistributed0003', 0, async function (done) {
L
liangzhenyu123 已提交
115
        console.info(TAG + "************* testRdbStoreDistributed003 start *************");
Y
yanglei1217 已提交
116 117
        try {
            await rdbStore.setDistributedTables(['employee'])
L
liangzhenyu123 已提交
118
            console.info(TAG + "set employee to be distributed table success");
Y
yanglei1217 已提交
119 120
            expect(rdbStore).assertEqual(rdbStore)
        } catch (err) {
L
liangzhenyu123 已提交
121
            console.info(TAG + "set employee to be distributed table failed");
Y
yanglei1217 已提交
122 123 124
            expect(null).assertFail();
        }
        done()
L
liangzhenyu123 已提交
125
        console.info(TAG + "************* testRdbStoreDistributed003 end *************");
Y
yanglei1217 已提交
126 127 128 129 130 131 132 133
    })

    /**
     * @tc.name set distributed table using two table name
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_004
     * @tc.desc set distributed table using two table name
     */
    it('testRdbStoreDistributed0004', 0, async function (done) {
L
liangzhenyu123 已提交
134
        console.info(TAG + "************* testRdbStoreDistributed004 start *************");
Y
yanglei1217 已提交
135 136
        try {
            await rdbStore.setDistributedTables(['employee', 'product'])
L
liangzhenyu123 已提交
137
            console.info(TAG + "set employee and product to be distributed table success");
Y
yanglei1217 已提交
138 139
            expect(rdbStore).assertEqual(rdbStore)
        } catch (err) {
L
liangzhenyu123 已提交
140
            console.info(TAG + "set employee and product to be distributed table failed");
Y
yanglei1217 已提交
141 142 143
            expect(null).assertFail();
        }
        done()
L
liangzhenyu123 已提交
144
        console.info(TAG + "************* testRdbStoreDistributed004 end *************");
Y
yanglei1217 已提交
145 146 147 148 149 150 151 152
    })

    /**
     * @tc.name insert record after setting distributed table
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_005
     * @tc.desc insert record after setting distributed table
     */
    it('testRdbStoreDistributed0005', 0, async function (done) {
L
liangzhenyu123 已提交
153
        console.info(TAG + "************* testRdbStoreDistributed005 start *************");
Y
yanglei1217 已提交
154 155 156 157 158 159
        const record = {
            "name": "Jim",
            "age": 20,
        }
        try {
            let rowId = await rdbStore.insert("employee", record)
L
liangzhenyu123 已提交
160
            console.info(TAG + "insert one record success " + rowId)
Y
yanglei1217 已提交
161 162
            expect(1).assertEqual(rowId)
        } catch (err) {
L
liangzhenyu123 已提交
163
            console.info(TAG + "insert one record failed");
Y
yanglei1217 已提交
164 165 166
            expect(null).assertFail();
        }
        done()
L
liangzhenyu123 已提交
167
        console.info(TAG + "************* testRdbStoreDistributed005 end *************");
Y
yanglei1217 已提交
168 169 170 171 172 173 174 175
    })

    /**
     * @tc.name update record after setting distributed table
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_006
     * @tc.desc update record after setting distributed table
     */
    it('testRdbStoreDistributed0006', 0, async function (done) {
L
liangzhenyu123 已提交
176
        console.info(TAG + "************* testRdbStoreDistributed006 start *************");
Y
yanglei1217 已提交
177 178 179 180 181 182 183 184 185
        const record = {
            "name": "Jim",
            "age": 30,
        }
        try {
            let predicate = new dataRdb.RdbPredicates("employee");
            predicate.equalTo("id", 1);
            try {
                let rowId = await rdbStore.update(record, predicate);
L
liangzhenyu123 已提交
186
                console.info(TAG + "update one record success " + rowId)
Y
yanglei1217 已提交
187 188
                expect(1).assertEqual(rowId)
            } catch (err) {
L
liangzhenyu123 已提交
189
                console.info(TAG + "update one record failed");
Y
yanglei1217 已提交
190 191 192
                expect(null).assertFail();
            }
        } catch (err) {
L
liangzhenyu123 已提交
193
            console.info(TAG + "construct predicate failed");
Y
yanglei1217 已提交
194 195 196
            expect(null).assertFail();
        }
        done()
L
liangzhenyu123 已提交
197
        console.info(TAG + "************* testRdbStoreDistributed006 end *************");
Y
yanglei1217 已提交
198 199 200 201 202 203 204 205
    })

    /**
     * @tc.name query record after setting distributed table
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_007
     * @tc.desc query record after setting distributed table
     */
    it('testRdbStoreDistributed0007', 0, async function (done) {
L
liangzhenyu123 已提交
206
        console.info(TAG + "************* testRdbStoreDistributed0007 start *************");
Y
yanglei1217 已提交
207 208 209 210
        try {
            let predicates = new dataRdb.RdbPredicates("employee")
            let resultSet = await rdbStore.query(predicates)
            try {
L
liangzhenyu123 已提交
211
                console.info(TAG + "product resultSet query done");
Y
yanglei1217 已提交
212 213 214 215 216 217 218 219 220 221 222
                expect(true).assertEqual(resultSet.goToFirstRow())
                const id = await resultSet.getLong(resultSet.getColumnIndex("id"))
                const name = await resultSet.getString(resultSet.getColumnIndex("name"))
                const age = await resultSet.getLong(resultSet.getColumnIndex("age"))

                await expect(1).assertEqual(id);
                await expect("Jim").assertEqual(name);
                await expect(30).assertEqual(age);
                resultSet.close();
                expect(true).assertEqual(resultSet.isClosed)
            } catch (e) {
L
liangzhenyu123 已提交
223
                console.info(TAG + "result get value failed")
Y
yanglei1217 已提交
224 225 226
                expect(null).assertFail();
            }
        } catch (err) {
L
liangzhenyu123 已提交
227
            console.info("query failed");
Y
yanglei1217 已提交
228 229 230
            expect(null).assertFail();
        }
        done();
L
liangzhenyu123 已提交
231
        console.info(TAG + "************* testRdbStoreDistributed0007 end *************");
Y
yanglei1217 已提交
232 233 234 235 236 237 238 239
    })

    /**
     * @tc.name delete record after setting distributed table
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_008
     * @tc.desc delete record after setting distributed table
     */
    it('testRdbStoreDistributed0008', 0, async function (done) {
L
liangzhenyu123 已提交
240
        console.info(TAG + "************* testRdbStoreDistributed0008 start *************");
Y
yanglei1217 已提交
241 242 243
        let predicates = new dataRdb.RdbPredicates("employee")
        try {
            let number = await rdbStore.delete(predicates)
L
liangzhenyu123 已提交
244
            console.info(TAG + "employee Delete done: " + number)
Y
yanglei1217 已提交
245 246
            expect(1).assertEqual(number)
        } catch (err) {
L
liangzhenyu123 已提交
247
            console.info(TAG + "delete record failed");
Y
yanglei1217 已提交
248 249 250
            expect(null).assertFail()
        }
        done();
L
liangzhenyu123 已提交
251
        console.info(TAG + "************* testRdbStoreDistributed0008 end *************");
Y
yanglei1217 已提交
252 253 254 255 256 257 258 259
    })

    /**
     * @tc.name predicates inDevice
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_009
     * @tc.desc predicates inDevice
     */
    it('testRdbStoreDistributed0009', 0, async function (done) {
L
liangzhenyu123 已提交
260
        console.info(TAG + "************* testRdbStoreDistributed0009 start *************");
Y
yanglei1217 已提交
261 262 263
        let predicates = new dataRdb.RdbPredicates("employee")
        try {
            predicates = predicates.inDevices("1234567890");
L
liangzhenyu123 已提交
264
            console.info(TAG + "inDevices success");
Y
yanglei1217 已提交
265 266
            expect(predicates).assertEqual(predicates);
        } catch (err) {
L
liangzhenyu123 已提交
267
            console.info(TAG + "inDevices failed");
Y
yanglei1217 已提交
268 269 270
            expect(null).assertFail();
        }
        done();
L
liangzhenyu123 已提交
271
        console.info(TAG + "************* testRdbStoreDistributed0009 end *************");
Y
yanglei1217 已提交
272 273 274 275 276 277 278 279
    })

    /**
     * @tc.name predicates inAllDevices
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_010
     * @tc.desc predicates inAllDevices
     */
    it('testRdbStoreDistributed0010', 0, async function (done) {
L
liangzhenyu123 已提交
280
        console.info(TAG + "************* testRdbStoreDistributed0010 start *************");
Y
yanglei1217 已提交
281 282 283
        let predicates = new dataRdb.RdbPredicates("employee")
        try {
            predicates = predicates.inAllDevices();
L
liangzhenyu123 已提交
284
            console.info(TAG + "inAllDevices success");
Y
yanglei1217 已提交
285 286
            expect(predicates).assertEqual(predicates);
        } catch (err) {
L
liangzhenyu123 已提交
287
            console.info(TAG + "inAllDevices failed");
Y
yanglei1217 已提交
288 289 290
            expect(null).assertFail();
        }
        done();
L
liangzhenyu123 已提交
291
        console.info(TAG + "************* testRdbStoreDistributed0010 end *************");
Y
yanglei1217 已提交
292 293 294 295 296 297 298 299
    })

    /**
     * @tc.name sync test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_011
     * @tc.desc sync test
     */
    it('testRdbStoreDistributed0011', 0, async function (done) {
L
liangzhenyu123 已提交
300
        console.info(TAG + "************* testRdbStoreDistributed0011 start *************");
Y
yanglei1217 已提交
301 302 303
        let predicates = new dataRdb.RdbPredicates("employee")
        predicates = predicates.inDevices("12345678abcd");
        rdbStore.sync(dataRdb.SyncMode.SYNC_MODE_PUSH, predicates);
L
liangzhenyu123 已提交
304
        console.info(TAG + "sync push success");
Y
yanglei1217 已提交
305 306
        expect(rdbStore).assertEqual(rdbStore);
        rdbStore.sync(dataRdb.SyncMode.SYNC_MODE_PULL, predicates);
L
liangzhenyu123 已提交
307
        console.info(TAG + "sync pull success");
Y
yanglei1217 已提交
308 309
        expect(rdbStore).assertEqual(rdbStore);
        done();
L
liangzhenyu123 已提交
310
        console.info(TAG + "************* testRdbStoreDistributed0011 end *************");
Y
yanglei1217 已提交
311
    })
Y
yanglifeng1217 已提交
312 313 314 315 316 317 318
	
	/**
     * @tc.name sync Callback test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_Callback_011
     * @tc.desc sync Callback test
     */
    it('testRdbStoreDistributedCallback0011', 0, async function (done) {
L
liangzhenyu123 已提交
319
        console.info(TAG + "************* testRdbStoreDistributedCallback0011 start *************");
Y
yanglifeng1217 已提交
320 321 322
        let predicates = new dataRdb.RdbPredicates("employee")
        predicates = predicates.inDevices("12345678abcd");
        rdbStore.sync(dataRdb.SyncMode.SYNC_MODE_PUSH, predicates,(err,ret)=>{
L
liangzhenyu123 已提交
323
            console.info(TAG + "sync push success");
Y
yanglifeng1217 已提交
324 325 326
            expect(rdbStore).assertEqual(rdbStore);
        });
        rdbStore.sync(dataRdb.SyncMode.SYNC_MODE_PULL, predicates,(err,ret)=>{
L
liangzhenyu123 已提交
327
            console.info(TAG + "sync push success");
Y
yanglifeng1217 已提交
328 329 330
            expect(rdbStore).assertEqual(rdbStore);
        });
        done();
L
liangzhenyu123 已提交
331
        console.info(TAG + "************* testRdbStoreDistributedCallback0011 end *************");
Y
yanglifeng1217 已提交
332
    })
Y
yanglei1217 已提交
333 334 335 336 337 338 339

    /**
     * @tc.name subscribe test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_012
     * @tc.desc subscribe test
     */
    it('testRdbStoreDistributed0012', 0, async function (done) {
L
liangzhenyu123 已提交
340
        console.info(TAG + "************* testRdbStoreDistributed0012 start *************");
Y
yanglei1217 已提交
341
        rdbStore.on("dataChange", (device) => {
L
liangzhenyu123 已提交
342
            console.info(TAG + device + " dataChange");
Y
yanglei1217 已提交
343
        });
L
liangzhenyu123 已提交
344
        console.info(TAG + "on dataChange success");
Y
yanglei1217 已提交
345 346
        expect(rdbStore).assertEqual(rdbStore);
        done()
L
liangzhenyu123 已提交
347
        console.info(TAG + "************* testRdbStoreDistributed0012 end *************");
Y
yanglei1217 已提交
348 349 350 351 352 353 354 355
    })

    /**
     * @tc.name subscribe test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_013
     * @tc.desc subscribe test
     */
    it('testRdbStoreDistributed0013', 0, async function (done) {
L
liangzhenyu123 已提交
356
        console.info(TAG + "************* testRdbStoreDistributed0013 start *************");
Y
yanglei1217 已提交
357
        rdbStore.off("dataChange", (device) => {
L
liangzhenyu123 已提交
358
            console.info(TAG + device + " dataChange");
Y
yanglei1217 已提交
359
        });
L
liangzhenyu123 已提交
360
        console.info(TAG + "off dataChange success");
Y
yanglei1217 已提交
361 362
        expect(rdbStore).assertEqual(rdbStore);
        done()
L
liangzhenyu123 已提交
363
        console.info(TAG + "************* testRdbStoreDistributed0013 end *************");
Y
yanglei1217 已提交
364
    })
Y
yanglifeng1217 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	
	    /**
     * @tc.name obtainDistributedTableName Callback interface test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_014
     * @tc.desc obtainDistributedTableName test
     */
    it('testRdbStoreDistributed0014', 0, async function (done){
        await rdbStore.obtainDistributedTableName("deviceId", "EMPLOYEE", function (err, tableName) {
            expect(err != null).assertTrue();
            console.info('ObtainDistributedTableName failed, Unauthorized.' + err)
        })
        done();
    })

    /**
     * @tc.name obtainDistributedTableName Promise interface test
     * @tc.number SUB_DDM_AppDataFWK_JSRDB_Distributed_015
     * @tc.desc obtainDistributedTableName test
     */
     it('testRdbStoreDistributed0015',0,async function (done){
        await dataRdb.deleteRdbStore(STORE_NAME);
        const config = {
            "name": STORE_NAME,
        }
        rdbStore = await dataRdb.getRdbStore(config, 1);
        let promise = rdbStore.obtainDistributedTableName("deviceId", "EMPLOYEE")
        promise.then((tableName)=>{
            expect(tableName != "EMPLOYEE").assertTrue();
            console.info('ObtainDistributedTableName')
        }).catch((err)=>{
            expect(null).assertFail();
            console.info('ObtainDistributedTableName failed, Unauthorized.' + err)
        })
        done();
    })
	
L
liangzhenyu123 已提交
401
    console.info(TAG + "*************Unit Test End*************");
Y
yanglei1217 已提交
402
})