• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #include <gtest/gtest.h>
17 
18 #include "db_common.h"
19 #include "distributeddb_data_generate_unit_test.h"
20 #include "distributeddb_tools_unit_test.h"
21 #include "log_print.h"
22 #include "native_sqlite.h"
23 #include "relational_store_manager.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     constexpr const char* DB_SUFFIX = ".db";
33     constexpr const char* STORE_ID = "Relational_Store_ID";
34     const std::string DEVICE_A = "DEVICE_A";
35     std::string g_testDir;
36     std::string g_dbDir;
37     DistributedDB::RelationalStoreManager g_mgr(APP_ID, USER_ID);
38     VirtualCommunicatorAggregator* g_communicatorAggregator = nullptr;
39 }
40 
41 class DistributedDBInterfacesRelationalRoutinesTest : public testing::Test {
42 public:
43     static void SetUpTestCase(void);
44     static void TearDownTestCase(void);
45     void SetUp() override;
46     void TearDown() override;
47 protected:
48 };
49 
SetUpTestCase(void)50 void DistributedDBInterfacesRelationalRoutinesTest::SetUpTestCase(void)
51 {
52     DistributedDBToolsUnitTest::TestDirInit(g_testDir);
53     LOGD("Test dir is %s", g_testDir.c_str());
54     g_dbDir = g_testDir + "/";
55 
56     g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
57     ASSERT_TRUE(g_communicatorAggregator != nullptr);
58     RuntimeContext::GetInstance()->SetCommunicatorAggregator(g_communicatorAggregator);
59 }
60 
TearDownTestCase(void)61 void DistributedDBInterfacesRelationalRoutinesTest::TearDownTestCase(void)
62 {
63     RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
64     DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir);
65 }
66 
SetUp()67 void DistributedDBInterfacesRelationalRoutinesTest::SetUp()
68 {
69     DistributedDBToolsUnitTest::PrintTestCaseInfo();
70 }
71 
TearDown()72 void DistributedDBInterfacesRelationalRoutinesTest::TearDown()
73 {
74 }
75 
76 /**
77  * @tc.name: DBMaxTimeStampTest001
78  * @tc.desc: check db max timestamp when open
79  * @tc.type: FUNC
80  * @tc.require:
81  * @tc.author: xulianhui
82   */
83 HWTEST_F(DistributedDBInterfacesRelationalRoutinesTest, DBMaxTimeStampTest001, TestSize.Level1)
84 {
85     sqlite3 *ndb = NativeSqlite::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
86     ASSERT_NE(ndb, nullptr);
87     EXPECT_EQ(NativeSqlite::ExecSql(ndb, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
88     EXPECT_EQ(NativeSqlite::ExecSql(ndb, "create table if not exists t2(a int, b int);"), SQLITE_OK);
89     std::string t2Log = R""(CREATE TABLE naturalbase_rdb_aux_t2_log(
90         data_key    INT NOT NULL,
91         device      BLOB,
92         ori_device  BLOB,
93         timestamp   INT  NOT NULL,
94         wtimestamp  INT  NOT NULL,
95         flag        INT  NOT NULL,
96         hash_key    BLOB NOT NULL,
97         PRIMARY KEY(device, hash_key)
98     );)"";
99     EXPECT_EQ(NativeSqlite::ExecSql(ndb, t2Log), SQLITE_OK);
100 
101     int64_t fakeTimestamp = 26640033917191310; // 26640033917191310: Large enough fake timestamp
102     std::string insertLog = "insert into naturalbase_rdb_aux_t2_log " \
103         "values(1, '" + DEVICE_A + "', '" + DEVICE_A + "', " + std::to_string(fakeTimestamp) +
104         ", " + std::to_string(fakeTimestamp) + ", 0, 'hash_key')";
105     EXPECT_EQ(NativeSqlite::ExecSql(ndb, insertLog), SQLITE_OK);
106     EXPECT_EQ(sqlite3_close_v2(ndb), SQLITE_OK);
107 
108     sqlite3 *db = RelationalTestUtils::CreateDataBase(g_dbDir + STORE_ID + DB_SUFFIX);
109     ASSERT_NE(db, nullptr);
110     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "PRAGMA journal_mode=WAL;"), SQLITE_OK);
111     EXPECT_EQ(RelationalTestUtils::ExecSql(db, "create table if not exists t1(a int, b int);"), SQLITE_OK);
112 
113     RelationalStoreDelegate *delegate = nullptr;
114     DBStatus status = g_mgr.OpenStore(g_dbDir + STORE_ID + DB_SUFFIX, STORE_ID, {}, delegate);
115     EXPECT_EQ(status, OK);
116     ASSERT_NE(delegate, nullptr);
117     delegate->CreateDistributedTable("t1");
118     g_mgr.CloseStore(delegate);
119 
120     RelationalTestUtils::ExecSql(db, "insert into t1 values(1, 1);");
121 
122     int64_t logTime;
123     std::string checkSql = "select timestamp from naturalbase_rdb_aux_t1_log where data_key = 1";
__anonf58b60a50202(sqlite3_stmt *stmt) 124     RelationalTestUtils::ExecSql(db, checkSql, nullptr, [&logTime](sqlite3_stmt *stmt) {
125         logTime = sqlite3_column_int64(stmt, 0);
126         return E_OK;
127     });
128 
129     EXPECT_GT(logTime, fakeTimestamp);
130     EXPECT_EQ(sqlite3_close_v2(db), SQLITE_OK);
131 }
132