• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <iostream>
22 #include <string>
23 
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_manager.h"
27 #include "rdb_open_callback.h"
28 #include "rdb_predicates.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::NativeRdb;
32 using namespace OHOS::DistributedRdb;
33 
34 static std::shared_ptr<RdbStore> rdbStore;
35 
36 class RdbStoreDistributedTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
TearDownTestCase()39     static void TearDownTestCase() {
40         RdbHelper::DeleteRdbStore(RdbStoreDistributedTest::DRDB_PATH + RdbStoreDistributedTest::DRDB_NAME);
41     }
SetUp()42     void SetUp() {}
TearDown()43     void TearDown() {}
44 
45     void InsertValue(std::shared_ptr<RdbStore> &store);
46     void CheckResultSet(std::shared_ptr<RdbStore> &store);
47 
48     static constexpr int ddmsGroupId_ = 1000;
49     static const std::string DRDB_NAME;
50     static const  std::string DRDB_PATH;
51 };
52 
53 const std::string RdbStoreDistributedTest::DRDB_NAME = "distributed_rdb.db";
54 const std::string RdbStoreDistributedTest::DRDB_PATH = "/data/test/";
55 
56 class TestOpenCallback : public RdbOpenCallback {
57 public:
OnCreate(RdbStore & store)58     int OnCreate(RdbStore& store) override
59     {
60         std::cout << "on create" << std::endl;
61         std::string sql = "CREATE TABLE IF NOT EXISTS employee ("
62                           "id INTEGER PRIMARY KEY AUTOINCREMENT,"
63                           "name TEXT NOT NULL,"
64                           "age INTEGER,"
65                           "salary REAL,"
66                           "data BLOB)";
67         std::cout << "create table return " << store.ExecuteSql(sql) << std::endl;
68         return 0;
69     }
70 
OnOpen(RdbStore & store)71     int OnOpen(RdbStore& store) override
72     {
73         std::cout << "on open" << std::endl;
74         return 0;
75     }
76 
OnUpgrade(RdbStore & store,int currentVersion,int targetVersion)77     int OnUpgrade(RdbStore& store, int currentVersion, int targetVersion) override
78     {
79         std::cout << "on upgrade" << std::endl;
80         return 0;
81     }
82 };
83 
SetUpTestCase()84 void RdbStoreDistributedTest::SetUpTestCase()
85 {
86     int errCode = 0;
87     std::string path = RdbStoreDistributedTest::DRDB_PATH + RdbStoreDistributedTest::DRDB_NAME;
88     int fd = open(path.c_str(), O_CREAT, S_IRWXU | S_IRWXG);
89     if (fd < 0) {
90         std::cout << "open file failed" << std::endl;
91     }
92     if (fd > 0) {
93         close(fd);
94     }
95     chown(path.c_str(), 0, ddmsGroupId_);
96 
97     RdbStoreConfig config(path);
98     config.SetBundleName("com.example.distributed.rdb");
99     config.SetName(RdbStoreDistributedTest::DRDB_NAME);
100     TestOpenCallback callback;
101     rdbStore = RdbHelper::GetRdbStore(config, 1, callback, errCode);
102     if (rdbStore == nullptr) {
103         std::cout << "get rdb store failed" << std::endl;
104     } else {
105         std::cout << "get rdb store success" << std::endl;
106     }
107 }
108 
InsertValue(std::shared_ptr<RdbStore> & store)109 void RdbStoreDistributedTest::InsertValue(std::shared_ptr<RdbStore> &store)
110 {
111     int64_t id;
112     ValuesBucket values;
113 
114     values.PutInt("id", 1);
115     values.PutString("name", std::string("zhangsan"));
116     values.PutInt("age", 18); // 18 age
117     values.PutDouble("salary", 100.5); // 100.5
118     values.PutBlob("data", std::vector<uint8_t>{ 1, 2, 3 });
119     int ret = store->Insert(id, "employee", values);
120     EXPECT_EQ(ret, E_OK);
121     EXPECT_EQ(1, id);
122 }
123 
CheckResultSet(std::shared_ptr<RdbStore> & store)124 void RdbStoreDistributedTest::CheckResultSet(std::shared_ptr<RdbStore> &store)
125 {
126     std::unique_ptr<ResultSet> resultSet =
127         store->QuerySql("SELECT * FROM employee WHERE name = ?", { "zhangsan" });
128     EXPECT_NE(resultSet, nullptr);
129 
130     int columnIndex;
131     int intVal;
132     std::string strVal;
133     ColumnType columnType;
134     int position;
135     int ret = resultSet->GetRowIndex(position);
136     EXPECT_EQ(ret, E_OK);
137     EXPECT_EQ(position, -1);
138 
139     ret = resultSet->GetColumnType(0, columnType);
140     EXPECT_EQ(ret, E_INVALID_STATEMENT);
141 
142     ret = resultSet->GoToFirstRow();
143     EXPECT_EQ(ret, E_OK);
144 
145     ret = resultSet->GetColumnIndex("id", columnIndex);
146     EXPECT_EQ(ret, E_OK);
147     EXPECT_EQ(columnIndex, 0);
148     ret = resultSet->GetColumnType(columnIndex, columnType);
149     EXPECT_EQ(ret, E_OK);
150     EXPECT_EQ(columnType, ColumnType::TYPE_INTEGER);
151     ret = resultSet->GetInt(columnIndex, intVal);
152     EXPECT_EQ(ret, E_OK);
153     EXPECT_EQ(1, intVal);
154 
155     ret = resultSet->GetColumnIndex("name", columnIndex);
156     EXPECT_EQ(ret, E_OK);
157     ret = resultSet->GetColumnType(columnIndex, columnType);
158     EXPECT_EQ(ret, E_OK);
159     EXPECT_EQ(columnType, ColumnType::TYPE_STRING);
160     ret = resultSet->GetString(columnIndex, strVal);
161     EXPECT_EQ(ret, E_OK);
162     std::cout << "strVal=" << strVal << std::endl;
163     EXPECT_EQ("zhangsan", strVal);
164 }
165 
166 class DistributedTestStoreObserver : public RdbStoreObserver
167 {
168 public:
169     void OnChange(const std::vector<std::string> &devices) override;
170 };
171 
OnChange(const vector<std::string> & devices)172 void DistributedTestStoreObserver::OnChange(const vector<std::string> &devices) {}
173 
174 /**
175  * @tc.name: RdbStore_Distributed_Test_001
176  * @tc.desc: test RdbStore set distributed tables
177  * @tc.type: FUNC
178  * @tc.require: AR000GK58F
179  * @tc.author: wuchunbo
180  */
181 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_001, TestSize.Level1)
182 {
183     EXPECT_NE(rdbStore, nullptr) << "get rdb store failed";
184     InsertValue(rdbStore);
185     CheckResultSet(rdbStore);
186 }
187 
188 /**
189  * @tc.name: RdbStore_Distributed_Test_002
190  * @tc.desc: test RdbStore set distributed tables
191  * @tc.type: FUNC
192  * @tc.require:
193  * @tc.author:
194  */
195 HWTEST_F(RdbStoreDistributedTest, RdbStore_Distributed_002, TestSize.Level1)
196 {
197     EXPECT_NE(rdbStore, nullptr);
198     std::vector<std::string> tables;
199     tables.emplace_back("employee");
200     int ret = rdbStore->SetDistributedTables(tables);
201     EXPECT_EQ(ret == E_OK, false);
202     int errCode = E_ERROR;
203     std::string table = rdbStore->ObtainDistributedTableName("7001005458323933328a254f9b263900", "employee", errCode);
204     EXPECT_EQ(table, "");
205     SyncOption syncOption;
206     syncOption.mode = PUSH;
207     syncOption.isBlock = true;
208     OHOS::NativeRdb::RdbPredicates rdbPredicates("employee");
__anond91a896d0102(const SyncResult &result) 209     ret = rdbStore->Sync(syncOption, rdbPredicates, [](const SyncResult &result) {});
210     EXPECT_EQ(ret == E_OK, false);
211     SubscribeOption subscribeOption;
212     subscribeOption.mode = REMOTE;
213     DistributedTestStoreObserver observer;
214     ret = rdbStore->Subscribe(subscribeOption, &observer);
215     EXPECT_EQ(ret, E_OK);
216     ret = rdbStore->UnSubscribe(subscribeOption, &observer);
217     EXPECT_EQ(ret, E_OK);
218 }
219