1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16
17 #include "cloud_db_sync_utils_test.h"
18 #include "distributeddb_data_generate_unit_test.h"
19 #include "distributeddb_tools_unit_test.h"
20 #include "rdb_data_generator.h"
21 #include "relational_store_client.h"
22 #include "relational_store_manager.h"
23 #include "relational_virtual_device.h"
24 #include "virtual_communicator_aggregator.h"
25
26 using namespace testing::ext;
27 using namespace DistributedDB;
28 using namespace DistributedDBUnitTest;
29 using namespace std;
30
31 namespace {
32 string g_testDir;
33
34 class DistributedDBRDBCollaborationTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40 protected:
41 static DataBaseSchema GetSchema();
42 static TableSchema GetTableSchema(bool upgrade = false, bool pkInStr = false);
43 void InitStore();
44 void InitDelegate(DistributedTableMode mode = DistributedTableMode::COLLABORATION);
45 void CloseDb();
46 std::string storePath_;
47 sqlite3 *db_ = nullptr;
48 RelationalStoreDelegate *delegate_ = nullptr;
49 VirtualCommunicatorAggregator *communicatorAggregator_ = nullptr;
50 RelationalVirtualDevice *deviceB_ = nullptr;
51 RelationalStoreObserverUnitTest *delegateObserver_ = nullptr;
52
53 static constexpr const char *DEVICE_SYNC_TABLE = "DEVICE_SYNC_TABLE";
54 static constexpr const char *DEVICE_SYNC_TABLE_UPGRADE = "DEVICE_SYNC_TABLE_UPGRADE";
55 static constexpr const char *DEVICE_SYNC_TABLE_AUTOINCREMENT = "DEVICE_SYNC_TABLE_AUTOINCREMENT";
56 static constexpr const char *CLOUD_SYNC_TABLE = "CLOUD_SYNC_TABLE";
57 };
58
SetUpTestCase()59 void DistributedDBRDBCollaborationTest::SetUpTestCase()
60 {
61 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
62 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
63 LOGE("rm test db files error!");
64 }
65 }
66
TearDownTestCase()67 void DistributedDBRDBCollaborationTest::TearDownTestCase()
68 {
69 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
70 LOGE("rm test db files error!");
71 }
72 }
73
SetUp()74 void DistributedDBRDBCollaborationTest::SetUp()
75 {
76 DistributedDBToolsUnitTest::PrintTestCaseInfo();
77 InitStore();
78 communicatorAggregator_ = new (std::nothrow) VirtualCommunicatorAggregator();
79 ASSERT_TRUE(communicatorAggregator_ != nullptr);
80 RuntimeContext::GetInstance()->SetCommunicatorAggregator(communicatorAggregator_);
81
82 deviceB_ = new (std::nothrow) RelationalVirtualDevice(UnitTestCommonConstant::DEVICE_B);
83 ASSERT_NE(deviceB_, nullptr);
84 auto syncInterfaceB = new (std::nothrow) VirtualRelationalVerSyncDBInterface();
85 ASSERT_NE(syncInterfaceB, nullptr);
86 ASSERT_EQ(deviceB_->Initialize(communicatorAggregator_, syncInterfaceB), E_OK);
87 }
88
TearDown()89 void DistributedDBRDBCollaborationTest::TearDown()
90 {
91 CloseDb();
92 if (deviceB_ != nullptr) {
93 delete deviceB_;
94 deviceB_ = nullptr;
95 }
96 if (delegateObserver_ != nullptr) {
97 delete delegateObserver_;
98 delegateObserver_ = nullptr;
99 }
100 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
101 LOGE("rm test db files error.");
102 }
103 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
104 communicatorAggregator_ = nullptr;
105 }
106
GetSchema()107 DataBaseSchema DistributedDBRDBCollaborationTest::GetSchema()
108 {
109 DataBaseSchema schema;
110 auto table = GetTableSchema();
111 schema.tables.push_back(table);
112 table.name = CLOUD_SYNC_TABLE;
113 schema.tables.push_back(table);
114 schema.tables.push_back(GetTableSchema(true));
115 return schema;
116 }
117
GetTableSchema(bool upgrade,bool pkInStr)118 TableSchema DistributedDBRDBCollaborationTest::GetTableSchema(bool upgrade, bool pkInStr)
119 {
120 TableSchema tableSchema;
121 tableSchema.name = upgrade ? DEVICE_SYNC_TABLE_UPGRADE : DEVICE_SYNC_TABLE;
122 Field field;
123 if (!pkInStr) {
124 field.primary = true;
125 }
126 field.type = TYPE_INDEX<int64_t>;
127 field.colName = "pk";
128 tableSchema.fields.push_back(field);
129 field.primary = false;
130 field.colName = "int_field1";
131 tableSchema.fields.push_back(field);
132 field.colName = "int_field2";
133 tableSchema.fields.push_back(field);
134 field.colName = "123";
135 field.type = TYPE_INDEX<std::string>;
136 if (pkInStr) {
137 field.primary = true;
138 }
139 tableSchema.fields.push_back(field);
140 if (upgrade) {
141 field.primary = false;
142 field.colName = "int_field_upgrade";
143 field.type = TYPE_INDEX<int64_t>;
144 tableSchema.fields.push_back(field);
145 }
146 return tableSchema;
147 }
148
InitStore()149 void DistributedDBRDBCollaborationTest::InitStore()
150 {
151 if (storePath_.empty()) {
152 storePath_ = g_testDir + "/" + STORE_ID_1 + ".db";
153 }
154 db_ = RelationalTestUtils::CreateDataBase(storePath_);
155 ASSERT_NE(db_, nullptr);
156 auto schema = GetSchema();
157 EXPECT_EQ(RDBDataGenerator::InitDatabase(schema, *db_), SQLITE_OK);
158 }
159
InitDelegate(DistributedTableMode mode)160 void DistributedDBRDBCollaborationTest::InitDelegate(DistributedTableMode mode)
161 {
162 if (delegateObserver_ == nullptr) {
163 delegateObserver_ = new (std::nothrow) RelationalStoreObserverUnitTest();
164 ASSERT_NE(delegateObserver_, nullptr);
165 delegateObserver_->SetCallbackDetailsType(static_cast<uint32_t>(CallbackDetailsType::DETAILED));
166 }
167 RelationalStoreManager mgr(APP_ID, USER_ID);
168 RelationalStoreDelegate::Option option;
169 option.tableMode = mode;
170 option.observer = delegateObserver_;
171 ASSERT_EQ(mgr.OpenStore(storePath_, STORE_ID_1, option, delegate_), OK);
172 ASSERT_NE(delegate_, nullptr);
173 }
174
CloseDb()175 void DistributedDBRDBCollaborationTest::CloseDb()
176 {
177 if (db_ != nullptr) {
178 sqlite3_close_v2(db_);
179 db_ = nullptr;
180 }
181 if (delegate_ != nullptr) {
182 RelationalStoreManager mgr(APP_ID, USER_ID);
183 EXPECT_EQ(mgr.CloseStore(delegate_), OK);
184 delegate_ = nullptr;
185 }
186 }
187
188 /**
189 * @tc.name: SetSchema001
190 * @tc.desc: Test set distributed schema.
191 * @tc.type: FUNC
192 * @tc.require:
193 * @tc.author: zqq
194 */
195 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema001, TestSize.Level0)
196 {
197 /**
198 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
199 * @tc.expected: step1.ok
200 */
201 ASSERT_NO_FATAL_FAILURE(InitDelegate());
202 auto schema = GetSchema();
203 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
204 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
205 EXPECT_EQ(delegate_->CreateDistributedTable(CLOUD_SYNC_TABLE, TableSyncType::CLOUD_COOPERATION), OK);
206 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", CLOUD_SYNC_TABLE);
207 auto distributedSchema = RDBDataGenerator::ParseSchema(schema);
208 for (auto &table : distributedSchema.tables) {
209 for (auto &field : table.fields) {
210 field.isSpecified = false;
211 }
212 }
213 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
214 /**
215 * @tc.steps: step2. Insert update delete local
216 * @tc.expected: step2.ok
217 */
218 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
__anoneb31f3900202(ClientChangedData &changedData) 219 EXPECT_EQ(RegisterClientObserver(db_, [](ClientChangedData &changedData) {
220 for (const auto &table : changedData.tableData) {
221 EXPECT_FALSE(table.second.isTrackedDataChange);
222 EXPECT_TRUE(table.second.isP2pSyncDataChange);
223 }
224 }), OK);
225 EXPECT_EQ(RDBDataGenerator::UpdateLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
226 UnRegisterClientObserver(db_);
227 }
228
229 /**
230 * @tc.name: SetSchema002
231 * @tc.desc: Test upgrade table mode.
232 * @tc.type: FUNC
233 * @tc.require:
234 * @tc.author: zqq
235 */
236 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema002, TestSize.Level0)
237 {
238 /**
239 * @tc.steps: step1. Create cloud table in SPLIT_BY_DEVICE
240 * @tc.expected: step1.ok
241 */
242 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::SPLIT_BY_DEVICE));
243 EXPECT_EQ(delegate_->CreateDistributedTable(CLOUD_SYNC_TABLE, TableSyncType::CLOUD_COOPERATION), OK);
244 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", CLOUD_SYNC_TABLE);
245 CloseDb();
246 /**
247 * @tc.steps: step2. Create device table in COLLABORATION
248 * @tc.expected: step2.ok
249 */
250 ASSERT_NO_FATAL_FAILURE(InitDelegate());
251 auto schema = GetSchema();
252 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
253 EXPECT_EQ(delegate_->SetDistributedSchema(RDBDataGenerator::ParseSchema(schema)), OK);
254 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
255 CloseDb();
256 }
257
258 /**
259 * @tc.name: SetSchema003
260 * @tc.desc: Test set distributed schema.
261 * @tc.type: FUNC
262 * @tc.require:
263 * @tc.author: zqq
264 */
265 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema003, TestSize.Level0)
266 {
267 /**
268 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
269 * @tc.expected: step1.ok
270 */
271 ASSERT_NO_FATAL_FAILURE(InitDelegate());
272 auto schema = GetSchema();
273 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
274 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
275 EXPECT_EQ(delegate_->SetDistributedSchema(RDBDataGenerator::ParseSchema(schema, true)), OK);
276 /**
277 * @tc.steps: step2. Insert update delete local
278 * @tc.expected: step2.ok
279 */
280 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
__anoneb31f3900302(ClientChangedData &changedData) 281 EXPECT_EQ(RegisterClientObserver(db_, [](ClientChangedData &changedData) {
282 for (const auto &table : changedData.tableData) {
283 EXPECT_FALSE(table.second.isTrackedDataChange);
284 EXPECT_FALSE(table.second.isP2pSyncDataChange);
285 }
286 }), OK);
287 EXPECT_EQ(RDBDataGenerator::UpdateLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
288 UnRegisterClientObserver(db_);
289 }
290
291 /**
292 * @tc.name: SetSchema004
293 * @tc.desc: Test create distributed table without schema.
294 * @tc.type: FUNC
295 * @tc.require:
296 * @tc.author: zqq
297 */
298 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema004, TestSize.Level0)
299 {
300 /**
301 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
302 * @tc.expected: step1.ok
303 */
304 ASSERT_NO_FATAL_FAILURE(InitDelegate());
305 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
306 }
307
GetDistributedSchema(const std::string & tableName,const std::vector<std::string> & fields)308 DistributedSchema GetDistributedSchema(const std::string &tableName, const std::vector<std::string> &fields)
309 {
310 DistributedTable table;
311 table.tableName = tableName;
312 for (const auto &fieldName : fields) {
313 DistributedField field;
314 field.isP2pSync = true;
315 field.colName = fieldName;
316 table.fields.push_back(field);
317 }
318 DistributedSchema schema;
319 schema.tables.push_back(table);
320 return schema;
321 }
322
323 /**
324 * @tc.name: SetSchema005
325 * @tc.desc: Test set distributed schema with invalid schema
326 * @tc.type: FUNC
327 * @tc.require:
328 * @tc.author: liaoyonghuang
329 */
330 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema005, TestSize.Level0)
331 {
332 /**
333 * @tc.steps: step1. Prepare db
334 * @tc.expected: step1.ok
335 */
336 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
337 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
338 /**
339 * @tc.steps: step2. Test set distributed schema with invalid table name
340 * @tc.expected: step2. return SCHEMA_MISMATCH
341 */
342 DistributedSchema schema1 = GetDistributedSchema("", {});
343 EXPECT_EQ(delegate_->SetDistributedSchema(schema1), SCHEMA_MISMATCH);
344 DistributedSchema schema2 = GetDistributedSchema("xxx", {});
345 EXPECT_EQ(delegate_->SetDistributedSchema(schema2), SCHEMA_MISMATCH);
346 /**
347 * @tc.steps: step3. Test set distributed schema with invalid fields
348 * @tc.expected: step3. return SCHEMA_MISMATCH OR DISTRIBUTED_FIELD_DECREASE
349 */
350 DistributedSchema schema3 = GetDistributedSchema(DEVICE_SYNC_TABLE, {});
351 EXPECT_EQ(delegate_->SetDistributedSchema(schema3), SCHEMA_MISMATCH);
352 DistributedSchema schema4 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"xxx"});
353 EXPECT_EQ(delegate_->SetDistributedSchema(schema4), SCHEMA_MISMATCH);
354 DistributedSchema schema5 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk", "int_field1"});
355 EXPECT_EQ(delegate_->SetDistributedSchema(schema5), OK);
356 DistributedSchema schema6 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk"});
357 EXPECT_EQ(delegate_->SetDistributedSchema(schema6), DISTRIBUTED_FIELD_DECREASE);
358
359 /**
360 * @tc.steps: step4. Test set distributed schema with int_field1 but isP2pSync is false
361 * @tc.expected: step4. return DISTRIBUTED_FIELD_DECREASE
362 */
363 DistributedSchema distributedSchema1 = {0, {{DEVICE_SYNC_TABLE, {{"int_field1", false}}}}};
364 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema1), DISTRIBUTED_FIELD_DECREASE);
365 }
366
367 /**
368 * @tc.name: SetSchema006
369 * @tc.desc: Test register client observer
370 * @tc.type: FUNC
371 * @tc.require:
372 * @tc.author: liaoyonghuang
373 */
374 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema006, TestSize.Level0)
375 {
376 /**
377 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
378 * @tc.expected: step1.ok
379 */
380 ASSERT_NO_FATAL_FAILURE(InitDelegate());
381 DistributedSchema distributedSchema = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk", "int_field1"});
382 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
383 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
384 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
385 TrackerSchema trackerSchema = {
386 .tableName = DEVICE_SYNC_TABLE, .extendColNames = {"int_field1"}, .trackerColNames = {"int_field1"}
387 };
388 EXPECT_EQ(delegate_->SetTrackerTable(trackerSchema), OK);
389 /**
390 * @tc.steps: step2. Insert update local
391 * @tc.expected: step2.ok
392 */
__anoneb31f3900402(ClientChangedData &changedData) 393 EXPECT_EQ(RegisterClientObserver(db_, [](ClientChangedData &changedData) {
394 for (const auto &table : changedData.tableData) {
395 EXPECT_TRUE(table.second.isTrackedDataChange);
396 EXPECT_TRUE(table.second.isP2pSyncDataChange);
397 }
398 }), OK);
399 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
400 std::string sql = "update " + std::string(DEVICE_SYNC_TABLE) + " set int_field1 = int_field1 + 1 where pk = 0";
401 EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
402
403 /**
404 * @tc.steps: step3. Insert data that pk = 0 and sync to real device
405 * @tc.expected: step3.ok
406 */
407 auto schema = GetSchema();
408 deviceB_->SetDistributedSchema(distributedSchema);
409 auto tableSchema = GetTableSchema();
410 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
411 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
412 Query query = Query::Select(tableSchema.name);
413 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
414
415 /**
416 * @tc.steps: step4. Check extend_field
417 * @tc.expected: step4.ok
418 */
419 sql = "select count(*) from " + std::string(DBConstant::RELATIONAL_PREFIX) + std::string(DEVICE_SYNC_TABLE) + "_log"
420 " where json_extract(extend_field, '$.int_field1')=0";
421 EXPECT_EQ(sqlite3_exec(db_, sql.c_str(), CloudDBSyncUtilsTest::QueryCountCallback,
422 reinterpret_cast<void *>(1u), nullptr), SQLITE_OK);
423 UnRegisterClientObserver(db_);
424 }
425
GetTriggerSql(const std::string & tableName,const std::string & triggerTypeName,sqlite3 * db)426 std::string GetTriggerSql(const std::string &tableName, const std::string &triggerTypeName, sqlite3 *db)
427 {
428 if (db == nullptr) {
429 return "";
430 }
431 std::string sql = "select sql from sqlite_master where type = 'trigger' and tbl_name = '" + tableName +
432 "' and name = 'naturalbase_rdb_" + tableName + "_ON_" + triggerTypeName + "';";
433 sqlite3_stmt *stmt = nullptr;
434 int errCode = SQLiteUtils::GetStatement(db, sql, stmt);
435 if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_OK)) {
436 LOGE("[GetTriggerSql] prepare statement failed(%d), sys(%d), errmsg(%s)", errCode, errno, sqlite3_errmsg(db));
437 return "";
438 }
439 errCode = SQLiteUtils::StepWithRetry(stmt);
440 if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
441 LOGE("[GetTriggerSql] execute statement failed(%d), sys(%d), errmsg(%s)", errCode, errno, sqlite3_errmsg(db));
442 return "";
443 }
444 const std::string triggerSql = std::string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
445 int ret = E_OK;
446 SQLiteUtils::ResetStatement(stmt, true, ret);
447 return triggerSql;
448 }
449
450 /**
451 * @tc.name: SetSchema007
452 * @tc.desc: Test whether setting the schema multiple times will refresh the trigger
453 * @tc.type: FUNC
454 * @tc.require:
455 * @tc.author: liaoyonghuang
456 */
457 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema007, TestSize.Level0)
458 {
459 /**
460 * @tc.steps: step1. Prepare db
461 * @tc.expected: step1.ok
462 */
463 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
464 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
465 DistributedSchema schema1 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk"});
466 EXPECT_EQ(delegate_->SetDistributedSchema(schema1), OK);
467 /**
468 * @tc.steps:step2. delete triggers
469 * @tc.expected: step2. Return OK.
470 */
471 std::string oldInsertTriggerSql = GetTriggerSql(DEVICE_SYNC_TABLE, "INSERT", db_);
472 std::string oldUpdateTriggerSql = GetTriggerSql(DEVICE_SYNC_TABLE, "UPDATE", db_);
473 std::string oldDeleteTriggerSql = GetTriggerSql(DEVICE_SYNC_TABLE, "DELETE", db_);
474 EXPECT_FALSE(oldInsertTriggerSql.empty() || oldUpdateTriggerSql.empty() || oldDeleteTriggerSql.empty());
475 std::vector<std::string> triggerTypes = {"INSERT", "UPDATE", "DELETE"};
476 for (const auto &triggerType : triggerTypes) {
477 std::string sql = "DROP TRIGGER IF EXISTS naturalbase_rdb_" + std::string(DEVICE_SYNC_TABLE) + "_ON_" +
478 triggerType;
479 SQLiteUtils::ExecuteRawSQL(db_, sql);
480 }
481 /**
482 * @tc.steps:step3. Set distributed schema and check if the trigger exists
483 * @tc.expected: step3. Check OK.
484 */
485 DistributedSchema schema2 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk", "int_field1"});
486 EXPECT_EQ(delegate_->SetDistributedSchema(schema2), OK);
487 for (const auto &triggerType : triggerTypes) {
488 std::string sql = "select count(*) from sqlite_master where type = 'trigger' and tbl_name = '" +
489 std::string(DEVICE_SYNC_TABLE) + "' and name = 'naturalbase_rdb_" + std::string(DEVICE_SYNC_TABLE) +
490 "_ON_" + triggerType + "';";
491 int count = 0;
492 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
493 EXPECT_EQ(count, 1);
494 }
495 std::string newInsertTriggerSql = GetTriggerSql(DEVICE_SYNC_TABLE, "INSERT", db_);
496 std::string newUpdateTriggerSql = GetTriggerSql(DEVICE_SYNC_TABLE, "UPDATE", db_);
497 std::string newDeleteTriggerSql = GetTriggerSql(DEVICE_SYNC_TABLE, "DELETE", db_);
498 EXPECT_FALSE(newInsertTriggerSql.empty() || newUpdateTriggerSql.empty() || newDeleteTriggerSql.empty());
499 EXPECT_TRUE(oldInsertTriggerSql == newInsertTriggerSql);
500 EXPECT_TRUE(oldUpdateTriggerSql != newUpdateTriggerSql);
501 EXPECT_TRUE(oldDeleteTriggerSql == newDeleteTriggerSql);
502 }
503
504 /**
505 * @tc.name: SetSchema008
506 * @tc.desc: Test set distributed schema with pk but pk isP2pSync is false
507 * @tc.type: FUNC
508 * @tc.require:
509 * @tc.author: tankaisheng
510 */
511 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema008, TestSize.Level0)
512 {
513 /**
514 * @tc.steps: step1. Prepare db
515 * @tc.expected: step1.ok
516 */
517 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
518 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
519 /**
520 * @tc.steps: step2. Test set distributed schema without pk
521 * @tc.expected: step2. return OK
522 */
523 DistributedSchema distributedSchema = {0, {{DEVICE_SYNC_TABLE, {{"int_field1", true}}}}};
524 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
525 /**
526 * @tc.steps: step3. Test set distributed schema with pk but pk isP2pSync is false
527 * @tc.expected: step3. return SCHEMA_MISMATCH
528 */
529 DistributedSchema distributedSchema1 = {0, {{DEVICE_SYNC_TABLE, {{"pk", false}}}}};
530 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema1), SCHEMA_MISMATCH);
531 /**
532 * @tc.steps: step4. Test set same distributed schema
533 * @tc.expected: step4. return OK
534 */
535 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
536 }
537
538 /**
539 * @tc.name: SetSchema009
540 * @tc.desc: Test tableMode is SPLIT_BY_DEVICE then SetDistributedSchema.
541 * @tc.type: FUNC
542 * @tc.require:
543 * @tc.author: tankaisheng
544 */
545 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema009, TestSize.Level0)
546 {
547 /**
548 * @tc.steps: step1. Prepare db, tableMode is SPLIT_BY_DEVICE
549 * @tc.expected: step1.ok
550 */
551 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::SPLIT_BY_DEVICE));
552 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
553 /**
554 * @tc.steps: step2. Test set distributed schema without pk
555 * @tc.expected: step2. return NOT_SUPPORT
556 */
557 DistributedSchema distributedSchema = {0, {{DEVICE_SYNC_TABLE, {{"int_field1", true}}}}};
558 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), NOT_SUPPORT);
559 }
560
561 /**
562 * @tc.name: SetSchema010
563 * @tc.desc: Test create distributed table without communicator.
564 * @tc.type: FUNC
565 * @tc.require:
566 * @tc.author: zqq
567 */
568 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema010, TestSize.Level0)
569 {
570 if (deviceB_ != nullptr) {
571 delete deviceB_;
572 deviceB_ = nullptr;
573 }
574 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
575 ASSERT_NO_FATAL_FAILURE(InitDelegate());
576 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
577 }
578
579 /**
580 * @tc.name: SetSchema011
581 * @tc.desc: Test set schema with not null field but isP2pSync is false
582 * @tc.type: FUNC
583 * @tc.require:
584 * @tc.author: liaoyonghuang
585 */
586 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema011, TestSize.Level0)
587 {
588 /**
589 * @tc.steps: step1. Prepare db
590 * @tc.expected: step1.ok
591 */
592 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
593 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
594 /**
595 * @tc.steps: step2. Test set distributed schema
596 * @tc.expected: step2. return SCHEMA_MISMATCH
597 */
598 DistributedSchema schema1 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk"});
599 DistributedField &field1 = schema1.tables.front().fields.front();
600 field1.isP2pSync = false;
601 EXPECT_EQ(delegate_->SetDistributedSchema(schema1), SCHEMA_MISMATCH);
602 }
603
604 /**
605 * @tc.name: SetSchema012
606 * @tc.desc: Test call SetDistributedSchema with empty tables
607 * @tc.type: FUNC
608 * @tc.require:
609 * @tc.author: liuhongyang
610 */
611 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema012, TestSize.Level0)
612 {
613 /**
614 * @tc.steps: step1. Prepare db, tableMode is SPLIT_BY_DEVICE
615 * @tc.expected: step1.ok
616 */
617 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::SPLIT_BY_DEVICE));
618 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
619 /**
620 * @tc.steps: step2. Test set schema with empty tables vector
621 * @tc.expected: step2. return SCHEMA_MISMATCH
622 */
623 DistributedSchema distributedSchema = {0, {}};
624 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
625 /**
626 * @tc.steps: step3. Test set schema with a list of empty tables
627 * @tc.expected: step3. return SCHEMA_MISMATCH
628 */
629 distributedSchema = {0, {{}, {}, {}}};
630 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
631 /**
632 * @tc.steps: step4. Test set schema with a mix of empty and non-empty tables
633 * @tc.expected: step4. return SCHEMA_MISMATCH
634 */
635 distributedSchema = {0, {{DEVICE_SYNC_TABLE, {{"int_field1", true}}}, {}}};
636 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
637 }
638
639 /**
640 * @tc.name: SetSchema013
641 * @tc.desc: Test set tracker table for device table and check if timestamp has changed
642 * @tc.type: FUNC
643 * @tc.require:
644 * @tc.author: bty
645 */
646 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema013, TestSize.Level0)
647 {
648 /**
649 * @tc.steps: step1. Create device table
650 * @tc.expected: step1.ok
651 */
652 ASSERT_NO_FATAL_FAILURE(InitDelegate());
653 DistributedSchema distributedSchema = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk", "int_field1"});
654 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
655 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
656 TrackerSchema trackerSchema = {
657 .tableName = DEVICE_SYNC_TABLE, .extendColNames = {"int_field1"}, .trackerColNames = {"int_field1"}
658 };
659 /**
660 * @tc.steps: step2. Insert one data and query timestamp
661 * @tc.expected: step2.ok
662 */
663 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
664 sqlite3_stmt *stmt = nullptr;
665 std::string sql = "select timestamp from " + DBCommon::GetLogTableName(DEVICE_SYNC_TABLE) +
666 " where data_key=0";
667 EXPECT_EQ(SQLiteUtils::GetStatement(db_, sql, stmt), E_OK);
668 EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_ROW));
669 int64_t timestamp1 = static_cast<int64_t>(sqlite3_column_int64(stmt, 0));
670 int ret = E_OK;
671 SQLiteUtils::ResetStatement(stmt, true, ret);
672 /**
673 * @tc.steps: step3. Set tracker table and query timestamp
674 * @tc.expected: step3.Equal
675 */
676 EXPECT_EQ(delegate_->SetTrackerTable(trackerSchema), WITH_INVENTORY_DATA);
677 EXPECT_EQ(SQLiteUtils::GetStatement(db_, sql, stmt), E_OK);
678 EXPECT_EQ(SQLiteUtils::StepWithRetry(stmt), SQLiteUtils::MapSQLiteErrno(SQLITE_ROW));
679 int64_t timestamp2 = static_cast<int64_t>(sqlite3_column_int64(stmt, 0));
680 SQLiteUtils::ResetStatement(stmt, true, ret);
681 EXPECT_EQ(timestamp1, timestamp2);
682 }
683
684 /**
685 * @tc.name: SetSchema014
686 * @tc.desc: Test set tracker table for device table and check if timestamp has changed
687 * @tc.type: FUNC
688 * @tc.require:
689 * @tc.author: zqq
690 */
691 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema014, TestSize.Level0)
692 {
693 /**
694 * @tc.steps: step1. Create auto increment table and specified pk, distributed schema without not null field
695 * @tc.expected: step1. create distributed table ok but set schema failed
696 */
697 auto tableSchema = GetTableSchema();
698 tableSchema.name = DEVICE_SYNC_TABLE_AUTOINCREMENT;
699 ASSERT_EQ(RDBDataGenerator::InitTable(tableSchema, true, true, *db_), E_OK);
700 ASSERT_NO_FATAL_FAILURE(InitDelegate());
701 DistributedSchema distributedSchema;
702 DistributedTable distributedTable;
703 distributedTable.tableName = tableSchema.name;
704 DistributedField distributedField;
705 distributedField.colName = "pk";
706 distributedField.isSpecified = true;
707 distributedField.isP2pSync = true;
708 distributedTable.fields.push_back(distributedField);
709 distributedSchema.tables.push_back(distributedTable);
710 EXPECT_EQ(delegate_->CreateDistributedTable(tableSchema.name, TableSyncType::DEVICE_COOPERATION), OK);
711 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
712 /**
713 * @tc.steps: step2. Distributed schema with not null field
714 * @tc.expected: step2. ok
715 */
716 distributedSchema.tables.clear();
717 distributedField.colName = "123";
718 distributedField.isSpecified = false;
719 distributedTable.fields.push_back(distributedField);
720 distributedSchema.tables.push_back(distributedTable);
721 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
722 /**
723 * @tc.steps: step3. Distributed schema with error specified
724 * @tc.expected: step3. SCHEMA_MISMATCH
725 */
726 distributedSchema.tables.clear();
727 distributedField.colName = "123";
728 distributedField.isSpecified = true;
729 distributedTable.fields.push_back(distributedField);
730 distributedField.colName = "pk";
731 distributedField.isP2pSync = false;
732 distributedTable.fields.push_back(distributedField);
733 distributedSchema.tables.push_back(distributedTable);
734 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
735 }
736
737 /**
738 * @tc.name: SetSchema015
739 * @tc.desc: Test call SetDistributedSchema with mark more than one unique col isSpecified true
740 * @tc.type: FUNC
741 * @tc.require:
742 * @tc.author: tankaisheng
743 */
744 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema015, TestSize.Level0)
745 {
746 /**
747 * @tc.steps: step1. Prepare db, tableMode is COLLABORATION
748 * @tc.expected: step1.ok
749 */
750 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
751 std::string createSql =
752 "CREATE TABLE IF NOT EXISTS table_pk_int(integer_field INTEGER PRIMARY KEY AUTOINCREMENT,"
753 "int_field INT, char_field CHARACTER(20) UNIQUE, clob_field CLOB UNIQUE, UNIQUE(char_field, clob_field));";
754 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, createSql), E_OK);
755 EXPECT_EQ(delegate_->CreateDistributedTable("table_pk_int", TableSyncType::DEVICE_COOPERATION), OK);
756
757 /**
758 * @tc.steps: step2. Test mark more than one unique col isSpecified true
759 * @tc.expected: step2. return SCHEMA_MISMATCH
760 */
761 DistributedSchema distributedSchema = {0, {{"table_pk_int", {
762 {"integer_field", false, false},
763 {"int_field", true, false},
764 {"char_field", true, true},
765 {"clob_field", true, true}}}}};
766 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
767 }
768
769 /**
770 * @tc.name: SetSchema016
771 * @tc.desc: Test set isSpecified to false after isSpecified was set to true
772 * @tc.type: FUNC
773 * @tc.require:
774 * @tc.author: liaoyonghuang
775 */
776 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema016, TestSize.Level0)
777 {
778 /**
779 * @tc.steps: step1. Prepare db
780 * @tc.expected: step1.ok
781 */
782 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
783 std::string tableName = "multiPriKeyTable";
784 std::string sql = "CREATE TABLE IF NOT EXISTS " + tableName +
785 "(pk1 INTEGER, pk2 INT, PRIMARY KEY (pk1, pk2));";
786 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
787 EXPECT_EQ(delegate_->CreateDistributedTable(tableName, TableSyncType::DEVICE_COOPERATION), OK);
788 /**
789 * @tc.steps: step2. Test set distributed schema
790 * @tc.expected: step2. return OK
791 */
792 DistributedSchema schema1 = GetDistributedSchema(tableName, {"pk1", "pk2"});
793 EXPECT_EQ(delegate_->SetDistributedSchema(schema1), OK);
794 /**
795 * @tc.steps: step3. Test set distributed schema
796 * @tc.expected: step3. return SCHEMA_MISMATCH
797 */
798 DistributedSchema schema2 = GetDistributedSchema(tableName, {"pk1", "pk2"});
799 DistributedField &field2 = schema2.tables.front().fields.front();
800 field2.isSpecified = true;
801 EXPECT_EQ(delegate_->SetDistributedSchema(schema2), SCHEMA_MISMATCH);
802 }
803
GetHashKey(sqlite3 * db,const std::string & tableName,std::vector<std::string> & hashKeys)804 int GetHashKey(sqlite3 *db, const std::string &tableName, std::vector<std::string> &hashKeys)
805 {
806 if (db == nullptr) {
807 return -E_INVALID_DB;
808 }
809
810 std::string sql = "select cast(hash_key as text) from " + DBCommon::GetLogTableName(tableName) +
811 " order by timestamp;";
812 sqlite3_stmt *stmt = nullptr;
813 int errCode = SQLiteUtils::GetStatement(db, sql, stmt);
814 if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_OK)) {
815 LOGE("prepare statement failed(%d), sys(%d), msg(%s)", errCode, errno, sqlite3_errmsg(db));
816 return errCode;
817 }
818
819 do {
820 errCode = SQLiteUtils::StepWithRetry(stmt);
821 if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
822 errCode = E_OK;
823 } else if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
824 LOGE("[SQLiteUtils][ExecuteSQL] execute statement failed(%d), sys(%d), msg(%s)",
825 errCode, errno, sqlite3_errmsg(db));
826 } else {
827 const unsigned char *result = sqlite3_column_text(stmt, 0);
828 hashKeys.push_back(reinterpret_cast<const std::string::value_type *>(result));
829 }
830 } while (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW));
831
832 int ret = E_OK;
833 SQLiteUtils::ResetStatement(stmt, true, ret);
834 return errCode;
835 }
836
837 /**
838 * @tc.name: SetSchema017
839 * @tc.desc: Test whether to update hash_key after setting up distributed schema
840 * @tc.type: FUNC
841 * @tc.require:
842 * @tc.author: liaoyonghuang
843 */
844 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema017, TestSize.Level0)
845 {
846 /**
847 * @tc.steps: step1. Prepare db
848 * @tc.expected: step1.ok
849 */
850 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
851 std::string tableName = "multiPriKeyTable";
852 std::string sql = "CREATE TABLE IF NOT EXISTS " + tableName +
853 "(pk1 INTEGER PRIMARY KEY AUTOINCREMENT, pk2 INT UNIQUE);";
854 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
855 EXPECT_EQ(delegate_->CreateDistributedTable(tableName, TableSyncType::DEVICE_COOPERATION), OK);
856 /**
857 * @tc.steps: step2. Insert a record and get hash_key
858 * @tc.expected: step2.ok
859 */
860 int dataCount = 10;
861 for (int i = 0; i < dataCount; i++) {
862 sql = "insert into " + tableName + " values (" + std::to_string(i) + ", " + std::to_string(i + 1) + ");";
863 EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
864 }
865 std::vector<std::string> oldHashKeys;
866 EXPECT_EQ(GetHashKey(db_, tableName, oldHashKeys), E_OK);
867 ASSERT_EQ(oldHashKeys.size(), static_cast<size_t>(dataCount));
868 /**
869 * @tc.steps: step3. Set distributed schema and get old hash_key
870 * @tc.expected: step3.ok
871 */
872 DistributedSchema schema1 = GetDistributedSchema(tableName, {"pk1", "pk2"});
873 EXPECT_EQ(delegate_->SetDistributedSchema(schema1), OK);
874 std::vector<std::string> newHashKeys1;
875 EXPECT_EQ(GetHashKey(db_, tableName, newHashKeys1), E_OK);
876 ASSERT_EQ(newHashKeys1.size(), static_cast<size_t>(dataCount));
877 for (int i = 0; i < dataCount; i++) {
878 EXPECT_EQ(oldHashKeys[i], newHashKeys1[i]);
879 }
880 /**
881 * @tc.steps: step4. Set another distributed schema and get old hash_key
882 * @tc.expected: step4.ok
883 */
884 DistributedSchema schema2 = {0, {{"multiPriKeyTable", {
885 {"pk1", false, false},
886 {"pk2", true, true}}}}};
887 EXPECT_EQ(delegate_->SetDistributedSchema(schema2, true), OK);
888 std::vector<std::string> newHashKeys2;
889 EXPECT_EQ(GetHashKey(db_, tableName, newHashKeys2), E_OK);
890 ASSERT_EQ(newHashKeys2.size(), static_cast<size_t>(dataCount));
891 for (int i = 0; i < dataCount; i++) {
892 EXPECT_NE(newHashKeys1[i], newHashKeys2[i]);
893 }
894 EXPECT_NE(newHashKeys2, oldHashKeys);
895 }
896
897 /**
898 * @tc.name: SetSchema018
899 * @tc.desc: Test no primary key table setting isSpecified
900 * @tc.type: FUNC
901 * @tc.require:
902 * @tc.author: liaoyonghuang
903 */
904 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema018, TestSize.Level0)
905 {
906 /**
907 * @tc.steps: step1. Prepare db
908 * @tc.expected: step1.ok
909 */
910 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
911 std::string tableName = "noPriKeyTable";
912 std::string sql = "CREATE TABLE IF NOT EXISTS " + tableName +
913 "(field_int1 INTEGER, field_int2 INT);";
914 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
915 EXPECT_EQ(delegate_->CreateDistributedTable(tableName, TableSyncType::DEVICE_COOPERATION), OK);
916 /**
917 * @tc.steps: step2. Test set distributed schema
918 * @tc.expected: step2. return SCHEMA_MISMATCH
919 */
920 DistributedSchema schema = GetDistributedSchema(tableName, {"field_int1"});
921 DistributedField &field = schema.tables.front().fields.front();
922 field.isSpecified = true;
923 EXPECT_EQ(delegate_->SetDistributedSchema(schema), SCHEMA_MISMATCH);
924 }
925
926 /**
927 * @tc.name: SetSchema019
928 * @tc.desc: Test call SetDistributedSchema when unique col not set isP2pSync
929 * @tc.type: FUNC
930 * @tc.require:
931 * @tc.author: tankaisheng
932 */
933 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema019, TestSize.Level0)
934 {
935 /**
936 * @tc.steps: step1. Prepare db, tableMode is COLLABORATION
937 * @tc.expected: step1.ok
938 */
939 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
940 std::string createSql = "CREATE TABLE IF NOT EXISTS table_pk_integer(integer_field INTEGER UNIQUE,"
941 "int_field INT, char_field CHARACTER(20), clob_field CLOB);";
942 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, createSql), E_OK);
943 EXPECT_EQ(delegate_->CreateDistributedTable("table_pk_integer", TableSyncType::DEVICE_COOPERATION), OK);
944
945 /**
946 * @tc.steps: step2. Test mark unique col isP2pSync true
947 * @tc.expected: step2. return SCHEMA_MISMATCH
948 */
949 DistributedSchema distributedSchema = {1, {{"table_pk_integer", {
950 {"int_field", true},
951 {"char_field", true},
952 {"clob_field", true}}}}};
953 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
954 }
955
956 /**
957 * @tc.name: SetSchema020
958 * @tc.desc: Test call SetDistributedSchema when unique col and pk isP2pSync
959 * @tc.type: FUNC
960 * @tc.require:
961 * @tc.author: zqq
962 */
963 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema020, TestSize.Level0)
964 {
965 /**
966 * @tc.steps: step1. Prepare db, tableMode is COLLABORATION
967 * @tc.expected: step1.ok
968 */
969 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
970 std::string createSql = "CREATE TABLE IF NOT EXISTS table_pk_int(integer_field INTEGER PRIMARY KEY AUTOINCREMENT,"
971 "int_field INT UNIQUE, char_field CHARACTER(20), clob_field CLOB);";
972 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, createSql), E_OK);
973 EXPECT_EQ(delegate_->CreateDistributedTable("table_pk_int", TableSyncType::DEVICE_COOPERATION), OK);
974
975 /**
976 * @tc.steps: step2. Test mark unique col and pk isP2pSync true, specified unique col
977 * @tc.expected: step2. return NOT_SUPPORT
978 */
979 DistributedSchema distributedSchema = {1, {
980 {"table_pk_int", {
981 {"integer_field", true},
982 {"int_field", true, true},
983 {"char_field", true},
984 {"clob_field", true}
985 }}}
986 };
987 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), NOT_SUPPORT);
988 }
989
990 /**
991 * @tc.name: SetSchema022
992 * @tc.desc: Test setting the distributed schema with table names named keywords.
993 * @tc.type: FUNC
994 * @tc.require:
995 * @tc.author: liaoyonghuang
996 */
997 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema022, TestSize.Level0)
998 {
999 /**
1000 * @tc.steps: step1. Create device table named keywords.
1001 * @tc.expected: step1.ok
1002 */
1003 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1004 std::string tableName = "except";
1005 std::string sql = "CREATE TABLE IF NOT EXISTS '" + tableName + "'(field1 INTEGER, field2 INT);";
1006 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1007 /**
1008 * @tc.steps: step2. Create distributed table and set distributed schema
1009 * @tc.expected: step2.ok
1010 */
1011 auto distributedSchema = GetDistributedSchema(tableName, {"field1", "field2"});
1012 deviceB_->SetDistributedSchema(distributedSchema);
1013 EXPECT_EQ(delegate_->CreateDistributedTable(tableName, TableSyncType::DEVICE_COOPERATION), OK);
1014 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1015 }
1016
1017 /**
1018 * @tc.name: SetSchema025
1019 * @tc.desc: Test SetDistributedSchema with isForceUpgrade.
1020 * @tc.type: FUNC
1021 * @tc.require:
1022 * @tc.author: liaoyonghuang
1023 */
1024 HWTEST_F(DistributedDBRDBCollaborationTest, SetSchema025, TestSize.Level0)
1025 {
1026 /**
1027 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1028 * @tc.expected: step1.ok
1029 */
1030 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1031 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1032 DistributedSchema distributedSchema1 = {1, {{DEVICE_SYNC_TABLE, {{"pk", true}, {"int_field1", true}}}}};
1033 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema1), OK);
1034 /**
1035 * @tc.steps: step2. Set distributed schema with isForceUpgrade
1036 * @tc.expected: step2.ok
1037 */
1038 DistributedSchema distributedSchema2 = {2, {{DEVICE_SYNC_TABLE, {{"pk", true}}}}};
1039 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema2, true), OK);
1040 }
1041
1042 /**
1043 * @tc.name: NormalSync001
1044 * @tc.desc: Test set distributed schema and sync.
1045 * @tc.type: FUNC
1046 * @tc.require:
1047 * @tc.author: zqq
1048 */
1049 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync001, TestSize.Level0)
1050 {
1051 /**
1052 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1053 * @tc.expected: step1.ok
1054 */
1055 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1056 auto schema = GetSchema();
1057 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1058 deviceB_->SetDistributedSchema(distributedSchema);
1059 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1060 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1061 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1062 /**
1063 * @tc.steps: step2. Insert one data
1064 * @tc.expected: step2.ok
1065 */
1066 auto tableSchema = GetTableSchema();
1067 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
1068 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1069 /**
1070 * @tc.steps: step3. Sync to real device
1071 * @tc.expected: step3.ok
1072 */
1073 Query query = Query::Select(tableSchema.name);
1074 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1075 }
1076
1077 /**
1078 * @tc.name: NormalSync002
1079 * @tc.desc: Test sync with diff distributed schema [high version -> low version].
1080 * @tc.type: FUNC
1081 * @tc.require:
1082 * @tc.author: zqq
1083 */
1084 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync002, TestSize.Level0)
1085 {
1086 /**
1087 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1088 * @tc.expected: step1.ok
1089 */
1090 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1091 /**
1092 * @tc.steps: step2. DeviceB set schema [pk, int_field1, int_field2, int_field_upgrade]
1093 * @tc.expected: step2.ok
1094 */
1095 DataBaseSchema virtualSchema;
1096 auto tableSchema = GetTableSchema(true);
1097 tableSchema.name = DEVICE_SYNC_TABLE;
1098 virtualSchema.tables.push_back(tableSchema);
1099 auto distributedSchema = RDBDataGenerator::ParseSchema(virtualSchema);
1100 deviceB_->SetDistributedSchema(distributedSchema);
1101 /**
1102 * @tc.steps: step3. Real device set schema [pk, int_field1, int_field2]
1103 * @tc.expected: step3.ok
1104 */
1105 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1106 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1107 EXPECT_EQ(delegate_->SetDistributedSchema(RDBDataGenerator::ParseSchema(GetSchema())), OK);
1108 /**
1109 * @tc.steps: step4. Insert table info and virtual data into deviceB
1110 * @tc.expected: step4.ok
1111 */
1112 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
1113 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(DEVICE_SYNC_TABLE_UPGRADE, DEVICE_SYNC_TABLE, db_, deviceB_),
1114 E_OK);
1115 /**
1116 * @tc.steps: step5. Sync to real device
1117 * @tc.expected: step5.ok
1118 */
1119 Query query = Query::Select(tableSchema.name);
1120 EXPECT_EQ(deviceB_->GenericVirtualDevice::Sync(SYNC_MODE_PUSH_ONLY, query, true), E_OK);
1121 }
1122
1123 /**
1124 * @tc.name: NormalSync003
1125 * @tc.desc: Test sync with diff distributed schema [low version -> high version].
1126 * @tc.type: FUNC
1127 * @tc.require:
1128 * @tc.author: zqq
1129 */
1130 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync003, TestSize.Level0)
1131 {
1132 /**
1133 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1134 * @tc.expected: step1.ok
1135 */
1136 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1137 /**
1138 * @tc.steps: step2. DeviceB set schema [pk, int_field1, int_field2]
1139 * @tc.expected: step2.ok
1140 */
1141 DataBaseSchema virtualSchema;
1142 auto tableSchema = GetTableSchema();
1143 tableSchema.name = DEVICE_SYNC_TABLE_UPGRADE;
1144 virtualSchema.tables.push_back(tableSchema);
1145 auto distributedSchema = RDBDataGenerator::ParseSchema(virtualSchema);
1146 deviceB_->SetDistributedSchema(distributedSchema);
1147 /**
1148 * @tc.steps: step3. Real device set schema [pk, int_field1, int_field2, int_field_upgrade]
1149 * @tc.expected: step3.ok
1150 */
1151 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE_UPGRADE, TableSyncType::DEVICE_COOPERATION), OK);
1152 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1153 EXPECT_EQ(delegate_->SetDistributedSchema(RDBDataGenerator::ParseSchema(GetSchema())), OK);
1154 /**
1155 * @tc.steps: step4. Insert table info and virtual data into deviceB
1156 * @tc.expected: step4.ok
1157 */
1158 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
1159 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(DEVICE_SYNC_TABLE, DEVICE_SYNC_TABLE_UPGRADE, db_, deviceB_),
1160 E_OK);
1161 /**
1162 * @tc.steps: step5. Sync to real device
1163 * @tc.expected: step5.ok
1164 */
1165 Query query = Query::Select(tableSchema.name);
1166 EXPECT_EQ(deviceB_->GenericVirtualDevice::Sync(SYNC_MODE_PUSH_ONLY, query, true), E_OK);
1167 }
1168
1169 /**
1170 * @tc.name: NormalSync004
1171 * @tc.desc: Test sync when distributed schema was not set.
1172 * @tc.type: FUNC
1173 * @tc.require:
1174 * @tc.author: liaoyonghuang
1175 */
1176 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync004, TestSize.Level0)
1177 {
1178 /**
1179 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1180 * @tc.expected: step1.ok
1181 */
1182 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1183 auto tableSchema = GetTableSchema();
1184 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1185 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1186 /**
1187 * @tc.steps: step2. Sync to real device
1188 * @tc.expected: step2. return SCHEMA_MISMATCH.
1189 */
1190 Query query = Query::Select(tableSchema.name);
1191 DBStatus status = delegate_->Sync({UnitTestCommonConstant::DEVICE_B}, SYNC_MODE_PUSH_ONLY, query, nullptr, true);
1192 EXPECT_EQ(status, SCHEMA_MISMATCH);
1193 }
1194
1195 /**
1196 * @tc.name: NormalSync005
1197 * @tc.desc: Test sync with specified columns
1198 * @tc.type: FUNC
1199 * @tc.require:
1200 * @tc.author: liaoyonghuang
1201 */
1202 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync005, TestSize.Level0)
1203 {
1204 /**
1205 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1206 * @tc.expected: step1.ok
1207 */
1208 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
1209 DistributedSchema schema = GetDistributedSchema(DEVICE_SYNC_TABLE, {"pk", "int_field1"});
1210 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1211 deviceB_->SetDistributedSchema(schema);
1212 EXPECT_EQ(delegate_->SetDistributedSchema(schema), OK);
1213 /**
1214 * @tc.steps: step2. Init some data
1215 * @tc.expected: step2.ok
1216 */
1217 int64_t dataNum = 10;
1218 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, dataNum / 2, db_, GetTableSchema()), E_OK);
1219 std::string sql = "update " + std::string(DEVICE_SYNC_TABLE) + " set int_field1 = 1, int_field2 = 2 where pk >= 0";
1220 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1221 auto tableSchema = GetTableSchema();
1222 std::this_thread::sleep_for(std::chrono::seconds(1));
1223 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataNum, deviceB_, tableSchema), E_OK);
1224 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1225 /**
1226 * @tc.steps: step3. Sync to real device and check data
1227 * @tc.expected: step3.ok
1228 */
1229 Query query = Query::Select(tableSchema.name);
1230 EXPECT_EQ(deviceB_->GenericVirtualDevice::Sync(SYNC_MODE_PUSH_PULL, query, true), E_OK);
1231 sql = "select int_field1, int_field2 from " + std::string(DEVICE_SYNC_TABLE) + " order by pk;";
1232 sqlite3_stmt *stmt = nullptr;
1233 ASSERT_EQ(SQLiteUtils::GetStatement(db_, sql, stmt), E_OK);
1234 int dataIndex = 0;
1235 while (SQLiteUtils::StepWithRetry(stmt) == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
1236 int64_t intField1Value = sqlite3_column_int64(stmt, 0);
1237 int64_t intField2Value = sqlite3_column_int64(stmt, 1);
1238 EXPECT_EQ(intField1Value, dataIndex);
1239 dataIndex++;
1240 if (dataIndex <= dataNum / 2) {
1241 EXPECT_EQ(intField2Value, 2);
1242 } else {
1243 EXPECT_EQ(intField2Value, 0);
1244 }
1245 }
1246 EXPECT_EQ(dataIndex, dataNum);
1247 int errCode;
1248 SQLiteUtils::ResetStatement(stmt, true, errCode);
1249 }
1250
1251 /**
1252 * @tc.name: NormalSync006
1253 * @tc.desc: Test set distributed schema and sync.
1254 * @tc.type: FUNC
1255 * @tc.require:
1256 * @tc.author: zqq
1257 */
1258 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync006, TestSize.Level0)
1259 {
1260 /**
1261 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1262 * @tc.expected: step1.ok
1263 */
1264 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1265 auto schema = GetSchema();
1266 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1267 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1268 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1269 deviceB_->SetDistributedSchema(distributedSchema);
1270 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1271 /**
1272 * @tc.steps: step2. Insert one data
1273 * @tc.expected: step2.ok
1274 */
1275 auto tableSchema = GetTableSchema();
1276 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
1277 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1278 /**
1279 * @tc.steps: step3. Sync to real device
1280 * @tc.expected: step3.ok
1281 */
1282 Query query = Query::Select(tableSchema.name);
1283 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PUSH_ONLY, OK, {deviceB_->GetDeviceId()});
1284 /**
1285 * @tc.steps: step4. Change schema to non-existent table name, then sync
1286 * @tc.expected: step4.SCHEMA_MISMATCH
1287 */
1288 tableSchema.name = "not_config_table";
1289 ASSERT_EQ(RDBDataGenerator::InitTable(tableSchema, true, *db_), SQLITE_OK);
1290 ASSERT_EQ(delegate_->CreateDistributedTable(tableSchema.name), OK);
1291 Query query2 = Query::Select(tableSchema.name);
1292 std::map<std::string, std::vector<TableStatus>> statusMap;
1293 SyncStatusCallback callBack2;
1294 DBStatus callStatus = delegate_->Sync({deviceB_->GetDeviceId()}, SYNC_MODE_PUSH_ONLY, query2, callBack2, true);
1295 EXPECT_EQ(callStatus, SCHEMA_MISMATCH);
1296 }
1297
1298 /**
1299 * @tc.name: NormalSync007
1300 * @tc.desc: Test change distributed schema and sync.
1301 * @tc.type: FUNC
1302 * @tc.require:
1303 * @tc.author: zqq
1304 */
1305 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync007, TestSize.Level0)
1306 {
1307 /**
1308 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1309 * @tc.expected: step1.ok
1310 */
1311 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1312 auto schema = GetSchema();
1313 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1314 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1315 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1316 deviceB_->SetDistributedSchema(distributedSchema);
1317 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1318 /**
1319 * @tc.steps: step2. Insert one data
1320 * @tc.expected: step2.ok
1321 */
1322 auto tableSchema = GetTableSchema();
1323 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1324 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1325 /**
1326 * @tc.steps: step3. Sync to real device
1327 * @tc.expected: step3.ok
1328 */
1329 Query query = Query::Select(tableSchema.name);
1330 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1331 std::string sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE)
1332 .append(" where pk=0 and int_field1 is null and int_field2 is null");
1333 int count = 0;
1334 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1335 EXPECT_EQ(count, 1);
1336 /**
1337 * @tc.steps: step4. Change schema and sync again
1338 * @tc.expected: step4.ok
1339 */
1340 distributedSchema = RDBDataGenerator::ParseSchema(schema);
1341 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1342 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1343 sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE)
1344 .append(" where pk=0 and int_field1=0 and int_field2=0");
1345 count = 0;
1346 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1347 EXPECT_EQ(count, 1);
1348 auto changeData = delegateObserver_->GetSavedChangedData()[std::string(DEVICE_SYNC_TABLE)];
1349 EXPECT_TRUE(changeData.properties.isP2pSyncDataChange);
1350 }
1351
1352 /**
1353 * @tc.name: NormalSync008
1354 * @tc.desc: Test set distributed schema and sync with diff sort.
1355 * @tc.type: FUNC
1356 * @tc.require:
1357 * @tc.author: zqq
1358 */
1359 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync008, TestSize.Level0)
1360 {
1361 /**
1362 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1363 * @tc.expected: step1.ok
1364 */
1365 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1366 auto schema = GetSchema();
1367 auto distributedSchema = RDBDataGenerator::ParseSchema(schema);
1368 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1369 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1370 deviceB_->SetDistributedSchema(distributedSchema);
1371 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1372 /**
1373 * @tc.steps: step2. Insert one data with diff sort col
1374 * @tc.expected: step2.ok
1375 */
1376 auto tableSchema = GetTableSchema();
1377 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_,
1378 RDBDataGenerator::FlipTableSchema(tableSchema)), E_OK);
1379 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1380 /**
1381 * @tc.steps: step3. Sync to real device
1382 * @tc.expected: step3.ok
1383 */
1384 Query query = Query::Select(tableSchema.name);
1385 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1386 std::string sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE).append(" where pk=0;");
1387 int count = 0;
1388 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1389 EXPECT_EQ(count, 1);
1390 sql = std::string("select count(*) from ")
1391 .append(RelationalStoreManager::GetDistributedLogTableName(DEVICE_SYNC_TABLE))
1392 .append(" where data_key=0 and cursor=1;");
1393 count = 0;
1394 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1395 EXPECT_EQ(count, 1);
1396 }
1397
1398 /**
1399 * @tc.name: NormalSync009
1400 * @tc.desc: Test if distributed table will be created when sync with COLLABORATION mode.
1401 * @tc.type: FUNC
1402 * @tc.require:
1403 * @tc.author: liaoyonghuang
1404 */
1405 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync009, TestSize.Level0)
1406 {
1407 /**
1408 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1409 * @tc.expected: step1.ok
1410 */
1411 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1412 auto schema = GetSchema();
1413 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1414 deviceB_->SetDistributedSchema(distributedSchema);
1415 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1416 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1417 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1418 /**
1419 * @tc.steps: step2. Insert one data
1420 * @tc.expected: step2.ok
1421 */
1422 auto tableSchema = GetTableSchema();
1423 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
1424 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1425 /**
1426 * @tc.steps: step3. Sync to real device
1427 * @tc.expected: step3.ok
1428 */
1429 std::string checkSql = "select count(*) from sqlite_master where type='table' and name != '" +
1430 DBCommon::GetLogTableName(DEVICE_SYNC_TABLE) + "' and name like 'naturalbase_rdb_aux_" +
1431 std::string(DEVICE_SYNC_TABLE) + "_%'";
1432 int count = 0;
1433 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, checkSql, count), E_OK);
1434 EXPECT_EQ(count, 0);
1435 Query query = Query::Select(tableSchema.name);
1436 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1437 /**
1438 * @tc.steps: step4. Check if the distributed table exists
1439 * @tc.expected: step4.ok
1440 */
1441 count = 0;
1442 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, checkSql, count), E_OK);
1443 EXPECT_EQ(count, 0);
1444 }
1445
1446 /**
1447 * @tc.name: NormalSync010
1448 * @tc.desc: Test sync without not null col.
1449 * @tc.type: FUNC
1450 * @tc.require:
1451 * @tc.author: zqq
1452 */
1453 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync010, TestSize.Level0)
1454 {
1455 /**
1456 * @tc.steps: step1. Recreate not null table
1457 * @tc.expected: step1.ok
1458 */
1459 std::string sql = std::string("DROP TABLE ").append(DEVICE_SYNC_TABLE);
1460 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1461 auto tableSchema = GetTableSchema();
1462 ASSERT_EQ(RDBDataGenerator::InitTable(tableSchema, true, *db_), E_OK);
1463 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1464 auto schema = GetSchema();
1465 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1466 deviceB_->SetDistributedSchema(distributedSchema);
1467 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1468 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), SCHEMA_MISMATCH);
1469 /**
1470 * @tc.steps: step2. Insert one data
1471 * @tc.expected: step2.ok
1472 */
1473 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1474 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1475 /**
1476 * @tc.steps: step3. Sync without str col
1477 * @tc.expected: step3.ok
1478 */
1479 Query query = Query::Select(tableSchema.name);
1480 DBStatus callStatus = delegate_->Sync({deviceB_->GetDeviceId()}, SYNC_MODE_PULL_ONLY, query, nullptr, true);
1481 EXPECT_EQ(callStatus, SCHEMA_MISMATCH);
1482 /**
1483 * @tc.steps: step4. Check if the distributed table exists
1484 * @tc.expected: step4.ok
1485 */
1486 int count = 0;
1487 sql = std::string("select count(*) from ")
1488 .append(RelationalStoreManager::GetDistributedLogTableName(DEVICE_SYNC_TABLE));
1489 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1490 EXPECT_EQ(count, 0);
1491 }
1492
1493 /**
1494 * @tc.name: NormalSync011
1495 * @tc.desc: Test set the distributed schema first then sync.
1496 * @tc.type: FUNC
1497 * @tc.require:
1498 * @tc.author: bty
1499 */
1500 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync011, TestSize.Level0)
1501 {
1502 /**
1503 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1504 * @tc.expected: step1.ok
1505 */
1506 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1507 auto schema = GetSchema();
1508 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1509 deviceB_->SetDistributedSchema(distributedSchema);
1510 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1511 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1512 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1513 /**
1514 * @tc.steps: step2. Insert one data
1515 * @tc.expected: step2.ok
1516 */
1517 auto tableSchema = GetTableSchema();
1518 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
1519 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1520 /**
1521 * @tc.steps: step3. Sync to real device
1522 * @tc.expected: step3.ok
1523 */
1524 Query query = Query::Select().FromTable({tableSchema.name});
1525 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PUSH_ONLY, OK, {deviceB_->GetDeviceId()});
1526 }
1527
1528 /**
1529 * @tc.name: NormalSync012
1530 * @tc.desc: Test sync with autoincrement table.
1531 * @tc.type: FUNC
1532 * @tc.require:
1533 * @tc.author: zqq
1534 */
1535 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync012, TestSize.Level0)
1536 {
1537 /**
1538 * @tc.steps: step1. Create auto increment table and unique index
1539 * @tc.expected: step1.ok
1540 */
1541 auto tableSchema = GetTableSchema();
1542 tableSchema.name = DEVICE_SYNC_TABLE_AUTOINCREMENT;
1543 ASSERT_EQ(RDBDataGenerator::InitTable(tableSchema, true, true, *db_), E_OK);
1544 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1545 tableSchema = GetTableSchema(false, true);
1546 tableSchema.name = DEVICE_SYNC_TABLE_AUTOINCREMENT;
1547 auto schema = GetSchema();
1548 schema.tables.push_back(tableSchema);
1549 DistributedSchema distributedSchema = {0, {{tableSchema.name, {
1550 {"pk", false, false},
1551 {"int_field1", true, false},
1552 {"int_field2", true, false},
1553 {"123", true, true}}}}};
1554 deviceB_->SetDistributedSchema(distributedSchema);
1555 int errCode = SQLiteUtils::ExecuteRawSQL(db_, std::string("CREATE UNIQUE INDEX U_INDEX ON ")
1556 .append(tableSchema.name).append("('123')"));
1557 ASSERT_EQ(errCode, E_OK);
1558 EXPECT_EQ(delegate_->CreateDistributedTable(tableSchema.name, TableSyncType::DEVICE_COOPERATION), OK);
1559 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1560 /**
1561 * @tc.steps: step2. Insert one data
1562 * @tc.expected: step2.ok
1563 */
1564 ASSERT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, tableSchema), E_OK);
1565 std::this_thread::sleep_for(std::chrono::milliseconds(100)); // sleep 100 ms
1566 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 2, deviceB_, tableSchema), E_OK);
1567 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1568 /**
1569 * @tc.steps: step3. Sync to real device
1570 * @tc.expected: step3.ok
1571 */
1572 Query query = Query::Select(tableSchema.name);
1573 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1574 std::string sql = std::string("select count(*) from ").append(tableSchema.name);
1575 int count = 0;
1576 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1577 EXPECT_EQ(count, 2);
1578 /**
1579 * @tc.steps: step4. Update date and sync again
1580 * @tc.expected: step4.ok
1581 */
1582 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1583 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1584 sql = std::string("select count(*) from ").append(tableSchema.name);
1585 count = 0;
1586 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1587 EXPECT_EQ(count, 2);
1588 }
1589
1590 /**
1591 * @tc.name: NormalSync013
1592 * @tc.desc: Test chanage data after sync.
1593 * @tc.type: FUNC
1594 * @tc.require:
1595 * @tc.author: lg
1596 */
1597 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync013, TestSize.Level0)
1598 {
1599 /**
1600 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1601 * @tc.expected: step1.ok
1602 */
1603 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1604 auto schema = GetSchema();
1605 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1606 deviceB_->SetDistributedSchema(distributedSchema);
1607 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1608 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1609 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
1610 /**
1611 * @tc.steps: step2. Insert one data
1612 * @tc.expected: step2.ok
1613 */
1614 auto tableSchema = GetTableSchema();
1615 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
1616 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1617 /**
1618 * @tc.steps: step3. Sync to real device
1619 * @tc.expected: step3.ok
1620 */
1621 Query query = Query::Select(tableSchema.name);
1622 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1623 /**
1624 * @tc.steps: step4. check changData
1625 * @tc.expected: step4.ok
1626 */
1627 auto changeData = delegateObserver_->GetSavedChangedData();
1628 EXPECT_EQ(changeData[tableSchema.name].primaryData[0].size(), 10u);
1629 EXPECT_EQ(changeData[tableSchema.name].field.size(), 1u);
1630 }
1631
1632 /**
1633 * @tc.name: NormalSync014
1634 * @tc.desc: Test chanage data after sync.
1635 * @tc.type: FUNC
1636 * @tc.require:
1637 * @tc.author: tankaisheng
1638 */
1639 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync014, TestSize.Level0)
1640 {
1641 /**
1642 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1643 * @tc.expected: step1.ok
1644 */
1645 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1646 auto schema = GetSchema();
1647 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1648 deviceB_->SetDistributedSchema(distributedSchema);
1649 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1650 DistributedSchema schema1 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"int_field1"});
1651 EXPECT_EQ(delegate_->SetDistributedSchema(schema1), OK);
1652 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_COOPERATION);
1653 /**
1654 * @tc.steps: step2. Insert data
1655 * @tc.expected: step2.ok
1656 */
1657 auto tableSchema = GetTableSchema();
1658 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
1659 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1660 /**
1661 * @tc.steps: step3. Sync to real device
1662 * @tc.expected: step3.ok
1663 */
1664 Query query = Query::Select(tableSchema.name);
1665 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1666 /**
1667 * @tc.steps: step4. check changData
1668 * @tc.expected: step4.ok
1669 */
1670 auto changeData = delegateObserver_->GetSavedChangedData();
1671 EXPECT_EQ(changeData[tableSchema.name].primaryData[0].size(), 10u);
1672 EXPECT_EQ(changeData[tableSchema.name].field.size(), 1u);
1673 /**
1674 * @tc.steps: step5. SetDistributedSchema again
1675 * @tc.expected: step5.ok
1676 */
1677 DistributedSchema schema2 = GetDistributedSchema(DEVICE_SYNC_TABLE, {"int_field1", "int_field2"});
1678 EXPECT_EQ(delegate_->SetDistributedSchema(schema2), OK);
1679 /**
1680 * @tc.steps: step6. Sync to real device
1681 * @tc.expected: step6.ok
1682 */
1683 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1684 /**
1685 * @tc.steps: step7. check changData
1686 * @tc.expected: step7.ok
1687 */
1688 changeData = delegateObserver_->GetSavedChangedData();
1689 EXPECT_EQ(changeData[tableSchema.name].primaryData[1].size(), 10u);
1690 EXPECT_EQ(changeData[tableSchema.name].field.size(), 1u);
1691 }
1692
1693 /**
1694 * @tc.name: NormalSync015
1695 * @tc.desc: Test sync with multi primary key table.
1696 * @tc.type: FUNC
1697 * @tc.require:
1698 * @tc.author: liaoyonghuang
1699 */
1700 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync015, TestSize.Level0)
1701 {
1702 /**
1703 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1704 * @tc.expected: step1.ok
1705 */
1706 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1707 std::string tableName = "multiPriKeyTable";
1708 std::string sql = "CREATE TABLE IF NOT EXISTS " + tableName +
1709 "(pk1 INTEGER, pk2 INT, PRIMARY KEY (pk1, pk2));";
1710 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1711 /**
1712 * @tc.steps: step2. Create distributed table and set distributed schema
1713 * @tc.expected: step2.ok
1714 */
1715 auto distributedSchema = GetDistributedSchema(tableName, {"pk1", "pk2"});
1716 deviceB_->SetDistributedSchema(distributedSchema);
1717 EXPECT_EQ(delegate_->CreateDistributedTable(tableName, TableSyncType::DEVICE_COOPERATION), OK);
1718 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1719 /**
1720 * @tc.steps: step3. Init data and sync to real device
1721 * @tc.expected: step3.ok
1722 */
1723 TableSchema tableSchema;
1724 tableSchema.name = tableName;
1725 Field field;
1726 field.primary = true;
1727 field.type = TYPE_INDEX<int64_t>;
1728 field.colName = "pk1";
1729 tableSchema.fields.push_back(field);
1730 field.colName = "pk2";
1731 tableSchema.fields.push_back(field);
1732 uint32_t dataCount = 10;
1733 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount, deviceB_, tableSchema), E_OK);
1734 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableName, db_, deviceB_), E_OK);
1735 Query query = Query::Select(tableName);
1736 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1737 /**
1738 * @tc.steps: step4. check changData
1739 * @tc.expected: step4.ok
1740 */
1741 auto changeData = delegateObserver_->GetSavedChangedData();
1742 ASSERT_EQ(changeData[tableName].primaryData[OP_INSERT].size(), dataCount);
1743 for (uint32_t i = 0; i < dataCount; i++) {
1744 EXPECT_EQ(changeData[tableName].primaryData[OP_INSERT][i].size(), 3u); // primary key (pk1, pk2) and rowid
1745 }
1746 EXPECT_EQ(changeData[tableName].field.size(), 3u); // primary key (pk1, pk2) and rowid
1747 }
1748
1749 /**
1750 * @tc.name: NormalSync016
1751 * @tc.desc: Test sync with diff specified field.
1752 * @tc.type: FUNC
1753 * @tc.require:
1754 * @tc.author: zqq
1755 */
1756 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync016, TestSize.Level0)
1757 {
1758 /**
1759 * @tc.steps: step1. Prepare db, tableMode is COLLABORATION
1760 * @tc.expected: step1.ok
1761 */
1762 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::COLLABORATION));
1763 std::string createSql = "CREATE TABLE IF NOT EXISTS table_int(integer_field INTEGER PRIMARY KEY AUTOINCREMENT,"
1764 "int_field1 INT UNIQUE, int_field2 INT UNIQUE);";
1765 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, createSql), E_OK);
1766 EXPECT_EQ(delegate_->CreateDistributedTable("table_int", TableSyncType::DEVICE_COOPERATION), OK);
1767
1768 /**
1769 * @tc.steps: step2. Test mark one specified one is field1 another is field2
1770 * @tc.expected: step2. sync return SCHEMA_MISMATCH
1771 */
1772 DistributedSchema distributedSchema = {0, {{"table_int", {
1773 {"integer_field", false, false},
1774 {"int_field1", true, false},
1775 {"int_field2", true, true}}}}};
1776 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1777 distributedSchema = {0, {{"table_int", {
1778 {"integer_field", false, false},
1779 {"int_field1", true, true},
1780 {"int_field2", true, false}}}}};
1781 deviceB_->SetDistributedSchema(distributedSchema);
1782 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv("table_int", db_, deviceB_), E_OK);
1783 Query query = Query::Select("table_int");
1784 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, SCHEMA_MISMATCH,
1785 {deviceB_->GetDeviceId()});
1786 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PUSH_ONLY, SCHEMA_MISMATCH,
1787 {deviceB_->GetDeviceId()});
1788 }
1789
1790 /**
1791 * @tc.name: NormalSync017
1792 * @tc.desc: Test delete other device's data and sync
1793 * @tc.type: FUNC
1794 * @tc.require:
1795 * @tc.author: liaoyonghuang
1796 */
1797 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync017, TestSize.Level0)
1798 {
1799 /**
1800 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1801 * @tc.expected: step1.ok
1802 */
1803 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1804 auto schema = GetSchema();
1805 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1806 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1807 deviceB_->SetDistributedSchema(distributedSchema);
1808 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1809 /**
1810 * @tc.steps: step2. Insert one data
1811 * @tc.expected: step2.ok
1812 */
1813 auto tableSchema = GetTableSchema();
1814 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1815 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1816 /**
1817 * @tc.steps: step3. Sync to real device
1818 * @tc.expected: step3.ok
1819 */
1820 Query query = Query::Select(tableSchema.name);
1821 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1822 std::string sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE);
1823 int count = 0;
1824 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1825 EXPECT_EQ(count, 1);
1826 /**
1827 * @tc.steps: step4. Delete data and sync again
1828 * @tc.expected: step4.ok
1829 */
1830 EXPECT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1831 std::this_thread::sleep_for(std::chrono::milliseconds(100)); // sleep 100 ms
1832 sql = std::string("delete from ").append(DEVICE_SYNC_TABLE).append(" where 0 = 0");
1833 EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1834 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1835 sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE);
1836 count = 0;
1837 EXPECT_EQ(SQLiteUtils::GetCountBySql(db_, sql, count), E_OK);
1838 EXPECT_EQ(count, 0);
1839 auto changeData = delegateObserver_->GetSavedChangedData()[std::string(DEVICE_SYNC_TABLE)];
1840 EXPECT_TRUE(changeData.properties.isP2pSyncDataChange);
1841 }
1842
1843 /**
1844 * @tc.name: NormalSync018
1845 * @tc.desc: Test sync with no primary key table.
1846 * @tc.type: FUNC
1847 * @tc.require:
1848 * @tc.author: liaoyonghuang
1849 */
1850 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync018, TestSize.Level0)
1851 {
1852 /**
1853 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1854 * @tc.expected: step1.ok
1855 */
1856 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1857 std::string tableName = "noPriKeyTable";
1858 std::string sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(pk1 INTEGER, pk2 INT);";
1859 ASSERT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1860 /**
1861 * @tc.steps: step2. Create distributed table and set distributed schema
1862 * @tc.expected: step2.ok
1863 */
1864 auto distributedSchema = GetDistributedSchema(tableName, {"pk1", "pk2"});
1865 deviceB_->SetDistributedSchema(distributedSchema);
1866 EXPECT_EQ(delegate_->CreateDistributedTable(tableName, TableSyncType::DEVICE_COOPERATION), OK);
1867 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1868 /**
1869 * @tc.steps: step3. Init data and sync to real device
1870 * @tc.expected: step3.ok
1871 */
1872 TableSchema tableSchema;
1873 tableSchema.name = tableName;
1874 Field field;
1875 field.primary = true;
1876 field.type = TYPE_INDEX<int64_t>;
1877 field.colName = "pk1";
1878 tableSchema.fields.push_back(field);
1879 field.colName = "pk2";
1880 tableSchema.fields.push_back(field);
1881 uint32_t dataCount = 10;
1882 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount, deviceB_, tableSchema), E_OK);
1883 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableName, db_, deviceB_), E_OK);
1884 Query query = Query::Select(tableName);
1885 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1886 /**
1887 * @tc.steps: step4. check changData
1888 * @tc.expected: step4.ok
1889 */
1890 auto changeData = delegateObserver_->GetSavedChangedData();
1891 ASSERT_EQ(changeData[tableName].primaryData[OP_INSERT].size(), dataCount);
1892 for (uint32_t i = 0; i < dataCount; i++) {
1893 EXPECT_EQ(changeData[tableName].primaryData[OP_INSERT][i].size(), 1u); // rowid
1894 }
1895 EXPECT_EQ(changeData[tableName].field.size(), 1u); // rowid
1896 }
1897
1898 /**
1899 * @tc.name: NormalSync019
1900 * @tc.desc: Test whether there is an observer notification when local win.
1901 * @tc.type: FUNC
1902 * @tc.require:
1903 * @tc.author: liaoyonghuang
1904 */
1905 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync019, TestSize.Level0)
1906 {
1907 /**
1908 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1909 * @tc.expected: step1.ok
1910 */
1911 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1912 auto schema = GetSchema();
1913 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1914 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1915 deviceB_->SetDistributedSchema(distributedSchema);
1916 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1917 /**
1918 * @tc.steps: step2. Insert a piece of data from the other end and then locally insert the same data.
1919 * @tc.expected: step2.ok
1920 */
1921 auto tableSchema = GetTableSchema();
1922 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1923 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1924 std::string sql = std::string("insert into ").append(DEVICE_SYNC_TABLE).append("(pk) values (0)");
1925 EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1926 /**
1927 * @tc.steps: step3. Sync to real device
1928 * @tc.expected: step3.ok
1929 */
1930 Query query = Query::Select(tableSchema.name);
1931 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1932 /**
1933 * @tc.steps: step4. Check observer
1934 * @tc.expected: step4.No data change notification
1935 */
1936 auto onChangeCallCount = delegateObserver_->GetCloudCallCount();
1937 EXPECT_EQ(onChangeCallCount, 0u);
1938 }
1939
1940 /**
1941 * @tc.name: NormalSync020
1942 * @tc.desc: Test set distributed schema after recreating the table.
1943 * @tc.type: FUNC
1944 * @tc.require:
1945 * @tc.author: liaoyonghuang
1946 */
1947 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync020, TestSize.Level0)
1948 {
1949 /**
1950 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1951 * @tc.expected: step1.ok
1952 */
1953 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1954 auto schema = GetSchema();
1955 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1956 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1957 deviceB_->SetDistributedSchema(distributedSchema);
1958 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1959 /**
1960 * @tc.steps: step2. Sync a piece of data
1961 * @tc.expected: step2.ok
1962 */
1963 auto tableSchema = GetTableSchema();
1964 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 1, deviceB_, tableSchema), E_OK);
1965 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
1966 Query query = Query::Select(tableSchema.name);
1967 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
1968 /**
1969 * @tc.steps: step3. Recreate table and set distributed schema.
1970 * @tc.expected: step3.ok
1971 */
1972 std::string sql = std::string("drop table ").append(DEVICE_SYNC_TABLE);
1973 EXPECT_EQ(SQLiteUtils::ExecuteRawSQL(db_, sql), E_OK);
1974 RDBDataGenerator::InitTable(schema.tables.front(), false, *db_);
1975 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1976 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1977 }
1978
1979 /**
1980 * @tc.name: NormalSync021
1981 * @tc.desc: Test sync multi table.
1982 * @tc.type: FUNC
1983 * @tc.require:
1984 * @tc.author: liaoyonghuang
1985 */
1986 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync021, TestSize.Level0)
1987 {
1988 /**
1989 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
1990 * @tc.expected: step1.ok
1991 */
1992 ASSERT_NO_FATAL_FAILURE(InitDelegate());
1993 auto schema = GetSchema();
1994 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
1995 deviceB_->SetDistributedSchema(distributedSchema);
1996 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
1997 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
1998 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE_UPGRADE, TableSyncType::DEVICE_COOPERATION), OK);
1999 /**
2000 * @tc.steps: step2. Insert 10 data
2001 * @tc.expected: step2.ok
2002 */
2003 int dataCount = 10;
2004 auto tableSchema = GetTableSchema();
2005 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount, deviceB_, tableSchema), E_OK);
2006 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
2007 tableSchema = GetTableSchema(true);
2008 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount, deviceB_, tableSchema), E_OK);
2009 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
2010 /**
2011 * @tc.steps: step3. Sync to real device and check data
2012 * @tc.expected: step3.ok
2013 */
2014 Query query = Query::Select().FromTable({DEVICE_SYNC_TABLE, DEVICE_SYNC_TABLE_UPGRADE});
2015 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
2016 std::string sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE);
2017 int actualCount = 0;
2018 SQLiteUtils::GetCountBySql(db_, sql, actualCount);
2019 EXPECT_EQ(actualCount, dataCount);
2020 sql = std::string("select count(*) from ").append(DEVICE_SYNC_TABLE_UPGRADE);
2021 actualCount = 0;
2022 SQLiteUtils::GetCountBySql(db_, sql, actualCount);
2023 EXPECT_EQ(actualCount, dataCount);
2024 /**
2025 * @tc.steps: step4. Sync with invalid args
2026 * @tc.expected: step4.invalid args
2027 */
2028 query.And();
2029 DBStatus callStatus = delegate_->Sync({deviceB_->GetDeviceId()}, SYNC_MODE_PUSH_ONLY, query, nullptr, true);
2030 EXPECT_EQ(callStatus, NOT_SUPPORT);
2031 }
2032
2033 /**
2034 * @tc.name: NormalSync022
2035 * @tc.desc: Test set store config.
2036 * @tc.type: FUNC
2037 * @tc.require:
2038 * @tc.author: liaoyonghuang
2039 */
2040 HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync022, TestSize.Level0)
2041 {
2042 /**
2043 * @tc.steps: step1. Create device table and cloud table in SPLIT_BY_DEVICE
2044 * @tc.expected: step1.ok
2045 */
2046 ASSERT_NO_FATAL_FAILURE(InitDelegate(DistributedTableMode::SPLIT_BY_DEVICE));
2047 /**
2048 * @tc.steps: step2. Set store config.
2049 * @tc.expected: step2.ok
2050 */
2051 EXPECT_EQ(delegate_->SetStoreConfig({DistributedTableMode::COLLABORATION}), OK);
2052 auto schema = GetSchema();
2053 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
2054 deviceB_->SetDistributedSchema(distributedSchema);
2055 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
2056 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
2057 /**
2058 * @tc.steps: step3. Sync to real device
2059 * @tc.expected: step3.ok
2060 */
2061 auto tableSchema = GetTableSchema();
2062 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, 10, deviceB_, tableSchema), E_OK);
2063 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
2064 Query query = Query::Select(tableSchema.name);
2065 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, OK, {deviceB_->GetDeviceId()});
2066 }
2067
2068 /**
2069 * @tc.name: InvalidSync001
2070 * @tc.desc: Test remote set empty distributed schema and sync.
2071 * @tc.type: FUNC
2072 * @tc.require:
2073 * @tc.author: bty
2074 */
2075 HWTEST_F(DistributedDBRDBCollaborationTest, InvalidSync001, TestSize.Level0)
2076 {
2077 /**
2078 * @tc.steps: step1. Remote device set empty distributed schema
2079 * @tc.expected: step1.ok
2080 */
2081 ASSERT_NO_FATAL_FAILURE(InitDelegate());
2082 auto schema = GetSchema();
2083 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
2084 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
2085 LOGI("[DistributedDBCloudAsyncDownloadAssetsTest] CreateDistributedTable %s", DEVICE_SYNC_TABLE);
2086 DistributedSchema emptySchema;
2087 deviceB_->SetDistributedSchema(emptySchema);
2088 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
2089 /**
2090 * @tc.steps: step2. Insert one data
2091 * @tc.expected: step2.ok
2092 */
2093 auto tableSchema = GetTableSchema();
2094 EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK);
2095 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
2096 /**
2097 * @tc.steps: step3. Sync to real device
2098 * @tc.expected: step3.SCHEMA_MISMATCH
2099 */
2100 Query query = Query::Select(tableSchema.name);
2101 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PUSH_ONLY, SCHEMA_MISMATCH,
2102 {deviceB_->GetDeviceId()});
2103 /**
2104 * @tc.steps: step4. Remove device data
2105 * @tc.expected: step4. NOT_SUPPORT
2106 */
2107 EXPECT_EQ(delegate_->RemoveDeviceData("dev", DEVICE_SYNC_TABLE), NOT_SUPPORT);
2108 EXPECT_EQ(delegate_->RemoveDeviceData(), NOT_SUPPORT);
2109 EXPECT_EQ(delegate_->RemoveDeviceData("dev", ClearMode::DEFAULT), NOT_SUPPORT);
2110 }
2111
2112 /**
2113 * @tc.name: InvalidSync004
2114 * @tc.desc: Test sync with empty tables
2115 * @tc.type: FUNC
2116 * @tc.require:
2117 * @tc.author: liaoyonghuang
2118 */
2119 HWTEST_F(DistributedDBRDBCollaborationTest, InvalidSync004, TestSize.Level0)
2120 {
2121 ASSERT_NO_FATAL_FAILURE(InitDelegate());
2122 Query query = Query::Select().FromTable({});
2123 DBStatus callStatus = delegate_->Sync({deviceB_->GetDeviceId()}, SYNC_MODE_PUSH_ONLY, query, nullptr, true);
2124 EXPECT_EQ(callStatus, INVALID_ARGS);
2125 }
2126
2127 /**
2128 * @tc.name: InvalidSync005
2129 * @tc.desc: Test error returned by the other end during sync
2130 * @tc.type: FUNC
2131 * @tc.require:
2132 * @tc.author: liaoyonghuang
2133 */
2134 HWTEST_F(DistributedDBRDBCollaborationTest, InvalidSync005, TestSize.Level0)
2135 {
2136 /**
2137 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
2138 * @tc.expected: step1.ok
2139 */
2140 ASSERT_NO_FATAL_FAILURE(InitDelegate());
2141 auto schema = GetSchema();
2142 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
2143 deviceB_->SetDistributedSchema(distributedSchema);
2144 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
2145 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
2146 /**
2147 * @tc.steps: step2. Prepare device B
2148 * @tc.expected: step2.ok
2149 */
2150 int dataCount = 10;
2151 auto tableSchema = GetTableSchema();
2152 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount, deviceB_, tableSchema), E_OK);
2153 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
2154 /**
2155 * @tc.steps: step3. Set return E_DISTRIBUTED_SCHEMA_NOT_FOUND when get sync data in device B
2156 * @tc.expected: step3.ok
2157 */
2158 deviceB_->SetGetSyncDataResult(-E_DISTRIBUTED_SCHEMA_NOT_FOUND);
2159 /**
2160 * @tc.steps: step4. Sync
2161 * @tc.expected: step4. return SCHEMA_MISMATCH
2162 */
2163 Query query = Query::Select().FromTable({DEVICE_SYNC_TABLE});
2164 DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PULL_ONLY, SCHEMA_MISMATCH,
2165 {deviceB_->GetDeviceId()});
2166 }
2167
2168 /**
2169 * @tc.name: InvalidSync006
2170 * @tc.desc: Test FromTable and other predicates are combined when sync
2171 * @tc.type: FUNC
2172 * @tc.require:
2173 * @tc.author: liaoyonghuang
2174 */
2175 HWTEST_F(DistributedDBRDBCollaborationTest, InvalidSync006, TestSize.Level0)
2176 {
2177 /**
2178 * @tc.steps: step1. Create device table and cloud table in COLLABORATION
2179 * @tc.expected: step1.ok
2180 */
2181 ASSERT_NO_FATAL_FAILURE(InitDelegate());
2182 auto schema = GetSchema();
2183 auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true);
2184 deviceB_->SetDistributedSchema(distributedSchema);
2185 EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK);
2186 EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK);
2187 /**
2188 * @tc.steps: step2. Prepare device B
2189 * @tc.expected: step2.ok
2190 */
2191 int dataCount = 10;
2192 auto tableSchema = GetTableSchema();
2193 ASSERT_EQ(RDBDataGenerator::InsertVirtualLocalDBData(0, dataCount, deviceB_, tableSchema), E_OK);
2194 ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK);
2195 /**
2196 * @tc.steps: step3. Prepare query list
2197 * @tc.expected: step3.ok
2198 */
2199 std::set<Key> keys = {{1}, {2}, {3}};
2200 std::vector<int> values = {1, 2, 3};
2201 std::vector<Query> queryList = {
2202 Query::Select().FromTable({DEVICE_SYNC_TABLE}).BeginGroup().EqualTo("pk", 1),
2203 Query::Select().FromTable({DEVICE_SYNC_TABLE}).EqualTo("pk", 1),
2204 Query::Select().FromTable({DEVICE_SYNC_TABLE}).GreaterThan("pk", 1),
2205 Query::Select().FromTable({DEVICE_SYNC_TABLE}).GreaterThanOrEqualTo("pk", 1),
2206 Query::Select().FromTable({DEVICE_SYNC_TABLE}).In("pk", values),
2207 Query::Select().FromTable({DEVICE_SYNC_TABLE}).NotIn("pk", values),
2208 Query::Select().FromTable({DEVICE_SYNC_TABLE}).IsNotNull("pk"),
2209 Query::Select().FromTable({DEVICE_SYNC_TABLE}).LessThan("pk", 1),
2210 Query::Select().FromTable({DEVICE_SYNC_TABLE}).LessThanOrEqualTo("pk", 1),
2211 Query::Select().FromTable({DEVICE_SYNC_TABLE}).Like("pk", "abc"),
2212 Query::Select().FromTable({DEVICE_SYNC_TABLE}).NotLike("pk", "abc"),
2213 Query::Select().FromTable({DEVICE_SYNC_TABLE}).OrderByWriteTime(false),
2214 Query::Select().FromTable({DEVICE_SYNC_TABLE}).SuggestIndex("pk"),
2215 Query::Select().FromTable({DEVICE_SYNC_TABLE}).PrefixKey({1, 2, 3}),
2216 Query::Select().FromTable({DEVICE_SYNC_TABLE}).InKeys(keys)
2217 };
2218 /**
2219 * @tc.steps: step4. Sync
2220 * @tc.expected: step4. return NOT_SUPPORT
2221 */
2222 for (const auto &query : queryList) {
2223 DBStatus callStatus = delegate_->Sync({deviceB_->GetDeviceId()}, SYNC_MODE_PUSH_ONLY, query, nullptr, true);
2224 EXPECT_EQ(callStatus, NOT_SUPPORT);
2225 }
2226 }
2227 }