diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp index ba56dfc6aa656e48feaa85a92664d155ad6b69e0..37874142cf48f78fe2632d611a759a4ed577c396 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp @@ -2497,7 +2497,7 @@ int SQLiteSingleVerNaturalStore::TryHandle() const return E_OK; } -int SQLiteSingleVerNaturalStore::GetAndResizeLocalIdentity(std::string &outTarget) +int SQLiteSingleVerNaturalStore::GetAndResizeLocalIdentity(std::string &outTarget) const { int errCode = GetLocalIdentity(outTarget); if (errCode == -E_NOT_INIT) { diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h index 42a4271ab0ec6b5762b3402d418b6ac44b669f6c..864cf0ed67158de7a42cc23a022a7c51c87abad0 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h @@ -286,7 +286,7 @@ private: int RemoveDeviceDataInner(const std::string &hashDev, bool isNeedNotify); - int GetAndResizeLocalIdentity(std::string &outTarget); + int GetAndResizeLocalIdentity(std::string &outTarget) const; DECLARE_OBJECT_TAG(SQLiteSingleVerNaturalStore); diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp index 4dea7daf1c7757749afed7f66b9367b0116e8bcd..1aa3ee12d254e493aca781bfa3462c075c19e404 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.cpp @@ -318,7 +318,7 @@ int SyncAbleKvDB::EnableManualSync(void) return syncer_.EnableManualSync(); } -int SyncAbleKvDB::GetLocalIdentity(std::string &outTarget) +int SyncAbleKvDB::GetLocalIdentity(std::string &outTarget) const { return syncer_.GetLocalIdentity(outTarget); } diff --git a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h index c5e08092dceafd1ad5ea55a08c17f02127075010..2b4c079a297f286c4920728bee3aac61f4062774 100644 --- a/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h +++ b/frameworks/libs/distributeddb/storage/src/sync_able_kvdb.h @@ -114,7 +114,7 @@ protected: // Get the dataItem's append length, the append length = after-serialized-len - original-dataItem-len uint32_t GetAppendedLen() const; - int GetLocalIdentity(std::string &outTarget); + int GetLocalIdentity(std::string &outTarget) const; void TriggerSync(int notifyEvent); diff --git a/frameworks/libs/distributeddb/test/BUILD.gn b/frameworks/libs/distributeddb/test/BUILD.gn index b388fdf747b248682f45de57ec6b61df2b596beb..0b6f3588554e11a49e3333e412ff074e019edb40 100644 --- a/frameworks/libs/distributeddb/test/BUILD.gn +++ b/frameworks/libs/distributeddb/test/BUILD.gn @@ -246,8 +246,8 @@ ohos_source_set("src_file") { "../syncer/src/cloud/cloud_force_pull_strategy.cpp", "../syncer/src/cloud/cloud_force_push_strategy.cpp", "../syncer/src/cloud/cloud_merge_strategy.cpp", - "../syncer/src/cloud/cloud_syncer.cpp", "../syncer/src/cloud/cloud_sync_utils.cpp", + "../syncer/src/cloud/cloud_syncer.cpp", "../syncer/src/cloud/strategy_factory.cpp", "../syncer/src/commit_history_sync.cpp", "../syncer/src/communicator_proxy.cpp", @@ -393,7 +393,10 @@ distributeddb_unittest("DistributedDBInterfacesImportAndExportTest") { } distributeddb_unittest("DistributedDBCloudInterfacesRelationalSyncTest") { - sources = [ "unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_test.cpp" ] + sources = [ + "unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_test.cpp", + "unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.cpp", + ] } distributeddb_unittest("DistributedDBCloudSyncerProgressManagerTest") { diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_test.cpp index 23601e0717e848fc86a3f4609ca052ddb6351450..f4036021ab442366fd642b3a947dc500761f7711 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_test.cpp @@ -13,1108 +13,13 @@ * limitations under the License. */ #ifdef RELATIONAL_STORE -#include -#include -#include "cloud/cloud_storage_utils.h" -#include "cloud_db_constant.h" -#include "distributeddb_data_generate_unit_test.h" -#include "distributeddb_tools_unit_test.h" -#include "process_system_api_adapter_impl.h" -#include "relational_store_instance.h" -#include "relational_store_manager.h" -#include "runtime_config.h" -#include "sqlite_relational_store.h" -#include "sqlite_relational_utils.h" -#include "store_observer.h" -#include "time_helper.h" -#include "virtual_asset_loader.h" -#include "virtual_cloud_data_translate.h" -#include "virtual_cloud_db.h" -#include "mock_asset_loader.h" - +#include "distributeddb_cloud_interfaces_relational_sync_testcase.h" using namespace testing::ext; -using namespace DistributedDB; using namespace DistributedDBUnitTest; using namespace std; -namespace { - string g_storeID = "Relational_Store_SYNC"; - const string g_tableName1 = "worker1"; - const string g_tableName2 = "worker2"; - const string g_tableName3 = "worker3"; - const string g_tableName4 = "worker4"; - const string DEVICE_CLOUD = "cloud_dev"; - const string DB_SUFFIX = ".db"; - const int64_t g_syncWaitTime = 10; - const int g_arrayHalfSub = 2; - int g_syncIndex = 0; - string g_testDir; - string g_storePath; - std::mutex g_processMutex; - std::condition_variable g_processCondition; - std::shared_ptr g_virtualCloudDb; - std::shared_ptr g_virtualAssetLoader; - DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID); - RelationalStoreObserverUnitTest *g_observer = nullptr; - RelationalStoreDelegate *g_delegate = nullptr; - SyncProcess g_syncProcess; - using CloudSyncStatusCallback = std::function &onProcess)>; - const std::string CREATE_LOCAL_TABLE_SQL = - "CREATE TABLE IF NOT EXISTS " + g_tableName1 + "(" \ - "name TEXT PRIMARY KEY," \ - "height REAL ," \ - "married BOOLEAN ," \ - "photo BLOB NOT NULL," \ - "assert BLOB," \ - "age INT);"; - const std::string INTEGER_PRIMARY_KEY_TABLE_SQL = - "CREATE TABLE IF NOT EXISTS " + g_tableName2 + "(" \ - "id INTEGER PRIMARY KEY," \ - "name TEXT ," \ - "height REAL ," \ - "photo BLOB ," \ - "asserts BLOB," \ - "age INT);"; - const std::string CREATE_LOCAL_TABLE_WITHOUT_PRIMARY_KEY_SQL = - "CREATE TABLE IF NOT EXISTS " + g_tableName3 + "(" \ - "name TEXT," \ - "height REAL ," \ - "married BOOLEAN ," \ - "photo BLOB NOT NULL," \ - "assert BLOB," \ - "age INT);"; - const std::string INTEGER_PRIMARY_KEY_TABLE_SQL_WRONG_SYNC_MODE = - "CREATE TABLE IF NOT EXISTS " + g_tableName4 + "(" \ - "id INTEGER PRIMARY KEY," \ - "name TEXT ," \ - "height REAL ," \ - "photo BLOB ," \ - "asserts BLOB," \ - "age INT);"; - const std::vector g_cloudFiled1 = { - {"name", TYPE_INDEX, true}, {"height", TYPE_INDEX}, - {"married", TYPE_INDEX}, {"photo", TYPE_INDEX, false, false}, - {"assert", TYPE_INDEX}, {"age", TYPE_INDEX} - }; - const std::vector g_invalidCloudFiled1 = { - {"name", TYPE_INDEX, true}, {"height", TYPE_INDEX}, - {"married", TYPE_INDEX}, {"photo", TYPE_INDEX, false, false}, - {"assert", TYPE_INDEX}, {"age", TYPE_INDEX} - }; - const std::vector g_cloudFiled2 = { - {"id", TYPE_INDEX, true}, {"name", TYPE_INDEX}, - {"height", TYPE_INDEX}, {"photo", TYPE_INDEX}, - {"asserts", TYPE_INDEX}, {"age", TYPE_INDEX} - }; - const std::vector g_cloudFiledWithOutPrimaryKey3 = { - {"name", TYPE_INDEX, false, true}, {"height", TYPE_INDEX}, - {"married", TYPE_INDEX}, {"photo", TYPE_INDEX, false, false}, - {"assert", TYPE_INDEX}, {"age", TYPE_INDEX} - }; - const std::vector g_tables = {g_tableName1, g_tableName2}; - const std::vector g_tablesPKey = {g_cloudFiled1[0].colName, g_cloudFiled2[0].colName}; - const std::vector g_prefix = {"Local", ""}; - const Asset g_localAsset = { - .version = 1, .name = "Phone", .assetId = "0", .subpath = "/local/sync", .uri = "/local/sync", - .modifyTime = "123456", .createTime = "", .size = "256", .hash = "ASE" - }; - const Asset g_cloudAsset = { - .version = 2, .name = "Phone", .assetId = "0", .subpath = "/local/sync", .uri = "/cloud/sync", - .modifyTime = "123456", .createTime = "0", .size = "1024", .hash = "DEC" - }; - - void CreateUserDBAndTable(sqlite3 *&db) - { - EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); - EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_LOCAL_TABLE_SQL), SQLITE_OK); - EXPECT_EQ(RelationalTestUtils::ExecSql(db, INTEGER_PRIMARY_KEY_TABLE_SQL), SQLITE_OK); - EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_LOCAL_TABLE_WITHOUT_PRIMARY_KEY_SQL), SQLITE_OK); - } - - void InsertUserTableRecord(sqlite3 *&db, int64_t begin, int64_t count, int64_t photoSize, bool assetIsNull) - { - std::string photo(photoSize, 'v'); - int errCode; - std::vector assetBlob; - for (int64_t i = begin; i < begin + count; ++i) { - Asset asset = g_localAsset; - asset.name = asset.name + std::to_string(i); - RuntimeContext::GetInstance()->AssetToBlob(asset, assetBlob); - string sql = "INSERT OR REPLACE INTO " + g_tableName1 - + " (name, height, married, photo, assert, age) VALUES ('Local" + std::to_string(i) + - "', '175.8', '0', '" + photo + "', ? , '18');"; - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - if (assetIsNull) { - ASSERT_EQ(sqlite3_bind_null(stmt, 1), SQLITE_OK); - } else { - ASSERT_EQ(SQLiteUtils::BindBlobToStatement(stmt, 1, assetBlob, false), E_OK); - } - EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - for (int64_t i = begin; i < begin + count; ++i) { - std::vector assets; - Asset asset = g_localAsset; - asset.name = g_localAsset.name + std::to_string(i); - assets.push_back(asset); - asset.name = g_localAsset.name + std::to_string(i + 1); - assets.push_back(asset); - RuntimeContext::GetInstance()->AssetsToBlob(assets, assetBlob); - string sql = "INSERT OR REPLACE INTO " + g_tableName2 - + " (id, name, height, photo, asserts, age) VALUES ('" + std::to_string(i) + "', 'Local" - + std::to_string(i) + "', '155.10', '"+ photo + "', ? , '21');"; - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - if (assetIsNull) { - ASSERT_EQ(sqlite3_bind_null(stmt, 1), E_OK); - } else { - ASSERT_EQ(SQLiteUtils::BindBlobToStatement(stmt, 1, assetBlob, false), E_OK); - } - EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - LOGD("insert user record worker1[primary key]:[Local%" PRId64 " - Local%" PRId64 - ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); - } - - void UpdateUserTableRecord(sqlite3 *&db, int64_t begin, int64_t count) - { - for (size_t i = 0; i < g_tables.size(); i++) { - string updateAge = "UPDATE " + g_tables[i] + " SET age = '99' where " + g_tablesPKey[i] + " in ("; - for (int64_t j = begin; j < begin + count; ++j) { - updateAge += "'" + g_prefix[i] + std::to_string(j) + "',"; - } - updateAge.pop_back(); - updateAge += ");"; - ASSERT_EQ(RelationalTestUtils::ExecSql(db, updateAge), SQLITE_OK); - } - LOGD("update local record worker1[primary key]:[local%" PRId64 " - local%" PRId64 - ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); - } - - void DeleteUserTableRecord(sqlite3 *&db, int64_t begin, int64_t count) - { - for (size_t i = 0; i < g_tables.size(); i++) { - string updateAge = "Delete from " + g_tables[i] + " where " + g_tablesPKey[i] + " in ("; - for (int64_t j = begin; j < count; ++j) { - updateAge += "'" + g_prefix[i] + std::to_string(j) + "',"; - } - updateAge.pop_back(); - updateAge += ");"; - ASSERT_EQ(RelationalTestUtils::ExecSql(db, updateAge), SQLITE_OK); - } - LOGD("delete local record worker1[primary key]:[local%" PRId64 " - local%" PRId64 - ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); - } - - void InsertRecordWithoutPk2LocalAndCloud(sqlite3 *&db, int64_t begin, int64_t count, int photoSize) - { - std::vector photo(photoSize, 'v'); - std::string photoLocal(photoSize, 'v'); - Asset asset = { .version = 1, .name = "Phone" }; - std::vector assetBlob; - RuntimeContext::GetInstance()->BlobToAsset(assetBlob, asset); - std::string assetStr(assetBlob.begin(), assetBlob.end()); - std::vector record1; - std::vector extend1; - for (int64_t i = begin; i < count; ++i) { - Timestamp now = TimeHelper::GetSysCurrentTime(); - VBucket data; - data.insert_or_assign("name", "Cloud" + std::to_string(i)); - data.insert_or_assign("height", 166.0); // 166.0 is random double value - data.insert_or_assign("married", (bool)0); - data.insert_or_assign("photo", photo); - data.insert_or_assign("assert", KEY_1); - data.insert_or_assign("age", 13L); - record1.push_back(data); - VBucket log; - log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); - extend1.push_back(log); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); // wait for 1 ms - } - int errCode = g_virtualCloudDb->BatchInsert(g_tableName3, std::move(record1), extend1); - ASSERT_EQ(errCode, DBStatus::OK); - for (int64_t i = begin; i < count; ++i) { - string sql = "INSERT OR REPLACE INTO " + g_tableName3 - + " (name, height, married, photo, assert, age) VALUES ('Local" + std::to_string(i) + - "', '175.8', '0', '" + photoLocal + "', '" + assetStr + "', '18');"; - ASSERT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); - } - } - - void InsertCloudTableRecord(int64_t begin, int64_t count, int64_t photoSize, bool assetIsNull) - { - std::vector photo(photoSize, 'v'); - std::vector record1; - std::vector extend1; - std::vector record2; - std::vector extend2; - Timestamp now = TimeHelper::GetSysCurrentTime(); - for (int64_t i = begin; i < begin + count; ++i) { - VBucket data; - data.insert_or_assign("name", "Cloud" + std::to_string(i)); - data.insert_or_assign("height", 166.0); // 166.0 is random double value - data.insert_or_assign("married", false); - data.insert_or_assign("photo", photo); - data.insert_or_assign("age", 13L); - Asset asset = g_cloudAsset; - asset.name = asset.name + std::to_string(i); - assetIsNull ? data.insert_or_assign("assert", Nil()) : data.insert_or_assign("assert", asset); - record1.push_back(data); - VBucket log; - log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND + i); - log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND + i); - log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); - extend1.push_back(log); - - std::vector assets; - data.insert_or_assign("id", i); - data.insert_or_assign("height", 180.3); // 180.3 is random double value - for (int64_t j = i; j <= i + 2; j++) { // 2 extra num - asset.name = g_cloudAsset.name + std::to_string(j); - assets.push_back(asset); - } - data.erase("assert"); - data.erase("married"); - assetIsNull ? data.insert_or_assign("asserts", Nil()) : data.insert_or_assign("asserts", assets); - record2.push_back(data); - extend2.push_back(log); - } - ASSERT_EQ(g_virtualCloudDb->BatchInsert(g_tableName1, std::move(record1), extend1), DBStatus::OK); - ASSERT_EQ(g_virtualCloudDb->BatchInsert(g_tableName2, std::move(record2), extend2), DBStatus::OK); - LOGD("insert cloud record worker1[primary key]:[cloud%" PRId64 " - cloud%" PRId64 - ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); - std::this_thread::sleep_for(std::chrono::milliseconds(count)); - } - - void UpdateAssetForTest(sqlite3 *&db, AssetOpType opType, int64_t cloudCount, int64_t rowid) - { - string sql = "UPDATE " + g_tables[0] + " SET assert = ? where rowid = '" + std::to_string(rowid) + "';"; - std::vector assetBlob; - int errCode; - Asset asset = g_cloudAsset; - asset.name = "Phone" + std::to_string(rowid - cloudCount - 1); - if (opType == AssetOpType::UPDATE) { - asset.uri = "/data/test"; - } else if (opType == AssetOpType::INSERT) { - asset.name = "Test10"; - } - asset.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); - sqlite3_stmt *stmt = nullptr; - RuntimeContext::GetInstance()->AssetToBlob(asset, assetBlob); - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - if (SQLiteUtils::BindBlobToStatement(stmt, 1, assetBlob, false) == E_OK) { - EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); - } - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void UpdateAssetsForTest(sqlite3 *&db, AssetOpType opType, int64_t rowid) - { - string sql = "UPDATE " + g_tables[1] + " SET asserts = ? where rowid = '" + std::to_string(rowid) + "';"; - Asset asset1 = g_localAsset; - Asset asset2 = g_localAsset; - Assets assets; - asset1.name = g_localAsset.name + std::to_string(rowid); - asset1.status = static_cast(CloudStorageUtils::FlagToStatus(AssetOpType::NO_CHANGE)); - asset2.name = g_localAsset.name + std::to_string(rowid + 1); - asset2.status = static_cast(CloudStorageUtils::FlagToStatus(AssetOpType::NO_CHANGE)); - if (opType == AssetOpType::UPDATE) { - assets.push_back(asset1); - asset2.uri = "/data/test"; - asset2.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); - assets.push_back(asset2); - } else if (opType == AssetOpType::INSERT) { - assets.push_back(asset1); - assets.push_back(asset2); - Asset asset3; - asset3.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); - asset3.name = "Test10"; - assets.push_back(asset3); - } else if (opType == AssetOpType::DELETE) { - assets.push_back(asset1); - asset2.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); - assets.push_back(asset2); - } else { - assets.push_back(asset1); - assets.push_back(asset2); - } - sqlite3_stmt *stmt = nullptr; - std::vector assetsBlob; - RuntimeContext::GetInstance()->AssetsToBlob(assets, assetsBlob); - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - if (SQLiteUtils::BindBlobToStatement(stmt, 1, assetsBlob, false) == E_OK) { - EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void UpdateLocalAssets(sqlite3 *&db, Assets &assets, int64_t rowid) - { - string sql = "UPDATE " + g_tables[1] + " SET asserts = ? where rowid = '" + std::to_string(rowid) + "';"; - std::vector assetsBlob; - int errCode; - RuntimeContext::GetInstance()->AssetsToBlob(assets, assetsBlob); - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - if (SQLiteUtils::BindBlobToStatement(stmt, 1, assetsBlob, false) == E_OK) { - EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); - } - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void UpdateDiffType(int64_t begin) - { - std::vector hash = {"DEC", "update_", "insert_"}; - std::vector name = { - g_cloudAsset.name + std::to_string(0), - g_cloudAsset.name + std::to_string(1), - g_cloudAsset.name + std::to_string(3) // 3 is insert id - }; - std::vector record; - std::vector extend; - Assets assets; - for (int i = 0; i < 3; i ++) { // 3 is type num - Asset asset = g_cloudAsset; - asset.name = name[i]; - asset.hash = hash[i]; - assets.push_back(asset); - } - VBucket data; - data.insert_or_assign("name", "Cloud" + std::to_string(0)); - data.insert_or_assign("id", 0L); - data.insert_or_assign("asserts", assets); - Timestamp now = TimeHelper::GetSysCurrentTime(); - VBucket log; - log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); - log.insert_or_assign(CloudDbConstant::GID_FIELD, std::to_string(begin)); - log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - record.push_back(data); - extend.push_back(log); - ASSERT_EQ(g_virtualCloudDb->BatchUpdate(g_tableName2, std::move(record), extend), DBStatus::OK); - } - - void CheckDiffTypeAsset(sqlite3 *&db) - { - std::vector names = { - g_cloudAsset.name + std::to_string(0), - g_cloudAsset.name + std::to_string(1), - g_cloudAsset.name + std::to_string(3) // 3 is insert id - }; - std::string sql = "SELECT asserts from " + g_tables[1] + " WHERE rowid = 0;"; - sqlite3_stmt *stmt = nullptr; - int index = 0; - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_BLOB); - Type cloudValue; - ASSERT_EQ(SQLiteRelationalUtils::GetCloudValueByType(stmt, TYPE_INDEX, 0, cloudValue), E_OK); - std::vector assetsBlob; - Assets assets; - ASSERT_EQ(CloudStorageUtils::GetValueFromOneField(cloudValue, assetsBlob), E_OK); - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAssets(assetsBlob, assets), E_OK); - for (const Asset &asset: assets) { - ASSERT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - ASSERT_EQ(asset.name, names[index++]); - } - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void CheckAssetForAssetTest006() - { - VBucket extend; - extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); - std::vector data; - g_virtualCloudDb->Query(g_tables[1], extend, data); - for (size_t j = 0; j < data.size(); ++j) { - ASSERT_NE(data[j].find("asserts"), data[j].end()); - ASSERT_TRUE((data[j]["asserts"]).index() == TYPE_INDEX); - Assets &assets = std::get(data[j]["asserts"]); - ASSERT_TRUE(assets.size() > 0); - Asset &asset = assets[0]; - EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - EXPECT_EQ(asset.flag, static_cast(AssetOpType::DELETE)); - } - } - - void CheckFillAssetForTest10(sqlite3 *&db) - { - std::string sql = "SELECT assert from " + g_tables[0] + " WHERE rowid in ('27','28','29','30');"; - sqlite3_stmt *stmt = nullptr; - int index = 0; - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - int suffixId = 6; - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - if (index == 0 || index == 1 || index == 3) { // 3 is rowid index of 29 - ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_BLOB); - Type cloudValue; - ASSERT_EQ(SQLiteRelationalUtils::GetCloudValueByType(stmt, TYPE_INDEX, 0, cloudValue), E_OK); - std::vector assetBlob; - Asset asset; - ASSERT_EQ(CloudStorageUtils::GetValueFromOneField(cloudValue, assetBlob), E_OK); - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(assetBlob, asset), E_OK); - ASSERT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - if (index == 0) { - ASSERT_EQ(asset.name, g_cloudAsset.name + std::to_string(suffixId + index)); - } else if (index == 1) { - ASSERT_EQ(asset.name, "Test10"); - } else { - ASSERT_EQ(asset.name, g_cloudAsset.name + std::to_string(suffixId + index)); - ASSERT_EQ(asset.uri, "/data/test"); - } - } else { - ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_NULL); - } - index++; - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void CheckFillAssetsForTest10(sqlite3 *&db) - { - std::string sql = "SELECT asserts from " + g_tables[1] + " WHERE rowid in ('0','1','2','3');"; - sqlite3_stmt *stmt = nullptr; - int index = 0; - ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); - int insertIndex = 2; - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_BLOB); - Type cloudValue; - ASSERT_EQ(SQLiteRelationalUtils::GetCloudValueByType(stmt, TYPE_INDEX, 0, cloudValue), E_OK); - std::vector assetsBlob; - Assets assets; - ASSERT_EQ(CloudStorageUtils::GetValueFromOneField(cloudValue, assetsBlob), E_OK); - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAssets(assetsBlob, assets), E_OK); - if (index == 0) { - ASSERT_EQ(assets.size(), 2u); - ASSERT_EQ(assets[0].name, g_localAsset.name + std::to_string(index)); - ASSERT_EQ(assets[1].name, g_localAsset.name + std::to_string(index + 1)); - } else if (index == 1) { - ASSERT_EQ(assets.size(), 3u); - ASSERT_EQ(assets[insertIndex].name, "Test10"); - ASSERT_EQ(assets[insertIndex].status, static_cast(AssetStatus::NORMAL)); - } else if (index == 2) { // 2 is the third element - ASSERT_EQ(assets.size(), 1u); - ASSERT_EQ(assets[0].name, g_cloudAsset.name + std::to_string(index)); - } else { - ASSERT_EQ(assets.size(), 2u); - ASSERT_EQ(assets[1].uri, "/data/test"); - ASSERT_EQ(assets[1].status, static_cast(AssetStatus::NORMAL)); - } - index++; - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - int QueryCountCallback(void *data, int count, char **colValue, char **colName) - { - if (count != 1) { - return 0; - } - auto expectCount = reinterpret_cast(data); - EXPECT_EQ(strtol(colValue[0], nullptr, 10), expectCount); // 10: decimal - return 0; - } - - void CheckDownloadResult(sqlite3 *&db, std::vector expectCounts, std::string keyStr = "Cloud") - { - for (size_t i = 0; i < g_tables.size(); ++i) { - string queryDownload = "select count(*) from " + g_tables[i] + " where name " - + " like '" + keyStr + "%'"; - EXPECT_EQ(sqlite3_exec(db, queryDownload.c_str(), QueryCountCallback, - reinterpret_cast(expectCounts[i]), nullptr), SQLITE_OK); - } - } - - void CheckCloudRecordNum(sqlite3 *&db, std::vector tableList, std::vector countList) - { - int i = 0; - for (const auto &tableName: tableList) { - std::string sql = "select count(*) from " + DBCommon::GetLogTableName(tableName) + - " where device = 'cloud'" + " and cloud_gid is not null and cloud_gid != '' and flag & 0x2 = 0;"; - EXPECT_EQ(sqlite3_exec(db, sql.c_str(), QueryCountCallback, - reinterpret_cast(countList[i]), nullptr), SQLITE_OK); - i++; - } - } - - void CheckCleanLogNum(sqlite3 *&db, const std::vector tableList, int count) - { - for (const auto &tableName: tableList) { - std::string sql1 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + - " where device = 'cloud';"; - EXPECT_EQ(sqlite3_exec(db, sql1.c_str(), QueryCountCallback, - reinterpret_cast(count), nullptr), SQLITE_OK); - std::string sql2 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + - " where cloud_gid " + " is not null and cloud_gid != '';"; - EXPECT_EQ(sqlite3_exec(db, sql2.c_str(), QueryCountCallback, - reinterpret_cast(count), nullptr), SQLITE_OK); - std::string sql3 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + - " where flag & 0x02 = 0;"; - EXPECT_EQ(sqlite3_exec(db, sql3.c_str(), QueryCountCallback, - reinterpret_cast(count), nullptr), SQLITE_OK); - } - } - - void CheckCleanDataAndLogNum(sqlite3 *&db, const std::vector tableList, int count, - std::vector localNum) - { - int i = 0; - for (const auto &tableName: tableList) { - std::string sql1 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + - " where device = 'cloud';"; - EXPECT_EQ(sqlite3_exec(db, sql1.c_str(), QueryCountCallback, - reinterpret_cast(count), nullptr), SQLITE_OK); - std::string sql2 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + " where cloud_gid " - " is not null and cloud_gid != '';"; - EXPECT_EQ(sqlite3_exec(db, sql2.c_str(), QueryCountCallback, - reinterpret_cast(count), nullptr), SQLITE_OK); - std::string sql3 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + - " where flag & 0x02 = 0;"; - EXPECT_EQ(sqlite3_exec(db, sql3.c_str(), QueryCountCallback, - reinterpret_cast(count), nullptr), SQLITE_OK); - std::string local_sql = "select count(*) from " + tableName +";"; - EXPECT_EQ(sqlite3_exec(db, local_sql.c_str(), QueryCountCallback, - reinterpret_cast(localNum[i]), nullptr), SQLITE_OK); - i++; - } - } - - void CheckCloudTotalCount(std::vector expectCounts) - { - VBucket extend; - extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); - for (size_t i = 0; i < g_tables.size(); ++i) { - int64_t realCount = 0; - std::vector data; - g_virtualCloudDb->Query(g_tables[i], extend, data); - for (size_t j = 0; j < data.size(); ++j) { - auto entry = data[j].find(CloudDbConstant::DELETE_FIELD); - if (entry != data[j].end() && std::get(entry->second)) { - continue; - } - realCount++; - } - EXPECT_EQ(realCount, expectCounts[i]); // ExpectCount represents the total amount of cloud data. - } - } - - void GetCloudDbSchema(DataBaseSchema &dataBaseSchema) - { - TableSchema tableSchema1 = { - .name = g_tableName1, - .fields = g_cloudFiled1 - }; - TableSchema tableSchema2 = { - .name = g_tableName2, - .fields = g_cloudFiled2 - }; - TableSchema tableSchemaWithOutPrimaryKey = { - .name = g_tableName3, - .fields = g_cloudFiledWithOutPrimaryKey3 - }; - TableSchema tableSchema4 = { - .name = g_tableName4, - .fields = g_cloudFiled2 - }; - dataBaseSchema.tables.push_back(tableSchema1); - dataBaseSchema.tables.push_back(tableSchema2); - dataBaseSchema.tables.push_back(tableSchemaWithOutPrimaryKey); - dataBaseSchema.tables.push_back(tableSchema4); - } - - - void GetInvalidCloudDbSchema(DataBaseSchema &dataBaseSchema) - { - TableSchema tableSchema1 = { - .name = g_tableName1, - .fields = g_invalidCloudFiled1 - }; - TableSchema tableSchema2 = { - .name = g_tableName1, - .fields = g_cloudFiled2 - }; - dataBaseSchema.tables.push_back(tableSchema1); - dataBaseSchema.tables.push_back(tableSchema2); - } - - void InitProcessForTest1(const uint32_t &cloudCount, const uint32_t &localCount, - std::vector &expectProcess) - { - expectProcess.clear(); - std::vector infos; - uint32_t index = 1; - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} - }); - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} - }); - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} - }); - - for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { - SyncProcess syncProcess; - syncProcess.errCode = OK; - syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; - syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); - syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); - expectProcess.push_back(syncProcess); - } - } - - void InitProcessForMannualSync1(std::vector &expectProcess) - { - expectProcess.clear(); - std::vector infos; - // first notify, first table - infos.push_back(TableProcessInfo{ - FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - // first notify, second table - infos.push_back(TableProcessInfo{ - PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - // second notify, first table - infos.push_back(TableProcessInfo{ - FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - // second notify, second table - infos.push_back(TableProcessInfo{ - FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - // second notify, second table - infos.push_back(TableProcessInfo{ - FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { - SyncProcess syncProcess; - syncProcess.errCode = OK; - syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; - syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); - syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); - expectProcess.push_back(syncProcess); - } - } - - void InitProcessForCleanCloudData1(const uint32_t &cloudCount, std::vector &expectProcess) - { - // cloudCount also means data count in one batch - expectProcess.clear(); - std::vector infos; - uint32_t index = 1; - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { - SyncProcess syncProcess; - syncProcess.errCode = OK; - syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; - syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); - syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); - expectProcess.push_back(syncProcess); - } - } - - void InitProcessForTest2(const uint32_t &cloudCount, const uint32_t &localCount, - std::vector &expectProcess) - { - expectProcess.clear(); - std::vector infos; - uint32_t index = 1; - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} - }); - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} - }); - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount - cloudCount, localCount - cloudCount, 0} - }); - - for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { - SyncProcess syncProcess; - syncProcess.errCode = OK; - syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; - syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); - syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); - expectProcess.push_back(syncProcess); - } - } - - void InitProcessForTest9(const uint32_t &cloudCount, const uint32_t &localCount, - std::vector &expectProcess) - { - expectProcess.clear(); - std::vector infos; - uint32_t index = 1; - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - infos.push_back(TableProcessInfo{ - FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} - }); - - for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { - SyncProcess syncProcess; - syncProcess.errCode = OK; - syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; - syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); - syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); - expectProcess.push_back(syncProcess); - } - } - void GetCallback(SyncProcess &syncProcess, CloudSyncStatusCallback &callback, - std::vector &expectProcess) - { - g_syncIndex = 0; - callback = [&syncProcess, &expectProcess](const std::map &process) { - LOGI("devices size = %d", process.size()); - ASSERT_EQ(process.size(), 1u); - syncProcess = std::move(process.begin()->second); - ASSERT_EQ(process.begin()->first, DEVICE_CLOUD); - ASSERT_NE(syncProcess.tableProcess.empty(), true); - LOGI("current sync process status:%d, db status:%d ", syncProcess.process, syncProcess.errCode); - std::for_each(g_tables.begin(), g_tables.end(), [&](const auto &item) { - auto table1 = syncProcess.tableProcess.find(item); - if (table1 != syncProcess.tableProcess.end()) { - LOGI("table[%s], table process status:%d, [downloadInfo](batchIndex:%u, total:%u, successCount:%u, " - "failCount:%u) [uploadInfo](batchIndex:%u, total:%u, successCount:%u,failCount:%u", - item.c_str(), table1->second.process, table1->second.downLoadInfo.batchIndex, - table1->second.downLoadInfo.total, table1->second.downLoadInfo.successCount, - table1->second.downLoadInfo.failCount, table1->second.upLoadInfo.batchIndex, - table1->second.upLoadInfo.total, table1->second.upLoadInfo.successCount, - table1->second.upLoadInfo.failCount); - } - }); - if (expectProcess.empty()) { - if (syncProcess.process == FINISHED) { - g_processCondition.notify_one(); - } - return; - } - ASSERT_LE(static_cast(g_syncIndex), expectProcess.size()); - for (size_t i = 0; i < g_tables.size(); ++i) { - SyncProcess head = expectProcess[g_syncIndex]; - for (auto &expect : head.tableProcess) { - auto real = syncProcess.tableProcess.find(expect.first); - ASSERT_NE(real, syncProcess.tableProcess.end()); - EXPECT_EQ(expect.second.process, real->second.process); - EXPECT_EQ(expect.second.downLoadInfo.batchIndex, real->second.downLoadInfo.batchIndex); - EXPECT_EQ(expect.second.downLoadInfo.total, real->second.downLoadInfo.total); - EXPECT_EQ(expect.second.downLoadInfo.successCount, real->second.downLoadInfo.successCount); - EXPECT_EQ(expect.second.downLoadInfo.failCount, real->second.downLoadInfo.failCount); - EXPECT_EQ(expect.second.upLoadInfo.batchIndex, real->second.upLoadInfo.batchIndex); - EXPECT_EQ(expect.second.upLoadInfo.total, real->second.upLoadInfo.total); - EXPECT_EQ(expect.second.upLoadInfo.successCount, real->second.upLoadInfo.successCount); - EXPECT_EQ(expect.second.upLoadInfo.failCount, real->second.upLoadInfo.failCount); - } - } - g_syncIndex++; - if (syncProcess.process == FINISHED) { - g_processCondition.notify_one(); - } - }; - } - - void CheckAllAssetAfterUpload(int64_t localCount) - { - VBucket extend; - extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); - std::vector data1; - g_virtualCloudDb->Query(g_tables[0], extend, data1); - for (size_t j = 0; j < data1.size(); ++j) { - auto entry = data1[j].find("assert"); - ASSERT_NE(entry, data1[j].end()); - Asset asset = std::get(entry->second); - bool isLocal = j >= (size_t)(localCount / g_arrayHalfSub); - Asset baseAsset = isLocal ? g_localAsset : g_cloudAsset; - EXPECT_EQ(asset.version, baseAsset.version); - EXPECT_EQ(asset.name, baseAsset.name + std::to_string(isLocal ? j - localCount / g_arrayHalfSub : j)); - EXPECT_EQ(asset.uri, baseAsset.uri); - EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); - EXPECT_EQ(asset.createTime, baseAsset.createTime); - EXPECT_EQ(asset.size, baseAsset.size); - EXPECT_EQ(asset.hash, baseAsset.hash); - } - - std::vector data2; - g_virtualCloudDb->Query(g_tables[1], extend, data2); - for (size_t j = 0; j < data2.size(); ++j) { - auto entry = data2[j].find("asserts"); - ASSERT_NE(entry, data2[j].end()); - Assets assets = std::get(entry->second); - Asset baseAsset = j >= (size_t)(localCount / g_arrayHalfSub) ? g_localAsset : g_cloudAsset; - int index = j; - for (const auto &asset: assets) { - EXPECT_EQ(asset.version, baseAsset.version); - EXPECT_EQ(asset.name, baseAsset.name + std::to_string(index++)); - EXPECT_EQ(asset.uri, baseAsset.uri); - EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); - EXPECT_EQ(asset.createTime, baseAsset.createTime); - EXPECT_EQ(asset.size, baseAsset.size); - EXPECT_EQ(asset.hash, baseAsset.hash); - } - } - } - - void CheckAssetsAfterDownload(sqlite3 *&db, int64_t localCount) - { - string queryDownload = "select asserts from " + g_tables[1] + " where rowid in ("; - for (int64_t i = 0; i < localCount; ++i) { - queryDownload += "'" + std::to_string(i) + "',"; - } - queryDownload.pop_back(); - queryDownload += ");"; - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); - int index = 0; - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - std::vector blobValue; - ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); - Assets assets; - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAssets(blobValue, assets), E_OK); - bool isLocal = index >= localCount / g_arrayHalfSub; - Asset baseAsset = isLocal ? g_localAsset : g_cloudAsset; - int nameIndex = index; - for (const auto &asset: assets) { - EXPECT_EQ(asset.version, baseAsset.version); - EXPECT_EQ(asset.name, baseAsset.name + std::to_string(nameIndex)); - EXPECT_EQ(asset.uri, baseAsset.uri); - EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); - EXPECT_EQ(asset.createTime, baseAsset.createTime); - EXPECT_EQ(asset.size, baseAsset.size); - EXPECT_EQ(asset.hash, baseAsset.hash); - EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - nameIndex++; - } - index++; - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void CheckAssetAfterDownload(sqlite3 *&db, int64_t localCount) - { - string queryDownload = "select assert from " + g_tables[0] + " where rowid in ("; - for (int64_t i = 0; i < localCount; ++i) { - queryDownload += "'" + std::to_string(i) + "',"; - } - queryDownload.pop_back(); - queryDownload += ");"; - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); - int index = 0; - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - std::vector blobValue; - ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); - Asset asset; - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(blobValue, asset), E_OK); - bool isCloud = index >= localCount; - Asset baseAsset = isCloud ? g_cloudAsset : g_localAsset; - EXPECT_EQ(asset.version, baseAsset.version); - EXPECT_EQ(asset.name, - baseAsset.name + std::to_string(isCloud ? index - localCount / g_arrayHalfSub : index)); - EXPECT_EQ(asset.uri, baseAsset.uri); - EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); - EXPECT_EQ(asset.createTime, baseAsset.createTime); - EXPECT_EQ(asset.size, baseAsset.size); - EXPECT_EQ(asset.hash, baseAsset.hash); - EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - index++; - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void UpdateCloudAssetForDownloadAssetTest003() - { - VBucket data; - std::vector photo(1, 'x'); - data.insert_or_assign("name", "Cloud" + std::to_string(0)); - data.insert_or_assign("photo", photo); - data.insert_or_assign("assert", g_cloudAsset); - Timestamp now = TimeHelper::GetSysCurrentTime(); - VBucket log; - std::vector record; - std::vector extend; - log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); - log.insert_or_assign(CloudDbConstant::GID_FIELD, std::to_string(0)); - log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - record.push_back(data); - extend.push_back(log); - ASSERT_EQ(g_virtualCloudDb->BatchUpdate(g_tableName1, std::move(record), extend), DBStatus::OK); - } - - void CheckAssetForDownloadAssetTest003(sqlite3 *&db) - { - string queryDownload = "select assert from " + g_tables[0] + " where rowid = '11';"; - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); - int index = 0; - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - std::vector blobValue; - ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); - Asset asset; - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(blobValue, asset), E_OK); - EXPECT_EQ(asset.name, g_cloudAsset.name); - EXPECT_EQ(asset.hash, g_cloudAsset.hash); - EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - index++; - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void CheckAssetAfterDownload2(sqlite3 *&db, int64_t localCount) - { - string queryDownload = "select assert from " + g_tables[0] + " where rowid in ("; - for (int64_t i = localCount + 1; i < localCount + localCount; ++i) { - queryDownload += "'" + std::to_string(i) + "',"; - } - queryDownload.pop_back(); - queryDownload += ");"; - sqlite3_stmt *stmt = nullptr; - ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); - int index = 0; - while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { - std::vector blobValue; - ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); - Asset asset; - ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(blobValue, asset), E_OK); - EXPECT_EQ(asset.version, g_cloudAsset.version); - if (index % 6u == 0) { // 6 is AssetStatus type num, include invalid type - EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); - } else { - EXPECT_EQ(asset.status, static_cast(AssetStatus::ABNORMAL)); - } - index++; - } - int errCode; - SQLiteUtils::ResetStatement(stmt, true, errCode); - } - - void WaitForSyncFinish(SyncProcess &syncProcess, const int64_t &waitTime) - { - std::unique_lock lock(g_processMutex); - bool result = g_processCondition.wait_for(lock, std::chrono::seconds(waitTime), [&syncProcess]() { - return syncProcess.process == FINISHED; - }); - ASSERT_EQ(result, true); - LOGD("-------------------sync end--------------"); - } - - void CloseDb() - { - delete g_observer; - g_virtualCloudDb = nullptr; - if (g_delegate != nullptr) { - EXPECT_EQ(g_mgr.CloseStore(g_delegate), DBStatus::OK); - g_delegate = nullptr; - } - } - - void InitMockAssetLoader(DBStatus &status, int &index) - { - std::shared_ptr assetLoader = make_shared(); - ASSERT_EQ(g_delegate->SetIAssetLoader(assetLoader), DBStatus::OK); - EXPECT_CALL(*assetLoader, Download(testing::_, testing::_, testing::_, testing::_)) - .WillRepeatedly([&status, &index](const std::string &, const std::string &gid, const Type &, - std::map &assets) { - LOGD("Download GID:%s", gid.c_str()); - for (auto &item: assets) { - for (auto &asset: item.second) { - EXPECT_EQ(asset.status, static_cast(AssetStatus::DOWNLOADING)); - LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag); - asset.status = (index++) % 6u; // 6 is AssetStatus type num, include invalid type - } - } - return status; - }); - } +namespace DistributedDB { + std::shared_ptr testOj = nullptr; class DistributedDBCloudInterfacesRelationalSyncTest : public testing::Test { public: @@ -1129,9 +34,12 @@ namespace { void DistributedDBCloudInterfacesRelationalSyncTest::SetUpTestCase(void) { - DistributedDBToolsUnitTest::TestDirInit(g_testDir); - g_storePath = g_testDir + "/" + g_storeID + DB_SUFFIX; - LOGI("The test db is:%s", g_testDir.c_str()); + testOj = std::make_shared(); + ASSERT_NE(testOj, nullptr); + testOj->g_mgr = std::make_shared(APP_ID, USER_ID); + DistributedDBToolsUnitTest::TestDirInit(testOj->g_testDir); + testOj->g_storePath = testOj->g_testDir + "/" + testOj->g_storeID + testOj->DB_SUFFIX; + LOGI("The test db is:%s", testOj->g_testDir.c_str()); RuntimeConfig::SetCloudTranslate(std::make_shared()); } @@ -1140,41 +48,41 @@ namespace { void DistributedDBCloudInterfacesRelationalSyncTest::SetUp(void) { - if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) { + if (DistributedDBToolsUnitTest::RemoveTestDbFiles(testOj->g_testDir) != 0) { LOGE("rm test db files error."); } DistributedDBToolsUnitTest::PrintTestCaseInfo(); - LOGD("Test dir is %s", g_testDir.c_str()); - db = RelationalTestUtils::CreateDataBase(g_storePath); + LOGD("Test dir is %s", testOj->g_testDir.c_str()); + db = RelationalTestUtils::CreateDataBase(testOj->g_storePath); ASSERT_NE(db, nullptr); - CreateUserDBAndTable(db); - g_observer = new (std::nothrow) RelationalStoreObserverUnitTest(); - ASSERT_NE(g_observer, nullptr); - ASSERT_EQ(g_mgr.OpenStore(g_storePath, g_storeID, RelationalStoreDelegate::Option { .observer = g_observer }, - g_delegate), DBStatus::OK); - ASSERT_NE(g_delegate, nullptr); - ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName1, CLOUD_COOPERATION), DBStatus::OK); - ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName2, CLOUD_COOPERATION), DBStatus::OK); - ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName3, CLOUD_COOPERATION), DBStatus::OK); - g_virtualCloudDb = make_shared(); - g_virtualAssetLoader = make_shared(); - g_syncProcess = {}; - ASSERT_EQ(g_delegate->SetCloudDB(g_virtualCloudDb), DBStatus::OK); - ASSERT_EQ(g_delegate->SetIAssetLoader(g_virtualAssetLoader), DBStatus::OK); + testOj->CreateUserDBAndTable(db); + testOj->g_observer = new (std::nothrow) RelationalStoreObserverUnitTest(); + ASSERT_NE(testOj->g_observer, nullptr); + ASSERT_EQ(testOj->g_mgr->OpenStore(testOj->g_storePath, testOj->g_storeID, + RelationalStoreDelegate::Option { .observer = testOj->g_observer }, testOj->g_delegate), DBStatus::OK); + ASSERT_NE(testOj->g_delegate, nullptr); + ASSERT_EQ(testOj->g_delegate->CreateDistributedTable(testOj->g_tableName1, CLOUD_COOPERATION), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->CreateDistributedTable(testOj->g_tableName2, CLOUD_COOPERATION), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->CreateDistributedTable(testOj->g_tableName3, CLOUD_COOPERATION), DBStatus::OK); + testOj->g_virtualCloudDb = make_shared(); + testOj->g_virtualAssetLoader = make_shared(); + testOj->g_syncProcess = {}; + ASSERT_EQ(testOj->g_delegate->SetCloudDB(testOj->g_virtualCloudDb), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->SetIAssetLoader(testOj->g_virtualAssetLoader), DBStatus::OK); // sync before setting cloud db schema,it should return SCHEMA_MISMATCH - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); CloudSyncStatusCallback callback; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::SCHEMA_MISMATCH); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::SCHEMA_MISMATCH); DataBaseSchema dataBaseSchema; - GetCloudDbSchema(dataBaseSchema); - ASSERT_EQ(g_delegate->SetCloudDbSchema(dataBaseSchema), DBStatus::OK); + testOj->GetCloudDbSchema(dataBaseSchema); + ASSERT_EQ(testOj->g_delegate->SetCloudDbSchema(dataBaseSchema), DBStatus::OK); } void DistributedDBCloudInterfacesRelationalSyncTest::TearDown(void) { EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK); - if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) { + if (DistributedDBToolsUnitTest::RemoveTestDbFiles(testOj->g_testDir) != 0) { LOGE("rm test db files error."); } } @@ -1190,35 +98,36 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest001, TestS { int64_t paddingSize = 10; int64_t cloudCount = 20; - int64_t localCount = cloudCount / g_arrayHalfSub; + int64_t localCount = cloudCount / testOj->g_arrayHalfSub; ChangedData changedDataForTable1; ChangedData changedDataForTable2; - changedDataForTable1.tableName = g_tableName1; - changedDataForTable2.tableName = g_tableName2; + changedDataForTable1.tableName = testOj->g_tableName1; + changedDataForTable2.tableName = testOj->g_tableName2; changedDataForTable1.field.push_back(std::string("name")); changedDataForTable2.field.push_back(std::string("id")); for (int i = 0; i < cloudCount; i++) { changedDataForTable1.primaryData[ChangeType::OP_INSERT].push_back({"Cloud" + std::to_string(i)}); changedDataForTable2.primaryData[ChangeType::OP_INSERT].push_back({(int64_t)i + 10}); } - g_observer->SetExpectedResult(changedDataForTable1); - g_observer->SetExpectedResult(changedDataForTable2); - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->g_observer->SetExpectedResult(changedDataForTable1); + testOj->g_observer->SetExpectedResult(changedDataForTable2); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForTest1(cloudCount, localCount, expectProcess); + testOj->InitProcessForTest1(cloudCount, localCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - EXPECT_TRUE(g_observer->IsAllChangedDataEq()); - g_observer->ClearChangedData(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + EXPECT_TRUE(testOj->g_observer->IsAllChangedDataEq()); + testOj->g_observer->ClearChangedData(); LOGD("expect download:worker1[primary key]:[cloud0 - cloud20), worker2[primary key]:[10 - 20)"); - CheckDownloadResult(db, {20L, 10L}); // 20 and 10 means the num of downloads from cloud db by worker1 and worker2 + testOj->CheckDownloadResult(db, {20L, 10L}); // 20 and 10 means the num of downloads from cloud db LOGD("expect upload:worker1[primary key]:[local0 - local10), worker2[primary key]:[0 - 10)"); - CheckCloudTotalCount({30L, 20L}); // 30 and 20 means the total num of worker1 and worker2 from the cloud db - CloseDb(); + testOj->CheckCloudTotalCount({30L, 20L}); // 30 and 20 means the total num of worker1 and worker2 from the cloud db + testOj->CloseDb(); } /** @@ -1233,20 +142,21 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest002, TestS int64_t localCount = 20; int64_t cloudCount = 10; int64_t paddingSize = 100; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForTest2(cloudCount, localCount, expectProcess); + testOj->InitProcessForTest2(cloudCount, localCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); LOGD("expect download:worker1[primary key]:[cloud0 - cloud10), worker2[primary key]:[0 - 10)"); - CheckDownloadResult(db, {10L, 10L}); // 10 and 10 means the num of downloads from cloud db by worker1 and worker2 + testOj->CheckDownloadResult(db, {10L, 10L}); // 10 and 10 means the num of downloads from cloud db LOGD("expect upload:worker1[primary key]:[local0 - local20), worker2[primary key]:[10 - 20)"); - CheckCloudTotalCount({30L, 20L}); // 30 and 20 means the total num of worker1 and worker2 from the cloud db - CloseDb(); + testOj->CheckCloudTotalCount({30L, 20L}); // 30 and 20 means the total num of worker1 and worker2 from the cloud db + testOj->CloseDb(); } /** @@ -1260,51 +170,54 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest003, TestS { int64_t paddingSize = 10; int cloudCount = 20; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, cloudCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, cloudCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForTest1(cloudCount, cloudCount, expectProcess); + testOj->InitProcessForTest1(cloudCount, cloudCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CheckDownloadResult(db, {20L, 0L}); // 20 and 0 means the num of downloads from cloud db by worker1 and worker2 - CheckCloudTotalCount({40L, 20L}); // 40 and 20 means the total num of worker1 and worker2 from the cloud db + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CheckDownloadResult(db, {20L, 0L}); // 20 and 0 means the num of downloads from cloud db + testOj->CheckCloudTotalCount({40L, 20L}); // 40 and 20 means the total num of worker1 and worker2 from the cloud db int updateCount = 10; - UpdateUserTableRecord(db, 5, updateCount); // 5 is start id to be updated - g_syncProcess = {}; - InitProcessForTest1(cloudCount, updateCount, expectProcess); - GetCallback(g_syncProcess, callback, expectProcess); + testOj->UpdateUserTableRecord(db, 5, updateCount); // 5 is start id to be updated + testOj->g_syncProcess = {}; + testOj->InitProcessForTest1(cloudCount, updateCount, expectProcess); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); LOGD("-------------------sync after update--------------"); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); VBucket extend; extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); std::vector data1; - g_virtualCloudDb->Query(g_tables[0], extend, data1); + testOj->g_virtualCloudDb->Query(testOj->g_tables[0], extend, data1); for (int j = 25; j < 35; ++j) { // index[25, 35) in cloud db expected to be updated EXPECT_EQ(std::get(data1[j]["age"]), 99); // 99 is the updated age field of cloud db } std::vector data2; - g_virtualCloudDb->Query(g_tables[1], extend, data2); + testOj->g_virtualCloudDb->Query(testOj->g_tables[1], extend, data2); for (int j = 5; j < 15; ++j) { // index[5, 15) in cloud db expected to be updated EXPECT_EQ(std::get(data2[j]["age"]), 99); // 99 is the updated age field of cloud db } int deleteCount = 3; - DeleteUserTableRecord(db, 0, deleteCount); - g_syncProcess = {}; - InitProcessForTest1(updateCount, deleteCount, expectProcess); - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - - CheckCloudTotalCount({37L, 17L}); // 37 and 17 means the total num of worker1 and worker2 from the cloud db - CloseDb(); + testOj->DeleteUserTableRecord(db, 0, deleteCount); + testOj->g_syncProcess = {}; + testOj->InitProcessForTest1(updateCount, deleteCount, expectProcess); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + + testOj->CheckCloudTotalCount({37L, 17L}); // 37 and 17 means the total num of worker1 and worker2 from the cloud db + testOj->CloseDb(); } /** @@ -1319,18 +232,21 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest004, TestS int64_t paddingSize = 1024 * 8; vector threads; int cloudCount = 1024; - threads.emplace_back(InsertCloudTableRecord, 0, cloudCount, paddingSize, false); - threads.emplace_back(InsertUserTableRecord, std::ref(db), 0, cloudCount, paddingSize, false); + threads.emplace_back(&DistributedDBCloudInterfacesRelationalSyncTestcase::InsertCloudTableRecord, testOj, + 0, cloudCount, paddingSize, false); + threads.emplace_back(&DistributedDBCloudInterfacesRelationalSyncTestcase::InsertUserTableRecord, testOj, + std::ref(db), 0, cloudCount, paddingSize, false); for (auto &thread: threads) { thread.join(); } - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, 20); // 20 is wait time - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, 20); // 20 is wait time + testOj->CloseDb(); } /** @@ -1342,14 +258,14 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest004, TestS */ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest005, TestSize.Level0) { - Query query = Query::Select().FromTable(g_tables).OrderBy("123", true); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, nullptr, g_syncWaitTime), - DBStatus::NOT_SUPPORT); + Query query = Query::Select().FromTable(testOj->g_tables).OrderBy("123", true); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + nullptr, testOj->g_syncWaitTime), DBStatus::NOT_SUPPORT); query = Query::Select(); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, nullptr, g_syncWaitTime), - DBStatus::INVALID_ARGS); - CloseDb(); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + nullptr, testOj->g_syncWaitTime), DBStatus::INVALID_ARGS); + testOj->CloseDb(); } /** @@ -1365,48 +281,49 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest006, TestS int cloudCount = 20; ChangedData changedDataForTable1; ChangedData changedDataForTable2; - changedDataForTable1.tableName = g_tableName1; - changedDataForTable2.tableName = g_tableName2; + changedDataForTable1.tableName = testOj->g_tableName1; + changedDataForTable2.tableName = testOj->g_tableName2; changedDataForTable1.field.push_back(std::string("name")); changedDataForTable2.field.push_back(std::string("id")); for (int i = 0; i < cloudCount; i++) { changedDataForTable1.primaryData[ChangeType::OP_INSERT].push_back({"Cloud" + std::to_string(i)}); changedDataForTable2.primaryData[ChangeType::OP_INSERT].push_back({(int64_t)i + 10}); } - g_observer->SetExpectedResult(changedDataForTable1); - g_observer->SetExpectedResult(changedDataForTable2); - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, cloudCount / g_arrayHalfSub, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->g_observer->SetExpectedResult(changedDataForTable1); + testOj->g_observer->SetExpectedResult(changedDataForTable2); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, cloudCount / testOj->g_arrayHalfSub, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); // Set correct cloudDbSchema (correct version) DataBaseSchema correctSchema; - GetCloudDbSchema(correctSchema); - ASSERT_EQ(g_delegate->SetCloudDbSchema(correctSchema), DBStatus::OK); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - EXPECT_TRUE(g_observer->IsAllChangedDataEq()); - g_observer->ClearChangedData(); + testOj->GetCloudDbSchema(correctSchema); + ASSERT_EQ(testOj->g_delegate->SetCloudDbSchema(correctSchema), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + EXPECT_TRUE(testOj->g_observer->IsAllChangedDataEq()); + testOj->g_observer->ClearChangedData(); LOGD("expect download:worker1[primary key]:[cloud0 - cloud20), worker2[primary key]:[10 - 20)"); - CheckDownloadResult(db, {20L, 10L}); // 20 and 10 means the num of downloads from cloud db by worker1 and worker2 + testOj->CheckDownloadResult(db, {20L, 10L}); // 20 and 10 means the num of downloads from cloud db LOGD("expect upload:worker1[primary key]:[local0 - local10), worker2[primary key]:[0 - 10)"); - CheckCloudTotalCount({30L, 20L}); // 30 and 20 means the total num of worker1 and worker2 from the cloud db + testOj->CheckCloudTotalCount({30L, 20L}); // 30 and 20 means the total num of worker1 and worker2 from the cloud db // Reset cloudDbSchema (invalid version - null) DataBaseSchema nullSchema; - ASSERT_EQ(g_delegate->SetCloudDbSchema(nullSchema), DBStatus::OK); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::SCHEMA_MISMATCH); + ASSERT_EQ(testOj->g_delegate->SetCloudDbSchema(nullSchema), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::SCHEMA_MISMATCH); // Reset cloudDbSchema (invalid version - field mismatch) DataBaseSchema invalidSchema; - GetInvalidCloudDbSchema(invalidSchema); - ASSERT_EQ(g_delegate->SetCloudDbSchema(invalidSchema), DBStatus::OK); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::SCHEMA_MISMATCH); - CloseDb(); + testOj->GetInvalidCloudDbSchema(invalidSchema); + ASSERT_EQ(testOj->g_delegate->SetCloudDbSchema(invalidSchema), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::SCHEMA_MISMATCH); + testOj->CloseDb(); } /** @@ -1420,19 +337,20 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest007, TestS { int64_t paddingSize = 100; int localCount = 20; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, localCount / g_arrayHalfSub, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, localCount / testOj->g_arrayHalfSub, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - - CheckAssetAfterDownload(db, localCount); - CheckAllAssetAfterUpload(localCount); - CheckAssetsAfterDownload(db, localCount); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + + testOj->CheckAssetAfterDownload(db, localCount); + testOj->CheckAllAssetAfterUpload(localCount); + testOj->CheckAssetsAfterDownload(db, localCount); + testOj->CloseDb(); } /* @@ -1444,11 +362,12 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest007, TestS */ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest008, TestSize.Level0) { - ASSERT_EQ(g_delegate->SetCloudDB(nullptr), OK); // it will not happen because cloudb has been set in SetUp() - Query query = Query::Select().FromTable({g_tableName3}); + ASSERT_EQ(testOj->g_delegate->SetCloudDB(nullptr), OK); // it will not happen because cloudb has been set in SetUp() + Query query = Query::Select().FromTable({testOj->g_tableName3}); // clouddb has been set in SetUp() and it's not null - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, nullptr, g_syncWaitTime), OK); - CloseDb(); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + nullptr, testOj->g_syncWaitTime), OK); + testOj->CloseDb(); } /** @@ -1462,27 +381,29 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest009, TestS { int64_t paddingSize = 10; int cloudCount = 20; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, cloudCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, cloudCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForTest1(cloudCount, cloudCount, expectProcess); + testOj->InitProcessForTest1(cloudCount, cloudCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); LOGD("expect download:worker1[primary key]:[cloud0 - cloud20), worker2[primary key]:none"); - CheckDownloadResult(db, {20L, 0L}); // 20 and 0 means the num of downloads from cloud db by worker1 and worker2 + testOj->CheckDownloadResult(db, {20L, 0L}); // 20 and 0 means the num of downloads from cloud db LOGD("expect upload:worker1[primary key]:[local0 - local20), worker2[primary key]:[0 - 20)"); - CheckCloudTotalCount({40L, 20L}); // 40 and 20 means the total num of worker1 and worker2 from the cloud db + testOj->CheckCloudTotalCount({40L, 20L}); // 40 and 20 means the total num of worker1 and worker2 from the cloud db - g_syncProcess = {}; - InitProcessForTest9(cloudCount, 0, expectProcess); - GetCallback(g_syncProcess, callback, expectProcess); + testOj->g_syncProcess = {}; + testOj->InitProcessForTest9(cloudCount, 0, expectProcess); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); LOGD("--------------the second sync-------------"); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest0010, TestSize.Level0) @@ -1490,36 +411,38 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest0010, Test int64_t paddingSize = 10; int cloudCount = 20; int localCount = 10; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); int rowid = 27; - UpdateAssetForTest(db, AssetOpType::NO_CHANGE, cloudCount, rowid++); - UpdateAssetForTest(db, AssetOpType::INSERT, cloudCount, rowid++); - UpdateAssetForTest(db, AssetOpType::DELETE, cloudCount, rowid++); - UpdateAssetForTest(db, AssetOpType::UPDATE, cloudCount, rowid++); + testOj->UpdateAssetForTest(db, AssetOpType::NO_CHANGE, cloudCount, rowid++); + testOj->UpdateAssetForTest(db, AssetOpType::INSERT, cloudCount, rowid++); + testOj->UpdateAssetForTest(db, AssetOpType::DELETE, cloudCount, rowid++); + testOj->UpdateAssetForTest(db, AssetOpType::UPDATE, cloudCount, rowid++); int id = 0; - UpdateAssetsForTest(db, AssetOpType::NO_CHANGE, id++); - UpdateAssetsForTest(db, AssetOpType::INSERT, id++); - UpdateAssetsForTest(db, AssetOpType::DELETE, id++); - UpdateAssetsForTest(db, AssetOpType::UPDATE, id++); + testOj->UpdateAssetsForTest(db, AssetOpType::NO_CHANGE, id++); + testOj->UpdateAssetsForTest(db, AssetOpType::INSERT, id++); + testOj->UpdateAssetsForTest(db, AssetOpType::DELETE, id++); + testOj->UpdateAssetsForTest(db, AssetOpType::UPDATE, id++); - g_syncProcess = {}; - GetCallback(g_syncProcess, callback, expectProcess); + testOj->g_syncProcess = {}; + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); LOGD("--------------the second sync-------------"); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); - CheckFillAssetForTest10(db); - CheckFillAssetsForTest10(db); - CloseDb(); + testOj->CheckFillAssetForTest10(db); + testOj->CheckFillAssetsForTest10(db); + testOj->CloseDb(); } /** @@ -1531,15 +454,15 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest0010, Test */ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest011, TestSize.Level0) { - Query query = Query::Select().FromTable({g_tableName1, g_tableName1}); + Query query = Query::Select().FromTable({testOj->g_tableName1, testOj->g_tableName1}); bool syncFinish = false; std::mutex syncMutex; std::condition_variable cv; std::atomic callCount = 0; CloudSyncStatusCallback callback = [&callCount, &cv, &syncFinish, &syncMutex]( const std::map &onProcess) { - ASSERT_NE(onProcess.find(DEVICE_CLOUD), onProcess.end()); - SyncProcess syncProcess = onProcess.at(DEVICE_CLOUD); + ASSERT_NE(onProcess.find(testOj->DEVICE_CLOUD), onProcess.end()); + SyncProcess syncProcess = onProcess.at(testOj->DEVICE_CLOUD); callCount++; if (syncProcess.process == FINISHED) { std::lock_guard autoLock(syncMutex); @@ -1547,14 +470,15 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest011, TestS } cv.notify_all(); }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); std::unique_lock uniqueLock(syncMutex); cv.wait(uniqueLock, [&syncFinish]() { return syncFinish; }); RuntimeContext::GetInstance()->StopTaskPool(); EXPECT_EQ(callCount, 2); // 2 is onProcess count - CloseDb(); + testOj->CloseDb(); } HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest012, TestSize.Level0) @@ -1562,37 +486,40 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest012, TestS int64_t localCount = 20; int64_t cloudCount = 10; int64_t paddingSize = 10; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, true); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, true); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - - InsertCloudTableRecord(localCount + cloudCount, cloudCount, paddingSize, false); - InsertUserTableRecord(db, localCount + cloudCount, localCount, paddingSize, true); - g_syncProcess = {}; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - - InsertCloudTableRecord(2 * (localCount + cloudCount), cloudCount, paddingSize, false); // 2 is offset - InsertUserTableRecord(db, 2 * (localCount + cloudCount), localCount, paddingSize, false); // 2 is offset - g_syncProcess = {}; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - - - InsertCloudTableRecord(3 * (localCount + cloudCount), cloudCount, paddingSize, true); // 3 is offset - InsertUserTableRecord(db, 3 * (localCount + cloudCount), localCount, paddingSize, true); // 3 is offset - g_syncProcess = {}; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + + testOj->InsertCloudTableRecord(localCount + cloudCount, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, localCount + cloudCount, localCount, paddingSize, true); + testOj->g_syncProcess = {}; + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + + testOj->InsertCloudTableRecord(2 * (localCount + cloudCount), cloudCount, paddingSize, false); // 2 is offset + testOj->InsertUserTableRecord(db, 2 * (localCount + cloudCount), localCount, paddingSize, false); // 2 is offset + testOj->g_syncProcess = {}; + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + + testOj->InsertCloudTableRecord(3 * (localCount + cloudCount), cloudCount, paddingSize, true); // 3 is offset + testOj->InsertUserTableRecord(db, 3 * (localCount + cloudCount), localCount, paddingSize, true); // 3 is offset + testOj->g_syncProcess = {}; + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } /* @@ -1611,23 +538,24 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest013, TestS int64_t paddingSize = 10; int64_t cloudCount = 10; SyncProcess syncProcess; - InsertCloudTableRecord(0, cloudCount, paddingSize, true); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, true); /** * @tc.steps: try to cloud sync * @tc.expected: return ok. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); CloudSyncStatusCallback callback = [&syncProcess](const std::map &process) { LOGI("devices size = %d", process.size()); ASSERT_EQ(process.size(), 1u); syncProcess = std::move(process.begin()->second); if (syncProcess.process == FINISHED) { - g_processCondition.notify_one(); + testOj->g_processCondition.notify_one(); } }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(syncProcess, g_syncWaitTime); - uint32_t queryTimes = g_virtualCloudDb->GetQueryTimes(g_tableName1); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(syncProcess, testOj->g_syncWaitTime); + uint32_t queryTimes = testOj->g_virtualCloudDb->GetQueryTimes(testOj->g_tableName1); /** * @tc.steps: insert some increment data into cloud db * @tc.expected: return ok. @@ -1644,13 +572,14 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest013, TestS log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); log.insert_or_assign(CloudDbConstant::CURSOR_FIELD, "0123"); - g_virtualCloudDb->SetIncrementData(g_tableName1, data, log); + testOj->g_virtualCloudDb->SetIncrementData(testOj->g_tableName1, data, log); syncProcess.process = PREPARED; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(syncProcess, g_syncWaitTime); - uint32_t lastQueryTimes = g_virtualCloudDb->GetQueryTimes(g_tableName1); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(syncProcess, testOj->g_syncWaitTime); + uint32_t lastQueryTimes = testOj->g_virtualCloudDb->GetQueryTimes(testOj->g_tableName1); ASSERT_EQ(lastQueryTimes - queryTimes, 2u); - CloseDb(); + testOj->CloseDb(); } void TestSyncForStatus(RelationalStoreDelegate *delegate, DBStatus expectStatus) @@ -1665,15 +594,16 @@ void TestSyncForStatus(RelationalStoreDelegate *delegate, DBStatus expectStatus) { std::lock_guard autoLock(dataMutex); syncProcess = process; - if (syncProcess[DEVICE_CLOUD].process == FINISHED) { + if (syncProcess[testOj->DEVICE_CLOUD].process == FINISHED) { finish = true; } - res = syncProcess[DEVICE_CLOUD].errCode; + res = syncProcess[testOj->DEVICE_CLOUD].errCode; } cv.notify_one(); }; - Query query = Query::Select().FromTable({g_tableName3}); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); + Query query = Query::Select().FromTable({testOj->g_tableName3}); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); { std::unique_lock uniqueLock(dataMutex); cv.wait(uniqueLock, [&finish] { @@ -1693,23 +623,23 @@ void TestSyncForStatus(RelationalStoreDelegate *delegate, DBStatus expectStatus) */ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest015, TestSize.Level0) { - g_virtualCloudDb->SetActionStatus(CLOUD_NETWORK_ERROR); - TestSyncForStatus(g_delegate, CLOUD_NETWORK_ERROR); + testOj->g_virtualCloudDb->SetActionStatus(CLOUD_NETWORK_ERROR); + TestSyncForStatus(testOj->g_delegate, CLOUD_NETWORK_ERROR); - g_virtualCloudDb->SetActionStatus(CLOUD_SYNC_UNSET); - TestSyncForStatus(g_delegate, CLOUD_SYNC_UNSET); + testOj->g_virtualCloudDb->SetActionStatus(CLOUD_SYNC_UNSET); + TestSyncForStatus(testOj->g_delegate, CLOUD_SYNC_UNSET); - g_virtualCloudDb->SetActionStatus(CLOUD_FULL_RECORDS); - TestSyncForStatus(g_delegate, CLOUD_FULL_RECORDS); + testOj->g_virtualCloudDb->SetActionStatus(CLOUD_FULL_RECORDS); + TestSyncForStatus(testOj->g_delegate, CLOUD_FULL_RECORDS); - g_virtualCloudDb->SetActionStatus(CLOUD_LOCK_ERROR); - TestSyncForStatus(g_delegate, CLOUD_LOCK_ERROR); + testOj->g_virtualCloudDb->SetActionStatus(CLOUD_LOCK_ERROR); + TestSyncForStatus(testOj->g_delegate, CLOUD_LOCK_ERROR); - g_virtualCloudDb->SetActionStatus(DB_ERROR); - TestSyncForStatus(g_delegate, CLOUD_ERROR); + testOj->g_virtualCloudDb->SetActionStatus(DB_ERROR); + TestSyncForStatus(testOj->g_delegate, CLOUD_ERROR); - g_virtualCloudDb->SetActionStatus(OK); - CloseDb(); + testOj->g_virtualCloudDb->SetActionStatus(OK); + testOj->CloseDb(); } /* @@ -1729,36 +659,36 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncTest014, TestS option.securityLabel = S0; return DB_ERROR; }); - Query query = Query::Select().FromTable({g_tableName3}); - EXPECT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, nullptr, g_syncWaitTime), - SECURITY_OPTION_CHECK_ERROR); + Query query = Query::Select().FromTable({testOj->g_tableName3}); + EXPECT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + nullptr, testOj->g_syncWaitTime), SECURITY_OPTION_CHECK_ERROR); // sync failed because get S4 adapter->ForkGetSecurityOption([](const std::string&, SecurityOption &option) { option.securityLabel = S4; return NOT_SUPPORT; }); - Query invalidQuery = Query::Select().FromTable({g_tableName3}).PrefixKey({'k'}); - EXPECT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, invalidQuery, nullptr, g_syncWaitTime), - NOT_SUPPORT); + Query invalidQuery = Query::Select().FromTable({testOj->g_tableName3}).PrefixKey({'k'}); + EXPECT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, + invalidQuery, nullptr, testOj->g_syncWaitTime), NOT_SUPPORT); // sync failed because get S4 adapter->ForkGetSecurityOption([](const std::string&, SecurityOption &option) { option.securityLabel = S4; return OK; }); - EXPECT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, nullptr, g_syncWaitTime), - SECURITY_OPTION_CHECK_ERROR); + EXPECT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + nullptr, testOj->g_syncWaitTime), SECURITY_OPTION_CHECK_ERROR); // sync failed because S4 has been cached adapter->ForkGetSecurityOption([](const std::string&, SecurityOption &option) { option.securityLabel = S0; return OK; }); - EXPECT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, nullptr, g_syncWaitTime), - SECURITY_OPTION_CHECK_ERROR); + EXPECT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + nullptr, testOj->g_syncWaitTime), SECURITY_OPTION_CHECK_ERROR); RuntimeConfig::SetProcessSystemAPIAdapter(nullptr); - CloseDb(); + testOj->CloseDb(); } /* @@ -1772,14 +702,15 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, DataNotifier001, TestSi { int64_t paddingSize = 10; int localCount = 20; - InsertRecordWithoutPk2LocalAndCloud(db, 0, localCount, paddingSize); - Query query = Query::Select().FromTable({g_tableName3}); + testOj->InsertRecordWithoutPk2LocalAndCloud(db, 0, localCount, paddingSize); + Query query = Query::Select().FromTable({testOj->g_tableName3}); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } /** @@ -1793,18 +724,19 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest001, { int64_t paddingSize = 100; int localCount = 20; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, localCount / g_arrayHalfSub, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, localCount / testOj->g_arrayHalfSub, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - - CheckAssetAfterDownload(db, localCount); - CheckAllAssetAfterUpload(localCount); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + + testOj->CheckAssetAfterDownload(db, localCount); + testOj->CheckAllAssetAfterUpload(localCount); + testOj->CloseDb(); } /* @@ -1818,16 +750,16 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, MannualNotify001, TestS { int64_t paddingSize = 10; int localCount = 10; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForMannualSync1(expectProcess); + testOj->InitProcessForMannualSync1(expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } /** @@ -1843,29 +775,30 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudProcessNotify001, * @tc.steps: step1. table work1 and work2 insert 1 record which name is local0, then sync(). * @tc.expected: step 1. table work1 and work2 download result is 0. table work1 and work2 upload 1 record. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; int64_t paddingSize = 10; int64_t localCount = 1; - InsertUserTableRecord(db, 0, localCount, paddingSize, true); - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - EXPECT_TRUE(g_observer->IsAllChangedDataEq()); - g_observer->ClearChangedData(); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, true); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + EXPECT_TRUE(testOj->g_observer->IsAllChangedDataEq()); + testOj->g_observer->ClearChangedData(); LOGD("expect download:worker1[primary key]:[], worker2[primary key]:[]"); - CheckDownloadResult(db, {0L, 0L}); // 0 and 0 means the num of downloads from cloud db by worker1 and worker2 + testOj->CheckDownloadResult(db, {0L, 0L}); // 0 and 0 means the num of downloads from cloud db LOGD("expect upload:worker1[primary key]:[local0], worker2[primary key]:[0]"); - CheckCloudTotalCount({1L, 1L}); // 1 and 1 means the total num of worker1 and worker2 from the cloud db + testOj->CheckCloudTotalCount({1L, 1L}); // 1 and 1 means the total num of worker1 and worker2 from the cloud db /** * @tc.steps: step2. reset data * @tc.expected: step2. return ok. */ std::this_thread::sleep_for(std::chrono::milliseconds(100)); - g_syncProcess = {}; - ASSERT_EQ(g_delegate->SetCloudDB(g_virtualCloudDb), DBStatus::OK); + testOj->g_syncProcess = {}; + ASSERT_EQ(testOj->g_delegate->SetCloudDB(testOj->g_virtualCloudDb), DBStatus::OK); /** * @tc.steps: step3. table work1 delete record which gid is 0 and name is local0 on cloud. @@ -1873,7 +806,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudProcessNotify001, */ VBucket idMap; idMap.insert_or_assign("#_gid", std::to_string(0)); - ASSERT_EQ(g_virtualCloudDb->DeleteByGid(g_tableName1, idMap), DBStatus::OK); + ASSERT_EQ(testOj->g_virtualCloudDb->DeleteByGid(testOj->g_tableName1, idMap), DBStatus::OK); /** * @tc.steps: step4. table work1 insert record which gid is 0 and name is local0 on cloud. @@ -1881,50 +814,37 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudProcessNotify001, */ std::vector record1; std::vector extend1; - VBucket data; - std::vector photo(10, 'v'); - data.insert_or_assign("name", "Local" + std::to_string(0)); - data.insert_or_assign("height", 166.0); // 166.0 is random double value - data.insert_or_assign("married", false); - data.insert_or_assign("age", 13L); - data.insert_or_assign("photo", photo); - data.insert_or_assign("assert", Nil()); - record1.push_back(data); - VBucket log; - Timestamp now = TimeHelper::GetSysCurrentTime(); - log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); - log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); - log.insert_or_assign("#_gid", std::to_string(2)); - extend1.push_back(log); - ASSERT_EQ(g_virtualCloudDb->BatchInsertWithGid(g_tableName1, std::move(record1), extend1), DBStatus::OK); + testOj->InsertCloudForCloudProcessNotify001(record1, extend1); + ASSERT_EQ(testOj->g_virtualCloudDb->BatchInsertWithGid(testOj->g_tableName1, std::move(record1), extend1), + DBStatus::OK); /** * @tc.steps: step5. sync() and check local data. * @tc.expected: step5. return ok. */ ChangedData changedDataForTable1; - changedDataForTable1.tableName = g_tableName1; + changedDataForTable1.tableName = testOj->g_tableName1; changedDataForTable1.field.push_back(std::string("name")); changedDataForTable1.primaryData[ChangeType::OP_UPDATE].push_back({"Local" + std::to_string(0)}); - g_observer->SetExpectedResult(changedDataForTable1); - - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - EXPECT_TRUE(g_observer->IsAllChangedDataEq()); - g_observer->ClearChangedData(); + testOj->g_observer->SetExpectedResult(changedDataForTable1); + + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + EXPECT_TRUE(testOj->g_observer->IsAllChangedDataEq()); + testOj->g_observer->ClearChangedData(); LOGD("expect download:worker1[primary key]:[Local0], worker2[primary key]:[0]"); // 1 and 1 means the num of downloads from cloud db by worker1 and worker2 - CheckDownloadResult(db, {1L, 1L}, "Local"); + testOj->CheckDownloadResult(db, {1L, 1L}, "Local"); LOGD("expect upload:worker1[primary key]:[local0], worker2[primary key]:[0]"); - CheckCloudTotalCount({1L, 1L}); // 0 and 0 means the total num of worker1 and worker2 from the cloud db + testOj->CheckCloudTotalCount({1L, 1L}); // 0 and 0 means the total num of worker1 and worker2 from the cloud db /** - * @tc.steps: step6. CloseDb(). + * @tc.steps: step6. testOj->CloseDb(). * @tc.expected: step6. return ok. */ - CloseDb(); + testOj->CloseDb(); } /* @@ -1939,25 +859,25 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CleanCloudDataTest001, int64_t paddingSize = 10; int localCount = 10; int cloudCount = 20; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForCleanCloudData1(cloudCount, expectProcess); + testOj->InitProcessForCleanCloudData1(cloudCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); std::string device = ""; - CheckCloudRecordNum(db, g_tables, {20, 20}); - ASSERT_EQ(g_delegate->RemoveDeviceData(device, FLAG_ONLY), DBStatus::OK); - CheckCleanLogNum(db, g_tables, 0); + testOj->CheckCloudRecordNum(db, testOj->g_tables, {20, 20}); + ASSERT_EQ(testOj->g_delegate->RemoveDeviceData(device, FLAG_ONLY), DBStatus::OK); + testOj->CheckCleanLogNum(db, testOj->g_tables, 0); - ASSERT_EQ(g_delegate->RemoveDeviceData(device, ClearMode(BUTT + 1)), DBStatus::INVALID_ARGS); - ASSERT_EQ(g_delegate->RemoveDeviceData(device, ClearMode(-1)), DBStatus::INVALID_ARGS); + ASSERT_EQ(testOj->g_delegate->RemoveDeviceData(device, ClearMode(BUTT + 1)), DBStatus::INVALID_ARGS); + ASSERT_EQ(testOj->g_delegate->RemoveDeviceData(device, ClearMode(-1)), DBStatus::INVALID_ARGS); - CloseDb(); + testOj->CloseDb(); } /* @@ -1972,21 +892,21 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CleanCloudDataTest002, int64_t paddingSize = 10; int localCount = 10; int cloudCount = 20; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForCleanCloudData1(cloudCount, expectProcess); + testOj->InitProcessForCleanCloudData1(cloudCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); std::string device = ""; - CheckCloudRecordNum(db, g_tables, {20, 20}); // 20 means cloud data num - ASSERT_EQ(g_delegate->RemoveDeviceData(device, FLAG_AND_DATA), DBStatus::OK); - CheckCleanDataAndLogNum(db, g_tables, 0, {localCount, 0}); - CloseDb(); + testOj->CheckCloudRecordNum(db, testOj->g_tables, {20, 20}); // 20 means cloud data num + ASSERT_EQ(testOj->g_delegate->RemoveDeviceData(device, FLAG_AND_DATA), DBStatus::OK); + testOj->CheckCleanDataAndLogNum(db, testOj->g_tables, 0, {localCount, 0}); + testOj->CloseDb(); } /* @@ -2004,50 +924,50 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CleanCloudDataTest003, int64_t paddingSize = 10; int localCount = 10; int cloudCount = 20; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); /** * @tc.steps: step2. call Sync with cloud force pull strategy, and after that, local will has 20 records. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; - InitProcessForCleanCloudData1(cloudCount, expectProcess); + testOj->InitProcessForCleanCloudData1(cloudCount, expectProcess); CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CheckCloudRecordNum(db, g_tables, {20, 20}); // 20 means cloud data num + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CheckCloudRecordNum(db, testOj->g_tables, {20, 20}); // 20 means cloud data num /** * @tc.steps: step3. insert 10 records into local, so local will has 20 local records and 20 cloud records. */ - InsertUserTableRecord(db, 21, localCount, paddingSize, false); // 21 means insert start index + testOj->InsertUserTableRecord(db, 21, localCount, paddingSize, false); // 21 means insert start index /** * @tc.steps: step4. call RemoveDeviceData synchronize with Sync with cloud force push strategy. */ - g_syncProcess = {}; + testOj->g_syncProcess = {}; std::vector expectProcess2; - InitProcessForCleanCloudData1(cloudCount, expectProcess2); + testOj->InitProcessForCleanCloudData1(cloudCount, expectProcess2); CloudSyncStatusCallback callback2; - GetCallback(g_syncProcess, callback2, expectProcess2); + testOj->GetCallback(testOj->g_syncProcess, callback2, expectProcess2); std::string device = ""; std::thread thread1([&]() { - ASSERT_EQ(g_delegate->RemoveDeviceData(device, FLAG_AND_DATA), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->RemoveDeviceData(device, FLAG_AND_DATA), DBStatus::OK); }); std::thread thread2([&]() { - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, callback2, g_syncWaitTime), - DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PULL, query, + callback2, testOj->g_syncWaitTime), DBStatus::OK); LOGD("-------------------sync end--------------"); }); thread1.join(); thread2.join(); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); RuntimeContext::GetInstance()->StopTaskPool(); - CheckCleanLogNum(db, g_tables, 20); + testOj->CheckCleanLogNum(db, testOj->g_tables, 20); LOGD("================================== test clean cloud data 003 end ==================================="); - CloseDb(); + testOj->CloseDb(); } static void InitGetCloudSyncTaskCountTest001(sqlite3 *&db) @@ -2055,8 +975,8 @@ static void InitGetCloudSyncTaskCountTest001(sqlite3 *&db) int64_t localCount = 20; int64_t cloudCount = 10; int64_t paddingSize = 100; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); } /* * @tc.name: GetCloudSyncTaskCountTest001 @@ -2068,7 +988,7 @@ static void InitGetCloudSyncTaskCountTest001(sqlite3 *&db) HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, GetCloudSyncTaskCountTest001, TestSize.Level0) { InitGetCloudSyncTaskCountTest001(db); - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::mutex dataMutex1, dataMutex2; std::condition_variable cv1, cv2; bool finish1 = false, finish2 = false; @@ -2082,7 +1002,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, GetCloudSyncTaskCountTe { std::lock_guard autoLock(dataMutex1); syncProcess = process; - if (syncProcess[DEVICE_CLOUD].process == FINISHED) { + if (syncProcess[testOj->DEVICE_CLOUD].process == FINISHED) { finish1 = true; } } @@ -2092,7 +1012,8 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, GetCloudSyncTaskCountTe * @tc.steps: step2. Call Sync twice. * @tc.expected: OK. */ - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback1, g_syncWaitTime), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback1, testOj->g_syncWaitTime), DBStatus::OK); CloudSyncStatusCallback callback2 = [&dataMutex2, &cv2, &finish2]( const std::map &process) { @@ -2100,18 +1021,19 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, GetCloudSyncTaskCountTe { std::lock_guard autoLock(dataMutex2); syncProcess = process; - if (syncProcess[DEVICE_CLOUD].process == FINISHED) { + if (syncProcess[testOj->DEVICE_CLOUD].process == FINISHED) { finish2 = true; } } cv2.notify_one(); }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback2, g_syncWaitTime), DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback2, + testOj->g_syncWaitTime), DBStatus::OK); /** * @tc.steps: step3. Call Get Cloud Sync Task Count * @tc.expected: OK. */ - EXPECT_EQ(g_delegate->GetCloudSyncTaskCount(), 2); // 2 is task count + EXPECT_EQ(testOj->g_delegate->GetCloudSyncTaskCount(), 2); // 2 is task count /** * @tc.steps: step3. Wait For Sync Task Finished * @tc.expected: OK. @@ -2129,7 +1051,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, GetCloudSyncTaskCountTe }); } RuntimeContext::GetInstance()->StopTaskPool(); - CloseDb(); + testOj->CloseDb(); } /* @@ -2144,14 +1066,14 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CleanCloudDataTest004, DataBaseSchema dataBaseSchema; TableSchema tableSchema1 = { .name = "table_not_existed", - .fields = g_cloudFiled1 + .fields = testOj->g_cloudFiled1 }; dataBaseSchema.tables.push_back(tableSchema1); - GetCloudDbSchema(dataBaseSchema); - ASSERT_EQ(g_delegate->SetCloudDbSchema(dataBaseSchema), DBStatus::OK); + testOj->GetCloudDbSchema(dataBaseSchema); + ASSERT_EQ(testOj->g_delegate->SetCloudDbSchema(dataBaseSchema), DBStatus::OK); std::string device = ""; - ASSERT_EQ(g_delegate->RemoveDeviceData(device, FLAG_AND_DATA), DBStatus::OK); - CloseDb(); + ASSERT_EQ(testOj->g_delegate->RemoveDeviceData(device, FLAG_AND_DATA), DBStatus::OK); + testOj->CloseDb(); } /* @@ -2170,13 +1092,13 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CalPrimaryKeyHash001, T std::string photo(1u, 'v'); std::string name = "Local0"; std::map primaryKey = {{"name", name}}; - string sql = "INSERT OR REPLACE INTO " + g_tableName1 + string sql = "INSERT OR REPLACE INTO " + testOj->g_tableName1 + " (name, height, married, photo, age) VALUES ('Local" + std::to_string(0) + "', '175.8', '0', '" + photo + "', '18');"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); std::vector result = RelationalStoreManager::CalcPrimaryKeyHash(primaryKey); EXPECT_NE(result.size(), 0u); - std::string logTableName = RelationalStoreManager::GetDistributedLogTableName(g_tableName1); + std::string logTableName = RelationalStoreManager::GetDistributedLogTableName(testOj->g_tableName1); /** * @tc.steps: step1. query timestamp use hashKey * @tc.expected: OK. @@ -2200,7 +1122,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CalPrimaryKeyHash001, T } EXPECT_EQ(errCode, E_OK); SQLiteUtils::ResetStatement(statement, true, errCode); - CloseDb(); + testOj->CloseDb(); } /* @@ -2218,13 +1140,13 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CalPrimaryKeyHash002, T */ int64_t id = 1; std::map primaryKey = {{"id", id}}; - std::string sql = "INSERT OR REPLACE INTO " + g_tableName2 + std::string sql = "INSERT OR REPLACE INTO " + testOj->g_tableName2 + " (id, name, height) VALUES ('" + '1' + "', 'Local" + std::to_string(0) + "', '155.10');"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); std::vector result = RelationalStoreManager::CalcPrimaryKeyHash(primaryKey); EXPECT_NE(result.size(), 0u); - std::string logTableName = RelationalStoreManager::GetDistributedLogTableName(g_tableName2); + std::string logTableName = RelationalStoreManager::GetDistributedLogTableName(testOj->g_tableName2); /** * @tc.steps: step1. query timestamp use hashKey * @tc.expected: OK. @@ -2248,7 +1170,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CalPrimaryKeyHash002, T } EXPECT_EQ(errCode, E_OK); SQLiteUtils::ResetStatement(statement, true, errCode); - CloseDb(); + testOj->CloseDb(); } /* @@ -2263,16 +1185,16 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest002, int64_t paddingSize = 10; int localCount = 3; int cloudCount = 3; - InsertCloudTableRecord(0, cloudCount, paddingSize, true); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, true); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PUSH, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_FORCE_PUSH, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } /* @@ -2287,32 +1209,32 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest003, int64_t paddingSize = 10; int localCount = 3; int cloudCount = 3; - InsertCloudTableRecord(0, cloudCount, paddingSize, true); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, true); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); Assets assets; - assets.push_back(g_localAsset); - assets.push_back(g_localAsset); - UpdateLocalAssets(db, assets, 1); - Query query = Query::Select().FromTable(g_tables); + assets.push_back(testOj->g_localAsset); + assets.push_back(testOj->g_localAsset); + testOj->UpdateLocalAssets(db, assets, 1); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback = [](const std::map &process) { ASSERT_EQ(process.size(), 1u); - g_syncProcess = std::move(process.begin()->second); + testOj->g_syncProcess = std::move(process.begin()->second); - if (g_syncProcess.process == FINISHED) { - g_processCondition.notify_one(); + if (testOj->g_syncProcess.process == FINISHED) { + testOj->g_processCondition.notify_one(); } }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); { - std::unique_lock lock(g_processMutex); - g_processCondition.wait(lock, []() { - return g_syncProcess.process == FINISHED; + std::unique_lock lock(testOj->g_processMutex); + testOj->g_processCondition.wait(lock, []() { + return testOj->g_syncProcess.process == FINISHED; }); - ASSERT_EQ(g_syncProcess.errCode, DBStatus::CLOUD_ERROR); + ASSERT_EQ(testOj->g_syncProcess.errCode, DBStatus::CLOUD_ERROR); } - CloseDb(); + testOj->CloseDb(); } /* @@ -2327,37 +1249,37 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest004, int64_t paddingSize = 10; int localCount = 3; int cloudCount = 3; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); - UpdateDiffType(localCount); - g_syncProcess = {}; + testOj->UpdateDiffType(localCount); + testOj->g_syncProcess = {}; CloudSyncStatusCallback callback1 = [](const std::map &process) { ASSERT_EQ(process.size(), 1u); - g_syncProcess = std::move(process.begin()->second); - if (g_syncProcess.process == FINISHED) { - g_processCondition.notify_one(); + testOj->g_syncProcess = std::move(process.begin()->second); + if (testOj->g_syncProcess.process == FINISHED) { + testOj->g_processCondition.notify_one(); } }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback1, g_syncWaitTime), - DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback1, testOj->g_syncWaitTime), DBStatus::OK); { - std::unique_lock lock(g_processMutex); - g_processCondition.wait(lock, []() { - return g_syncProcess.process == FINISHED; + std::unique_lock lock(testOj->g_processMutex); + testOj->g_processCondition.wait(lock, []() { + return testOj->g_syncProcess.process == FINISHED; }); - ASSERT_EQ(g_syncProcess.errCode, DBStatus::OK); + ASSERT_EQ(testOj->g_syncProcess.errCode, DBStatus::OK); } - CheckDiffTypeAsset(db); - CloseDb(); + testOj->CheckDiffTypeAsset(db); + testOj->CloseDb(); } /* @@ -2376,46 +1298,46 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest005, int64_t paddingSize = 10; int localCount = 3; int cloudCount = 3; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); Assets assets; for (int64_t j = 0; j < cloudCount; j++) { - Asset asset = g_cloudAsset; - asset.name = g_cloudAsset.name + std::to_string(j); + Asset asset = testOj->g_cloudAsset; + asset.name = testOj->g_cloudAsset.name + std::to_string(j); assets.push_back(asset); } - UpdateLocalAssets(db, assets, 0); + testOj->UpdateLocalAssets(db, assets, 0); std::this_thread::sleep_for(std::chrono::milliseconds(cloudCount)); /** * @tc.steps:step2. Construct cloud data * @tc.expected: step2. return ok. */ - InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); /** * @tc.steps:step3. sync, expect EraseNoChangeAsset to erase all Nochange assets * @tc.expected: step3. return ok. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback = [](const std::map &process) { ASSERT_EQ(process.size(), 1u); - g_syncProcess = std::move(process.begin()->second); + testOj->g_syncProcess = std::move(process.begin()->second); - if (g_syncProcess.process == FINISHED) { - g_processCondition.notify_one(); + if (testOj->g_syncProcess.process == FINISHED) { + testOj->g_processCondition.notify_one(); } }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); { - std::unique_lock lock(g_processMutex); - g_processCondition.wait(lock, []() { - return g_syncProcess.process == FINISHED; + std::unique_lock lock(testOj->g_processMutex); + testOj->g_processCondition.wait(lock, []() { + return testOj->g_syncProcess.process == FINISHED; }); - ASSERT_EQ(g_syncProcess.errCode, DBStatus::OK); + ASSERT_EQ(testOj->g_syncProcess.errCode, DBStatus::OK); } - CloseDb(); + testOj->CloseDb(); } /* @@ -2434,22 +1356,22 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest006, int64_t paddingSize = 10; int localCount = 6; int cloudCount = 3; - InsertUserTableRecord(db, 0, localCount, paddingSize, true); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, true); std::this_thread::sleep_for(std::chrono::milliseconds(cloudCount)); - InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); /** * @tc.steps:step2. sync, upload new data without assets, * @tc.expected: step2. return ok. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } /* @@ -2469,50 +1391,50 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest007, int64_t paddingSize = 10; int localCount = 1; int cloudCount = 1; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); /** * @tc.steps:step2. local asset is set to delete, but hash is not set to empty * @tc.expected: step2. return ok. */ Assets assets; for (int64_t j = 0; j < cloudCount; j++) { - Asset asset = g_cloudAsset; - asset.name = g_cloudAsset.name + std::to_string(j); + Asset asset = testOj->g_cloudAsset; + asset.name = testOj->g_cloudAsset.name + std::to_string(j); asset.status = static_cast(AssetStatus::DELETE); assets.push_back(asset); } - UpdateLocalAssets(db, assets, 0); + testOj->UpdateLocalAssets(db, assets, 0); std::this_thread::sleep_for(std::chrono::milliseconds(cloudCount)); /** * @tc.steps:step3. Do sync * @tc.expected: step3. return ok. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback = [](const std::map &process) { ASSERT_EQ(process.size(), 1u); - g_syncProcess = std::move(process.begin()->second); + testOj->g_syncProcess = std::move(process.begin()->second); - if (g_syncProcess.process == FINISHED) { - g_processCondition.notify_one(); + if (testOj->g_syncProcess.process == FINISHED) { + testOj->g_processCondition.notify_one(); } }; - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, + callback, testOj->g_syncWaitTime), DBStatus::OK); { - std::unique_lock lock(g_processMutex); - g_processCondition.wait(lock, []() { - return g_syncProcess.process == FINISHED; + std::unique_lock lock(testOj->g_processMutex); + testOj->g_processCondition.wait(lock, []() { + return testOj->g_syncProcess.process == FINISHED; }); - ASSERT_EQ(g_syncProcess.errCode, DBStatus::OK); + ASSERT_EQ(testOj->g_syncProcess.errCode, DBStatus::OK); } /** * @tc.steps:step4. Check result. Cloud db should not contain asset. * @tc.expected: step4. return ok. */ - CheckAssetForAssetTest006(); - CloseDb(); + testOj->CheckAssetForAssetTest006(); + testOj->CloseDb(); } @@ -2531,7 +1453,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, DownloadAssetTest001, T */ DBStatus expectStatus = DBStatus::OK; int index = 0; - InitMockAssetLoader(expectStatus, index); + testOj->InitMockAssetLoader(expectStatus, index); /** * @tc.steps:step2. init download data @@ -2539,26 +1461,27 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, DownloadAssetTest001, T */ int64_t paddingSize = 1; int localCount = 120; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, localCount / g_arrayHalfSub, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, localCount / testOj->g_arrayHalfSub, paddingSize, false); /** * @tc.steps:step3. sync * @tc.expected: step3. return ok. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); /** * @tc.steps:step4. Expect all states to be normal * @tc.expected: step4. return ok. */ - CheckAssetAfterDownload(db, localCount); - CloseDb(); + testOj->CheckAssetAfterDownload(db, localCount); + testOj->CloseDb(); } /* @@ -2576,40 +1499,40 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, CloudSyncAssetTest008, int64_t paddingSize = 10; int localCount = 1; int cloudCount = 1; - InsertCloudTableRecord(0, cloudCount, paddingSize, false); - InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, cloudCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); /** * @tc.steps:step2. set download asset status failed */ - g_virtualAssetLoader->SetDownloadStatus(CLOUD_ASSET_SPACE_INSUFFICIENT); - Query query = Query::Select().FromTable(g_tables); + testOj->g_virtualAssetLoader->SetDownloadStatus(CLOUD_ASSET_SPACE_INSUFFICIENT); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback = [](const std::map &process) { for (const auto &item: process) { - g_syncProcess = item.second; + testOj->g_syncProcess = item.second; } - if (g_syncProcess.process == FINISHED) { - g_processCondition.notify_one(); + if (testOj->g_syncProcess.process == FINISHED) { + testOj->g_processCondition.notify_one(); } }; /** * @tc.steps:step3. sync and wait sync finished. * @tc.expected: step3. sync return CLOUD_ASSET_SPACE_INSUFFICIENT. */ - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::OK); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); { - std::unique_lock lock(g_processMutex); - g_processCondition.wait(lock, []() { - return g_syncProcess.process == FINISHED; + std::unique_lock lock(testOj->g_processMutex); + testOj->g_processCondition.wait(lock, []() { + return testOj->g_syncProcess.process == FINISHED; }); - ASSERT_EQ(g_syncProcess.errCode, DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); + ASSERT_EQ(testOj->g_syncProcess.errCode, DBStatus::CLOUD_ASSET_SPACE_INSUFFICIENT); } /** * @tc.steps:step4. clear data. */ - g_virtualAssetLoader->SetDownloadStatus(OK); - CloseDb(); + testOj->g_virtualAssetLoader->SetDownloadStatus(OK); + testOj->CloseDb(); } /** @@ -2627,7 +1550,7 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, DownloadAssetTest002, T */ DBStatus expectStatus = DBStatus::CLOUD_ERROR; int index = 0; - InitMockAssetLoader(expectStatus, index); + testOj->InitMockAssetLoader(expectStatus, index); int64_t paddingSize = 1; int localCount = 100; @@ -2635,26 +1558,27 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, DownloadAssetTest002, T * @tc.steps:step2. init download data * @tc.expected: step2. return ok. */ - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, localCount, paddingSize, false); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, localCount, paddingSize, false); /** * @tc.steps:step3. sync * @tc.expected: step3. return ok. */ - Query query = Query::Select().FromTable(g_tables); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); /** * @tc.steps:step4. Those status that are not normal are all be abnormal after sync. * @tc.expected: step4. return ok. */ - CheckAssetAfterDownload2(db, localCount); - CloseDb(); + testOj->CheckAssetAfterDownload2(db, localCount); + testOj->CloseDb(); } /** @@ -2672,36 +1596,38 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, DownloadAssetTest003, T */ int64_t paddingSize = 1; int localCount = 10; - InsertUserTableRecord(db, 0, localCount, paddingSize, false); - InsertCloudTableRecord(0, localCount, paddingSize, false); - Query query = Query::Select().FromTable(g_tables); + testOj->InsertUserTableRecord(db, 0, localCount, paddingSize, false); + testOj->InsertCloudTableRecord(0, localCount, paddingSize, false); + Query query = Query::Select().FromTable(testOj->g_tables); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); /** * @tc.steps:step2. update cloud Asset where gid = 0 * @tc.expected: step2. return ok. */ - UpdateCloudAssetForDownloadAssetTest003(); + testOj->UpdateCloudAssetForDownloadAssetTest003(); /** * @tc.steps:step3. sync again * @tc.expected: step3. return ok. */ - g_syncProcess = {}; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); + testOj->g_syncProcess = {}; + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); /** * @tc.steps:step4. check asset after download where gid = 0 * @tc.expected: step4. return ok. */ - CheckAssetForDownloadAssetTest003(db); - CloseDb(); + testOj->CheckAssetForDownloadAssetTest003(db); + testOj->CloseDb(); } /** @@ -2717,19 +1643,20 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, SchemaTest001, TestSize * @tc.steps:step1. Set different status out of parameters, and the code returns CLOUD_ERROR * @tc.expected: step1. return ok. */ - EXPECT_EQ(RelationalTestUtils::ExecSql(db, INTEGER_PRIMARY_KEY_TABLE_SQL_WRONG_SYNC_MODE), SQLITE_OK); - ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName4, CLOUD_COOPERATION), DBStatus::OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, testOj->INTEGER_PRIMARY_KEY_TABLE_SQL_WRONG_SYNC_MODE), SQLITE_OK); + ASSERT_EQ(testOj->g_delegate->CreateDistributedTable(testOj->g_tableName4, CLOUD_COOPERATION), DBStatus::OK); /** * @tc.steps:step1. Set different status out of parameters, and the code returns CLOUD_ERROR * @tc.expected: step1. return ok. */ - Query query = Query::Select().FromTable({g_tableName4}); + Query query = Query::Select().FromTable({testOj->g_tableName4}); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), DBStatus::OK); - WaitForSyncFinish(g_syncProcess, g_syncWaitTime); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::OK); + testOj->WaitForSyncFinish(testOj->g_syncProcess, testOj->g_syncWaitTime); + testOj->CloseDb(); } /** @@ -2745,19 +1672,19 @@ HWTEST_F(DistributedDBCloudInterfacesRelationalSyncTest, SchemaTest002, TestSize * @tc.steps:step1. Set different status out of parameters, and the code returns CLOUD_ERROR * @tc.expected: step1. return ok. */ - EXPECT_EQ(RelationalTestUtils::ExecSql(db, INTEGER_PRIMARY_KEY_TABLE_SQL_WRONG_SYNC_MODE), SQLITE_OK); - ASSERT_EQ(g_delegate->CreateDistributedTable(g_tableName4, DEVICE_COOPERATION), DBStatus::OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, testOj->INTEGER_PRIMARY_KEY_TABLE_SQL_WRONG_SYNC_MODE), SQLITE_OK); + ASSERT_EQ(testOj->g_delegate->CreateDistributedTable(testOj->g_tableName4, DEVICE_COOPERATION), DBStatus::OK); /** * @tc.steps:step1. Set different status out of parameters, and the code returns CLOUD_ERROR * @tc.expected: step1. return ok. */ - Query query = Query::Select().FromTable({g_tableName4}); + Query query = Query::Select().FromTable({testOj->g_tableName4}); std::vector expectProcess; CloudSyncStatusCallback callback; - GetCallback(g_syncProcess, callback, expectProcess); - ASSERT_EQ(g_delegate->Sync({DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, g_syncWaitTime), - DBStatus::SCHEMA_MISMATCH); - CloseDb(); + testOj->GetCallback(testOj->g_syncProcess, callback, expectProcess); + ASSERT_EQ(testOj->g_delegate->Sync({testOj->DEVICE_CLOUD}, SYNC_MODE_CLOUD_MERGE, query, callback, + testOj->g_syncWaitTime), DBStatus::SCHEMA_MISMATCH); + testOj->CloseDb(); } } diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acc6d614d62978d987978ed8e11920ad57917479 --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.cpp @@ -0,0 +1,1048 @@ +/* + * 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. + */ +#include "distributeddb_cloud_interfaces_relational_sync_testcase.h" + +using namespace DistributedDBUnitTest; +using namespace std; +namespace DistributedDB { +namespace { + int QueryCountCallback(void *data, int count, char **colValue, char **colName) + { + if (count != 1) { + return 0; + } + auto expectCount = reinterpret_cast(data); + EXPECT_EQ(strtol(colValue[0], nullptr, 10), expectCount); // 10: decimal + return 0; + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CreateUserDBAndTable(sqlite3 *&db) +{ + EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_LOCAL_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, INTEGER_PRIMARY_KEY_TABLE_SQL), SQLITE_OK); + EXPECT_EQ(RelationalTestUtils::ExecSql(db, CREATE_LOCAL_TABLE_WITHOUT_PRIMARY_KEY_SQL), SQLITE_OK); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InsertUserTableRecord(sqlite3 *&db, + int64_t begin, int64_t count, int64_t photoSize, bool assetIsNull) +{ + std::string photo(photoSize, 'v'); + int errCode; + std::vector assetBlob; + for (int64_t i = begin; i < begin + count; ++i) { + Asset asset = g_localAsset; + asset.name = asset.name + std::to_string(i); + RuntimeContext::GetInstance()->AssetToBlob(asset, assetBlob); + string sql = "INSERT OR REPLACE INTO " + g_tableName1 + + " (name, height, married, photo, assert, age) VALUES ('Local" + std::to_string(i) + + "', '175.8', '0', '" + photo + "', ? , '18');"; + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + if (assetIsNull) { + ASSERT_EQ(sqlite3_bind_null(stmt, 1), SQLITE_OK); + } else { + ASSERT_EQ(SQLiteUtils::BindBlobToStatement(stmt, 1, assetBlob, false), E_OK); + } + EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); + SQLiteUtils::ResetStatement(stmt, true, errCode); + } + for (int64_t i = begin; i < begin + count; ++i) { + std::vector assets; + Asset asset = g_localAsset; + asset.name = g_localAsset.name + std::to_string(i); + assets.push_back(asset); + asset.name = g_localAsset.name + std::to_string(i + 1); + assets.push_back(asset); + RuntimeContext::GetInstance()->AssetsToBlob(assets, assetBlob); + string sql = "INSERT OR REPLACE INTO " + g_tableName2 + + " (id, name, height, photo, asserts, age) VALUES ('" + std::to_string(i) + "', 'Local" + + std::to_string(i) + "', '155.10', '"+ photo + "', ? , '21');"; + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + if (assetIsNull) { + ASSERT_EQ(sqlite3_bind_null(stmt, 1), E_OK); + } else { + ASSERT_EQ(SQLiteUtils::BindBlobToStatement(stmt, 1, assetBlob, false), E_OK); + } + EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); + SQLiteUtils::ResetStatement(stmt, true, errCode); + } + LOGD("insert user record worker1[primary key]:[Local%" PRId64 " - Local%" PRId64 + ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::UpdateUserTableRecord(sqlite3 *&db, int64_t begin, + int64_t count) +{ + for (size_t i = 0; i < g_tables.size(); i++) { + string updateAge = "UPDATE " + g_tables[i] + " SET age = '99' where " + g_tablesPKey[i] + " in ("; + for (int64_t j = begin; j < begin + count; ++j) { + updateAge += "'" + g_prefix[i] + std::to_string(j) + "',"; + } + updateAge.pop_back(); + updateAge += ");"; + ASSERT_EQ(RelationalTestUtils::ExecSql(db, updateAge), SQLITE_OK); + } + LOGD("update local record worker1[primary key]:[local%" PRId64 " - local%" PRId64 + ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::DeleteUserTableRecord(sqlite3 *&db, int64_t begin, + int64_t count) +{ + for (size_t i = 0; i < g_tables.size(); i++) { + string updateAge = "Delete from " + g_tables[i] + " where " + g_tablesPKey[i] + " in ("; + for (int64_t j = begin; j < count; ++j) { + updateAge += "'" + g_prefix[i] + std::to_string(j) + "',"; + } + updateAge.pop_back(); + updateAge += ");"; + ASSERT_EQ(RelationalTestUtils::ExecSql(db, updateAge), SQLITE_OK); + } + LOGD("delete local record worker1[primary key]:[local%" PRId64 " - local%" PRId64 + ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InsertRecordWithoutPk2LocalAndCloud(sqlite3 *&db, + int64_t begin, int64_t count, int photoSize) +{ + std::vector photo(photoSize, 'v'); + std::string photoLocal(photoSize, 'v'); + Asset asset = { .version = 1, .name = "Phone" }; + std::vector assetBlob; + RuntimeContext::GetInstance()->BlobToAsset(assetBlob, asset); + std::string assetStr(assetBlob.begin(), assetBlob.end()); + std::vector record1; + std::vector extend1; + for (int64_t i = begin; i < count; ++i) { + Timestamp now = TimeHelper::GetSysCurrentTime(); + VBucket data; + data.insert_or_assign("name", "Cloud" + std::to_string(i)); + data.insert_or_assign("height", 166.0); // 166.0 is random double value + data.insert_or_assign("married", (bool)0); + data.insert_or_assign("photo", photo); + data.insert_or_assign("assert", KEY_1); + data.insert_or_assign("age", 13L); + record1.push_back(data); + VBucket log; + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + extend1.push_back(log); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); // wait for 1 ms + } + int errCode = g_virtualCloudDb->BatchInsert(g_tableName3, std::move(record1), extend1); + ASSERT_EQ(errCode, DBStatus::OK); + for (int64_t i = begin; i < count; ++i) { + string sql = "INSERT OR REPLACE INTO " + g_tableName3 + + " (name, height, married, photo, assert, age) VALUES ('Local" + std::to_string(i) + + "', '175.8', '0', '" + photoLocal + "', '" + assetStr + "', '18');"; + ASSERT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InsertCloudTableRecord(int64_t begin, + int64_t count, int64_t photoSize, bool assetIsNull) +{ + std::vector photo(photoSize, 'v'); + std::vector record1; + std::vector extend1; + std::vector record2; + std::vector extend2; + Timestamp now = TimeHelper::GetSysCurrentTime(); + for (int64_t i = begin; i < begin + count; ++i) { + VBucket data; + data.insert_or_assign("name", "Cloud" + std::to_string(i)); + data.insert_or_assign("height", 166.0); // 166.0 is random double value + data.insert_or_assign("married", false); + data.insert_or_assign("photo", photo); + data.insert_or_assign("age", 13L); + Asset asset = g_cloudAsset; + asset.name = asset.name + std::to_string(i); + assetIsNull ? data.insert_or_assign("assert", Nil()) : data.insert_or_assign("assert", asset); + record1.push_back(data); + VBucket log; + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND + i); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND + i); + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + extend1.push_back(log); + + std::vector assets; + data.insert_or_assign("id", i); + data.insert_or_assign("height", 180.3); // 180.3 is random double value + for (int64_t j = i; j <= i + 2; j++) { // 2 extra num + asset.name = g_cloudAsset.name + std::to_string(j); + assets.push_back(asset); + } + data.erase("assert"); + data.erase("married"); + assetIsNull ? data.insert_or_assign("asserts", Nil()) : data.insert_or_assign("asserts", assets); + record2.push_back(data); + extend2.push_back(log); + } + ASSERT_EQ(g_virtualCloudDb->BatchInsert(g_tableName1, std::move(record1), extend1), DBStatus::OK); + ASSERT_EQ(g_virtualCloudDb->BatchInsert(g_tableName2, std::move(record2), extend2), DBStatus::OK); + LOGD("insert cloud record worker1[primary key]:[cloud%" PRId64 " - cloud%" PRId64 + ") , worker2[primary key]:[%" PRId64 "- %" PRId64")", begin, count, begin, count); + std::this_thread::sleep_for(std::chrono::milliseconds(count)); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::UpdateAssetForTest(sqlite3 *&db, AssetOpType opType, + int64_t cloudCount, int64_t rowid) +{ + string sql = "UPDATE " + g_tables[0] + " SET assert = ? where rowid = '" + std::to_string(rowid) + "';"; + std::vector assetBlob; + int errCode; + Asset asset = g_cloudAsset; + asset.name = "Phone" + std::to_string(rowid - cloudCount - 1); + if (opType == AssetOpType::UPDATE) { + asset.uri = "/data/test"; + } else if (opType == AssetOpType::INSERT) { + asset.name = "Test10"; + } + asset.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); + sqlite3_stmt *stmt = nullptr; + RuntimeContext::GetInstance()->AssetToBlob(asset, assetBlob); + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + if (SQLiteUtils::BindBlobToStatement(stmt, 1, assetBlob, false) == E_OK) { + EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); + } + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::UpdateAssetsForTest(sqlite3 *&db, AssetOpType opType, + int64_t rowid) +{ + string sql = "UPDATE " + g_tables[1] + " SET asserts = ? where rowid = '" + std::to_string(rowid) + "';"; + Asset asset1 = g_localAsset; + Asset asset2 = g_localAsset; + Assets assets; + asset1.name = g_localAsset.name + std::to_string(rowid); + asset1.status = static_cast(CloudStorageUtils::FlagToStatus(AssetOpType::NO_CHANGE)); + asset2.name = g_localAsset.name + std::to_string(rowid + 1); + asset2.status = static_cast(CloudStorageUtils::FlagToStatus(AssetOpType::NO_CHANGE)); + if (opType == AssetOpType::UPDATE) { + assets.push_back(asset1); + asset2.uri = "/data/test"; + asset2.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); + assets.push_back(asset2); + } else if (opType == AssetOpType::INSERT) { + assets.push_back(asset1); + assets.push_back(asset2); + Asset asset3; + asset3.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); + asset3.name = "Test10"; + assets.push_back(asset3); + } else if (opType == AssetOpType::DELETE) { + assets.push_back(asset1); + asset2.status = static_cast(CloudStorageUtils::FlagToStatus(opType)); + assets.push_back(asset2); + } else { + assets.push_back(asset1); + assets.push_back(asset2); + } + sqlite3_stmt *stmt = nullptr; + std::vector assetsBlob; + RuntimeContext::GetInstance()->AssetsToBlob(assets, assetsBlob); + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + if (SQLiteUtils::BindBlobToStatement(stmt, 1, assetsBlob, false) == E_OK) { + EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::UpdateLocalAssets(sqlite3 *&db, Assets &assets, int64_t rowid) +{ + string sql = "UPDATE " + g_tables[1] + " SET asserts = ? where rowid = '" + std::to_string(rowid) + "';"; + std::vector assetsBlob; + int errCode; + RuntimeContext::GetInstance()->AssetsToBlob(assets, assetsBlob); + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + if (SQLiteUtils::BindBlobToStatement(stmt, 1, assetsBlob, false) == E_OK) { + EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)); + } + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::UpdateDiffType(int64_t begin) +{ + std::vector hash = {"DEC", "update_", "insert_"}; + std::vector name = { + g_cloudAsset.name + std::to_string(0), + g_cloudAsset.name + std::to_string(1), + g_cloudAsset.name + std::to_string(3) // 3 is insert id + }; + std::vector record; + std::vector extend; + Assets assets; + for (int i = 0; i < 3; i ++) { // 3 is type num + Asset asset = g_cloudAsset; + asset.name = name[i]; + asset.hash = hash[i]; + assets.push_back(asset); + } + VBucket data; + data.insert_or_assign("name", "Cloud" + std::to_string(0)); + data.insert_or_assign("id", 0L); + data.insert_or_assign("asserts", assets); + Timestamp now = TimeHelper::GetSysCurrentTime(); + VBucket log; + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + log.insert_or_assign(CloudDbConstant::GID_FIELD, std::to_string(begin)); + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + record.push_back(data); + extend.push_back(log); + ASSERT_EQ(g_virtualCloudDb->BatchUpdate(g_tableName2, std::move(record), extend), DBStatus::OK); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckDiffTypeAsset(sqlite3 *&db) +{ + std::vector names = { + g_cloudAsset.name + std::to_string(0), + g_cloudAsset.name + std::to_string(1), + g_cloudAsset.name + std::to_string(3) // 3 is insert id + }; + std::string sql = "SELECT asserts from " + g_tables[1] + " WHERE rowid = 0;"; + sqlite3_stmt *stmt = nullptr; + int index = 0; + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_BLOB); + Type cloudValue; + ASSERT_EQ(SQLiteRelationalUtils::GetCloudValueByType(stmt, TYPE_INDEX, 0, cloudValue), E_OK); + std::vector assetsBlob; + Assets assets; + ASSERT_EQ(CloudStorageUtils::GetValueFromOneField(cloudValue, assetsBlob), E_OK); + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAssets(assetsBlob, assets), E_OK); + for (const Asset &asset: assets) { + ASSERT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + ASSERT_EQ(asset.name, names[index++]); + } + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckAssetForAssetTest006() +{ + VBucket extend; + extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); + std::vector data; + g_virtualCloudDb->Query(g_tables[1], extend, data); + for (size_t j = 0; j < data.size(); ++j) { + ASSERT_NE(data[j].find("asserts"), data[j].end()); + ASSERT_TRUE((data[j]["asserts"]).index() == TYPE_INDEX); + Assets &assets = std::get(data[j]["asserts"]); + ASSERT_TRUE(assets.size() > 0); + Asset &asset = assets[0]; + EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + EXPECT_EQ(asset.flag, static_cast(AssetOpType::DELETE)); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckFillAssetForTest10(sqlite3 *&db) +{ + std::string sql = "SELECT assert from " + g_tables[0] + " WHERE rowid in ('27','28','29','30');"; + sqlite3_stmt *stmt = nullptr; + int index = 0; + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + int suffixId = 6; + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + if (index == 0 || index == 1 || index == 3) { // 3 is rowid index of 29 + ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_BLOB); + Type cloudValue; + ASSERT_EQ(SQLiteRelationalUtils::GetCloudValueByType(stmt, TYPE_INDEX, 0, cloudValue), E_OK); + std::vector assetBlob; + Asset asset; + ASSERT_EQ(CloudStorageUtils::GetValueFromOneField(cloudValue, assetBlob), E_OK); + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(assetBlob, asset), E_OK); + ASSERT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + if (index == 0) { + ASSERT_EQ(asset.name, g_cloudAsset.name + std::to_string(suffixId + index)); + } else if (index == 1) { + ASSERT_EQ(asset.name, "Test10"); + } else { + ASSERT_EQ(asset.name, g_cloudAsset.name + std::to_string(suffixId + index)); + ASSERT_EQ(asset.uri, "/data/test"); + } + } else { + ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_NULL); + } + index++; + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckFillAssetsForTest10(sqlite3 *&db) +{ + std::string sql = "SELECT asserts from " + g_tables[1] + " WHERE rowid in ('0','1','2','3');"; + sqlite3_stmt *stmt = nullptr; + int index = 0; + ASSERT_EQ(SQLiteUtils::GetStatement(db, sql, stmt), E_OK); + int insertIndex = 2; + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + ASSERT_EQ(sqlite3_column_type(stmt, 0), SQLITE_BLOB); + Type cloudValue; + ASSERT_EQ(SQLiteRelationalUtils::GetCloudValueByType(stmt, TYPE_INDEX, 0, cloudValue), E_OK); + std::vector assetsBlob; + Assets assets; + ASSERT_EQ(CloudStorageUtils::GetValueFromOneField(cloudValue, assetsBlob), E_OK); + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAssets(assetsBlob, assets), E_OK); + if (index == 0) { + ASSERT_EQ(assets.size(), 2u); + ASSERT_EQ(assets[0].name, g_localAsset.name + std::to_string(index)); + ASSERT_EQ(assets[1].name, g_localAsset.name + std::to_string(index + 1)); + } else if (index == 1) { + ASSERT_EQ(assets.size(), 3u); + ASSERT_EQ(assets[insertIndex].name, "Test10"); + ASSERT_EQ(assets[insertIndex].status, static_cast(AssetStatus::NORMAL)); + } else if (index == 2) { // 2 is the third element + ASSERT_EQ(assets.size(), 1u); + ASSERT_EQ(assets[0].name, g_cloudAsset.name + std::to_string(index)); + } else { + ASSERT_EQ(assets.size(), 2u); + ASSERT_EQ(assets[1].uri, "/data/test"); + ASSERT_EQ(assets[1].status, static_cast(AssetStatus::NORMAL)); + } + index++; + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckDownloadResult(sqlite3 *&db, + std::vector expectCounts, std::string keyStr) +{ + for (size_t i = 0; i < g_tables.size(); ++i) { + string queryDownload = "select count(*) from " + g_tables[i] + " where name " + + " like '" + keyStr + "%'"; + EXPECT_EQ(sqlite3_exec(db, queryDownload.c_str(), QueryCountCallback, + reinterpret_cast(expectCounts[i]), nullptr), SQLITE_OK); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckCloudRecordNum(sqlite3 *&db, + std::vector tableList, std::vector countList) +{ + int i = 0; + for (const auto &tableName: tableList) { + std::string sql = "select count(*) from " + DBCommon::GetLogTableName(tableName) + + " where device = 'cloud'" + " and cloud_gid is not null and cloud_gid != '' and flag & 0x2 = 0;"; + EXPECT_EQ(sqlite3_exec(db, sql.c_str(), QueryCountCallback, + reinterpret_cast(countList[i]), nullptr), SQLITE_OK); + i++; + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckCleanLogNum(sqlite3 *&db, + const std::vector tableList, int count) +{ + for (const auto &tableName: tableList) { + std::string sql1 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + + " where device = 'cloud';"; + EXPECT_EQ(sqlite3_exec(db, sql1.c_str(), QueryCountCallback, + reinterpret_cast(count), nullptr), SQLITE_OK); + std::string sql2 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + + " where cloud_gid " + " is not null and cloud_gid != '';"; + EXPECT_EQ(sqlite3_exec(db, sql2.c_str(), QueryCountCallback, + reinterpret_cast(count), nullptr), SQLITE_OK); + std::string sql3 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + + " where flag & 0x02 = 0;"; + EXPECT_EQ(sqlite3_exec(db, sql3.c_str(), QueryCountCallback, + reinterpret_cast(count), nullptr), SQLITE_OK); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckCleanDataAndLogNum(sqlite3 *&db, + const std::vector tableList, int count, std::vector localNum) +{ + int i = 0; + for (const auto &tableName: tableList) { + std::string sql1 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + + " where device = 'cloud';"; + EXPECT_EQ(sqlite3_exec(db, sql1.c_str(), QueryCountCallback, + reinterpret_cast(count), nullptr), SQLITE_OK); + std::string sql2 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + " where cloud_gid " + " is not null and cloud_gid != '';"; + EXPECT_EQ(sqlite3_exec(db, sql2.c_str(), QueryCountCallback, + reinterpret_cast(count), nullptr), SQLITE_OK); + std::string sql3 = "select count(*) from " + DBCommon::GetLogTableName(tableName) + + " where flag & 0x02 = 0;"; + EXPECT_EQ(sqlite3_exec(db, sql3.c_str(), QueryCountCallback, + reinterpret_cast(count), nullptr), SQLITE_OK); + std::string local_sql = "select count(*) from " + tableName +";"; + EXPECT_EQ(sqlite3_exec(db, local_sql.c_str(), QueryCountCallback, + reinterpret_cast(localNum[i]), nullptr), SQLITE_OK); + i++; + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckCloudTotalCount(std::vector expectCounts) +{ + VBucket extend; + extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); + for (size_t i = 0; i < g_tables.size(); ++i) { + int64_t realCount = 0; + std::vector data; + g_virtualCloudDb->Query(g_tables[i], extend, data); + for (size_t j = 0; j < data.size(); ++j) { + auto entry = data[j].find(CloudDbConstant::DELETE_FIELD); + if (entry != data[j].end() && std::get(entry->second)) { + continue; + } + realCount++; + } + EXPECT_EQ(realCount, expectCounts[i]); // ExpectCount represents the total amount of cloud data. + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::GetCloudDbSchema(DataBaseSchema &dataBaseSchema) +{ + TableSchema tableSchema1 = { + .name = g_tableName1, + .fields = g_cloudFiled1 + }; + TableSchema tableSchema2 = { + .name = g_tableName2, + .fields = g_cloudFiled2 + }; + TableSchema tableSchemaWithOutPrimaryKey = { + .name = g_tableName3, + .fields = g_cloudFiledWithOutPrimaryKey3 + }; + TableSchema tableSchema4 = { + .name = g_tableName4, + .fields = g_cloudFiled2 + }; + dataBaseSchema.tables.push_back(tableSchema1); + dataBaseSchema.tables.push_back(tableSchema2); + dataBaseSchema.tables.push_back(tableSchemaWithOutPrimaryKey); + dataBaseSchema.tables.push_back(tableSchema4); +} + + +void DistributedDBCloudInterfacesRelationalSyncTestcase::GetInvalidCloudDbSchema(DataBaseSchema &dataBaseSchema) +{ + TableSchema tableSchema1 = { + .name = g_tableName1, + .fields = g_invalidCloudFiled1 + }; + TableSchema tableSchema2 = { + .name = g_tableName1, + .fields = g_cloudFiled2 + }; + dataBaseSchema.tables.push_back(tableSchema1); + dataBaseSchema.tables.push_back(tableSchema2); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InitProcessForTest1(const uint32_t &cloudCount, + const uint32_t &localCount, std::vector &expectProcess) +{ + expectProcess.clear(); + std::vector infos; + uint32_t index = 1; + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} + }); + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} + }); + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} + }); + + for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { + SyncProcess syncProcess; + syncProcess.errCode = OK; + syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; + syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); + syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); + expectProcess.push_back(syncProcess); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InitProcessForMannualSync1( + std::vector &expectProcess) +{ + expectProcess.clear(); + std::vector infos; + // first notify, first table + infos.push_back(TableProcessInfo{ + FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + // first notify, second table + infos.push_back(TableProcessInfo{ + PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + // second notify, first table + infos.push_back(TableProcessInfo{ + FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + // second notify, second table + infos.push_back(TableProcessInfo{ + FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + // second notify, second table + infos.push_back(TableProcessInfo{ + FINISHED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { + SyncProcess syncProcess; + syncProcess.errCode = OK; + syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; + syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); + syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); + expectProcess.push_back(syncProcess); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InitProcessForCleanCloudData1(const uint32_t &cloudCount, + std::vector &expectProcess) +{ + // cloudCount also means data count in one batch + expectProcess.clear(); + std::vector infos; + uint32_t index = 1; + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { + SyncProcess syncProcess; + syncProcess.errCode = OK; + syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; + syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); + syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); + expectProcess.push_back(syncProcess); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InitProcessForTest2(const uint32_t &cloudCount, + const uint32_t &localCount, std::vector &expectProcess) +{ + expectProcess.clear(); + std::vector infos; + uint32_t index = 1; + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} + }); + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount, localCount, 0} + }); + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {index, localCount - cloudCount, localCount - cloudCount, 0} + }); + + for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { + SyncProcess syncProcess; + syncProcess.errCode = OK; + syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; + syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); + syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); + expectProcess.push_back(syncProcess); + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InitProcessForTest9(const uint32_t &cloudCount, + const uint32_t &localCount, std::vector &expectProcess) +{ + expectProcess.clear(); + std::vector infos; + uint32_t index = 1; + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PREPARED, {0, 0, 0, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + PROCESSING, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + infos.push_back(TableProcessInfo{ + FINISHED, {index, cloudCount, cloudCount, 0}, {0, 0, 0, 0} + }); + + for (size_t i = 0; i < infos.size() / g_arrayHalfSub; ++i) { + SyncProcess syncProcess; + syncProcess.errCode = OK; + syncProcess.process = i == infos.size() ? FINISHED : PROCESSING; + syncProcess.tableProcess.insert_or_assign(g_tables[0], std::move(infos[g_arrayHalfSub * i])); + syncProcess.tableProcess.insert_or_assign(g_tables[1], std::move(infos[g_arrayHalfSub * i + 1])); + expectProcess.push_back(syncProcess); + } +} +void DistributedDBCloudInterfacesRelationalSyncTestcase::GetCallback(SyncProcess &syncProcess, + CloudSyncStatusCallback &callback, std::vector &expectProcess) +{ + g_syncIndex = 0; + callback = [&syncProcess, &expectProcess, this](const std::map &process) { + LOGI("devices size = %d", process.size()); + ASSERT_EQ(process.size(), 1u); + syncProcess = std::move(process.begin()->second); + ASSERT_EQ(process.begin()->first, DEVICE_CLOUD); + ASSERT_NE(syncProcess.tableProcess.empty(), true); + LOGI("current sync process status:%d, db status:%d ", syncProcess.process, syncProcess.errCode); + std::for_each(g_tables.begin(), g_tables.end(), [&](const auto &item) { + auto table1 = syncProcess.tableProcess.find(item); + if (table1 != syncProcess.tableProcess.end()) { + LOGI("table[%s], table process status:%d, [downloadInfo](batchIndex:%u, total:%u, successCount:%u, " + "failCount:%u) [uploadInfo](batchIndex:%u, total:%u, successCount:%u,failCount:%u", + item.c_str(), table1->second.process, table1->second.downLoadInfo.batchIndex, + table1->second.downLoadInfo.total, table1->second.downLoadInfo.successCount, + table1->second.downLoadInfo.failCount, table1->second.upLoadInfo.batchIndex, + table1->second.upLoadInfo.total, table1->second.upLoadInfo.successCount, + table1->second.upLoadInfo.failCount); + } + }); + if (expectProcess.empty()) { + if (syncProcess.process == FINISHED) { + g_processCondition.notify_one(); + } + return; + } + ASSERT_LE(static_cast(g_syncIndex), expectProcess.size()); + for (size_t i = 0; i < g_tables.size(); ++i) { + SyncProcess head = expectProcess[g_syncIndex]; + for (auto &expect : head.tableProcess) { + auto real = syncProcess.tableProcess.find(expect.first); + ASSERT_NE(real, syncProcess.tableProcess.end()); + EXPECT_EQ(expect.second.process, real->second.process); + EXPECT_EQ(expect.second.downLoadInfo.batchIndex, real->second.downLoadInfo.batchIndex); + EXPECT_EQ(expect.second.downLoadInfo.total, real->second.downLoadInfo.total); + EXPECT_EQ(expect.second.downLoadInfo.successCount, real->second.downLoadInfo.successCount); + EXPECT_EQ(expect.second.downLoadInfo.failCount, real->second.downLoadInfo.failCount); + EXPECT_EQ(expect.second.upLoadInfo.batchIndex, real->second.upLoadInfo.batchIndex); + EXPECT_EQ(expect.second.upLoadInfo.total, real->second.upLoadInfo.total); + EXPECT_EQ(expect.second.upLoadInfo.successCount, real->second.upLoadInfo.successCount); + EXPECT_EQ(expect.second.upLoadInfo.failCount, real->second.upLoadInfo.failCount); + } + } + g_syncIndex++; + if (syncProcess.process == FINISHED) { + g_processCondition.notify_one(); + } + }; +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckAllAssetAfterUpload(int64_t localCount) +{ + VBucket extend; + extend[CloudDbConstant::CURSOR_FIELD] = std::to_string(0); + std::vector data1; + g_virtualCloudDb->Query(g_tables[0], extend, data1); + for (size_t j = 0; j < data1.size(); ++j) { + auto entry = data1[j].find("assert"); + ASSERT_NE(entry, data1[j].end()); + Asset asset = std::get(entry->second); + bool isLocal = j >= (size_t)(localCount / g_arrayHalfSub); + Asset baseAsset = isLocal ? g_localAsset : g_cloudAsset; + EXPECT_EQ(asset.version, baseAsset.version); + EXPECT_EQ(asset.name, baseAsset.name + std::to_string(isLocal ? j - localCount / g_arrayHalfSub : j)); + EXPECT_EQ(asset.uri, baseAsset.uri); + EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); + EXPECT_EQ(asset.createTime, baseAsset.createTime); + EXPECT_EQ(asset.size, baseAsset.size); + EXPECT_EQ(asset.hash, baseAsset.hash); + } + + std::vector data2; + g_virtualCloudDb->Query(g_tables[1], extend, data2); + for (size_t j = 0; j < data2.size(); ++j) { + auto entry = data2[j].find("asserts"); + ASSERT_NE(entry, data2[j].end()); + Assets assets = std::get(entry->second); + Asset baseAsset = j >= (size_t)(localCount / g_arrayHalfSub) ? g_localAsset : g_cloudAsset; + int index = j; + for (const auto &asset: assets) { + EXPECT_EQ(asset.version, baseAsset.version); + EXPECT_EQ(asset.name, baseAsset.name + std::to_string(index++)); + EXPECT_EQ(asset.uri, baseAsset.uri); + EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); + EXPECT_EQ(asset.createTime, baseAsset.createTime); + EXPECT_EQ(asset.size, baseAsset.size); + EXPECT_EQ(asset.hash, baseAsset.hash); + } + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckAssetsAfterDownload(sqlite3 *&db, int64_t localCount) +{ + string queryDownload = "select asserts from " + g_tables[1] + " where rowid in ("; + for (int64_t i = 0; i < localCount; ++i) { + queryDownload += "'" + std::to_string(i) + "',"; + } + queryDownload.pop_back(); + queryDownload += ");"; + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); + int index = 0; + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + std::vector blobValue; + ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); + Assets assets; + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAssets(blobValue, assets), E_OK); + bool isLocal = index >= localCount / g_arrayHalfSub; + Asset baseAsset = isLocal ? g_localAsset : g_cloudAsset; + int nameIndex = index; + for (const auto &asset: assets) { + EXPECT_EQ(asset.version, baseAsset.version); + EXPECT_EQ(asset.name, baseAsset.name + std::to_string(nameIndex)); + EXPECT_EQ(asset.uri, baseAsset.uri); + EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); + EXPECT_EQ(asset.createTime, baseAsset.createTime); + EXPECT_EQ(asset.size, baseAsset.size); + EXPECT_EQ(asset.hash, baseAsset.hash); + EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + nameIndex++; + } + index++; + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckAssetAfterDownload(sqlite3 *&db, int64_t localCount) +{ + string queryDownload = "select assert from " + g_tables[0] + " where rowid in ("; + for (int64_t i = 0; i < localCount; ++i) { + queryDownload += "'" + std::to_string(i) + "',"; + } + queryDownload.pop_back(); + queryDownload += ");"; + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); + int index = 0; + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + std::vector blobValue; + ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); + Asset asset; + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(blobValue, asset), E_OK); + bool isCloud = index >= localCount; + Asset baseAsset = isCloud ? g_cloudAsset : g_localAsset; + EXPECT_EQ(asset.version, baseAsset.version); + EXPECT_EQ(asset.name, + baseAsset.name + std::to_string(isCloud ? index - localCount / g_arrayHalfSub : index)); + EXPECT_EQ(asset.uri, baseAsset.uri); + EXPECT_EQ(asset.modifyTime, baseAsset.modifyTime); + EXPECT_EQ(asset.createTime, baseAsset.createTime); + EXPECT_EQ(asset.size, baseAsset.size); + EXPECT_EQ(asset.hash, baseAsset.hash); + EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + index++; + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::UpdateCloudAssetForDownloadAssetTest003() +{ + VBucket data; + std::vector photo(1, 'x'); + data.insert_or_assign("name", "Cloud" + std::to_string(0)); + data.insert_or_assign("photo", photo); + data.insert_or_assign("assert", g_cloudAsset); + Timestamp now = TimeHelper::GetSysCurrentTime(); + VBucket log; + std::vector record; + std::vector extend; + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + log.insert_or_assign(CloudDbConstant::GID_FIELD, std::to_string(0)); + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + record.push_back(data); + extend.push_back(log); + ASSERT_EQ(g_virtualCloudDb->BatchUpdate(g_tableName1, std::move(record), extend), DBStatus::OK); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckAssetForDownloadAssetTest003(sqlite3 *&db) +{ + string queryDownload = "select assert from " + g_tables[0] + " where rowid = '11';"; + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); + int index = 0; + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + std::vector blobValue; + ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); + Asset asset; + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(blobValue, asset), E_OK); + EXPECT_EQ(asset.name, g_cloudAsset.name); + EXPECT_EQ(asset.hash, g_cloudAsset.hash); + EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + index++; + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CheckAssetAfterDownload2(sqlite3 *&db, int64_t localCount) +{ + string queryDownload = "select assert from " + g_tables[0] + " where rowid in ("; + for (int64_t i = localCount + 1; i < localCount + localCount; ++i) { + queryDownload += "'" + std::to_string(i) + "',"; + } + queryDownload.pop_back(); + queryDownload += ");"; + sqlite3_stmt *stmt = nullptr; + ASSERT_EQ(SQLiteUtils::GetStatement(db, queryDownload, stmt), E_OK); + int index = 0; + while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) { + std::vector blobValue; + ASSERT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, blobValue), E_OK); + Asset asset; + ASSERT_EQ(RuntimeContext::GetInstance()->BlobToAsset(blobValue, asset), E_OK); + EXPECT_EQ(asset.version, g_cloudAsset.version); + if (index % 6u == 0) { // 6 is AssetStatus type num, include invalid type + EXPECT_EQ(asset.status, static_cast(AssetStatus::NORMAL)); + } else { + EXPECT_EQ(asset.status, static_cast(AssetStatus::ABNORMAL)); + } + index++; + } + int errCode; + SQLiteUtils::ResetStatement(stmt, true, errCode); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::WaitForSyncFinish(SyncProcess &syncProcess, + const int64_t &waitTime) +{ + std::unique_lock lock(g_processMutex); + bool result = g_processCondition.wait_for(lock, std::chrono::seconds(waitTime), [&syncProcess]() { + return syncProcess.process == FINISHED; + }); + ASSERT_EQ(result, true); + LOGD("-------------------sync end--------------"); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::CloseDb() +{ + delete g_observer; + g_virtualCloudDb = nullptr; + if (g_delegate != nullptr) { + EXPECT_EQ(g_mgr->CloseStore(g_delegate), DBStatus::OK); + g_delegate = nullptr; + } +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InitMockAssetLoader(DBStatus &status, int &index) +{ + std::shared_ptr assetLoader = make_shared(); + ASSERT_EQ(g_delegate->SetIAssetLoader(assetLoader), DBStatus::OK); + EXPECT_CALL(*assetLoader, Download(testing::_, testing::_, testing::_, testing::_)) + .WillRepeatedly([&status, &index](const std::string &, const std::string &gid, const Type &, + std::map &assets) { + LOGD("Download GID:%s", gid.c_str()); + for (auto &item: assets) { + for (auto &asset: item.second) { + EXPECT_EQ(asset.status, static_cast(AssetStatus::DOWNLOADING)); + LOGD("asset [name]:%s, [status]:%u, [flag]:%u", asset.name.c_str(), asset.status, asset.flag); + asset.status = (index++) % 6u; // 6 is AssetStatus type num, include invalid type + } + } + return status; + }); +} + +void DistributedDBCloudInterfacesRelationalSyncTestcase::InsertCloudForCloudProcessNotify001( + std::vector &record, std::vector &extend) +{ + VBucket data; + std::vector photo(10, 'v'); + data.insert_or_assign("name", "Local" + std::to_string(0)); + data.insert_or_assign("height", 166.0); // 166.0 is random double value + data.insert_or_assign("married", false); + data.insert_or_assign("age", 13L); + data.insert_or_assign("photo", photo); + data.insert_or_assign("assert", Nil()); + record.push_back(data); + VBucket log; + Timestamp now = TimeHelper::GetSysCurrentTime(); + log.insert_or_assign(CloudDbConstant::CREATE_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + log.insert_or_assign(CloudDbConstant::MODIFY_FIELD, (int64_t)now / CloudDbConstant::TEN_THOUSAND); + log.insert_or_assign(CloudDbConstant::DELETE_FIELD, false); + log.insert_or_assign("#_gid", std::to_string(2)); + extend.push_back(log); +} +} \ No newline at end of file diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.h b/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.h new file mode 100644 index 0000000000000000000000000000000000000000..a051be17db8c551f0c2f13440b933c1bb85ca317 --- /dev/null +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/cloud/distributeddb_cloud_interfaces_relational_sync_testcase.h @@ -0,0 +1,179 @@ +/* + * 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. + */ + +#ifndef DISTRIBUTEDDB_CLOUD_INTERFACES_RELATIONAL_SYNC_TESTCASE_H +#define DISTRIBUTEDDB_CLOUD_INTERFACES_RELATIONAL_SYNC_TESTCASE_H + +#include +#include +#include "cloud/cloud_storage_utils.h" +#include "cloud_db_constant.h" +#include "distributeddb_data_generate_unit_test.h" +#include "distributeddb_tools_unit_test.h" +#include "process_system_api_adapter_impl.h" +#include "relational_store_instance.h" +#include "relational_store_manager.h" +#include "runtime_config.h" +#include "sqlite_relational_store.h" +#include "sqlite_relational_utils.h" +#include "store_observer.h" +#include "time_helper.h" +#include "virtual_asset_loader.h" +#include "virtual_cloud_data_translate.h" +#include "virtual_cloud_db.h" +#include "mock_asset_loader.h" + +namespace DistributedDB { +using CloudSyncStatusCallback = std::function &onProcess)>; + +class DistributedDBCloudInterfacesRelationalSyncTestcase final { +public: + DistributedDBCloudInterfacesRelationalSyncTestcase() {}; + ~DistributedDBCloudInterfacesRelationalSyncTestcase() {}; + + void CreateUserDBAndTable(sqlite3 *&db); + void CheckAssetForDownloadAssetTest003(sqlite3 *&db); + void CheckAssetAfterDownload2(sqlite3 *&db, int64_t localCount); + void InitProcessForTest9(const uint32_t &cloudCount, const uint32_t &localCount, + std::vector &expectProcess); + void InitProcessForTest2(const uint32_t &cloudCount, const uint32_t &localCount, + std::vector &expectProcess); + void UpdateLocalAssets(sqlite3 *&db, Assets &assets, int64_t rowid); + void CheckCleanDataAndLogNum(sqlite3 *&db, const std::vector tableList, int count, + std::vector localNum); + void InitProcessForTest001(const uint32_t &cloudCount, const uint32_t &localCount, + std::vector &expectProcess); + void InitProcessForMannualSync1(std::vector &expectProcess); + void InitProcessForCleanCloudData1(const uint32_t &cloudCount, + std::vector &expectProcess); + void InsertRecordWithoutPk2LocalAndCloud(sqlite3 *&db, + int64_t begin, int64_t count, int photoSize); + void InsertUserTableRecord(sqlite3 *&db, int64_t begin, int64_t count, int64_t photoSize, bool assetIsNull); + void UpdateUserTableRecord(sqlite3 *&db, int64_t begin, int64_t count); + void GetCloudDbSchema(DataBaseSchema &dataBaseSchema); + void InitMockAssetLoader(DBStatus &status, int &index); + void CloseDb(); + void WaitForSyncFinish(SyncProcess &syncProcess, const int64_t &waitTime); + void UpdateCloudAssetForDownloadAssetTest003(); + void CheckAssetAfterDownload(sqlite3 *&db, int64_t localCount); + void CheckAssetsAfterDownload(sqlite3 *&db, int64_t localCount); + void CheckAllAssetAfterUpload(int64_t localCount); + void GetCallback(SyncProcess &syncProcess, + CloudSyncStatusCallback &callback, std::vector &expectProcess); + void InitProcessForTest1(const uint32_t &cloudCount, const uint32_t &localCount, + std::vector &expectProcess); + void GetInvalidCloudDbSchema(DataBaseSchema &dataBaseSchema); + void CheckCloudTotalCount(std::vector expectCounts); + void CheckCleanLogNum(sqlite3 *&db, const std::vector tableList, int count); + void CheckCloudRecordNum(sqlite3 *&db, std::vector tableList, std::vector countList); + void CheckDownloadResult(sqlite3 *&db, std::vector expectCounts, std::string keyStr = "Cloud"); + void CheckFillAssetsForTest10(sqlite3 *&db); + void CheckFillAssetForTest10(sqlite3 *&db); + void CheckAssetForAssetTest006(); + void CheckDiffTypeAsset(sqlite3 *&db); + void UpdateDiffType(int64_t begin); + void UpdateAssetsForTest(sqlite3 *&db, AssetOpType opType, int64_t rowid); + void UpdateAssetForTest(sqlite3 *&db, AssetOpType opType, int64_t cloudCount, int64_t rowid); + void InsertCloudTableRecord(int64_t begin, int64_t count, int64_t photoSize, bool assetIsNull); + void DeleteUserTableRecord(sqlite3 *&db, int64_t begin, int64_t count); + void InsertCloudForCloudProcessNotify001(std::vector &record, std::vector &extend); + + const std::string g_tableName1 = "worker1"; + const std::string g_tableName2 = "worker2"; + const std::string g_tableName3 = "worker3"; + const std::string g_tableName4 = "worker4"; + const std::string CREATE_LOCAL_TABLE_SQL = + "CREATE TABLE IF NOT EXISTS " + g_tableName1 + "(" \ + "name TEXT PRIMARY KEY," \ + "height REAL ," \ + "married BOOLEAN ," \ + "photo BLOB NOT NULL," \ + "assert BLOB," \ + "age INT);"; + const std::string INTEGER_PRIMARY_KEY_TABLE_SQL = + "CREATE TABLE IF NOT EXISTS " + g_tableName2 + "(" \ + "id INTEGER PRIMARY KEY," \ + "name TEXT ," \ + "height REAL ," \ + "photo BLOB ," \ + "asserts BLOB," \ + "age INT);"; + const std::string CREATE_LOCAL_TABLE_WITHOUT_PRIMARY_KEY_SQL = + "CREATE TABLE IF NOT EXISTS " + g_tableName3 + "(" \ + "name TEXT," \ + "height REAL ," \ + "married BOOLEAN ," \ + "photo BLOB NOT NULL," \ + "assert BLOB," \ + "age INT);"; + const std::string INTEGER_PRIMARY_KEY_TABLE_SQL_WRONG_SYNC_MODE = + "CREATE TABLE IF NOT EXISTS " + g_tableName4 + "(" \ + "id INTEGER PRIMARY KEY," \ + "name TEXT ," \ + "height REAL ," \ + "photo BLOB ," \ + "asserts BLOB," \ + "age INT);"; + const std::vector g_cloudFiled1 = { + {"name", TYPE_INDEX, true}, {"height", TYPE_INDEX}, + {"married", TYPE_INDEX}, {"photo", TYPE_INDEX, false, false}, + {"assert", TYPE_INDEX}, {"age", TYPE_INDEX} + }; + const std::vector g_invalidCloudFiled1 = { + {"name", TYPE_INDEX, true}, {"height", TYPE_INDEX}, + {"married", TYPE_INDEX}, {"photo", TYPE_INDEX, false, false}, + {"assert", TYPE_INDEX}, {"age", TYPE_INDEX} + }; + const std::vector g_cloudFiled2 = { + {"id", TYPE_INDEX, true}, {"name", TYPE_INDEX}, + {"height", TYPE_INDEX}, {"photo", TYPE_INDEX}, + {"asserts", TYPE_INDEX}, {"age", TYPE_INDEX} + }; + const std::vector g_cloudFiledWithOutPrimaryKey3 = { + {"name", TYPE_INDEX, false, true}, {"height", TYPE_INDEX}, + {"married", TYPE_INDEX}, {"photo", TYPE_INDEX, false, false}, + {"assert", TYPE_INDEX}, {"age", TYPE_INDEX} + }; + const std::vector g_tablesPKey = {g_cloudFiled1[0].colName, g_cloudFiled2[0].colName}; + const std::vector g_prefix = {"Local", ""}; + const Asset g_localAsset = { + .version = 1, .name = "Phone", .assetId = "0", .subpath = "/local/sync", .uri = "/local/sync", + .modifyTime = "123456", .createTime = "", .size = "256", .hash = "ASE" + }; + const Asset g_cloudAsset = { + .version = 2, .name = "Phone", .assetId = "0", .subpath = "/local/sync", .uri = "/cloud/sync", + .modifyTime = "123456", .createTime = "0", .size = "1024", .hash = "DEC" + }; + const std::vector g_tables = {g_tableName1, g_tableName2}; + const std::string g_storeID = "Relational_Store_SYNC"; + const std::string DB_SUFFIX = ".db"; + const std::string DEVICE_CLOUD = "cloud_dev"; + const int64_t g_syncWaitTime = 10; + const int g_arrayHalfSub = 2; + + std::string g_testDir; + std::string g_storePath; + std::shared_ptr g_virtualCloudDb; + DistributedDBUnitTest::RelationalStoreObserverUnitTest *g_observer = nullptr; + RelationalStoreDelegate *g_delegate = nullptr; + std::shared_ptr g_virtualAssetLoader; + SyncProcess g_syncProcess; + int g_syncIndex = 0; + std::mutex g_processMutex; + std::condition_variable g_processCondition; + std::shared_ptr g_mgr; +}; +} +#endif //DISTRIBUTEDDB_CLOUD_INTERFACES_RELATIONAL_SYNC_TESTCASE_H