• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sys/stat.h>
17 #include <sys/types.h>
18 
19 #include <string>
20 
21 #include "accesstoken_kit.h"
22 #include "common.h"
23 #include "grd_api_manager.h"
24 #include "rdb_errno.h"
25 #include "relational_store.h"
26 #include "relational_store_error_code.h"
27 #include "relational_store_impl.h"
28 #include "token_setproc.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::NativeRdb;
32 using namespace OHOS::Security::AccessToken;
33 using namespace OHOS::RdbNdk;
34 
35 class RdbVectorTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
InitRdbConfig()41     static OH_Rdb_ConfigV2 *InitRdbConfig()
42     {
43         OH_Rdb_ConfigV2 *config = OH_Rdb_CreateConfig();
44         EXPECT_NE(config, nullptr);
45         OH_Rdb_SetDatabaseDir(config, RDB_TEST_PATH);
46         OH_Rdb_SetStoreName(config, "rdb_vector_test.db");
47         OH_Rdb_SetBundleName(config, "com.ohos.example.distributedndk");
48         OH_Rdb_SetEncrypted(config, false);
49         OH_Rdb_SetSecurityLevel(config, OH_Rdb_SecurityLevel::S1);
50         OH_Rdb_SetArea(config, RDB_SECURITY_AREA_EL1);
51 
52         EXPECT_EQ(OH_Rdb_ErrCode::RDB_OK, OH_Rdb_SetDbType(config, RDB_CAYLEY));
53         return config;
54     }
55 };
56 
57 OH_Rdb_Store *store_;
58 OH_Rdb_ConfigV2 *config_;
59 float test_[] = { 1.2, 2.3 };
60 
SetUpTestCase(void)61 void RdbVectorTest::SetUpTestCase(void)
62 {
63     config_ = InitRdbConfig();
64     mkdir(RDB_TEST_PATH, 0770);
65     int errCode = 0;
66     store_ = OH_Rdb_CreateOrOpen(config_, &errCode);
67     EXPECT_NE(store_, NULL);
68 }
69 
TearDownTestCase(void)70 void RdbVectorTest::TearDownTestCase(void)
71 {
72     int errCode = OH_Rdb_CloseStore(store_);
73     EXPECT_EQ(errCode, RDB_OK);
74     errCode = OH_Rdb_DeleteStoreV2(config_);
75     EXPECT_EQ(errCode, RDB_OK);
76 }
77 
SetUp(void)78 void RdbVectorTest::SetUp(void)
79 {
80     if (!OHOS::NativeRdb::IsUsingArkData()) {
81         GTEST_SKIP() << "Current testcase is not compatible from current gdb";
82     }
83     char createTableSql[] = "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 floatvector(2));";
84     EXPECT_EQ(OH_Rdb_ErrCode::RDB_OK, OH_Rdb_ExecuteByTrxId(store_, 0, createTableSql));
85     OH_Data_Values *values = OH_Values_Create();
86     OH_Values_PutInt(values, 1);
87     size_t len = sizeof(test_) / sizeof(test_[0]);
88     OH_Values_PutFloatVector(values, test_, len);
89     char insertSql[] = "INSERT INTO test (id, data1) VALUES (?, ?);";
90     auto errCode = OH_Rdb_ExecuteV2(store_, insertSql, values, nullptr);
91     EXPECT_EQ(errCode, RDB_OK);
92     OH_Values_Destroy(values);
93 }
94 
TearDown(void)95 void RdbVectorTest::TearDown(void)
96 {
97     char dropTableSql[] = "DROP TABLE IF EXISTS test";
98     int errCode = OH_Rdb_ExecuteByTrxId(store_, 0, dropTableSql);
99     EXPECT_EQ(errCode, RDB_OK);
100 }
101 
102 /**
103  * @tc.name: RDB_vector_test_001
104  * @tc.desc: Normal testCase of queryV2.
105  * @tc.type: FUNC
106  */
107 HWTEST_F(RdbVectorTest, RDB_vector_001, TestSize.Level1)
108 {
109     char querySql[] = "select * from test where id = ?;";
110     OH_Data_Values *values = OH_Values_Create();
111     OH_Values_PutInt(values, 1);
112     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
113     EXPECT_NE(cursor, NULL);
114 
115     int rowCount = 0;
116     cursor->getRowCount(cursor, &rowCount);
117     EXPECT_EQ(rowCount, 1);
118 
119     cursor->goToNextRow(cursor);
120     size_t count = 0;
121     auto errCode = OH_Cursor_GetFloatVectorCount(cursor, 1, &count);
122     EXPECT_EQ(errCode, RDB_OK);
123     float test[count];
124     size_t outLen;
125     OH_Cursor_GetFloatVector(cursor, 1, test, count, &outLen);
126     EXPECT_EQ(outLen, 2);
127     EXPECT_EQ(test[0], test_[0]);
128     EXPECT_EQ(test[1], test_[1]);
129 
130     float test1[10];
131     OH_Cursor_GetFloatVector(cursor, 1, test1, 10, &outLen);
132     EXPECT_EQ(outLen, 2);
133     EXPECT_EQ(test1[0], test_[0]);
134     EXPECT_EQ(test1[1], test_[1]);
135 
136     OH_Values_Destroy(values);
137 }
138 
139 /**
140  * @tc.name: RDB_vector_test_002
141  * @tc.desc: Abnormal testCase of queryV2.
142  * @tc.type: FUNC
143  */
144 HWTEST_F(RdbVectorTest, RDB_vector_002, TestSize.Level1)
145 {
146     char querySql[] = "select * from test where id = ?;";
147     OH_Data_Values *values = OH_Values_Create();
148     OH_Values_PutInt(values, 1);
149     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(nullptr, querySql, values);
150     EXPECT_EQ(cursor, NULL);
151 
152     cursor = OH_Rdb_ExecuteQueryV2(store_, nullptr, values);
153     EXPECT_EQ(cursor, NULL);
154 
155     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, nullptr);
156     EXPECT_NE(cursor, NULL);
157     int rowCount = 0;
158     cursor->getRowCount(cursor, &rowCount);
159     EXPECT_EQ(rowCount, 0);
160 
161     OH_Values_PutInt(values, 1);
162     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
163     EXPECT_NE(cursor, NULL);
164     cursor->getRowCount(cursor, &rowCount);
165     EXPECT_EQ(rowCount, -1);
166 
167     OH_Values_Destroy(values);
168 }
169 
170 /**
171  * @tc.name: RDB_vector_test_003
172  * @tc.desc: Abnormal testCase of getFloatVector.
173  * @tc.type: FUNC
174  */
175 HWTEST_F(RdbVectorTest, RDB_vector_003, TestSize.Level1)
176 {
177     char querySql[] = "select * from test where id = ?;";
178     OH_Data_Values *values = OH_Values_Create();
179     OH_Values_PutInt(values, 1);
180     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
181     EXPECT_NE(cursor, NULL);
182 
183     int rowCount = 0;
184     cursor->getRowCount(cursor, &rowCount);
185     EXPECT_EQ(rowCount, 1);
186 
187     cursor->goToNextRow(cursor);
188     size_t count = 0;
189     auto errCode = OH_Cursor_GetFloatVectorCount(cursor, 0, &count);
190     EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
191     float test[count];
192     size_t outLen;
193     errCode = OH_Cursor_GetFloatVector(cursor, 0, test, count, &outLen);
194     EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
195     OH_Values_Destroy(values);
196 }
197 
198 /**
199  * @tc.name: RDB_vector_test_004
200  * @tc.desc: Normal testCase of executeV2.
201  * @tc.type: FUNC
202  */
203 HWTEST_F(RdbVectorTest, RDB_vector_004, TestSize.Level1)
204 {
205     char querySql[] = "select * from test where id = ?;";
206     OH_Data_Values *values = OH_Values_Create();
207     OH_Values_PutInt(values, 1);
208     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
209     EXPECT_NE(cursor, NULL);
210 
211     int rowCount = 0;
212     cursor->getRowCount(cursor, &rowCount);
213     EXPECT_EQ(rowCount, 1);
214 
215     float test[2] = { 5.5, 6.6 };
216     OH_Data_Values *values1 = OH_Values_Create();
217     OH_Values_PutFloatVector(values1, test, 2);
218     OH_Values_PutInt(values1, 1);
219     auto errCode = OH_Rdb_ExecuteV2(store_, "update test set data1 = ? where id = ?", values1, nullptr);
220     EXPECT_EQ(errCode, RDB_OK);
221 
222     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
223     EXPECT_NE(cursor, NULL);
224     cursor->getRowCount(cursor, &rowCount);
225     EXPECT_EQ(rowCount, 1);
226     cursor->goToNextRow(cursor);
227     size_t count = 0;
228     errCode = OH_Cursor_GetFloatVectorCount(cursor, 1, &count);
229     EXPECT_EQ(errCode, RDB_OK);
230     float test1[count];
231     size_t outLen;
232     OH_Cursor_GetFloatVector(cursor, 1, test1, count, &outLen);
233     EXPECT_EQ(outLen, 2);
234     EXPECT_EQ(test1[0], test[0]);
235     EXPECT_EQ(test1[1], test[1]);
236 
237     errCode = OH_Rdb_ExecuteV2(store_, "delete from test where id = ?", values, nullptr);
238     EXPECT_EQ(errCode, RDB_OK);
239     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
240     EXPECT_NE(cursor, NULL);
241     cursor->getRowCount(cursor, &rowCount);
242     EXPECT_EQ(rowCount, 0);
243 
244     OH_Values_Destroy(values);
245     OH_Values_Destroy(values1);
246 }
247 
248 /**
249  * @tc.name: RDB_vector_test_005
250  * @tc.desc: Abnormal testCase of executeV2.
251  * @tc.type: FUNC
252  */
253 HWTEST_F(RdbVectorTest, RDB_vector_005, TestSize.Level1)
254 {
255     OH_Data_Values *values = OH_Values_Create();
256     OH_Values_PutFloatVector(values, test_, 2);
257     OH_Values_PutInt(values, 1);
258     auto errCode = OH_Rdb_ExecuteV2(nullptr, "update test set data1 = ? where id = ?",
259         values, nullptr);
260     EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
261 
262     errCode = OH_Rdb_ExecuteV2(store_, nullptr, values, nullptr);
263     EXPECT_EQ(errCode, RDB_E_INVALID_ARGS);
264 
265     errCode = OH_Rdb_ExecuteV2(store_, "update test set data1 = ? where id = ?", nullptr, nullptr);
266     EXPECT_EQ(errCode, RDB_OK);
267 
268     OH_Values_PutInt(values, 2);
269     errCode = OH_Rdb_ExecuteV2(store_, "update test set data1 = ? where id = ?", values, nullptr);
270     EXPECT_EQ(errCode, RDB_E_ERROR);
271 }
272 
273 /**
274  * @tc.name: RDB_vector_test_006
275  * @tc.desc: Normal testCase of executeQueryV2.
276  * @tc.type: FUNC
277  */
278 HWTEST_F(RdbVectorTest, RDB_vector_006, TestSize.Level1)
279 {
280     char querySql1[] = "select id, data1 <-> ? from test where id = ?;";
281     OH_Data_Values *values = OH_Values_Create();
282     float test[] = { 2.1, 3.0 };
283     OH_Values_PutFloatVector(values, test, 2);
284     OH_Values_PutInt(values, 1);
285     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(store_, querySql1, values);
286     EXPECT_NE(cursor, NULL);
287     int rowCount = 0;
288     cursor->getRowCount(cursor, &rowCount);
289     EXPECT_EQ(rowCount, 1);
290 
291     char querySql2[] = "select id, data1 <=> ? from test where id = ?;";
292     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql2, values);
293     EXPECT_NE(cursor, NULL);
294     cursor->getRowCount(cursor, &rowCount);
295     EXPECT_EQ(rowCount, 1);
296 
297     char querySql3[] = "select id, data1 <-> '[ 2.1, 3.0 ]' from test;";
298     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql3, nullptr);
299     EXPECT_NE(cursor, NULL);
300     cursor->getRowCount(cursor, &rowCount);
301     EXPECT_EQ(rowCount, 1);
302 
303     char querySql4[] = "select id, data1 <=> '[ 2.1, 3.0 ]' from test;";
304     cursor = OH_Rdb_ExecuteQueryV2(store_, querySql4, nullptr);
305     EXPECT_NE(cursor, NULL);
306     cursor->getRowCount(cursor, &rowCount);
307     EXPECT_EQ(rowCount, 1);
308 
309     OH_Values_Destroy(values);
310 }
311 
312 /**
313  * @tc.name: RDB_vector_test_007
314  * @tc.desc: Normal testCase of executeQueryV2.
315  * @tc.type: FUNC
316  */
317 HWTEST_F(RdbVectorTest, RDB_vector_007, TestSize.Level1)
318 {
319     char createTableSql[] = "CREATE TABLE test1 (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 floatvector(2));";
320     auto errCode = OH_Rdb_ExecuteV2(store_, createTableSql, nullptr, nullptr);
321     EXPECT_EQ(errCode, RDB_OK);
322     char insertSql[] = "INSERT INTO test1 (id, data1) VALUES (1, '[3.1, 2.2]');";
323     errCode = OH_Rdb_ExecuteV2(store_, insertSql, nullptr, nullptr);
324     EXPECT_EQ(errCode, RDB_OK);
325 
326     char querySql[] = "select id, data1 <=> '[ 2.1, 3.0 ]' from test where id in (select id from test1);";
327     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, nullptr);
328     EXPECT_NE(cursor, NULL);
329     int rowCount = 0;
330     cursor->getRowCount(cursor, &rowCount);
331     EXPECT_EQ(rowCount, 1);
332 
333     char updateSql[] = "update test1 set data1 = '[2.0, 1.0]' where id = 1;";
334     errCode = OH_Rdb_ExecuteV2(store_, updateSql, nullptr, nullptr);
335     EXPECT_EQ(errCode, RDB_OK);
336     cursor = OH_Rdb_ExecuteQueryV2(store_, "select * from test1 where id = 1;", nullptr);
337     EXPECT_NE(cursor, NULL);
338     cursor->goToNextRow(cursor);
339     float test1[2];
340     size_t outLen;
341     OH_Cursor_GetFloatVector(cursor, 1, test1, 2, &outLen);
342     EXPECT_EQ(outLen, 2);
343     EXPECT_EQ(test1[0], 2.0);
344     EXPECT_EQ(test1[1], 1.0);
345 
346     errCode = OH_Rdb_ExecuteV2(store_, "delete from test1 where id = 1", nullptr, nullptr);
347     EXPECT_EQ(errCode, RDB_OK);
348 
349     char dropTableSql[] = "DROP TABLE IF EXISTS test1";
350     errCode = OH_Rdb_ExecuteV2(store_, dropTableSql, nullptr, nullptr);
351     EXPECT_EQ(errCode, RDB_OK);
352 }
353 
354 /**
355  * @tc.name: RDB_vector_test_008
356  * @tc.desc: Normal testCase of executeQueryV2.
357  * @tc.type: FUNC
358  */
359 HWTEST_F(RdbVectorTest, RDB_vector_008, TestSize.Level1)
360 {
361     char insertSql[] = "INSERT INTO test (id, data1) VALUES (2, '[3.1, 2.2]');";
362     auto errCode = OH_Rdb_ExecuteV2(store_, insertSql, nullptr, nullptr);
363     EXPECT_EQ(errCode, RDB_OK);
364 
365     char querySql[] = "select id, data1 from test where id > ? group by id having "
366                       "max(data1<=>?);";
367     float test[] = { 2.1, 3.0 };
368     OH_Data_Values *values = OH_Values_Create();
369     OH_Values_PutInt(values, 0);
370     OH_Values_PutFloatVector(values, test, 2);
371     OH_Cursor *cursor = OH_Rdb_ExecuteQueryV2(store_, querySql, values);
372     EXPECT_NE(cursor, NULL);
373     int rowCount = 0;
374     cursor->getRowCount(cursor, &rowCount);
375     EXPECT_EQ(rowCount, 2);
376 
377     OH_Values_Destroy(values);
378 }