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 #ifndef OMIT_JSON
16 #include <gtest/gtest.h>
17
18 #include "db_common.h"
19 #include "db_constant.h"
20 #include "distributeddb_data_generate_unit_test.h"
21 #include "distributeddb_tools_unit_test.h"
22 #include "kvdb_manager.h"
23 #include "log_print.h"
24 #include "platform_specific.h"
25 #include "res_finalizer.h"
26 #include "sqlite_single_ver_natural_store_connection.h"
27 #include "sqlite_single_ver_result_set.h"
28 #include "sqlite_utils.h"
29 #include "store_types.h"
30
31 using namespace testing::ext;
32 using namespace DistributedDB;
33 using namespace DistributedDBUnitTest;
34
35 namespace {
36 const int INSERT_NUMBER = 10;
37 const Key EMPTY_KEY;
38 const SQLiteSingleVerResultSet::Option OPTION = {ResultSetCacheMode::CACHE_ENTRY_ID_ONLY, 1};
39
40 string g_testDir;
41 string g_identifier;
42 SQLiteSingleVerNaturalStore *g_store = nullptr;
43 SQLiteSingleVerNaturalStoreConnection *g_connection = nullptr;
44 KvDBProperties g_Property;
45 const string STORE_ID = STORE_ID_SYNC;
46 }
47 class DistributedDBStorageResultAndJsonOptimizeTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53 };
54
SetUpTestCase(void)55 void DistributedDBStorageResultAndJsonOptimizeTest::SetUpTestCase(void)
56 {
57 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
58 std::string origIdentifier = USER_ID + "-" + APP_ID + "-" + STORE_ID;
59 std::string identifier = DBCommon::TransferHashString(origIdentifier);
60 g_identifier = DBCommon::TransferStringToHex(identifier);
61 std::string dir = g_testDir + g_identifier + "/" + DBConstant::SINGLE_SUB_DIR;
62 DIR *dirTmp = opendir(dir.c_str());
63 if (dirTmp == nullptr) {
64 OS::MakeDBDirectory(dir);
65 } else {
66 closedir(dirTmp);
67 }
68 g_Property.SetStringProp(KvDBProperties::DATA_DIR, g_testDir);
69 g_Property.SetStringProp(KvDBProperties::STORE_ID, STORE_ID);
70 g_Property.SetStringProp(KvDBProperties::IDENTIFIER_DIR, g_identifier);
71 g_Property.SetIntProp(KvDBProperties::DATABASE_TYPE, KvDBProperties::SINGLE_VER_TYPE);
72 }
73
TearDownTestCase(void)74 void DistributedDBStorageResultAndJsonOptimizeTest::TearDownTestCase(void)
75 {
76 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
77 LOGE("rm test db files error!");
78 }
79 }
80
SetUp(void)81 void DistributedDBStorageResultAndJsonOptimizeTest::SetUp(void)
82 {
83 DistributedDBToolsUnitTest::PrintTestCaseInfo();
84 /**
85 * @tc.setup: 1. Create a SQLiteSingleVerNaturalStore.
86 * 2. Set the ResultSet cache mode to CACHE_ENTRY_ID_ONLY.
87 * 3. Put 10 records.
88 */
89 g_store = new (std::nothrow) SQLiteSingleVerNaturalStore;
90 ASSERT_NE(g_store, nullptr);
91 ASSERT_EQ(g_store->Open(g_Property), E_OK);
92
93 int errCode = E_OK;
94 g_connection = static_cast<SQLiteSingleVerNaturalStoreConnection *>(g_store->GetDBConnection(errCode));
95 ASSERT_NE(g_connection, nullptr);
96 g_store->DecObjRef(g_store);
97 EXPECT_EQ(errCode, E_OK);
98
99 IOption option;
100 option.dataType = IOption::SYNC_DATA;
101 g_connection->Clear(option);
102 Key insertKey;
103 ASSERT_EQ(g_connection->StartTransaction(), E_OK);
104 for (int i = 1; i < INSERT_NUMBER + 1; i++) {
105 insertKey.clear();
106 insertKey.push_back(i);
107 ASSERT_EQ(g_connection->Put(option, insertKey, VALUE_1), OK);
108 }
109 ASSERT_EQ(g_connection->Commit(), E_OK);
110 }
111
TearDown(void)112 void DistributedDBStorageResultAndJsonOptimizeTest::TearDown(void)
113 {
114 /**
115 * @tc.teardown: Release the SQLiteSingleVerNaturalStore.
116 */
117 if (g_connection != nullptr) {
118 g_connection->Close();
119 g_connection = nullptr;
120 }
121
122 g_store = nullptr;
123 KvDBManager::RemoveDatabase(g_Property);
124 }
125
126 /**
127 * @tc.name: ResultSetOpen001
128 * @tc.desc: Test the SQLiteSingleVerResultSet Open function
129 * @tc.type: FUNC
130 * @tc.require: AR000F3OP0
131 * @tc.author: xushaohua
132 */
133 HWTEST_F(DistributedDBStorageResultAndJsonOptimizeTest, ResultSetOpen001, TestSize.Level1)
134 {
135 /**
136 * @tc.steps: step1. Create a SQLiteSingleVerResultSet.
137 */
138 std::unique_ptr<SQLiteSingleVerResultSet> resultSet1 =
139 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
140
141 /**
142 * @tc.steps: step2. Call SQLiteSingleVerResultSet.Open with parameter true.
143 * @tc.expected: step2. Expect return E_OK.
144 */
145 EXPECT_EQ(resultSet1->Open(true), E_OK);
146
147 /**
148 * @tc.steps: step3. Create a SQLiteSingleVerResultSet.
149 */
150 std::unique_ptr<SQLiteSingleVerResultSet> resultSet2 =
151 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
152
153 /**
154 * @tc.steps: step4. Call SQLiteSingleVerResultSet.Open with parameter false.
155 * @tc.expected: step4. Expect return E_OK.
156 */
157 EXPECT_EQ(resultSet2->Open(false), E_OK);
158
159 /**
160 * @tc.steps: step5. Close all ResultSet.
161 */
162 resultSet1->Close();
163 resultSet2->Close();
164 }
165
166 /**
167 * @tc.name: ResultSetGetCount001
168 * @tc.desc: Test the SQLiteSingleVerResultSet GetCount function.
169 * @tc.type: FUNC
170 * @tc.require: AR000F3OP0
171 * @tc.author: xushaohua
172 */
173 HWTEST_F(DistributedDBStorageResultAndJsonOptimizeTest, ResultSetGetCount001, TestSize.Level1)
174 {
175 /**
176 * @tc.steps: step1. Create a SQLiteSingleVerResultSet.
177 */
178 std::unique_ptr<SQLiteSingleVerResultSet> resultSet =
179 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
180
181 /**
182 * @tc.steps: step2. Call SQLiteSingleVerResultSet.Open
183 * @tc.expected: step2. Expect return E_OK.Gits
184 */
185 EXPECT_EQ(resultSet->Open(false), E_OK);
186
187 /**
188 * @tc.steps: step2. Call SQLiteSingleVerResultSet.GetCount
189 * @tc.expected: step2. Expect return INSERT_NUMBER.
190 */
191 EXPECT_EQ(resultSet->GetCount(), INSERT_NUMBER);
192
193 /**
194 * @tc.steps: step3. Close the ResultSet.
195 */
196 resultSet->Close();
197 }
198
199 /**
200 * @tc.name: ResultSetMoveTo001
201 * @tc.desc: Test the SQLiteSingleVerResultSet MoveTo And GetPosition function.
202 * @tc.type: FUNC
203 * @tc.require: AR000F3OP0
204 * @tc.author: xushaohua
205 */
206 HWTEST_F(DistributedDBStorageResultAndJsonOptimizeTest, ResultSetMoveTo001, TestSize.Level1)
207 {
208 /**
209 * @tc.steps: step1. Create a SQLiteSingleVerResultSet.
210 */
211 std::unique_ptr<SQLiteSingleVerResultSet> resultSet =
212 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
213
214 /**
215 * @tc.steps: step2. Call SQLiteSingleVerResultSet.Open.
216 * @tc.expected: step2. Expect return E_OK.
217 */
218 EXPECT_EQ(resultSet->Open(false), E_OK);
219
220 /**
221 * @tc.steps: step3. Call SQLiteSingleVerResultSet MoveTo INSERT_NUMBER - 1
222 * @tc.expected: step3. Expect return E_OK.
223 */
224 EXPECT_EQ(resultSet->MoveTo(INSERT_NUMBER - 1), E_OK);
225
226 /**
227 * @tc.steps: step4. Call SQLiteSingleVerResultSet GetPosition
228 * @tc.expected: step5. Expect return INSERT_NUMBER - 1.
229 */
230 EXPECT_EQ(resultSet->GetPosition(), INSERT_NUMBER - 1);
231
232 /**
233 * @tc.steps: step5. Call SQLiteSingleVerResultSet MoveTo INSERT_NUMBER
234 * @tc.expected: step5. Expect return -E_INVALID_ARGS.
235 */
236 EXPECT_EQ(resultSet->MoveTo(INSERT_NUMBER), -E_INVALID_ARGS);
237
238 /**
239 * @tc.steps: step6. Call SQLiteSingleVerResultSet GetPosition
240 * @tc.expected: step6. Expect return INSERT_NUMBER.
241 */
242 EXPECT_EQ(resultSet->GetPosition(), INSERT_NUMBER);
243
244 /**
245 * @tc.steps: step7. Call SQLiteSingleVerResultSet MoveTo -1
246 * @tc.expected: step7. Expect return E_INVALID_ARGS.
247 */
248 EXPECT_EQ(resultSet->MoveTo(-1), -E_INVALID_ARGS);
249
250 /**
251 * @tc.steps: step8. Call SQLiteSingleVerResultSet GetPosition
252 * @tc.expected: step8. Expect return 0.
253 */
254 EXPECT_EQ(resultSet->GetPosition(), -1);
255
256 /**
257 * @tc.steps: step9. Call SQLiteSingleVerResultSet MoveTo 0
258 * @tc.expected: step9. Expect return E_OK.
259 */
260 EXPECT_EQ(resultSet->MoveTo(0), E_OK);
261
262 /**
263 * @tc.steps: step10. Call SQLiteSingleVerResultSet GetPosition
264 * @tc.expected: step10. Expect return 0.
265 */
266 EXPECT_EQ(resultSet->GetPosition(), 0);
267
268 /**
269 * @tc.steps: step11. Close the ResultSet.
270 */
271 resultSet->Close();
272 }
273
274 /**
275 * @tc.name: ResultSetGetEntry001
276 * @tc.desc: Test the SQLiteSingleVerResultSet GetEntry function.
277 * @tc.type: FUNC
278 * @tc.require: AR000F3OP0
279 * @tc.author: xushaohua
280 */
281 HWTEST_F(DistributedDBStorageResultAndJsonOptimizeTest, ResultSetGetEntry001, TestSize.Level1)
282 {
283 /**
284 * @tc.steps: step1. Create a SQLiteSingleVerResultSet.
285 */
286 std::unique_ptr<SQLiteSingleVerResultSet> resultSet =
287 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
288
289 /**
290 * @tc.steps: step2. Call SQLiteSingleVerResultSet.Open
291 * @tc.expected: step2. Expect return E_OK.
292 */
293 EXPECT_EQ(resultSet->Open(false), E_OK);
294
295 /**
296 * @tc.steps: step2. Call SQLiteSingleVerResultSet MoveTo 0 And GetEntry
297 * @tc.expected: step2. Expect return E_OK.
298 */
299 Entry entry;
300 ASSERT_EQ(resultSet->MoveTo(0), E_OK);
301 EXPECT_EQ(resultSet->GetEntry(entry), E_OK);
302
303 /**
304 * @tc.expected: step2. Expect return Key == { 1 }, value == VALUE_1.
305 */
306 const Key key = { 1 };
307 EXPECT_EQ(entry.key, key);
308 EXPECT_EQ(entry.value, VALUE_1);
309
310 /**
311 * @tc.steps: step3. Close the ResultSet.
312 */
313 resultSet->Close();
314 }
315
316 /**
317 * @tc.name: ResultSetTest001
318 * @tc.desc: Check the resultSet open
319 * @tc.type: FUNC
320 * @tc.require:
321 * @tc.author: bty
322 */
323 HWTEST_F(DistributedDBStorageResultAndJsonOptimizeTest, ResultSetTest001, TestSize.Level1)
324 {
325 /**
326 * @tc.steps: step1. Check resultSet Open when db is nullptr
327 * @tc.expected: step2. Expect return -E_INVALID_ARGS.
328 */
329 std::unique_ptr<SQLiteSingleVerResultSet> resultSet1 =
330 std::make_unique<SQLiteSingleVerResultSet>(nullptr, EMPTY_KEY, OPTION);
331 ASSERT_NE(resultSet1, nullptr);
332 EXPECT_EQ(resultSet1->Open(true), -E_INVALID_ARGS);
333
334 /**
335 * @tc.steps: step2. Check resultSet Open when it is opened
336 * @tc.expected: step2. Expect return E_OK.
337 */
338 std::unique_ptr<SQLiteSingleVerResultSet> resultSet2 =
339 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
340 ASSERT_NE(resultSet2, nullptr);
341 EXPECT_EQ(resultSet2->Open(true), E_OK);
342 EXPECT_EQ(resultSet2->Open(true), E_OK);
343
344 /**
345 * @tc.steps: step3. Check resultSet Open when db it is not opened
346 * @tc.expected: step3. Expect Open return -E_INVALID_DB.
347 */
348 SQLiteSingleVerNaturalStore *errStore = new (std::nothrow) SQLiteSingleVerNaturalStore;
349 ASSERT_NE(errStore, nullptr);
350 std::unique_ptr<SQLiteSingleVerResultSet> resultSet3 =
351 std::make_unique<SQLiteSingleVerResultSet>(errStore, EMPTY_KEY, OPTION);
352 ASSERT_NE(resultSet3, nullptr);
353 EXPECT_EQ(resultSet3->Open(true), -E_INVALID_DB);
354 RefObject::KillAndDecObjRef(errStore);
355
356 /**
357 * @tc.steps: step4. Check resultSet Open when ResultSetType is query
358 * @tc.expected: step4. Expect return E_OK.
359 */
360 QueryObject queryObject;
361 std::unique_ptr<SQLiteSingleVerResultSet> resultSet4 =
362 std::make_unique<SQLiteSingleVerResultSet>(g_store, queryObject, OPTION);
363 ASSERT_NE(resultSet4, nullptr);
364 EXPECT_EQ(resultSet4->Open(true), E_OK);
365
366 resultSet1->Close();
367 resultSet2->Close();
368 resultSet3->Close();
369 resultSet4->Close();
370 }
371
372 /**
373 * @tc.name: ResultSetTest002
374 * @tc.desc: Check the resultSet move,then get
375 * @tc.type: FUNC
376 * @tc.require:
377 * @tc.author: bty
378 */
379 HWTEST_F(DistributedDBStorageResultAndJsonOptimizeTest, ResultSetTest002, TestSize.Level1)
380 {
381 /**
382 * @tc.steps: step1. Check resultSet MoveTo when resultSet is not opened
383 * @tc.expected: step1. Expect return -E_RESULT_SET_STATUS_INVALID.
384 */
385 std::unique_ptr<SQLiteSingleVerResultSet> resultSet1 =
386 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
387 ASSERT_NE(resultSet1, nullptr);
388 EXPECT_EQ(resultSet1->MoveTo(INSERT_NUMBER - 1), -E_RESULT_SET_STATUS_INVALID);
389
390 /**
391 * @tc.steps: step2. Then get the Entry
392 * @tc.expected: step2. Expect return -E_NO_SUCH_ENTRY.
393 */
394 Entry entry;
395 EXPECT_EQ(resultSet1->GetEntry(entry), -E_NO_SUCH_ENTRY);
396 resultSet1->Close();
397
398 /**
399 * @tc.steps: step3. Check resultSet MoveTo when db is empty
400 * @tc.expected: step3. Expect return -E_RESULT_SET_EMPTY.
401 */
402 std::unique_ptr<SQLiteSingleVerResultSet> resultSet2 =
403 std::make_unique<SQLiteSingleVerResultSet>(g_store, EMPTY_KEY, OPTION);
404 ASSERT_NE(resultSet2, nullptr);
405 IOption option;
406 option.dataType = IOption::SYNC_DATA;
407 Key insertKey;
408 ASSERT_EQ(g_connection->StartTransaction(), E_OK);
409 for (int i = 1; i < INSERT_NUMBER + 1; i++) {
410 insertKey.clear();
411 insertKey.push_back(i);
412 ASSERT_EQ(g_connection->Delete(option, insertKey), OK);
413 }
414 ASSERT_EQ(g_connection->Commit(), E_OK);
415 resultSet2->Open(true);
416 EXPECT_EQ(resultSet2->MoveTo(INSERT_NUMBER - 1), -E_RESULT_SET_EMPTY);
417
418 /**
419 * @tc.steps: step4. Get resultSet Entry when db is empty
420 * @tc.expected: step4. Expect return -E_NO_SUCH_ENTRY.
421 */
422 EXPECT_EQ(resultSet2->GetEntry(entry), -E_NO_SUCH_ENTRY);
423 resultSet2->Close();
424 }
425 #endif