From 27aa8c81c7f1eb1f412e6de2dc566a5906baee7a Mon Sep 17 00:00:00 2001 From: lobty Date: Sun, 11 May 2025 11:24:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=87=BD=E6=95=B0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=88=86=E6=94=AF=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lobty --- ...uteddb_cloud_interfaces_reference_test.cpp | 33 +++++++ ...b_cloud_interfaces_relational_ext_test.cpp | 41 ++++++++ ...teddb_relational_syncable_storage_test.cpp | 99 +++++++++++++++++++ 3 files changed, 173 insertions(+) diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_reference_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_reference_test.cpp index 030b967fec3..333066a2bfe 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_reference_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_reference_test.cpp @@ -20,6 +20,7 @@ #include "db_constant.h" #include "distributeddb_data_generate_unit_test.h" #include "distributeddb_tools_unit_test.h" +#include "relational_store_delegate_impl.h" #include "relational_store_manager.h" using namespace testing::ext; @@ -884,4 +885,36 @@ namespace { tableReferenceProperty.columns = columns; EXPECT_EQ(g_delegate->SetReference({tableReferenceProperty}), PROPERTY_CHANGED); } + + /** + * @tc.name: FuncExceptionTest001 + * @tc.desc: Test the interception expection of the delegate interface when the conn is empty. + * @tc.type: FUNC + * @tc.require: + * @tc.author: bty + */ + HWTEST_F(DistributedDBCloudInterfacesReferenceTest, FuncExceptionTest001, TestSize.Level1) + { + RelationalStoreConnection *iConn = nullptr; + std::string iPath = ""; + auto iDelegate = std::make_shared(iConn, iPath); + EXPECT_EQ(iDelegate->SetStoreConfig({}), OK); + EXPECT_EQ(iDelegate->SetStoreConfig({DistributedTableMode::COLLABORATION}), DB_ERROR); + + ClearMetaDataOption iOption; + iOption.tableNameList.insert(STORE_ID); + #ifdef USE_DISTRIBUTEDDB_CLOUD + EXPECT_EQ(iDelegate->ClearMetaData(iOption), NOT_SUPPORT); + iOption.tableNameList.clear(); + EXPECT_EQ(iDelegate->ClearMetaData(iOption), DB_ERROR); + #endif + iOption.mode = ClearMetaDataMode::BUTT; + EXPECT_EQ(iDelegate->ClearMetaData(iOption), INVALID_ARGS); + + EXPECT_EQ(iDelegate->GetDownloadingAssetsCount().first, DB_ERROR); + EXPECT_EQ(iDelegate->SetDistributedSchema({}, false), DB_ERROR); + + iDelegate->SetReleaseFlag(false); + iDelegate = nullptr; + } } diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_relational_ext_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_relational_ext_test.cpp index 2faacb653eb..10738f88e13 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_relational_ext_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_cloud_interfaces_relational_ext_test.cpp @@ -1784,6 +1784,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalExtTest, RegisterStoreObserverTes begin = 20; // 21 is begin id PrepareDataForStoreObserver(db, tableName, begin, dataCounts); EXPECT_TRUE(g_changedData.empty()); + UnregisterDbHook(db); EXPECT_EQ(sqlite3_close_v2(db), E_OK); } @@ -1912,4 +1913,44 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalExtTest, AbnormalDelegateImplTest EXPECT_EQ(g_mgr.CloseStore(delegate), OK); delegate = nullptr; } + +/** + * @tc.name: FuncExceptionTest001 + * @tc.desc: Test the interception expection of the interface + * @tc.type: FUNC + * @tc.require: + * @tc.author: bty + */ +HWTEST_F(DistributedDBCloudInterfacesRelationalExtTest, FuncExceptionTest001, TestSize.Level1) +{ + std::shared_ptr iStoreObserver = nullptr; + EXPECT_EQ(RegisterStoreObserver(nullptr, iStoreObserver), INVALID_ARGS); + EXPECT_EQ(UnregisterStoreObserver(nullptr, iStoreObserver), INVALID_ARGS); + iStoreObserver = std::make_shared(); + EXPECT_EQ(RegisterStoreObserver(nullptr, iStoreObserver), INVALID_ARGS); + EXPECT_EQ(UnregisterStoreObserver(nullptr, iStoreObserver), INVALID_ARGS); + + EXPECT_EQ(RelationalStoreManager::CalcPrimaryKeyHash({}, {}).size(), 0u); + std::map pkMap1 = {{"fielddInt", 0L}}; + std::map ctMap1 = {{"fielddInt", CollateType::COLLATE_BUTT}}; + std::vector result = RelationalStoreManager::CalcPrimaryKeyHash(pkMap1, ctMap1); + EXPECT_TRUE(result.empty()); + + std::map pkMap2 = {{"FIELDINT", 0L}, {"FIELDSTR", std::string("FIELDSTR")}}; + std::map ctMap2; + ctMap2.insert_or_assign("fieldint", CollateType::COLLATE_BUTT); + ctMap2.insert_or_assign("fieldstr", CollateType::COLLATE_BUTT); + result = RelationalStoreManager::CalcPrimaryKeyHash(pkMap2, ctMap2); + EXPECT_TRUE(result.empty()); + + ctMap2.insert_or_assign("fieldint", CollateType::COLLATE_NOCASE); + ctMap2.insert_or_assign("fieldstr", CollateType::COLLATE_NOCASE); + result = RelationalStoreManager::CalcPrimaryKeyHash(pkMap2, ctMap2); + EXPECT_FALSE(result.empty()); + + std::map entries2; + pkMap2.insert_or_assign("FIELDINT", entries2); + result = RelationalStoreManager::CalcPrimaryKeyHash(pkMap2, ctMap2); + EXPECT_TRUE(result.empty()); +} } diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_syncable_storage_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_syncable_storage_test.cpp index f6ebd0ae3cc..6d943a93e97 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_syncable_storage_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_relational_syncable_storage_test.cpp @@ -19,6 +19,8 @@ #include "distributeddb_tools_unit_test.h" #include "relational_sync_able_storage.h" #include "sqlite_single_relational_storage_engine.h" +#include "sqlite_relational_store_connection.h" +#include "sqlite_relational_utils.h" #include "sqlite_utils.h" using namespace testing::ext; @@ -120,4 +122,101 @@ HWTEST_F(DistributedDBRelationalSyncableStorageTest, SchemaRefTest001, TestSize. RefObject::DecObjRef(syncAbleStorage); EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); } + +/** + * @tc.name: FuncExceptionTest001 + * @tc.desc: Test the interception expection of the delegate interface when the store is empty. + * @tc.type: FUNC + * @tc.require: + * @tc.author: bty + */ +HWTEST_F(DistributedDBRelationalSyncableStorageTest, FuncExceptionTest001, TestSize.Level1) +{ + auto conn = std::make_shared(nullptr); + EXPECT_EQ(conn->Close(), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->GetIdentifier().size(), 0u); + EXPECT_EQ(conn->CreateDistributedTable("", {}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->RemoveDeviceData(), -E_INVALID_CONNECTION); + +#ifdef USE_DISTRIBUTEDB_CLOUD + EXPECT_EQ(conn->GetCloudSyncTaskCount(), -1); + EXPECT_EQ(conn->DoClean({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->ClearCloudWatermark({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetCloudDB({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->PrepareAndSetCloudDbSchema({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetIAssetLoader({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetCloudSyncConfig({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->Sync({}, {}, 0L), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->GetCloudTaskStatus(0u).errCode, DB_ERROR); +#endif + EXPECT_EQ(conn->RemoveDeviceData({}), -E_INVALID_CONNECTION); + std::vector devices; + Query query; + RelationalStoreConnection::SyncInfo syncInfo{devices, {}, nullptr, query, true}; + EXPECT_EQ(conn->SyncToDevice(syncInfo), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->RegisterLifeCycleCallback({}), -E_INVALID_CONNECTION); + std::shared_ptr resultSet = nullptr; + EXPECT_EQ(conn->RemoteQuery({}, {}, 0u, resultSet), -E_INVALID_CONNECTION); + std::string userId; + std::string appId; + std::string storeId; + EXPECT_EQ(conn->GetStoreInfo(userId, appId, storeId), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetTrackerTable({}), -E_INVALID_CONNECTION); + std::vector records; + EXPECT_EQ(conn->ExecuteSql({}, records), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetReference({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->CleanTrackerData({}, 0L), -E_INVALID_CONNECTION); + PragmaData pragmaData; + PragmaCmd cmd = AUTO_SYNC; + EXPECT_EQ(conn->Pragma(cmd, pragmaData), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->UpsertData({}, {}, {}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetDistributedDbSchema({}, false), -E_INVALID_CONNECTION); + int32_t count; + EXPECT_EQ(conn->GetDownloadingAssetsCount(count), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->SetTableMode({}), -E_INVALID_CONNECTION); + EXPECT_EQ(conn->OperateDataStatus(0u), -E_INVALID_CONNECTION); + conn = nullptr; +} + +/** + * @tc.name: FuncExceptionTest002 + * @tc.desc: Test the interception expection of the delegate interface when the store is empty. + * @tc.type: FUNC + * @tc.require: + * @tc.author: bty + */ +HWTEST_F(DistributedDBRelationalSyncableStorageTest, FuncExceptionTest002, TestSize.Level1) +{ + sqlite3 *db = nullptr; + int64_t timeOffset; + EXPECT_EQ(SQLiteRelationalUtils::GetExistedDataTimeOffset(db, {}, false, timeOffset), -E_INVALID_DB); + std::unique_ptr logMgrPtr = nullptr; + SQLiteRelationalUtils::GenLogParam param; + param.db = db; + EXPECT_EQ(SQLiteRelationalUtils::GeneLogInfoForExistedData({}, {}, logMgrPtr, param), -E_INVALID_DB); + EXPECT_EQ(SQLiteRelationalUtils::SetLogTriggerStatus(db, false), -E_INVALID_DB); + EXPECT_EQ(SQLiteRelationalUtils::InitCursorToMeta(db, false, {}), -E_INVALID_DB); + EXPECT_EQ(SQLiteRelationalUtils::PutKvData(db, false, {}, {}), -E_INVALID_DB); + Value value; + EXPECT_EQ(SQLiteRelationalUtils::GetKvData(db, false, {}, value), -E_INVALID_DB); + EXPECT_EQ(SQLiteRelationalUtils::CreateRelationalMetaTable(db), -E_INVALID_DB); + EXPECT_EQ(SQLiteRelationalUtils::GetCurrentVirtualTime(db).first, -E_INVALID_DB); + int64_t tOffset; + EXPECT_EQ(SQLiteRelationalUtils::GetMetaLocalTimeOffset(db, tOffset), -E_INVALID_DB); + uint64_t cursor; + EXPECT_EQ(SQLiteRelationalUtils::GetCursor(db, {}, cursor), -E_INVALID_DB); + int64_t count; + EXPECT_EQ(SQLiteRelationalUtils::QueryCount(db, {}, count), -E_INVALID_DB); + + LogInfo logInfo; + EXPECT_EQ(SQLiteRelationalUtils::GetLogInfoPre(nullptr, {}, {}, logInfo), -E_INVALID_ARGS); + DistributedSchema schema; + schema.tables.push_back({STORE_ID, {}}); + schema.tables.push_back({STORE_ID, {}}); + EXPECT_NE(SQLiteRelationalUtils::FilterRepeatDefine(schema).tables.size(), 0u); + EXPECT_EQ(SQLiteRelationalUtils::CheckDistributedSchemaValid({}, {}, false, nullptr), -E_INVALID_ARGS); + auto executor = + std::make_shared(db, true, DistributedTableMode::COLLABORATION); + EXPECT_EQ(SQLiteRelationalUtils::CheckDistributedSchemaValid({}, {}, false, executor.get()), -E_NOT_FOUND); +} #endif // RELATIONAL_STORE -- Gitee