代码拉取完成,页面将自动刷新
/*
* Copyright (c) 2025 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.
*/
#include <gtest/gtest.h>
#include "cloud_db_sync_utils_test.h"
#include "distributeddb_data_generate_unit_test.h"
#include "distributeddb_tools_unit_test.h"
#include "get_query_info.h"
#include "rdb_data_generator.h"
#include "relational_store_client.h"
#include "relational_store_manager.h"
#include "relational_virtual_device.h"
#include "virtual_communicator_aggregator.h"
using namespace testing::ext;
using namespace DistributedDB;
using namespace DistributedDBUnitTest;
using namespace std;
namespace {
string g_testDir;
class DistributedDBRelationalMultiUserSyncTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
protected:
static DataBaseSchema GetSchema();
static TableSchema GetTableSchema();
//static void BlockSync(RelationalStoreDelegate &delegate, const DeviceSyncParam ¶m, DBStatus exceptSyncStatus,
//const std::map<DeviceSyncTarget, std::vector<TableStatus>> &exceptStatusMap);
void InitStore(const std::string &storeId, std::string &storePath, sqlite3 *&db);
void InitDelegate(const std::string &storePath, const std::string &storeId, RelationalStoreDelegate *&delegate);
void CloseDb(sqlite3 *&db, RelationalStoreDelegate *delegate);
void GetDevice(RelationalVirtualDevice* &device, const std::string &deviceName,
const std::string &targetUserId, VirtualRelationalVerSyncDBInterface* &storage);
VirtualCommunicatorAggregator *communicatorAggregator_ = nullptr;
static constexpr const char *DEVICE_SYNC_TABLE = "DEVICE_SYNC_TABLE";
static constexpr const char *DEVICE_SYNC_TABLE_UPGRADE = "DEVICE_SYNC_TABLE_UPGRADE";
static constexpr const char *CLOUD_SYNC_TABLE = "CLOUD_SYNC_TABLE";
static constexpr const char *DEVICE_B = "DEVICE_B";
static constexpr const char *DEVICE_C = "DEVICE_C";
static constexpr const char *USER_ID_1 = "userId1";
static constexpr const char *USER_ID_2 = "userId2";
};
void DistributedDBRelationalMultiUserSyncTest::SetUpTestCase()
{
DistributedDBToolsUnitTest::TestDirInit(g_testDir);
if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
LOGE("rm test db files error!");
}
}
void DistributedDBRelationalMultiUserSyncTest::TearDownTestCase()
{
if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
LOGE("rm test db files error!");
}
}
void DistributedDBRelationalMultiUserSyncTest::SetUp()
{
DistributedDBToolsUnitTest::PrintTestCaseInfo();
communicatorAggregator_ = new (std::nothrow) VirtualCommunicatorAggregator();
ASSERT_TRUE(communicatorAggregator_ != nullptr);
RuntimeContext::GetInstance()->SetCommunicatorAggregator(communicatorAggregator_);
}
void DistributedDBRelationalMultiUserSyncTest::TearDown()
{
if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
LOGE("rm test db files error.");
}
RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
communicatorAggregator_ = nullptr;
}
void DistributedDBRelationalMultiUserSyncTest::GetDevice(RelationalVirtualDevice* &device,
const std::string &deviceName, const std::string &targetUserId, VirtualRelationalVerSyncDBInterface* &storage)
{
device = new (std::nothrow) RelationalVirtualDevice(deviceName);
ASSERT_NE(device, nullptr);
storage = new (std::nothrow) VirtualRelationalVerSyncDBInterface();
ASSERT_NE(storage, nullptr);
ASSERT_EQ(device->Initialize(communicatorAggregator_, storage), E_OK);
VirtualCommunicator *communicator = device->GetCommunicator();
communicator->SetTargetUserId(targetUserId);
}
DataBaseSchema DistributedDBRelationalMultiUserSyncTest::GetSchema()
{
DataBaseSchema schema;
auto table = GetTableSchema();
schema.tables.push_back(table);
return schema;
}
TableSchema DistributedDBRelationalMultiUserSyncTest::GetTableSchema()
{
TableSchema tableSchema;
tableSchema.name = DEVICE_SYNC_TABLE;
Field field;
field.primary = true;
field.type = TYPE_INDEX<int64_t>;
field.colName = "pk";
tableSchema.fields.push_back(field);
field.primary = false;
field.colName = "int_field1";
tableSchema.fields.push_back(field);
field.colName = "int_field2";
tableSchema.fields.push_back(field);
field.colName = "123";
field.type = TYPE_INDEX<std::string>;
tableSchema.fields.push_back(field);
return tableSchema;
}
void DistributedDBRelationalMultiUserSyncTest::InitStore(const std::string &storeId, std::string &storePath,
sqlite3 *&db)
{
storePath = g_testDir + "/" + storeId + ".db";
db = RelationalTestUtils::CreateDataBase(storePath);
ASSERT_NE(db, nullptr);
auto schema = GetSchema();
EXPECT_EQ(RDBDataGenerator::InitDatabase(schema, *db), SQLITE_OK);
}
void DistributedDBRelationalMultiUserSyncTest::InitDelegate(const std::string &storePath, const std::string &storeId,
RelationalStoreDelegate *&delegate)
{
RelationalStoreManager mgr(APP_ID, USER_ID);
RelationalStoreDelegate::Option option;
ASSERT_EQ(mgr.OpenStore(storePath, storeId, option, delegate), OK);
ASSERT_NE(delegate, nullptr);
EXPECT_EQ(delegate->CreateDistributedTable(DEVICE_SYNC_TABLE), OK);
}
void DistributedDBRelationalMultiUserSyncTest::CloseDb(sqlite3 *&db, RelationalStoreDelegate *delegate)
{
if (db != nullptr) {
sqlite3_close_v2(db);
db = nullptr;
}
if (delegate != nullptr) {
RelationalStoreManager mgr(APP_ID, USER_ID);
EXPECT_EQ(mgr.CloseStore(delegate), OK);
delegate = nullptr;
}
}
void CheckDataCount(sqlite3 *db, const std::string &tableName, const std::string &deviceName, int expectCount)
{
std::string sql = "select count(*) from " + DBCommon::GetDistributedTableNameWithHash(deviceName, tableName);
int actualCount;
SQLiteUtils::GetCountBySql(db, sql, actualCount);
EXPECT_EQ(actualCount, expectCount);
}
/**
* @tc.name: MultiUserSyncTest001
* @tc.desc: Test sync with userId.
* @tc.type: FUNC
* @tc.require:
* @tc.author: liaoyonghuang
*/
HWTEST_F(DistributedDBRelationalMultiUserSyncTest, MultiUserSyncTest001, TestSize.Level4)
{
/**
* @tc.steps: step1. Create device table
* @tc.expected: step1.ok
*/
RelationalVirtualDevice* device1 = nullptr;
VirtualRelationalVerSyncDBInterface* storage1 = nullptr;
GetDevice(device1, DEVICE_B, USER_ID_1, storage1);
ASSERT_NE(device1, nullptr);
ASSERT_NE(storage1, nullptr);
std::string storePath1;
sqlite3 *db1 = nullptr;
InitStore(STORE_ID_1, storePath1, db1);
ASSERT_NE(db1, nullptr);
ASSERT_FALSE(storePath1.empty());
int dataCount1 = 10;
ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount1, device1, GetTableSchema()), E_OK);
ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(DEVICE_SYNC_TABLE, db1, device1), E_OK);
RelationalStoreDelegate *delegate1 = nullptr;
InitDelegate(storePath1, STORE_ID_1, delegate1);
Query query = Query::Select(DEVICE_SYNC_TABLE);
DistributedDBToolsUnitTest::BlockSync(*delegate1, query, SYNC_MODE_PULL_ONLY, OK, {DEVICE_B});
CheckDataCount(db1, DEVICE_SYNC_TABLE, DEVICE_B, dataCount1);
delete device1;
device1 = nullptr;
RelationalStoreManager mgr1(APP_ID, USER_ID);
EXPECT_EQ(mgr1.CloseStore(delegate1), OK);
delegate1 = nullptr;
/**
* @tc.steps: step2. Insert one data
* @tc.expected: step2.ok
*/
RelationalVirtualDevice* device2 = nullptr;
VirtualRelationalVerSyncDBInterface* storage2 = nullptr;
GetDevice(device2, DEVICE_B, USER_ID_2, storage2);
ASSERT_NE(device2, nullptr);
ASSERT_NE(storage2, nullptr);
std::string storePath2;
sqlite3 *db2 = nullptr;
InitStore(STORE_ID_2, storePath2, db2);
ASSERT_NE(db2, nullptr);
ASSERT_FALSE(storePath2.empty());
int dataCount2 = 20;
ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(dataCount1, dataCount2, device2, GetTableSchema()), E_OK);
ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(DEVICE_SYNC_TABLE, db2, device2), E_OK);
RelationalStoreDelegate *delegate2 = nullptr;
InitDelegate(storePath2, STORE_ID_2, delegate2);
DistributedDBToolsUnitTest::BlockSync(*delegate2, query, SYNC_MODE_PULL_ONLY, OK, {DEVICE_B});
CheckDataCount(db1, DEVICE_SYNC_TABLE, DEVICE_B, dataCount2);
delete device2;
device2 = nullptr;
RelationalStoreManager mgr2(APP_ID, USER_ID);
EXPECT_EQ(mgr2.CloseStore(delegate2), OK);
delegate2 = nullptr;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。