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 #ifndef OMIT_JSON
17 #include <gtest/gtest.h>
18 #include <vector>
19 #include <string>
20
21 #include "kv_store_delegate.h"
22 #include "kv_store_nb_delegate.h"
23 #include "kv_store_delegate_manager.h"
24 #include "distributed_test_tools.h"
25 #include "distributeddb_nb_test_tools.h"
26 #include "distributeddb_data_generator.h"
27 #include "distributeddb_schema_test_tools.h"
28
29 using namespace std;
30 using namespace testing;
31 #if defined TESTCASES_USING_GTEST_EXT
32 using namespace testing::ext;
33 #endif
34 using namespace DistributedDB;
35 using namespace DistributedDBDataGenerator;
36
37 namespace DistributeddbNbPredicateQueryExpand {
38 KvStoreNbDelegate *g_nbQueryDelegate = nullptr;
39 KvStoreDelegateManager *g_manager = nullptr;
40 const int CURSOR_POSITION_NEGATIVE1 = -1;
41 const int CURSOR_POSITION_1 = 1;
42
43 DistributedDB::CipherPassword g_passwd1;
44
45 class DistributeddbNbPredicateQueryExpandTest : public testing::Test {
46 public:
47 static void SetUpTestCase(void);
48 static void TearDownTestCase(void);
49 void SetUp();
50 void TearDown();
51 private:
52 };
53
SetUpTestCase(void)54 void DistributeddbNbPredicateQueryExpandTest::SetUpTestCase(void)
55 {
56 (void)g_passwd1.SetValue(PASSWD_VECTOR_1.data(), PASSWD_VECTOR_1.size());
57 }
58
TearDownTestCase(void)59 void DistributeddbNbPredicateQueryExpandTest::TearDownTestCase(void)
60 {
61 }
62
SetUp(void)63 void DistributeddbNbPredicateQueryExpandTest::SetUp(void)
64 {
65 RemoveDir(DistributedDBConstant::NB_DIRECTOR);
66
67 UnitTest *test = UnitTest::GetInstance();
68 ASSERT_NE(test, nullptr);
69 const TestInfo *testinfo = test->current_test_info();
70 ASSERT_NE(testinfo, nullptr);
71 string testCaseName = string(testinfo->name());
72 MST_LOG("[SetUp] test case %s is start to run", testCaseName.c_str());
73
74 string validSchema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, VALID_COMBINATION_DEFINE, VALID_INDEX_1);
75 Option option = g_option;
76 option.isMemoryDb = false;
77 option.schema = validSchema;
78 g_nbQueryDelegate = DistributedDBNbTestTools::GetNbDelegateSuccess(g_manager, g_dbParameter1, option);
79 ASSERT_TRUE(g_nbQueryDelegate != nullptr && g_manager != nullptr);
80 }
81
TearDown(void)82 void DistributeddbNbPredicateQueryExpandTest::TearDown(void)
83 {
84 MST_LOG("TearDownTestCase after case.");
85 ASSERT_NE(g_manager, nullptr);
86 EXPECT_TRUE(EndCaseDeleteDB(g_manager, g_nbQueryDelegate, STORE_ID_1, false));
87 }
88
PresetDatasToDB(KvStoreNbDelegate * & delegate,std::vector<uint8_t> keyPrefix,vector<string> & schemasValue,vector<Entry> & entries)89 void PresetDatasToDB(KvStoreNbDelegate *&delegate, std::vector<uint8_t> keyPrefix, vector<string> &schemasValue,
90 vector<Entry> &entries)
91 {
92 int num = schemasValue.size();
93 for (int index = 1; index <= num; index++) {
94 DistributedDB::Entry entry;
95 std::string ind = std::to_string(index);
96 entry.key.push_back(keyPrefix[0]);
97 for (auto ch = ind.begin(); ch != ind.end(); ch++) {
98 entry.key.push_back(*ch);
99 }
100 Value value(schemasValue[index - 1].begin(), schemasValue[index - 1].end());
101 entry.value = value;
102 EXPECT_EQ(DistributedDBNbTestTools::Put(*delegate, entry.key, entry.value), OK);
103 entries.push_back(entry);
104 }
105 }
106
107 /**
108 * @tc.name: BracketsTest 001
109 * @tc.desc: Verify the Brackets can change priority of query methods if brackets appear in pairs.
110 * @tc.type: FUNC
111 * @tc.require: SR000EPA23
112 * @tc.author: fengxiaoyun
113 */
114 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, BracketsTest001, TestSize.Level0)
115 {
116 /**
117 * @tc.steps: step1. create schema db and put 7 entries which has valid schema constructor
118 * and has the value given to db.
119 * @tc.expected: step1. create and put successfully.
120 */
121 vector<vector<string>> fieldValue = {
122 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
123 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
124 {"ab000", "true", "33", "30", "149"}, {"abxxx", "true", "12", "120", "-79"},
125 {"ab", "true", "20", "82", "150.999"}};
126 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
127 vector<Entry> entries;
128 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
129
130 /**
131 * @tc.steps: step2. test the Query without brackets and check the result with GetEntries.
132 * @tc.expected: step2. GetEntries return right result.
133 */
134 Query query1 = Query::Select().EqualTo("$.field2.field3", true).And().LessThanOrEqualTo("$.field2.field4.field5",
135 33).Or().LessThanOrEqualTo("$.field2.field4.field6.field7", 1000).And().NotLike("$.field1", "%c").
136 OrderBy("$.field2.field4.field6.field8", true);
137 vector<Entry> expectEntry1 = {entries[INDEX_FIRST], entries[INDEX_FIFTH], entries[INDEX_ZEROTH],
138 entries[INDEX_THIRD], entries[INDEX_SECOND], entries[INDEX_FORTH], entries[INDEX_SIXTH]};
139 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, expectEntry1,
140 DBStatus::OK, false));
141
142 /**
143 * @tc.steps: step3. test the Query with brackets appear in pairs and check the result with GetEntries.
144 * @tc.expected: step3. GetEntries return right result.
145 */
146 Query query2 = Query::Select().BeginGroup().EqualTo("$.field2.field3", true).And().
147 LessThanOrEqualTo("$.field2.field4.field5", 33).Or().LessThanOrEqualTo("$.field2.field4.field6.field7", 1000).
148 EndGroup().And().NotLike("$.field1", "%c").OrderBy("$.field2.field4.field6.field8", true);
149 vector<Entry> expectEntry2 = {entries[INDEX_FIRST], entries[INDEX_FIFTH], entries[INDEX_THIRD],
150 entries[INDEX_SECOND], entries[INDEX_FORTH], entries[INDEX_SIXTH]};
151 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, expectEntry2,
152 DBStatus::OK, false));
153
154 /**
155 * @tc.steps: step4. test the Query with brackets appear in pairs and check the result with GetCount.
156 * @tc.expected: step4. GetCount return 7.
157 */
158 Query query3 = Query::Select().BeginGroup().EqualTo("$.field2.field3", true).And().
159 LessThanOrEqualTo("$.field2.field4.field5", 33).Or().LessThanOrEqualTo("$.field2.field4.field6.field7", 1000).
160 EndGroup().And().NotLike("$.field1", "%c");
161 int cnt = 0;
162 int expectCnt = expectEntry2.size();
163 EXPECT_EQ(g_nbQueryDelegate->GetCount(query3, cnt), OK);
164 EXPECT_EQ(cnt, expectCnt);
165 }
166
167 /**
168 * @tc.name: BracketsTest 002
169 * @tc.desc: Verify the Brackets can change priority of query methods if brackets nested appear.
170 * @tc.type: FUNC
171 * @tc.require: SR000EPA23
172 * @tc.author: fengxiaoyun
173 */
174 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, BracketsTest002, TestSize.Level0)
175 {
176 /**
177 * @tc.steps: step1. create schema db and put 7 entries which has valid schema constructor
178 * and has the value given to db.
179 * @tc.expected: step1. create and put successfully.
180 */
181 vector<vector<string>> fieldValue = {
182 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
183 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
184 {"ab000", "true", "33", "30", "149"}, {"abxxx", "true", "12", "120", "-79"},
185 {"ab", "true", "20", "82", "150.999"}};
186 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
187 vector<Entry> entries;
188 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
189
190 /**
191 * @tc.steps: step2. test the Query without brackets and check the result with GetEntries.
192 * @tc.expected: step2. GetEntries return right result.
193 */
194 Query query1 = Query::Select().EqualTo("$.field2.field3", true).And().LessThanOrEqualTo("$.field2.field4.field5",
195 33).Or().LessThanOrEqualTo("$.field2.field4.field6.field7", 1000).And().NotLike("$.field1", "%c").
196 OrderBy("$.field2.field4.field6.field8", true);
197 vector<Entry> expectEntry1 = {entries[INDEX_FIRST], entries[INDEX_FIFTH], entries[INDEX_ZEROTH],
198 entries[INDEX_THIRD], entries[INDEX_SECOND], entries[INDEX_FORTH], entries[INDEX_SIXTH]};
199 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, expectEntry1,
200 DBStatus::OK, false));
201
202 /**
203 * @tc.steps: step3. test the Query with brackets nested appeared and check the result with GetEntries.
204 * @tc.expected: step3. GetEntries return right result.
205 */
206 Query query2 = Query::Select().EqualTo("$.field2.field3", true).And().BeginGroup().
207 LessThanOrEqualTo("$.field2.field4.field5", 33).Or().BeginGroup().
208 LessThanOrEqualTo("$.field2.field4.field6.field7", 1000).And().NotLike("$.field1", "%c").
209 EndGroup().EndGroup().OrderBy("$.field2.field4.field6.field8", true);
210 vector<Entry> expectEntry2 = {entries[INDEX_FIRST], entries[INDEX_FIFTH], entries[INDEX_ZEROTH],
211 entries[INDEX_SECOND], entries[INDEX_FORTH], entries[INDEX_SIXTH]};
212 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, expectEntry2,
213 DBStatus::OK, false));
214
215 /**
216 * @tc.steps: step4. test the Query with brackets nested appeared and check the result with GetCount.
217 * @tc.expected: step4. GetCount return 6.
218 */
219 Query query3 = Query::Select().EqualTo("$.field2.field3", true).And().BeginGroup().
220 LessThanOrEqualTo("$.field2.field4.field5", 33).Or().BeginGroup().
221 LessThanOrEqualTo("$.field2.field4.field6.field7", 1000).And().NotLike("$.field1", "%c").EndGroup().EndGroup();
222 int cnt = 0;
223 int expectCnt = expectEntry2.size();
224 EXPECT_EQ(g_nbQueryDelegate->GetCount(query3, cnt), OK);
225 EXPECT_EQ(cnt, expectCnt);
226 }
227
228 /**
229 * @tc.name: BracketsTest 003
230 * @tc.desc: Verify the Brackets appear abnormally then call predicate query interface will return INVALID_QUERY_FORMAT.
231 * @tc.type: FUNC
232 * @tc.require: SR000EPA23
233 * @tc.author: fengxiaoyun
234 */
235 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, BracketsTest003, TestSize.Level0)
236 {
237 /**
238 * @tc.steps: step1. create schema db and put 7 entries which has valid schema constructor
239 * and has the value given to db.
240 * @tc.expected: step1. create and put successfully.
241 */
242 vector<vector<string>> fieldValue = {
243 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
244 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
245 {"ab000", "true", "33", "30", "149"}, {"abxxx", "true", "12", "120", "-79"},
246 {"ab", "true", "20", "82", "150.999"}};
247 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
248 vector<Entry> entries;
249 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
250
251 /**
252 * @tc.steps: step2. generate query that the right bracket is more than left bracket,then call GetEntries()/
253 * GetCount().
254 * @tc.expected: step2. call GetEntries()/GetCount() return INVALID_QUERY_FORMAT.
255 */
256 Query query1 = Query::Select();
257 int bracketNum1 = GetRandInt(0, 5); // generate the number of bracket from 0 to 5 randomly.
258 DistributedDBSchemaTestTools::GenerateRandQuery(query1, bracketNum1, bracketNum1 + 1);
259 vector<Entry> expectEntry;
260 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, expectEntry,
261 DBStatus::INVALID_QUERY_FORMAT, true));
262
263 /**
264 * @tc.steps: step3. generate query that the right bracket is less than left bracket,then call GetEntries()/
265 * GetCount().
266 * @tc.expected: step3. call GetEntries()/GetCount() return INVALID_QUERY_FORMAT.
267 */
268 Query query2 = Query::Select();
269 int bracketNum2 = GetRandInt(1, 5); // generate the number of bracket from 1 to 5 randomly.
270 DistributedDBSchemaTestTools::GenerateRandQuery(query2, bracketNum2, bracketNum2 - 1);
271 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, expectEntry,
272 DBStatus::INVALID_QUERY_FORMAT, true));
273 }
274
275 /**
276 * @tc.name: BracketsTest 004
277 * @tc.desc: Verify test the Query with brackets that having And() after BeginGroup() or having Or() before EndGroup()
278 * will return INVALID_QUERY_FORMAT.
279 * @tc.type: FUNC
280 * @tc.require: SR000EPA23
281 * @tc.author: fengxiaoyun
282 */
283 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, BracketsTest004, TestSize.Level0)
284 {
285 /**
286 * @tc.steps: step1. create schema db and put 7 entries which has valid schema constructor
287 * and has the value given to db.
288 * @tc.expected: step1. create and put successfully.
289 */
290 vector<vector<string>> fieldValue = {
291 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
292 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
293 {"ab000", "true", "33", "30", "149"}, {"abxxx", "true", "12", "120", "-79"},
294 {"ab", "true", "20", "82", "150.999"}};
295 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
296 vector<Entry> entries;
297 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
298
299 /**
300 * @tc.steps: step2. test the Query with brackets that having And() after BeginGroup() and check the result
301 * with GetEntries/GetCount.
302 * @tc.expected: step2. GetEntries and GetCount return INVALID_QUERY_FORMAT.
303 */
304 Query query1 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 10).And().BeginGroup().And().
305 EqualTo("$.field2.field3", true).Or().Like("$.field1", "ab%").EndGroup();
306 vector<Entry> expectEntry;
307 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, expectEntry,
308 DBStatus::INVALID_QUERY_FORMAT, true));
309
310 /**
311 * @tc.steps: step3. test the Query with brackets that having or having Or() before EndGroup() and check the result
312 * with GetEntries/GetCount.
313 * @tc.expected: step3. GetEntries and GetCount return INVALID_QUERY_FORMAT.
314 */
315 Query query2 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 10).And().BeginGroup().
316 EqualTo("$.field2.field3", true).Or().Like("$.field1", "ab%").Or().EndGroup();
317 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, expectEntry,
318 DBStatus::INVALID_QUERY_FORMAT, true));
319
320 /**
321 * @tc.steps: step4. test the Query with brackets that right bracket before left bracket.
322 * @tc.expected: step4. GetEntries and GetCount return INVALID_QUERY_FORMAT.
323 */
324 Query query3 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 10).And().EndGroup().
325 EqualTo("$.field2.field3", true).Or().Like("$.field1", "ab%").Or().BeginGroup();
326 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, expectEntry,
327 DBStatus::INVALID_QUERY_FORMAT, true));
328 }
329
330 /**
331 * @tc.name: BracketsTest 005
332 * @tc.desc: Verify query with OrderBy/Limit in brackets will return INVALID_QUERY_FORMAT.
333 * @tc.type: FUNC
334 * @tc.require:
335 * @tc.author: fengxiaoyun
336 */
337 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, BracketsTest005, TestSize.Level0)
338 {
339 /**
340 * @tc.steps: step1. create schema db and put 7 entries which has valid schema constructor
341 * and has the value given to db.
342 * @tc.expected: step1. create and put successfully.
343 */
344 vector<vector<string>> fieldValue = {
345 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
346 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
347 {"ab000", "true", "33", "30", "149"}, {"abxxx", "true", "12", "120", "-79"},
348 {"ab", "true", "20", "82", "150.999"}};
349 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
350 vector<Entry> entries;
351 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
352
353 /**
354 * @tc.steps: step2. test the Query with OrderBy in the brackets and check the result.
355 * with GetEntries/GetCount.
356 * @tc.expected: step2. GetEntries and GetCount return INVALID_QUERY_FORMAT.
357 */
358 Query query1 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 10).And().BeginGroup().
359 EqualTo("$.field2.field3", true).Or().Like("$.field1", "ab%").OrderBy("$.field2.field4.field6.field8", true).
360 EndGroup();
361 vector<Entry> expectEntry;
362 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, expectEntry,
363 DBStatus::INVALID_QUERY_FORMAT, true));
364
365 /**
366 * @tc.steps: step3. test the Query with Limit in the brackets and check the result
367 * @tc.expected: step3. GetEntries and GetCount return INVALID_QUERY_FORMAT.
368 */
369 Query query2 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 10).And().BeginGroup().
370 EqualTo("$.field2.field3", true).Or().Like("$.field1", "ab%").Limit(5, 0).EndGroup();
371 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, expectEntry,
372 DBStatus::INVALID_QUERY_FORMAT, false));
373 }
374
375 /**
376 * @tc.name: NotNullTest 001
377 * @tc.desc: Verify query IsNotNull can get right result when put right param.
378 * @tc.type: FUNC
379 * @tc.require: SR000EPA23
380 * @tc.author: fengxiaoyun
381 */
382 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, NotNullTest001, TestSize.Level0)
383 {
384 /**
385 * @tc.steps: step1. create schema db and put 5 entries which has valid schema constructor
386 * and has the value given to db.
387 * @tc.expected: step1. create and put successfully.
388 */
389 vector<vector<string>> fieldValue = {
390 {"null", "true", "9", "1000", "12"}, {"abc123", "null", "88", "-100", "-99"},
391 {"abfxy", "true", "null", "0", "38"}, {"ab789", "false", "999", "null", "15.8"},
392 {"ab000", "true", "33", "30", "null"}};
393 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
394 vector<Entry> entries;
395 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
396 vector<vector<Entry>> expectEntries = {entries, entries, entries, entries, entries};
397 expectEntries[0].erase(expectEntries[0].begin());
398 expectEntries[INDEX_FIRST].erase(expectEntries[INDEX_FIRST].begin() + INDEX_FIRST);
399 expectEntries[INDEX_SECOND].erase(expectEntries[INDEX_SECOND].begin() + INDEX_SECOND);
400 expectEntries[INDEX_THIRD].erase(expectEntries[INDEX_THIRD].begin() + INDEX_THIRD);
401 expectEntries[INDEX_FORTH].pop_back();
402
403 /**
404 * @tc.steps: step2. test the Query with IsNotNull interface to test each field.
405 * @tc.expected: step2. construct success.
406 */
407 vector<Query> queries;
408 queries.push_back(Query::Select().IsNotNull("$.field1"));
409 queries.push_back(Query::Select().IsNotNull("$.field2.field3"));
410 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field5"));
411 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field6.field7"));
412 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field6.field8"));
413 /**
414 * @tc.steps: step3. call GetEntries(query, Entries), GetEntries(query, resultSet), and GetCount()
415 * to check the query
416 * @tc.expected: step3. GetEntries success and each query can return right result.
417 */
418 for (vector<Entry>::size_type index = 0; index < queries.size(); index++) {
419 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, queries[index],
420 expectEntries[index], DBStatus::OK, true));
421 }
422 }
423
424 /**
425 * @tc.name: NotNullTest 002
426 * @tc.desc: Verify query IsNotNull can get right result when put right param.
427 * @tc.type: FUNC
428 * @tc.require: SR000EPA23
429 * @tc.author: fengxiaoyun
430 */
431 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, NotNullTest002, TestSize.Level0)
432 {
433 /**
434 * @tc.steps: step1. create schema db and put 5 entries which has valid schema constructor
435 * and has the value given to db.
436 * @tc.expected: step1. create and put successfully.
437 */
438 vector<vector<string>> fieldValue = {
439 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
440 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
441 {"ab000", "true", "33", "30", "149"}};
442 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
443 vector<Entry> entries, entries2;
444 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
445
446 /**
447 * @tc.steps: step2. test the Query with IsNotNull interface to test each field.
448 * @tc.expected: step2. construct success.
449 */
450 vector<Query> queries;
451 queries.push_back(Query::Select().IsNotNull("$.field1"));
452 queries.push_back(Query::Select().IsNotNull("$.field2.field3"));
453 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field5"));
454 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field6.field7"));
455 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field6.field8"));
456 /**
457 * @tc.steps: step3. call GetEntries(query, Entries), GetEntries(query, resultSet), and GetCount()
458 * to check the query
459 * @tc.expected: step3. GetEntries success and each query can return right result.
460 */
461 for (vector<Entry>::size_type index = 0; index < queries.size(); index++) {
462 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, queries[index],
463 entries, DBStatus::OK, true));
464 }
465
466 /**
467 * @tc.steps: step4. delete all the 5 entries and insert new 5 records that include null element.
468 * @tc.expected: step4. delete and insert success.
469 */
470 vector<Key> keys;
471 for (vector<Entry>::iterator iter = entries.begin(); iter != entries.end(); iter++) {
472 keys.push_back(iter->key);
473 }
474 EXPECT_EQ(DistributedDBNbTestTools::DeleteLocalBatch(*g_nbQueryDelegate, keys), OK);
475
476 vector<vector<string>> fieldValue2 = {
477 {"null", "null", "null", "null", "null"}, {"null", "null", "null", "null", "null"},
478 {"null", "null", "null", "null", "null"}, {"null", "null", "null", "null", "null"},
479 {"null", "null", "null", "null", "null"}};
480 vector<string> schemasValue2 = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue2);
481 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue2, entries2);
482
483 /**
484 * @tc.steps: step5. get the Query again to check the IsNotNull interface.
485 * @tc.expected: step5. query success.
486 */
487 queries.clear();
488 queries.push_back(Query::Select().IsNotNull("$.field1"));
489 queries.push_back(Query::Select().IsNotNull("$.field2.field3"));
490 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field5"));
491 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field6.field7"));
492 queries.push_back(Query::Select().IsNotNull("$.field2.field4.field6.field8"));
493 /**
494 * @tc.steps: step6. call GetEntries(query, Entries), GetEntries(query, resultSet), and GetCount()
495 * to check the query
496 * @tc.expected: step6. GetEntries success and each query can return right result.
497 */
498 vector<vector<Entry>> expectEntries = {{}, {}, {}, {}, {}};
499 for (vector<Entry>::size_type index = 0; index < queries.size(); index++) {
500 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, queries[index],
501 expectEntries[index], DBStatus::NOT_FOUND, true));
502 }
503 }
504
505 /**
506 * @tc.name: NotNullTest 003
507 * @tc.desc: Verify that the field put to IsNotNull is illegal, the query got will return INVALID_QUERY_FIELD when use
508 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount() to check.
509 * @tc.type: FUNC
510 * @tc.require: SR000EPA23
511 * @tc.author: fengxiaoyun
512 */
513 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, NotNullTest003, TestSize.Level0)
514 {
515 /**
516 * @tc.steps: step1. create schema db and put 5 entries each schema of which has one null value constructor
517 * and has the value given to db.
518 * @tc.expected: step1. create and put successfully.
519 */
520 vector<vector<string>> fieldValue = {
521 {"null", "true", "9", "1000", "12"}, {"abc123", "null", "88", "-100", "-99"},
522 {"abfxy", "true", "null", "0", "38"}, {"ab789", "false", "999", "null", "15.8"},
523 {"ab000", "true", "33", "30", "null"}};
524 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
525 vector<Entry> entries, entriesExpect;
526 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entries);
527 vector<vector<Entry>> expectEntries = {entries, entries, entries, entries, entries};
528 expectEntries[0].erase(expectEntries[0].begin());
529 expectEntries[INDEX_FIRST].erase(expectEntries[INDEX_FIRST].begin() + INDEX_FIRST);
530 expectEntries[INDEX_SECOND].erase(expectEntries[INDEX_SECOND].begin() + INDEX_SECOND);
531 expectEntries[INDEX_THIRD].erase(expectEntries[INDEX_THIRD].begin() + INDEX_THIRD);
532 expectEntries[INDEX_FORTH].pop_back();
533
534 /**
535 * @tc.steps: step2. test the Query with IsNotNull interface on $.field9 which is not exist and check the query use
536 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
537 * @tc.expected: step2. query success but the GetEntries will return INVALID_QUERY_FIELD.
538 */
539 Query query1 = Query::Select().IsNotNull("$.field9");
540 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, entriesExpect,
541 DBStatus::INVALID_QUERY_FIELD, true));
542 /**
543 * @tc.steps: step3. test the Query with IsNotNull interface on $.field2 which is illegal and check the query use
544 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
545 * @tc.expected: step3. query success but the GetEntries will return INVALID_QUERY_FIELD.
546 */
547 Query query2 = Query::Select().IsNotNull("$.field2");
548 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, entriesExpect,
549 DBStatus::INVALID_QUERY_FIELD, true));
550 /**
551 * @tc.steps: step4. test the Query with IsNotNull interface on $.field2.field3.field4.field5 which is illegal
552 * and check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
553 * @tc.expected: step4. query success but the GetEntries will return INVALID_QUERY_FIELD.
554 */
555 Query query3 = Query::Select().IsNotNull("$.field2.field3.field4.field5");
556 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, entriesExpect,
557 DBStatus::INVALID_QUERY_FIELD, true));
558 /**
559 * @tc.steps: step5. test the Query with IsNotNull interface on $..field2.field3 the format of which is illegal
560 * and check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
561 * @tc.expected: step5. query success but the GetEntries will return INVALID_QUERY_FORMAT.
562 */
563 Query query4 = Query::Select().IsNotNull("$..field2.field3");
564 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query4, entriesExpect,
565 DBStatus::INVALID_QUERY_FORMAT, true));
566 /**
567 * @tc.steps: step6. test the Query with IsNotNull interface on field2.field3 the format of which is illegal
568 * and check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
569 * @tc.expected: step6. query success but the GetEntries will return INVALID_QUERY_FORMAT.
570 */
571 Query query5 = Query::Select().IsNotNull(".field2.field3");
572 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query5, entriesExpect,
573 DBStatus::INVALID_QUERY_FORMAT, true));
574 }
575
576 /**
577 * @tc.name: PrefixKeyTest 001
578 * @tc.desc: Verify that PrefixKey interface can return right records.
579 * @tc.type: FUNC
580 * @tc.require: SR000EPA23
581 * @tc.author: fengxiaoyun
582 */
583 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest001, TestSize.Level0)
584 {
585 /**
586 * @tc.steps: step1. create schema db and put 10 entries 5 of which has prefixKey {'k'} and rest of them has
587 * prefixKey {'a'}.
588 * @tc.expected: step1. create and put successfully.
589 */
590 vector<vector<string>> fieldValue = {
591 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
592 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
593 {"ab000", "true", "33", "30", "149"}};
594 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
595 vector<Entry> entriesK, entriesA, entriesExpect, entriesGot;
596 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesK);
597 PresetDatasToDB(g_nbQueryDelegate, KEY_A, schemasValue, entriesA);
598
599 /**
600 * @tc.steps: step2. test the Query with PrefixKey('') interface and then check the query use
601 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
602 * @tc.expected: step2. query success and all of the 3 interface can find 10 records.
603 */
604 for (vector<Entry>::size_type index = 0; index < entriesA.size(); index++) {
605 entriesExpect.push_back(entriesA[index]);
606 }
607 for (vector<Entry>::size_type index = 0; index < entriesK.size(); index++) {
608 entriesExpect.push_back(entriesK[index]);
609 }
610 Query query1 = Query::Select().PrefixKey({});
611 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, entriesExpect,
612 DBStatus::OK, true));
613
614 /**
615 * @tc.steps: step3. test the Query with PrefixKey({'k'}) interface and then check the query use
616 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
617 * @tc.expected: step3. query success and all of the 3 interface can find 5 records.
618 */
619 Query query2 = Query::Select().PrefixKey({'k'});
620 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, entriesK,
621 DBStatus::OK, true));
622 /**
623 * @tc.steps: step4. test the Query with PrefixKey({'a'}) interface and then check the query use
624 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
625 * @tc.expected: step4. query success and all of the 3 interface can find 5 records.
626 */
627 Query query3 = Query::Select().PrefixKey({'b'});
628 entriesExpect.clear();
629 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, entriesExpect,
630 DBStatus::NOT_FOUND, true));
631 }
632
633 /**
634 * @tc.name: PrefixKeyTest 002
635 * @tc.desc: Verify that none-schema DB use PrefixKey can return right result
636 * @tc.type: FUNC
637 * @tc.require: SR000EPA23
638 * @tc.author: fengxiaoyun
639 */
640 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest002, TestSize.Level0)
641 {
642 KvStoreNbDelegate *delegate = nullptr;
643 KvStoreDelegateManager *manager = nullptr;
644 Option option = g_option;
645 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
646 ASSERT_TRUE(delegate != nullptr && manager != nullptr);
647 /**
648 * @tc.steps: step1. put 10 records to DB, 5 of which has the prefix key 'k', and rest of which has prefix key 'a';
649 * @tc.expected: step1. create and put successfully.
650 */
651 vector<vector<string>> fieldValue = {
652 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
653 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
654 {"ab000", "true", "33", "30", "149"}};
655 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
656 vector<Entry> entriesK, entriesA, entriesExpect, entriesGot;
657 PresetDatasToDB(delegate, KEY_K, schemasValue, entriesK);
658 PresetDatasToDB(delegate, KEY_A, schemasValue, entriesA);
659
660 /**
661 * @tc.steps: step2. test the Query with PrefixKey({}) interface and then check the query use
662 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
663 * @tc.expected: step2. query success and all of the 3 interface can find 10 records.
664 */
665 for (vector<Entry>::size_type index = 0; index < entriesA.size(); index++) {
666 entriesExpect.push_back(entriesA[index]);
667 }
668 for (vector<Entry>::size_type index = 0; index < entriesK.size(); index++) {
669 entriesExpect.push_back(entriesK[index]);
670 }
671 Query query1 = Query::Select().PrefixKey({});
672 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query1, entriesExpect,
673 DBStatus::OK, true));
674
675 /**
676 * @tc.steps: step3. test the Query with PrefixKey({'k'}) interface and then check the query use
677 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
678 * @tc.expected: step3. query success and all of the 3 interface can find 5 records.
679 */
680 Query query2 = Query::Select().PrefixKey({'k'});
681 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query2, entriesK,
682 DBStatus::OK, true));
683 /**
684 * @tc.steps: step4. test the Query with PrefixKey({'b'}) interface and then check the query use
685 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
686 * @tc.expected: step4. query success and all of the 3 interface can find 0 records.
687 */
688 entriesA.clear();
689 Query query3 = Query::Select().PrefixKey({'b'});
690 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query3, entriesA,
691 DBStatus::NOT_FOUND, true));
692
693 EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, option.isMemoryDb));
694 }
695
696 /**
697 * @tc.name: PrefixKeyTest 003
698 * @tc.desc: Verify that use PrefixKey check schema DB can return right result, and prefixKey can't be call many time
699 * @tc.type: FUNC
700 * @tc.require: SR000EPA23
701 * @tc.author: fengxiaoyun
702 */
703 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest003, TestSize.Level0)
704 {
705 /**
706 * @tc.steps: step1. create schema db and put 10 entries 5 of which has prefixKey {'k'} and rest of them has
707 * prefixKey {'a'}.
708 * @tc.expected: step1. create and put successfully.
709 */
710 vector<vector<string>> fieldValue = {
711 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
712 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
713 {"ab000", "true", "33", "30", "149"}};
714 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
715 vector<Entry> entriesK, entriesA, entriesGot;
716 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesK);
717 PresetDatasToDB(g_nbQueryDelegate, KEY_A, schemasValue, entriesA);
718
719 /**
720 * @tc.steps: step2. test the Query with PrefixKey({}).Limit(5, 0) interface and then check the query use
721 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
722 * @tc.expected: step2. query success and all of the 3 interface can find 5 records.
723 */
724 Query query1 = Query::Select().PrefixKey({}).Limit(5, 0);
725 vector<Entry> entriesCheck = {entriesA[0], entriesA[INDEX_FIRST], entriesA[INDEX_SECOND],
726 entriesA[INDEX_THIRD], entriesA[INDEX_FORTH]};
727 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, entriesCheck,
728 DBStatus::OK, false));
729
730 /**
731 * @tc.steps: step3. test the Query with PrefixKey({}).And().GreaterThan("$.field2.field4.field5", 10).Limit(5, 0)
732 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
733 * @tc.expected: step3. query success and all of the 3 interface can find 4 records.
734 */
735 Query query2 = Query::Select().PrefixKey({}).GreaterThan("$.field2.field4.field5", "10").Limit(5, 0);
736 entriesCheck = {entriesA[INDEX_FIRST], entriesA[INDEX_THIRD], entriesA[INDEX_FORTH],
737 entriesK[INDEX_FIRST], entriesK[INDEX_THIRD]};
738 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, entriesCheck,
739 DBStatus::OK, false));
740 /**
741 * @tc.steps: step4. test the Query with PrefixKey({}).And().PrefixKey({'k'}).And().PrefixKey({'b'})
742 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
743 * @tc.expected: step4. query failed and returned INVALID_QUERY_FORMAT.
744 */
745 Query query3 = Query::Select().PrefixKey({}).PrefixKey({'k'}).And().PrefixKey({'b'});
746 entriesCheck.clear();
747 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, entriesCheck,
748 DBStatus::INVALID_QUERY_FORMAT, false));
749 }
750
751 /**
752 * @tc.name: PrefixKeyTest 004
753 * @tc.desc: Verify that none-schema DB can't use query interface except PrefixKey and Limit.
754 * @tc.type: FUNC
755 * @tc.require: SR000EPA23
756 * @tc.author: fengxiaoyun
757 */
758 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest004, TestSize.Level0)
759 {
760 KvStoreNbDelegate *delegate = nullptr;
761 KvStoreDelegateManager *manager = nullptr;
762 Option option = g_option;
763 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
764 ASSERT_TRUE(delegate != nullptr && manager != nullptr);
765 /**
766 * @tc.steps: step1. put 10 records to DB, 5 of which has the prefix key 'k', and rest of which has prefix key 'a';
767 * @tc.expected: step1. create and put successfully.
768 */
769 vector<vector<string>> fieldValue = {
770 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
771 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
772 {"ab000", "true", "33", "30", "149"}};
773 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
774 vector<Entry> entriesK, entriesA, entriesExpect, entriesGot;
775 PresetDatasToDB(delegate, KEY_K, schemasValue, entriesK);
776 PresetDatasToDB(delegate, KEY_A, schemasValue, entriesA);
777
778 /**
779 * @tc.steps: step2. test the Query with PrefixKey('').Limit(5, 0) interface and then check the query use
780 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
781 * @tc.expected: step2. query success and all of the 3 interface can find 5 records.
782 */
783 Query query1 = Query::Select().PrefixKey({ }).Limit(5, 0);
784 vector<Entry> entriesCheck = {entriesA[0], entriesA[INDEX_FIRST], entriesA[INDEX_SECOND],
785 entriesA[INDEX_THIRD], entriesA[INDEX_FORTH]};
786 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query1, entriesCheck,
787 DBStatus::OK, false));
788
789 /**
790 * @tc.steps: step3. test the Query with PrefixKey({}).And().GreaterThan("$.field2.field4.field5", 10).Limit(5, 0)
791 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
792 * @tc.expected: step3. query success and all of the 3 interface can find 4 records.
793 */
794 Query query2 = Query::Select().PrefixKey({}).GreaterThan("$.field2.field4.field5", 10).Limit(5, 0);
795 entriesCheck.clear();
796 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query2, entriesCheck,
797 DBStatus::NOT_SUPPORT, false));
798 /**
799 * @tc.steps: step4. test the Query with PrefixKey({}).And().PrefixKey({'k'}).And().PrefixKey({'b'})
800 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
801 * @tc.expected: step4. query failed and returned INVALID_QUERY_FORMAT.
802 */
803 Query query3 = Query::Select().PrefixKey({}).PrefixKey({'k'}).PrefixKey({'b'});
804 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query3, entriesCheck,
805 DBStatus::INVALID_QUERY_FORMAT, false));
806
807 EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, option.isMemoryDb));
808 }
809
810 /**
811 * @tc.name: PrefixKeyTest 005
812 * @tc.desc: Verify that schema DB can use prefixKey and other interface constructor legal query
813 * @tc.type: FUNC
814 * @tc.require: SR000EPA23
815 * @tc.author: fengxiaoyun
816 */
817 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest005, TestSize.Level0)
818 {
819 /**
820 * @tc.steps: step1. create schema db and put 10 entries 5 of which has prefixKey {'k'} and rest of them has
821 * prefixKey {'a'}.
822 * @tc.expected: step1. create and put successfully.
823 */
824 vector<vector<string>> fieldValue = {
825 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
826 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
827 {"ab000", "true", "33", "30", "149"}};
828 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
829 vector<Entry> entriesK, entriesA, entriesGot;
830 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesK);
831 PresetDatasToDB(g_nbQueryDelegate, KEY_A, schemasValue, entriesA);
832
833 /**
834 * @tc.steps: step2. test the Query with PrefixKey({}).And().EqualTo("$.field2.field3",true).OrderBy("field8")
835 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
836 * @tc.expected: step2. query success and all of the 3 interface can find 8 records.
837 */
838 Query query1 = Query::Select().PrefixKey({}).EqualTo("$.field2.field3",
839 true).OrderBy("$.field2.field4.field6.field8");
840 vector<Entry> entriesExpect = {entriesA[INDEX_FIRST], entriesK[INDEX_FIRST], entriesA[0], entriesK[0],
841 entriesA[INDEX_SECOND], entriesK[INDEX_SECOND], entriesA[INDEX_FORTH], entriesK[INDEX_FORTH]};
842 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, entriesExpect,
843 DBStatus::OK, false));
844
845 /**
846 * @tc.steps: step3. test the Query with NotEqualTo("$.field2.field3",false).PrefixKey({}).OrderBy("field8")
847 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
848 * @tc.expected: step3. query success and all of the 3 interface can find 8 records.
849 */
850 Query query2 = Query::Select().NotEqualTo("$.field2.field3", false).PrefixKey({}).
851 OrderBy("$.field2.field4.field6.field8");
852 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, entriesExpect,
853 DBStatus::OK, false));
854
855 /**
856 * @tc.steps: step4. test the Query with LessThan("$.field2.field4.field6.field8",
857 * 149).And().PrefixKey({}).Limit(3, 0) interface and then check the query use GetEntries(query, Entries),
858 * GetEntries(query, resultSet), GetCount().
859 * @tc.expected: step4. query successfully and returned {entriesA[0], entriesA[1], entriesA[2]}.
860 */
861 Query query3 = Query::Select().LessThan("$.field2.field4.field6.field8", 149).PrefixKey({}).Limit(3, 0);
862 entriesExpect = {entriesA[0], entriesA[INDEX_FIRST], entriesA[INDEX_SECOND]};
863 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, entriesExpect,
864 DBStatus::OK, false));
865 /**
866 * @tc.steps: step5. test the Query with GreaterThanOrEqualTo("$.field2.field4.field5", 9).And().PrefixKey({'a'}).
867 * LessThanOrEqualTo("field7", 50).OrderBy("$.field2.field4.field6.field8").Limit(4, 0) interface and then
868 * check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
869 * @tc.expected: step5. query failed and can't find any records.
870 */
871 Query query4 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 9).PrefixKey({'a'}).And().
872 LessThanOrEqualTo("$.field2.field4.field6.field7", 50).OrderBy("$.field2.field4.field6.field8").Limit(4, 0);
873 entriesExpect = {entriesA[INDEX_FIRST], entriesA[INDEX_THIRD], entriesA[INDEX_SECOND], entriesA[INDEX_FORTH]};
874 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query4, entriesExpect,
875 DBStatus::OK, false));
876 /**
877 * @tc.steps: step6. test the Query with GreaterThanOrEqualTo("$.field2.field4.field5", 9).And().PrefixKey({'b'}).
878 * LessThanOrEqualTo("field7", 50).OrderBy("$.field2.field4.field6.field8").Limit(4, 0) interface and then
879 * check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
880 * @tc.expected: step6. query failed and can't find any records.
881 */
882 Query query5 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 9).PrefixKey({'b'}).And().
883 LessThanOrEqualTo("$.field2.field4.field6.field7", 50).OrderBy("$.field2.field4.field6.field8").
884 OrderBy("$.field1");
885 entriesExpect = {};
886 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query5, entriesExpect,
887 DBStatus::NOT_FOUND, false));
888 }
889
890 /**
891 * @tc.name: PrefixKeyTest 006
892 * @tc.desc: Verify that schema DB can use prefixKey and other interface constructor legal query
893 * @tc.type: FUNC
894 * @tc.require: SR000EPA23
895 * @tc.author: fengxiaoyun
896 */
897 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest006, TestSize.Level0)
898 {
899 /**
900 * @tc.steps: step1. create schema db and put 10 entries 5 of which has prefixKey {'k'} and rest of them has
901 * prefixKey {'a'}.
902 * @tc.expected: step1. create and put successfully.
903 */
904 vector<vector<string>> fieldValue = {
905 {"null", "true", "9", "1000", "12"}, {"abc123", "null", "88", "-100", "-99"},
906 {"abfxy", "true", "null", "0", "38"}, {"ab789", "false", "999", "null", "15.8"},
907 {"ab000", "true", "33", "30", "null"}};
908 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
909 vector<Entry> entriesK, entriesA, entriesGot;
910 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesK);
911 PresetDatasToDB(g_nbQueryDelegate, KEY_A, schemasValue, entriesA);
912
913 /**
914 * @tc.steps: step2. test the Query with PrefixKey({'b'}).And().Like("$.field1", ab%).OrderBy("field8")
915 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
916 * @tc.expected: step2. query success and all of the 3 interface can find 8 records.
917 */
918 Query query1 = Query::Select().PrefixKey({'b'}).Like("$.field1", "ab%").
919 OrderBy("$.field2.field4.field6.field8");
920 vector<Entry> entriesExpect;
921 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, entriesExpect,
922 DBStatus::NOT_FOUND, false));
923
924 /**
925 * @tc.steps: step3. test the Query with NotLike("$.field1", ab%).And().PrefixKey({}).OrderBy("field8")
926 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
927 * @tc.expected: step3. query success and all of the 3 interface can find 8 records.
928 */
929 Query query2 = Query::Select().NotLike("$.field1", "ab%").PrefixKey({}).
930 OrderBy("$.field2.field4.field6.field8");
931 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, entriesExpect,
932 DBStatus::NOT_FOUND, false));
933 /**
934 * @tc.steps: step4. test the Query with In("$.field2.field4.field6.field7",
935 * [0, 30, 50, 1000]).Limit(3, 3) interface and then check the query use GetEntries(query, Entries),
936 * GetEntries(query, resultSet), GetCount().
937 * @tc.expected: step4. query successfully and returned {entriesA[0], entriesA[1], entriesA[2]}.
938 */
939 vector<string> scope = {"0", "30", "50", "1000"};
940 Query query3 = Query::Select().PrefixKey({}).In("$.field2.field4.field6.field7", scope).Limit(3, 3);
941 entriesExpect = {entriesK[0], entriesK[INDEX_SECOND], entriesK[INDEX_FORTH]};
942 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, entriesExpect,
943 DBStatus::OK, false));
944 /**
945 * @tc.steps: step5. test the Query with NotIn("$.field2.field4.field6.field7", [-100]).And().PrefixKey({}).
946 * Limit(3, 3) interface and then check the query use
947 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
948 * @tc.expected: step5. query failed and can't find any records.
949 */
950 scope = {"-100"};
951 Query query4 = Query::Select().NotIn("$.field2.field4.field6.field7", scope).PrefixKey({}).Limit(3, 3);
952 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query4, entriesExpect,
953 DBStatus::OK, false));
954 /**
955 * @tc.steps: step6. test the Query with IsNull("$.field2.field4.field5").And().PrefixKey({}).
956 IsNotNull("$.field2.field3").OrderBy("$.field2.field4.field6.field8") interface
957 * and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
958 * @tc.expected: step6. query failed and can't find any records.
959 */
960 Query query5 = Query::Select().IsNull("$.field2.field4.field5").And().PrefixKey({}).
961 IsNotNull("$.field2.field3").OrderBy("$.field2.field4.field6.field8");
962 entriesExpect = {entriesA[INDEX_SECOND], entriesK[INDEX_SECOND]};
963 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query5, entriesExpect,
964 DBStatus::OK, false));
965 }
966
967 /**
968 * @tc.name: PrefixKeyTest 007
969 * @tc.desc: the query interface that without any condition can get all the records.
970 * @tc.type: FUNC
971 * @tc.require: SR000EPA23
972 * @tc.author: fengxiaoyun
973 */
974 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest007, TestSize.Level0)
975 {
976 KvStoreNbDelegate *delegate = nullptr;
977 KvStoreDelegateManager *manager = nullptr;
978 Option option = g_option;
979 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
980 ASSERT_TRUE(delegate != nullptr && manager != nullptr);
981 /**
982 * @tc.steps: step1. create schema db and non-schema DB and put 10 entries to them.
983 * @tc.expected: step1. create and put successfully.
984 */
985 vector<vector<string>> fieldValue = {
986 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
987 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
988 {"ab000", "true", "33", "30", "149"}, {"abc", "true", "9", "1000", "12"},
989 {"abc123", "true", "88", "-100", "-99"}, {"abfxy", "true", "10", "0", "38"},
990 {"ab789", "false", "999", "50", "15.8"}, {"ab000", "true", "33", "30", "149"}};
991 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
992 vector<Entry> entriesSchema, entriesGot;
993 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesSchema);
994 EXPECT_EQ(DistributedDBNbTestTools::PutBatch(*delegate, entriesSchema), OK);
995
996 /**
997 * @tc.steps: step2. test the Query Select() from the schema DB and then check the query
998 * use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
999 * @tc.expected: step2. query success and all of the 3 interface can find 10 records.
1000 */
1001 Query query = Query::Select();
1002 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query, entriesSchema,
1003 DBStatus::OK, true));
1004
1005 /**
1006 * @tc.steps: step3. test the Query Select() from the non-schema DB and then check the query
1007 * use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
1008 * @tc.expected: step3. query success and all of the 3 interface can find 10 records.
1009 */
1010 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*delegate, query, entriesSchema,
1011 DBStatus::OK, true));
1012
1013 EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, option.isMemoryDb));
1014 }
1015
1016 /**
1017 * @tc.name: PrefixKeyTest 008
1018 * @tc.desc: the query that the position of prefixKey do not effect the result.
1019 * @tc.type: FUNC
1020 * @tc.require: SR000EPA23
1021 * @tc.author: fengxiaoyun
1022 */
1023 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest008, TestSize.Level0)
1024 {
1025 /**
1026 * @tc.steps: step1. create schema db and put 10 entries 5 of which has prefixKey {'k'} and rest of them has
1027 * prefixKey {'a'}.
1028 * @tc.expected: step1. create and put successfully.
1029 */
1030 vector<vector<string>> fieldValue = {
1031 {"abc", "true", "9", "1000", "12"}, {"abc123", "true", "88", "-100", "-99"},
1032 {"abfxy", "true", "10", "0", "38"}, {"ab789", "false", "999", "50", "15.8"},
1033 {"ab000", "true", "33", "30", "149"}};
1034 vector<string> schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
1035 vector<Entry> entriesK, entriesA, entriesGot;
1036 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesK);
1037 PresetDatasToDB(g_nbQueryDelegate, KEY_A, schemasValue, entriesA);
1038
1039 /**
1040 * @tc.steps: step2. test the Query with Like("$.field1", "ab%").OrderBy("$.field2.field4.field6.field8").
1041 * PrefixKey({'a'}).Limit(5, 1) interface and then check the query use GetEntries(query, Entries),
1042 * GetEntries(query, resultSet), GetCount().
1043 * @tc.expected: step2. query success and all of the 3 interface can find 4 records.
1044 */
1045 Query query1 = Query::Select().Like("$.field1", "ab%").OrderBy("$.field2.field4.field6.field8").
1046 PrefixKey({'a'}).Limit(5, 1);
1047 vector<Entry> entriesExpect = {entriesA[INDEX_ZEROTH], entriesA[INDEX_THIRD], entriesA[INDEX_SECOND],
1048 entriesA[INDEX_FORTH]};
1049 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query1, entriesExpect,
1050 DBStatus::OK, false));
1051
1052 /**
1053 * @tc.steps: step3. test the Query with NotLike("$.field1", ab%).And().PrefixKey({}).OrderBy("field8")
1054 * interface and then check the query use GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
1055 * @tc.expected: step3. query success and all of the 3 interface can find 8 records.
1056 */
1057 Query query2 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 9).And().
1058 LessThanOrEqualTo("$.field2.field4.field6.field7", 50).OrderBy("$.field2.field4.field6.field8").
1059 PrefixKey({'a'}).Limit(4, 0);
1060 entriesExpect = {entriesA[INDEX_FIRST], entriesA[INDEX_THIRD], entriesA[INDEX_SECOND], entriesA[INDEX_FORTH]};
1061 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query2, entriesExpect,
1062 DBStatus::OK, false));
1063 /**
1064 * @tc.steps: step4. test the Query with GreaterThanOrEqualTo("$.field2.field4.field5", 9).And().
1065 * LessThanOrEqualTo("$.field2.field4.field6.field7", 50).OrderBy("$.field1").PrefixKey({'a'}).
1066 * OrderBy("$.field2.field4.field6.field8") interface and then check the query use GetEntries(query, Entries),
1067 * GetEntries(query, resultSet), GetCount().
1068 * @tc.expected: step4. query successfully and returned {entriesA[0], entriesA[1], entriesA[2]}.
1069 */
1070 Query query3 = Query::Select().GreaterThanOrEqualTo("$.field2.field4.field5", 9).And().
1071 LessThanOrEqualTo("$.field2.field4.field6.field7", 50).OrderBy("$.field1").PrefixKey({'a'}).
1072 OrderBy("$.field2.field4.field6.field8");
1073 entriesExpect = {entriesA[INDEX_FORTH], entriesA[INDEX_THIRD], entriesA[INDEX_FIRST], entriesA[INDEX_SECOND]};
1074 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query3, entriesExpect,
1075 DBStatus::OK, false));
1076 /**
1077 * @tc.steps: step5. test the Query with NotIn("$.field2.field4.field6.field7", [-100]).And().PrefixKey({}).
1078 * Limit(3, 3) interface and then check the query use
1079 * GetEntries(query, Entries), GetEntries(query, resultSet), GetCount().
1080 * @tc.expected: step5. query failed and can't find any records.
1081 */
1082 fieldValue = {
1083 {"abc", "true", "33", "1000", "12"}, {"abc", "true", "9", "-100", "-99"},
1084 {"ab789", "true", "100", "0", "38"}, {"ab789", "false", "99", "50", "15.8"},
1085 {"abc", "true", "9", "30", "149"}};
1086 schemasValue = DistributedDBSchemaTestTools::GenerateCombinationSchemaValue(fieldValue);
1087 entriesA.clear();
1088 entriesK.clear();
1089 PresetDatasToDB(g_nbQueryDelegate, KEY_A, schemasValue, entriesA);
1090 PresetDatasToDB(g_nbQueryDelegate, KEY_K, schemasValue, entriesK);
1091
1092 Query query4 = Query::Select().OrderBy("$.field1").OrderBy("$.field2.field4.field5").
1093 OrderBy("$.field2.field4.field6.field8").PrefixKey({'a'}).Limit(3, 3);
1094 entriesExpect = {entriesA[INDEX_FORTH], entriesA[INDEX_ZEROTH]};
1095 EXPECT_TRUE(DistributedDBSchemaTestTools::CombinationCheckQueryResult(*g_nbQueryDelegate, query4, entriesExpect,
1096 DBStatus::OK, false));
1097 }
1098
MoveCursor(KvStoreResultSet & resultSet,const vector<Entry> & entriesBatch)1099 void MoveCursor(KvStoreResultSet &resultSet, const vector<Entry> &entriesBatch)
1100 {
1101 int currentPosition = CURSOR_POSITION_NEGATIVE1;
1102 Entry entry;
1103 bool result = false;
1104 for (int position = CURSOR_POSITION_NEGATIVE1; position < TEN_RECORDS; ++position) {
1105 result = resultSet.MoveToNext();
1106 if (position < (TEN_RECORDS - CURSOR_POSITION_1)) {
1107 EXPECT_TRUE(result);
1108 } else {
1109 EXPECT_TRUE(result == false);
1110 }
1111 currentPosition = resultSet.GetPosition();
1112 EXPECT_TRUE(currentPosition == (position + CURSOR_POSITION_1));
1113 if (position < (TEN_RECORDS - CURSOR_POSITION_1)) {
1114 EXPECT_TRUE(resultSet.GetEntry(entry) == OK);
1115 EXPECT_TRUE(CompareVector(entry.key, entriesBatch[position + CURSOR_POSITION_1].key));
1116 EXPECT_TRUE(CompareVector(entry.value, entriesBatch[position + CURSOR_POSITION_1].value));
1117 } else {
1118 EXPECT_TRUE(resultSet.GetEntry(entry) == NOT_FOUND);
1119 }
1120 }
1121 /**
1122 * @tc.steps: step3. set the current position is 10, call MoveToPrevious, GetPostion, GetEntry looply.
1123 * @tc.expected: step3. return values are all right.
1124 */
1125 for (int position = TEN_RECORDS; position > CURSOR_POSITION_NEGATIVE1; --position) {
1126 result = resultSet.MoveToPrevious();
1127 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
1128 EXPECT_TRUE(result);
1129 } else {
1130 EXPECT_TRUE(result == false);
1131 }
1132 currentPosition = resultSet.GetPosition();
1133 EXPECT_TRUE(currentPosition == (position - CURSOR_POSITION_1));
1134 if (position > (CURSOR_POSITION_NEGATIVE1 + CURSOR_POSITION_1)) {
1135 EXPECT_TRUE(resultSet.GetEntry(entry) == OK);
1136 EXPECT_TRUE(CompareVector(entry.key, entriesBatch[position - CURSOR_POSITION_1].key));
1137 EXPECT_TRUE(CompareVector(entry.value, entriesBatch[position - CURSOR_POSITION_1].value));
1138 } else {
1139 EXPECT_TRUE(resultSet.GetEntry(entry) == NOT_FOUND);
1140 }
1141 }
1142 }
1143
1144 /*
1145 * @tc.name: PrefixKeyTest 009
1146 * @tc.desc: test MoveToNext, MoveToPrevious interface with 10 2M data by query= prefixkey.
1147 * @tc.type: FUNC
1148 * @tc.require: SR000EPA23
1149 * @tc.author: fengxiaoyun
1150 */
1151 HWTEST_F(DistributeddbNbPredicateQueryExpandTest, PrefixKeyTest009, TestSize.Level2)
1152 {
1153 KvStoreNbDelegate *delegate = nullptr;
1154 KvStoreDelegateManager *manager = nullptr;
1155 Option option = g_option;
1156 option.isMemoryDb = false;
1157 option.schema = SpliceToSchema(VALID_VERSION_1, VALID_MODE_1, PERF_SCHEMA_DEFINE,
1158 PERF_SCHEMA_SIX_INDEXES, SKIP_SIZE);
1159 delegate = DistributedDBNbTestTools::GetNbDelegateSuccess(manager, g_dbParameter2, option);
1160 ASSERT_NE(manager, nullptr);
1161 ASSERT_NE(delegate, nullptr);
1162
1163 vector<Entry> entriesBatch;
1164 vector<Key> allKeys;
1165 EntrySize entrySize = {KEY_EIGHT_BYTE, TWO_M_LONG_STRING};
1166 entriesBatch = DistributedDBSchemaTestTools::GenerateFixedJsonSchemaRecords(
1167 TEN_RECORDS, entrySize, 'k', '0', allKeys);
1168 EXPECT_EQ(DistributedDBNbTestTools::PutBatch(*delegate, entriesBatch), OK);
1169
1170 /**
1171 * @tc.steps: step1. call GetEntries interface to get KvStoreResultSet by query.
1172 * @tc.expected: step1. get success.
1173 */
1174 Query query = Query::Select().GreaterThanOrEqualTo("$.field1", 0).PrefixKey({'k'});
1175 KvStoreResultSet *resultSet = nullptr;
1176 EXPECT_TRUE(delegate->GetEntries(query, resultSet) == OK);
1177 sort(entriesBatch.begin(), entriesBatch.end(), DistributedTestTools::CompareKey);
1178 /**
1179 * @tc.steps: step2. set the current position is -1, call MoveToNext, GetPostion and GetEntry interface looply.
1180 * @tc.expected: step2. return values are all right.
1181 */
1182 ASSERT_NE(resultSet, nullptr);
1183 MoveCursor(*resultSet, entriesBatch);
1184 EXPECT_TRUE(delegate->CloseResultSet(resultSet) == OK);
1185 EXPECT_TRUE(EndCaseDeleteDB(manager, delegate, STORE_ID_2, false));
1186 }
1187 }
1188 #endif
1189