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