From 126238c68692bf7168c976a77f99fbe401e4088b Mon Sep 17 00:00:00 2001 From: yuxiaoya2 Date: Wed, 21 Jun 2023 14:51:26 +0800 Subject: [PATCH] =?UTF-8?q?relationalStoreJstest=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yuxiaoya2 Change-Id: I4f9072333fedf11c0d6dd2cb20ce182c3046fbd6 --- .../hap/src/main/js/test/List.test.js | 2 + ...elationalStoreAssetResultSetJsunit.test.js | 536 ++++++++++++++++++ 2 files changed, 538 insertions(+) create mode 100644 distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/RelationalStoreAssetResultSetJsunit.test.js diff --git a/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/List.test.js b/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/List.test.js index c725607a4..76d22e5e4 100644 --- a/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/List.test.js +++ b/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/List.test.js @@ -28,6 +28,7 @@ import relationalStoreEncryptionTest from './RelationalStoreEncryptionJsunit.tes import relationalStorePredicatesComplexFiledTest from './RelationalStorePredicatesComplexFiledJsunit.test.js' import relationalStoreBackupRestoreWithFAContextTest from './RelationalStoreBackupRestoreWithFAContextJsunit.test.js' import relationalStoreBackupRestoreCallbackTest from './RelationalStoreBackupRestoreCallbackJsunit.test.js' +import relationalStoreAssetResultSetTest from './RelationalStoreAssetResultSetJsunit.test.js' export default function testsuite() { relationalStoreDeleteTest() relationalStoreDistributedTest() @@ -44,4 +45,5 @@ export default function testsuite() { relationalStorePredicatesComplexFiledTest() relationalStoreBackupRestoreWithFAContextTest() relationalStoreBackupRestoreCallbackTest() + relationalStoreAssetResultSetTest() } diff --git a/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/RelationalStoreAssetResultSetJsunit.test.js b/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/RelationalStoreAssetResultSetJsunit.test.js new file mode 100644 index 000000000..7ed1f4961 --- /dev/null +++ b/distributeddatamgr/relationalStoretest/relationalStoreJstest/hap/src/main/js/test/RelationalStoreAssetResultSetJsunit.test.js @@ -0,0 +1,536 @@ +/* + * Copyright (C) 2023 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 '@ohos/hypium'; +import data_relationalStore from '@ohos.data.relationalStore' +import ability_featureAbility from '@ohos.ability.featureAbility' + + +var context = ability_featureAbility.getContext(); +// var contextApplication = context.getApplicationContext(); +const TAG = "[RelationalStore_JSKITS_TEST]"; +const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + + "data1 asset, " + "data2 asset, " + "data3 assets )"; +const DROP_TABLE_TEST = "DROP TABLE IF EXISTS test"; + +const STORE_CONFIG = { + name: "Asset.db", + securityLevel: data_relationalStore.SecurityLevel.S1, +} +const asset1 = { + name: "name1", + uri: "uri1", + createTime: "createTime1", + modifyTime: "modifyTime1", + size: "size1", + path: "path1", + status: 1, +} +const asset2 = { + name: "name2", + uri: "uri2", + createTime: "createTime2", + modifyTime: "modifyTime2", + size: "size2", + path: "path2", + status: 2, +} +const asset3 = { + name: "name3", + uri: "uri3", + createTime: "createTime3", + modifyTime: "modifyTime3", + size: "size3", + path: "path3", + status: 3, +} +const asset4 = { + name: "name4", + uri: "uri4", + createTime: "createTime4", + modifyTime: "modifyTime4", + size: "size4", + path: "path4", +} +var rdbStore = undefined; + +export default function relationalStoreAssetResultSetTest() { + describe('relationalStoreAssetResultSetTest', function () { + beforeAll(async function () { + console.info(TAG + 'beforeAll') + rdbStore = await data_relationalStore.getRdbStore(context, STORE_CONFIG); + await rdbStore.executeSql(CREATE_TABLE_TEST, null); + }) + + beforeEach(async function () { + await rdbStore.executeSql(CREATE_TABLE_TEST, null); + console.info(TAG + 'beforeEach'); + }) + + afterEach(async function () { + await rdbStore.executeSql(DROP_TABLE_TEST, null); + console.info(TAG + 'afterEach') + }) + + afterAll(async function () { + console.info(TAG + 'afterAll') + rdbStore = null + await data_relationalStore.deleteRdbStore(context, "Asset.db"); + }) + + // insert data + async function createTest() { + console.log(TAG + "createTest data start"); + const assets1 = [asset1]; + const assets2 = [asset1, asset2, asset3]; + let valuesBucket = { + "data1": asset1, + "data2": asset2, + "data3": assets1, + } + await rdbStore.insert("test", valuesBucket) + valuesBucket = { + "data1": asset2, + "data2": asset3, + "data3": assets2, + } + await rdbStore.insert("test", valuesBucket) + valuesBucket = { + "data1": asset1, + } + await rdbStore.insert("test", valuesBucket) + console.log(TAG + "createTest data end"); + } + + async function createErrorTest() { + console.log(TAG + "createErrorTest data start"); + let valuesBucket = { + "data1": asset4, + } + await rdbStore.insert("test", valuesBucket) + console.log(TAG + "createErrorTest data end"); + } + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0010 + * @tc.desc resultSet getAsset normal test + */ + it('testGetAsset0001', 0, async function (done) { + await createTest(); + console.log(TAG + "************* testGetAsset0001 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data1 = resultSet.getAsset(resultSet.getColumnIndex("data1")) + console.log(TAG + "id=" + id + ", data1=" + data1); + expect("name1").assertEqual(data1.name); + expect("uri1").assertEqual(data1.uri); + expect("createTime1").assertEqual(data1.createTime); + expect("modifyTime1").assertEqual(data1.modifyTime); + expect("size1").assertEqual(data1.size); + expect("path1").assertEqual(data1.path); + expect(1).assertEqual(data1.status); + + const data2 = resultSet.getAsset(resultSet.getColumnIndex("data2")) + console.log(TAG + "id=" + id + ", data2=" + data2); + expect("name2").assertEqual(data2.name); + expect("uri2").assertEqual(data2.uri); + expect("createTime2").assertEqual(data2.createTime); + expect("modifyTime2").assertEqual(data2.modifyTime); + expect("size2").assertEqual(data2.size); + expect("path2").assertEqual(data2.path); + expect(1).assertEqual(data2.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testGetAsset0001 end *************"); + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0020 + * @tc.desc resultSet getAsset normal test + */ + it('testGetAsset0002', 0, async function (done) { + await createTest(); + console.log(TAG + "************* testGetAsset0002 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + expect(true).assertEqual(resultSet.goToNextRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data1 = resultSet.getAsset(resultSet.getColumnIndex("data1")) + console.log(TAG + "id=" + id + ", data1=" + data1); + expect("name2").assertEqual(data1.name); + expect("uri2").assertEqual(data1.uri); + expect("createTime2").assertEqual(data1.createTime); + expect("modifyTime2").assertEqual(data1.modifyTime); + expect("size2").assertEqual(data1.size); + expect("path2").assertEqual(data1.path); + expect(1).assertEqual(data1.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testGetAsset0002 end *************"); + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0030 + * @tc.desc resultSet getAsset normal test + */ + it('testGetAsset0003', 0, async function (done) { + await createTest(); + console.log(TAG + "************* testGetAsset0003 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + expect(true).assertEqual(resultSet.goToNextRow()) + expect(true).assertEqual(resultSet.goToNextRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data2 = resultSet.getAsset(resultSet.getColumnIndex("data2")) + expect(null).assertFail(); + } catch (e) { + console.log(TAG + "testGetAsset0003 throw error: " + e); + expect(true).assetTrue(); + } finally { + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + resultSet = null + done(); + console.log(TAG + "************* testGetAsset0003 end *************"); + } + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0010 + * @tc.desc resultSet getAsset normal test + */ + it('testAssetNoStatus0010', 0, async function (done) { + console.log(TAG + "************* testAssetNoStatus0010 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data1 = resultSet.getAsset(resultSet.getColumnIndex("data1")) + console.log(TAG + "id=" + id + ", data1=" + data1); + expect("name4").assertEqual(data1.name); + expect("uri4").assertEqual(data1.uri); + expect("createTime4").assertEqual(data1.createTime); + expect("modifyTime4").assertEqual(data1.modifyTime); + expect("size4").assertEqual(data1.size); + expect("path4").assertEqual(data1.path); + expect(0).assertEqual(data1.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testAssetNoStatus0010 end *************"); + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0000 + * @tc.desc resultSet getAsset normal test + */ + it('testAssetStatusUndefined0010', 0, async function (done) { + console.log(TAG + "************* testAssetStatusUndefined0010 start *************"); + const asset = { + name: "name4", + uri: "uri4", + createTime: "createTime4", + modifyTime: "modifyTime4", + size: "size4", + path: "path4", + status: undefined, + } + let valuesBucket = { + "data1": asset, + } + + await rdbStore.insert("test", valuesBucket) + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data1 = resultSet.getAsset(resultSet.getColumnIndex("data1")) + console.log(TAG + "id=" + id + ", data1=" + data1); + expect("name4").assertEqual(data1.name); + expect("uri4").assertEqual(data1.uri); + expect("createTime4").assertEqual(data1.createTime); + expect("modifyTime4").assertEqual(data1.modifyTime); + expect("size4").assertEqual(data1.size); + expect("path4").assertEqual(data1.path); + expect(0).assertEqual(data1.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testAssetStatusUndefined0010 end *************"); + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0000 + * @tc.desc resultSet getAsset normal test + */ + it('testAssetStatusNull0010', 0, async function (done) { + console.log(TAG + "************* testAssetStatusNull0010 start *************"); + const asset = { + name: "name4", + uri: "uri4", + createTime: "createTime4", + modifyTime: "modifyTime4", + size: "size4", + path: "path4", + status: null, + } + let valuesBucket = { + "data1": asset, + } + + await rdbStore.insert("test", valuesBucket) + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data1 = resultSet.getAsset(resultSet.getColumnIndex("data1")) + console.log(TAG + "id=" + id + ", data1=" + data1); + expect("name4").assertEqual(data1.name); + expect("uri4").assertEqual(data1.uri); + expect("createTime4").assertEqual(data1.createTime); + expect("modifyTime4").assertEqual(data1.modifyTime); + expect("size4").assertEqual(data1.size); + expect("path4").assertEqual(data1.path); + expect(0).assertEqual(data1.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testAssetStatusNull0010 end *************"); + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0040 + * @tc.desc resultSet getAsset normal test + */ + it('testGetAssetUndefined', 0, async function (done) { + console.log(TAG + "************* testGetAssetUndefined start *************"); + const asset = { + name: undefined, + uri: undefined, + createTime: "createTime", + modifyTime: "modifyTime", + size: "size", + path: "path", + } + try { + let valuesBucket = { + "data1": asset, + } + await rdbStore.insert("test", valuesBucket); + console.error(TAG + "testGetAssetUndefined should not come here"); + expect(null).assertFail(); + done(); + } catch (e) { + console.log(TAG + "testGetAssetUndefined get error" + JSON.stringify(e)); + expect(14800000).assertEqual(e.code) + done(); + } + }) + + /** + * @tc.name resultSet getAsset normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0050 + * @tc.desc resultSet getAsset normal test + */ + it('testGetAssetNull', 0, async function (done) { + console.log(TAG + "************* testGetAssetNull start *************"); + const asset = { + name: null, + uri: null, + createTime: "createTime", + modifyTime: "modifyTime", + size: "size", + path: "path", + } + try { + let valuesBucket = { + "data1": asset, + } + await rdbStore.insert("test", valuesBucket); + console.error(TAG + "testGetAssetNull should not come here"); + expect(null).assertFail(); + done(); + } catch (e) { + console.log(TAG + "testGetAssetNull get error" + JSON.stringify(e)); + expect(14800000).assertEqual(e.code) + done(); + } + }) + + /** + * @tc.name resultSet getAssets normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0010 + * @tc.desc resultSet getAssets normal test + */ + it('testGetAssets0001', 0, async function (done) { + await createTest(); + console.log(TAG + "************* testGetAssets0001 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data3 = resultSet.getAssets(resultSet.getColumnIndex("data3")) + console.log(TAG + "id=" + id + ", data3=" + data3); + expect(1).assertEqual(data3.length); + let asset = data3[0]; + expect("name1").assertEqual(asset.name); + expect("uri1").assertEqual(asset.uri); + expect("createTime1").assertEqual(asset.createTime); + expect("modifyTime1").assertEqual(asset.modifyTime); + expect("size1").assertEqual(asset.size); + expect("path1").assertEqual(asset.path); + expect(1).assertEqual(asset.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testGetAssets0001 end *************"); + }) + + /** + * @tc.name resultSet getAssets normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0020 + * @tc.desc resultSet getAssets normal test + */ + it('testGetAssets0002', 0, async function (done) { + await createTest(); + console.log(TAG + "************* testGetAssets0002 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + expect(true).assertEqual(resultSet.goToNextRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data3 = resultSet.getAssets(resultSet.getColumnIndex("data3")) + console.log(TAG + "id=" + id + ", data3=" + data3); + expect(3).assertEqual(data3.length); + let asset = data3[0]; + expect("name1").assertEqual(asset.name); + expect("uri1").assertEqual(asset.uri); + expect("createTime1").assertEqual(asset.createTime); + expect("modifyTime1").assertEqual(asset.modifyTime); + expect("size1").assertEqual(asset.size); + expect("path1").assertEqual(asset.path); + expect(1).assertEqual(asset.status); + + asset = data3[1]; + expect("name2").assertEqual(asset.name); + expect("uri2").assertEqual(asset.uri); + expect("createTime2").assertEqual(asset.createTime); + expect("modifyTime2").assertEqual(asset.modifyTime); + expect("size2").assertEqual(asset.size); + expect("path2").assertEqual(asset.path); + expect(1).assertEqual(asset.status); + + asset = data3[2]; + expect("name3").assertEqual(asset.name); + expect("uri3").assertEqual(asset.uri); + expect("createTime3").assertEqual(asset.createTime); + expect("modifyTime3").assertEqual(asset.modifyTime); + expect("size3").assertEqual(asset.size); + expect("path3").assertEqual(asset.path); + expect(3).assertEqual(asset.status); + + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + } catch (e) { + expect(null).assertFail(); + } + resultSet = null + done(); + console.log(TAG + "************* testGetAssets0002 end *************"); + }) + + /** + * @tc.name resultSet getAssets normal test + * @tc.number SUB_DDM_AppDataFWK_JSRDB_ResultSet_0030 + * @tc.desc resultSet getAssets normal test + */ + it('testGetAssets0003', 0, async function (done) { + await createTest(); + console.log(TAG + "************* testGetAssets0003 start *************"); + let predicates = await new data_relationalStore.RdbPredicates("test") + let resultSet = await rdbStore.query(predicates) + try { + expect(true).assertEqual(resultSet.goToFirstRow()) + expect(true).assertEqual(resultSet.goToNextRow()) + expect(true).assertEqual(resultSet.goToNextRow()) + const id = resultSet.getLong(resultSet.getColumnIndex("id")) + const data3 = resultSet.getAssets(resultSet.getColumnIndex("data3")) + expect(null).assertFail(); + } catch (e) { + console.log(TAG + "testGetAssets0003 throw error" + e); + expect(true).assetTrue(); + } finally { + resultSet.close(); + expect(true).assertEqual(resultSet.isClosed) + resultSet = null + done(); + console.log(TAG + "************* testGetAssets0003 end *************"); + } + }) + console.info(TAG + "*************Unit Test End*************") + }) +} + -- GitLab