• 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 <string>
19 
20 #include "common.h"
21 #include "logger.h"
22 #include "rdb_errno.h"
23 #include "rdb_helper.h"
24 #include "rdb_open_callback.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::NativeRdb;
28 
29 class RdbStoreInterfaceTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 };
36 
37 class MyOpenCallback : public RdbOpenCallback {
38 public:
39     int OnCreate(RdbStore &rdbStore) override;
40     int OnUpgrade(RdbStore &rdbStore, int oldVersion, int newVersion) override;
41     static const std::string CREATE_TABLE_TEST;
42 };
43 
44 const std::string MyOpenCallback::CREATE_TABLE_TEST = std::string("CREATE TABLE IF NOT EXISTS test ")
45                                                       + std::string("(id INTEGER PRIMARY KEY AUTOINCREMENT, name "
46                                                                     "TEXT, "
47                                                                     "age INTEGER, salary REAL, blobType BLOB)");
48 
OnCreate(RdbStore & store)49 int MyOpenCallback::OnCreate(RdbStore &store)
50 {
51     return store.ExecuteSql(CREATE_TABLE_TEST);
52 }
53 
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)54 int MyOpenCallback::OnUpgrade(RdbStore &store, int oldVersion, int newVersion)
55 {
56     return E_OK;
57 }
58 
SetUpTestCase(void)59 void RdbStoreInterfaceTest::SetUpTestCase(void)
60 {
61 }
62 
TearDownTestCase(void)63 void RdbStoreInterfaceTest::TearDownTestCase(void)
64 {
65 }
66 
SetUp(void)67 void RdbStoreInterfaceTest::SetUp(void)
68 {
69 }
70 
TearDown(void)71 void RdbStoreInterfaceTest::TearDown(void)
72 {
73 }
74 
75 /**
76  * @tc.name: ValueObject_TEST_001
77  * @tc.desc: test ValueObject
78  * @tc.type: FUNC
79  * @tc.require: AR000CU2BO
80  * @tc.author: chenxi
81  */
82 HWTEST_F(RdbStoreInterfaceTest, ValueObject_TEST_001, TestSize.Level1)
83 {
84     ValueObject obj = ValueObject();
85     ValueObjectType type = obj.GetType();
86     EXPECT_EQ(type, ValueObjectType::TYPE_NULL);
87 }
88 
89 /**
90  * @tc.name: ValueObject_TEST_002
91  * @tc.desc: test ValueObject
92  * @tc.type: FUNC
93  * @tc.require: AR000CU2BO
94  * @tc.author: chenxi
95  */
96 HWTEST_F(RdbStoreInterfaceTest, ValueObject_TEST_002, TestSize.Level1)
97 {
98     int inputVal = 5;
99     int outputVal = 0;
100     ValueObject obj = ValueObject(inputVal);
101     ValueObjectType type = obj.GetType();
102     EXPECT_EQ(type, ValueObjectType::TYPE_INT);
103     int ret = obj.GetInt(outputVal);
104     EXPECT_EQ(ret, E_OK);
105     EXPECT_EQ(outputVal, 5);
106 }
107 
108 /**
109  * @tc.name: ValueObject_TEST_003
110  * @tc.desc: test ValueObject
111  * @tc.type: FUNC
112  * @tc.require: AR000CU2BO
113  * @tc.author: chenxi
114  */
115 HWTEST_F(RdbStoreInterfaceTest, ValueObject_TEST_003, TestSize.Level1)
116 {
117     bool inputVal = true;
118     bool outputVal = false;
119     ValueObject obj = ValueObject(inputVal);
120     ValueObjectType type = obj.GetType();
121     EXPECT_EQ(type, ValueObjectType::TYPE_BOOL);
122     int ret = obj.GetBool(outputVal);
123     EXPECT_EQ(ret, E_OK);
124     EXPECT_EQ(outputVal, true);
125 }
126 
127 /**
128  * @tc.name: ValueObject_TEST_004
129  * @tc.desc: test ValueObject
130  * @tc.type: FUNC
131  * @tc.require: AR000CU2BO
132  * @tc.author: chenxi
133  */
134 HWTEST_F(RdbStoreInterfaceTest, ValueObject_TEST_004, TestSize.Level1)
135 {
136     std::string inputVal = "hello";
137     std::string outputVal = "";
138     ValueObject obj = ValueObject(inputVal);
139     ValueObjectType type = obj.GetType();
140     EXPECT_EQ(type, ValueObjectType::TYPE_STRING);
141     int ret = obj.GetString(outputVal);
142     EXPECT_EQ(ret, E_OK);
143     EXPECT_EQ(outputVal, "hello");
144 }
145 
146 /**
147  * @tc.name: ValueObject_TEST_005
148  * @tc.desc: test ValueObject
149  * @tc.type: FUNC
150  * @tc.require: AR000CU2BO
151  * @tc.author: chenxi
152  */
153 HWTEST_F(RdbStoreInterfaceTest, ValueObject_TEST_005, TestSize.Level1)
154 {
155     std::vector<uint8_t> inputVal = { 'h', 'e', 'l', 'l', 'o' };
156     std::vector<uint8_t> outputVal;
157     ValueObject obj = ValueObject(inputVal);
158     ValueObjectType type = obj.GetType();
159     EXPECT_EQ(type, ValueObjectType::TYPE_BLOB);
160     int ret = obj.GetBlob(outputVal);
161     EXPECT_EQ(ret, E_OK);
162     EXPECT_EQ(static_cast<int>(outputVal.size()), 5);
163     EXPECT_EQ(outputVal[0], 'h');
164     EXPECT_EQ(outputVal[1], 'e');
165     EXPECT_EQ(outputVal[2], 'l');
166     EXPECT_EQ(outputVal[3], 'l');
167     EXPECT_EQ(outputVal[4], 'o');
168 }
169 
170 /**
171  * @tc.name: ValueObject_TEST_006
172  * @tc.desc: test ValueObject
173  * @tc.type: FUNC
174  * @tc.require: AR000CU2BO
175  * @tc.author: chenxi
176  */
177 HWTEST_F(RdbStoreInterfaceTest, ValueObject_TEST_006, TestSize.Level1)
178 {
179     int inputVal = 5;
180     ValueObject obj = ValueObject(inputVal);
181     ValueObject obj1 = ValueObject();
182     obj1 = obj;
183     ValueObjectType type = obj1.GetType();
184     EXPECT_EQ(type, ValueObjectType::TYPE_INT);
185 }
186 
187 /**
188  * @tc.name: ValuesBucket_001
189  * @tc.desc: test ValuesBucket
190  * @tc.type: FUNC
191  * @tc.require: AR000CU2BO
192  * @tc.author: chenxi
193  */
194 HWTEST_F(RdbStoreInterfaceTest, ValuesBucket_001, TestSize.Level1)
195 {
196     ValuesBucket values;
197     values.PutInt("id", 1);
198     values.PutNull("name");
199     values.PutInt("age", 18);
200     values.PutDouble("salary", 100.5);
201     values.PutBlob("blobType", std::vector<uint8_t>{ 1, 2, 3 });
202 
203     int size = values.Size();
204     EXPECT_EQ(size, 5);
205     bool contains = values.HasColumn("name");
206     EXPECT_EQ(contains, true);
207     ValueObject obj;
208     contains = values.GetObject("salary", obj);
209     double val = 0.0;
210     ValueObjectType type = obj.GetType();
211     EXPECT_EQ(type, ValueObjectType::TYPE_DOUBLE);
212     int ret = obj.GetDouble(val);
213     EXPECT_EQ(ret, E_OK);
214     EXPECT_EQ(val, 100.5);
215 
216     values.Delete("name");
217     size = values.Size();
218     EXPECT_EQ(size, 4);
219     contains = values.HasColumn("name");
220     EXPECT_EQ(contains, false);
221 
222     values.Clear();
223     size = values.Size();
224     EXPECT_EQ(size, 0);
225     contains = values.HasColumn("salary");
226     EXPECT_EQ(contains, false);
227 }
228 
229 /**
230  * @tc.name: ValuesBucket_002
231  * @tc.desc: test ValuesBucket
232  * @tc.type: FUNC
233  * @tc.require: AR000CU2BO
234  * @tc.author: chenxi
235  */
236 HWTEST_F(RdbStoreInterfaceTest, ValuesBucket_002, TestSize.Level1)
237 {
238     int errCode = E_OK;
239     const std::string dbPath = RDB_TEST_PATH + "InterfaceTest.db";
240     RdbStoreConfig config(dbPath);
241     MyOpenCallback helper;
242     std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
243     EXPECT_NE(store, nullptr);
244     EXPECT_EQ(errCode, E_OK);
245 
246     int64_t id;
247     ValuesBucket values;
248     values.PutInt("id", 1);
249     values.PutNull("name");
250     values.PutInt("age", 18);
251     values.PutDouble("salary", 100.5);
252     values.PutBlob("blobType", std::vector<uint8_t>{ 1, 2, 3 });
253     int ret = store->Insert(id, "test", values);
254     EXPECT_EQ(ret, E_OK);
255     EXPECT_EQ(1, id);
256 
257     std::unique_ptr<ResultSet> resultSet = store->QuerySql("SELECT * FROM test");
258     EXPECT_NE(resultSet, nullptr);
259 
260     int columnIndex;
261     std::string strVal;
262 
263     ret = resultSet->GoToFirstRow();
264     EXPECT_EQ(ret, E_OK);
265 
266     ret = resultSet->GetColumnIndex("name", columnIndex);
267     EXPECT_EQ(ret, E_OK);
268     ret = resultSet->GetString(columnIndex, strVal);
269     EXPECT_EQ(ret, E_OK);
270 
271     resultSet->Close();
272     resultSet = nullptr;
273     store = nullptr;
274     ret = RdbHelper::DeleteRdbStore(dbPath);
275     EXPECT_EQ(ret, E_OK);
276 }
277 
278 /**
279  * @tc.name: ValuesBucket_003
280  * @tc.desc: test ValuesBucket
281  * @tc.type: FUNC
282  * @tc.require: AR000CU2BO
283  * @tc.author: chenxi
284  */
285 HWTEST_F(RdbStoreInterfaceTest, ValuesBucket_003, TestSize.Level1)
286 {
287     ValuesBucket values;
288     values.PutBool("boolType", true);
289     values.PutLong("longType", 1);
290 
291     int size = values.Size();
292     EXPECT_EQ(size, 2);
293     bool contains = values.HasColumn("boolType");
294     EXPECT_EQ(contains, true);
295     ValueObject obj;
296     contains = values.GetObject("boolType", obj);
297     ValueObjectType type = obj.GetType();
298     EXPECT_EQ(type, ValueObjectType::TYPE_BOOL);
299     bool val1 = false;
300     int ret = obj.GetBool(val1);
301     EXPECT_EQ(ret, E_OK);
302     EXPECT_EQ(val1, true);
303 
304     contains = values.HasColumn("longType");
305     EXPECT_EQ(contains, true);
306     contains = values.GetObject("longType", obj);
307     type = obj.GetType();
308     EXPECT_EQ(type, ValueObjectType::TYPE_INT64);
309     int64_t val2 = 0;
310     ret = obj.GetLong(val2);
311     EXPECT_EQ(ret, E_OK);
312     EXPECT_EQ(val2, 1);
313 }
314