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 #define LOG_TAG "SingleKvStoreClientQueryTest"
17
18 #include <unistd.h>
19 #include <cstddef>
20 #include <cstdint>
21 #include <vector>
22 #include "distributed_kv_data_manager.h"
23 #include "types.h"
24 #include "gtest/gtest.h"
25 namespace {
26 using namespace testing::ext;
27 using namespace OHOS::DistributedKv;
28 class SingleKvStoreClientQueryTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31
32 static void TearDownTestCase(void);
33
34 void SetUp();
35
36 void TearDown();
37
38 static std::shared_ptr<SingleKvStore> singleKvStore;
39 static Status statusGetKvStore;
40 };
41
42 static constexpr const char *VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
43 "\"SCHEMA_MODE\":\"STRICT\","
44 "\"SCHEMA_SKIPSIZE\":0,"
45 "\"SCHEMA_DEFINE\":{"
46 "\"name\":\"INTEGER, NOT NULL\""
47 "},"
48 "\"SCHEMA_INDEXES\":[\"$.name\"]}";
49 std::shared_ptr<SingleKvStore> SingleKvStoreClientQueryTest::singleKvStore = nullptr;
50 Status SingleKvStoreClientQueryTest::statusGetKvStore = Status::ERROR;
51 static constexpr int32_t INVALID_NUMBER = -1;
52 static constexpr uint32_t MAX_QUERY_LENGTH = 1024;
53
SetUpTestCase(void)54 void SingleKvStoreClientQueryTest::SetUpTestCase(void)
55 {
56 std::string baseDir = "/data/service/el1/public/database/SingleKvStoreClientQueryTest";
57 mkdir(baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
58 }
59
TearDownTestCase(void)60 void SingleKvStoreClientQueryTest::TearDownTestCase(void)
61 {
62 (void)remove("/data/service/el1/public/database/SingleKvStoreClientQueryTest/key");
63 (void)remove("/data/service/el1/public/database/SingleKvStoreClientQueryTest/kvdb");
64 (void)remove("/data/service/el1/public/database/SingleKvStoreClientQueryTest");
65 }
66
SetUp(void)67 void SingleKvStoreClientQueryTest::SetUp(void)
68 {}
69
TearDown(void)70 void SingleKvStoreClientQueryTest::TearDown(void)
71 {}
72
73 /**
74 * @tc.name: DataQuery
75 * @tc.desc: the predicate is reset
76 * @tc.type: FUNC
77 * @tc.require:
78 * @tc.author: zuojiangjiang
79 */
80 HWTEST_F(SingleKvStoreClientQueryTest, TestQueryReset, TestSize.Level1)
81 {
82 DataQuery query;
83 EXPECT_TRUE(query.ToString().length() == 0);
84 std::string str = "test value";
85 query.EqualTo("$.test_field_name", str);
86 EXPECT_TRUE(query.ToString().length() > 0);
87 query.Reset();
88 EXPECT_TRUE(query.ToString().length() == 0);
89 }
90
91 /**
92 * @tc.name: DataQuery
93 * @tc.desc: the predicate is equalTo, the field is invalid
94 * @tc.type: FUNC
95 * @tc.require:
96 * @tc.author: zuojiangjiang
97 */
98 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryEqualToInvalidField, TestSize.Level1)
99 {
100 DataQuery query;
101 query.EqualTo("", 100);
102 EXPECT_TRUE(query.ToString().length() == 0);
103 query.EqualTo("$.test_field_name^", 100);
104 EXPECT_TRUE(query.ToString().length() == 0);
105 query.EqualTo("", (int64_t)100);
106 EXPECT_TRUE(query.ToString().length() == 0);
107 query.EqualTo("^", (int64_t)100);
108 EXPECT_TRUE(query.ToString().length() == 0);
109 query.EqualTo("", 1.23);
110 EXPECT_TRUE(query.ToString().length() == 0);
111 query.EqualTo("$.^", 1.23);
112 EXPECT_TRUE(query.ToString().length() == 0);
113 query.EqualTo("", false);
114 EXPECT_TRUE(query.ToString().length() == 0);
115 query.EqualTo("^$.test_field_name", false);
116 EXPECT_TRUE(query.ToString().length() == 0);
117 query.EqualTo("", std::string("str"));
118 EXPECT_TRUE(query.ToString().length() == 0);
119 query.EqualTo("^^^^^^^", std::string("str"));
120 EXPECT_TRUE(query.ToString().length() == 0);
121 }
122
123 /**
124 * @tc.name: DataQuery
125 * @tc.desc: the predicate is equalTo, the field is valid
126 * @tc.type: FUNC
127 * @tc.require:
128 * @tc.author: zuojiangjiang
129 */
130 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryEqualToValidField, TestSize.Level1)
131 {
132 DataQuery query;
133 query.EqualTo("$.test_field_name", 100);
134 EXPECT_TRUE(query.ToString().length() > 0);
135 query.Reset();
136 query.EqualTo("$.test_field_name", (int64_t) 100);
137 EXPECT_TRUE(query.ToString().length() > 0);
138 query.Reset();
139 query.EqualTo("$.test_field_name", 1.23);
140 EXPECT_TRUE(query.ToString().length() > 0);
141 query.Reset();
142 query.EqualTo("$.test_field_name", false);
143 EXPECT_TRUE(query.ToString().length() > 0);
144 query.Reset();
145 std::string str = "";
146 query.EqualTo("$.test_field_name", str);
147 EXPECT_TRUE(query.ToString().length() > 0);
148 }
149
150 /**
151 * @tc.name: DataQuery
152 * @tc.desc: the predicate is notEqualTo, the field is invalid
153 * @tc.type: FUNC
154 * @tc.require:
155 * @tc.author: zuojiangjiang
156 */
157 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryNotEqualToValidField, TestSize.Level1)
158 {
159 DataQuery query;
160 query.NotEqualTo("", 100);
161 EXPECT_TRUE(query.ToString().length() == 0);
162 query.NotEqualTo("$.test_field_name^test", 100);
163 EXPECT_TRUE(query.ToString().length() == 0);
164 query.NotEqualTo("", (int64_t)100);
165 EXPECT_TRUE(query.ToString().length() == 0);
166 query.NotEqualTo("^$.test_field_name", (int64_t)100);
167 EXPECT_TRUE(query.ToString().length() == 0);
168 query.NotEqualTo("", 1.23);
169 EXPECT_TRUE(query.ToString().length() == 0);
170 query.NotEqualTo("^", 1.23);
171 EXPECT_TRUE(query.ToString().length() == 0);
172 query.NotEqualTo("", false);
173 EXPECT_TRUE(query.ToString().length() == 0);
174 query.NotEqualTo("^^", false);
175 EXPECT_TRUE(query.ToString().length() == 0);
176 query.NotEqualTo("", std::string("test_value"));
177 EXPECT_TRUE(query.ToString().length() == 0);
178 query.NotEqualTo("$.test_field^_name", std::string("test_value"));
179 EXPECT_TRUE(query.ToString().length() == 0);
180 }
181
182 /**
183 * @tc.name: DataQuery
184 * @tc.desc: the predicate is notEqualTo, the field is valid
185 * @tc.type: FUNC
186 * @tc.require:
187 * @tc.author: zuojiangjiang
188 */
189 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryNotEqualToInvalidField, TestSize.Level1)
190 {
191 DataQuery query;
192 query.NotEqualTo("$.test_field_name", 100);
193 EXPECT_TRUE(query.ToString().length() > 0);
194 query.Reset();
195 query.NotEqualTo("$.test_field_name", (int64_t) 100);
196 EXPECT_TRUE(query.ToString().length() > 0);
197 query.Reset();
198 query.NotEqualTo("$.test_field_name", 1.23);
199 EXPECT_TRUE(query.ToString().length() > 0);
200 query.Reset();
201 query.NotEqualTo("$.test_field_name", false);
202 EXPECT_TRUE(query.ToString().length() > 0);
203 query.Reset();
204 std::string str = "test value";
205 query.NotEqualTo("$.test_field_name", str);
206 EXPECT_TRUE(query.ToString().length() > 0);
207 }
208
209 /**
210 * @tc.name: DataQuery
211 * @tc.desc: the predicate is greaterThan, the field is invalid.
212 * @tc.type: FUNC
213 * @tc.require:
214 * @tc.author: zuojiangjiang
215 */
216 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryGreaterThanInvalidField, TestSize.Level1)
217 {
218 DataQuery query;
219 query.GreaterThan("", 100);
220 EXPECT_TRUE(query.ToString().length() == 0);
221 query.GreaterThan("$.^^", 100);
222 EXPECT_TRUE(query.ToString().length() == 0);
223 query.GreaterThan("", (int64_t) 100);
224 EXPECT_TRUE(query.ToString().length() == 0);
225 query.GreaterThan("^$.test_field_name", (int64_t) 100);
226 EXPECT_TRUE(query.ToString().length() == 0);
227 query.GreaterThan("", 1.23);
228 EXPECT_TRUE(query.ToString().length() == 0);
229 query.GreaterThan("^", 1.23);
230 EXPECT_TRUE(query.ToString().length() == 0);
231 query.GreaterThan("", "test value");
232 EXPECT_TRUE(query.ToString().length() == 0);
233 query.GreaterThan("$.test_field_name^*%$#", "test value");
234 EXPECT_TRUE(query.ToString().length() == 0);
235 }
236
237 /**
238 * @tc.name: DataQuery
239 * @tc.desc: the predicate is greaterThan, the field is valid.
240 * @tc.type: FUNC
241 * @tc.require:
242 * @tc.author: zuojiangjiang
243 */
244 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryGreaterThanValidField, TestSize.Level1)
245 {
246 DataQuery query;
247 query.GreaterThan("$.test_field_name", 100);
248 EXPECT_TRUE(query.ToString().length() > 0);
249 query.Reset();
250 query.GreaterThan("$.test_field_name", (int64_t) 100);
251 EXPECT_TRUE(query.ToString().length() > 0);
252 query.Reset();
253 query.GreaterThan("$.test_field_name", 1.23);
254 EXPECT_TRUE(query.ToString().length() > 0);
255 query.Reset();
256 query.GreaterThan("$.test_field_name$$$", "test value");
257 EXPECT_TRUE(query.ToString().length() > 0);
258 }
259
260 /**
261 * @tc.name: DataQuery
262 * @tc.desc: the predicate is lessThan, the field is invalid.
263 * @tc.type: FUNC
264 * @tc.require:
265 * @tc.author: zuojiangjiang
266 */
267 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLessThanInvalidField, TestSize.Level1)
268 {
269 DataQuery query;
270 query.LessThan("", 100);
271 EXPECT_TRUE(query.ToString().length() == 0);
272 query.LessThan("$.^", 100);
273 EXPECT_TRUE(query.ToString().length() == 0);
274 query.LessThan("", (int64_t) 100);
275 EXPECT_TRUE(query.ToString().length() == 0);
276 query.LessThan("^$.test_field_name", (int64_t) 100);
277 EXPECT_TRUE(query.ToString().length() == 0);
278 query.LessThan("", 1.23);
279 EXPECT_TRUE(query.ToString().length() == 0);
280 query.LessThan("^^^", 1.23);
281 EXPECT_TRUE(query.ToString().length() == 0);
282 query.LessThan("", "test value");
283 EXPECT_TRUE(query.ToString().length() == 0);
284 query.LessThan("$.test_field_name^", "test value");
285 EXPECT_TRUE(query.ToString().length() == 0);
286 }
287
288 /**
289 * @tc.name: DataQuery
290 * @tc.desc: the predicate is lessThan, the field is valid.
291 * @tc.type: FUNC
292 * @tc.require:
293 * @tc.author: zuojiangjiang
294 */
295 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLessThanValidField, TestSize.Level1)
296 {
297 DataQuery query;
298 query.LessThan("$.test_field_name", 100);
299 EXPECT_TRUE(query.ToString().length() > 0);
300 query.Reset();
301 query.LessThan("$.test_field_name", (int64_t) 100);
302 EXPECT_TRUE(query.ToString().length() > 0);
303 query.Reset();
304 query.LessThan("$.test_field_name", 1.23);
305 EXPECT_TRUE(query.ToString().length() > 0);
306 query.Reset();
307 query.LessThan("$.test_field_name", "test value");
308 EXPECT_TRUE(query.ToString().length() > 0);
309 }
310
311 /**
312 * @tc.name: DataQuery
313 * @tc.desc: the predicate is greaterThanOrEqualTo, the field is invalid.
314 * @tc.type: FUNC
315 * @tc.require:
316 * @tc.author: zuojiangjiang
317 */
318 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryGreaterThanOrEqualToInvalidField, TestSize.Level1)
319 {
320 DataQuery query;
321 query.GreaterThanOrEqualTo("", 100);
322 EXPECT_TRUE(query.ToString().length() == 0);
323 query.GreaterThanOrEqualTo("^$.test_field_name", 100);
324 EXPECT_TRUE(query.ToString().length() == 0);
325 query.GreaterThanOrEqualTo("", (int64_t) 100);
326 EXPECT_TRUE(query.ToString().length() == 0);
327 query.GreaterThanOrEqualTo("$.test_field_name^", (int64_t) 100);
328 EXPECT_TRUE(query.ToString().length() == 0);
329 query.GreaterThanOrEqualTo("", 1.23);
330 EXPECT_TRUE(query.ToString().length() == 0);
331 query.GreaterThanOrEqualTo("^$.^", 1.23);
332 EXPECT_TRUE(query.ToString().length() == 0);
333 query.GreaterThanOrEqualTo("", "test value");
334 EXPECT_TRUE(query.ToString().length() == 0);
335 query.GreaterThanOrEqualTo("^^=", "test value");
336 EXPECT_TRUE(query.ToString().length() == 0);
337 }
338
339 /**
340 * @tc.name: DataQuery
341 * @tc.desc: the predicate is greaterThanOrEqualTo, the field is valid.
342 * @tc.type: FUNC
343 * @tc.require:
344 * @tc.author: zuojiangjiang
345 */
346 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryGreaterThanOrEqualToValidField, TestSize.Level1)
347 {
348 DataQuery query;
349 query.GreaterThanOrEqualTo("$.test_field_name", 100);
350 EXPECT_TRUE(query.ToString().length() > 0);
351 query.Reset();
352 query.GreaterThanOrEqualTo("$.test_field_name", (int64_t) 100);
353 EXPECT_TRUE(query.ToString().length() > 0);
354 query.Reset();
355 query.GreaterThanOrEqualTo("$.test_field_name", 1.23);
356 EXPECT_TRUE(query.ToString().length() > 0);
357 query.Reset();
358 query.GreaterThanOrEqualTo("$.test_field_name", "test value");
359 EXPECT_TRUE(query.ToString().length() > 0);
360 }
361
362 /**
363 * @tc.name: DataQuery
364 * @tc.desc: the predicate is lessThanOrEqualTo, the field is invalid.
365 * @tc.type: FUNC
366 * @tc.require:
367 * @tc.author: zuojiangjiang
368 */
369 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLessThanOrEqualToInvalidField, TestSize.Level1)
370 {
371 DataQuery query;
372 query.LessThanOrEqualTo("", 100);
373 EXPECT_TRUE(query.ToString().length() == 0);
374 query.LessThanOrEqualTo("^$.test_field_name", 100);
375 EXPECT_TRUE(query.ToString().length() == 0);
376 query.LessThanOrEqualTo("", (int64_t) 100);
377 EXPECT_TRUE(query.ToString().length() == 0);
378 query.LessThanOrEqualTo("$.test_field_name^", (int64_t) 100);
379 EXPECT_TRUE(query.ToString().length() == 0);
380 query.LessThanOrEqualTo("", 1.23);
381 EXPECT_TRUE(query.ToString().length() == 0);
382 query.LessThanOrEqualTo("^", 1.23);
383 EXPECT_TRUE(query.ToString().length() == 0);
384 query.LessThanOrEqualTo("", "test value");
385 EXPECT_TRUE(query.ToString().length() == 0);
386 query.LessThanOrEqualTo("678678^", "test value");
387 EXPECT_TRUE(query.ToString().length() == 0);
388 }
389
390 /**
391 * @tc.name: DataQuery
392 * @tc.desc: the predicate is lessThanOrEqualTo, the field is valid.
393 * @tc.type: FUNC
394 * @tc.require:
395 * @tc.author: zuojiangjiang
396 */
397 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLessThanOrEqualToValidField, TestSize.Level1)
398 {
399 DataQuery query;
400 query.LessThanOrEqualTo("$.test_field_name", 100);
401 EXPECT_TRUE(query.ToString().length() > 0);
402 query.Reset();
403 query.LessThanOrEqualTo("$.test_field_name", (int64_t) 100);
404 EXPECT_TRUE(query.ToString().length() > 0);
405 query.Reset();
406 query.LessThanOrEqualTo("$.test_field_name", 1.23);
407 EXPECT_TRUE(query.ToString().length() > 0);
408 query.Reset();
409 query.LessThanOrEqualTo("$.test_field_name", "test value");
410 EXPECT_TRUE(query.ToString().length() > 0);
411 }
412
413 /**
414 * @tc.name: DataQuery
415 * @tc.desc: the predicate is isNull, the field is invalid.
416 * @tc.type: FUNC
417 * @tc.require:
418 * @tc.author: zuojiangjiang
419 */
420 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryIsNullInvalidField, TestSize.Level1)
421 {
422 DataQuery query;
423 query.IsNull("");
424 EXPECT_TRUE(query.ToString().length() == 0);
425 query.IsNull("$.test^_field_name");
426 EXPECT_TRUE(query.ToString().length() == 0);
427 }
428
429 /**
430 * @tc.name: DataQuery
431 * @tc.desc: the predicate is isNull, the field is valid.
432 * @tc.type: FUNC
433 * @tc.require:
434 * @tc.author: zuojiangjiang
435 */
436 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryIsNullValidField, TestSize.Level1)
437 {
438 DataQuery query;
439 query.IsNull("$.test_field_name");
440 EXPECT_TRUE(query.ToString().length() > 0);
441 }
442
443 /**
444 * @tc.name: DataQuery
445 * @tc.desc: the predicate is in, the field is invalid.
446 * @tc.type: FUNC
447 * @tc.require:
448 * @tc.author: zuojiangjiang
449 */
450 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryInInvalidField, TestSize.Level1)
451 {
452 DataQuery query;
453 std::vector<int> vectInt{ 10, 20, 30 };
454 query.In("", vectInt);
455 EXPECT_TRUE(query.ToString().length() == 0);
456 query.In("^", vectInt);
457 EXPECT_TRUE(query.ToString().length() == 0);
458 std::vector<int64_t> vectLong{ (int64_t) 100, (int64_t) 200, (int64_t) 300 };
459 query.In("", vectLong);
460 EXPECT_TRUE(query.ToString().length() == 0);
461 query.In("$.test_field_name^", vectLong);
462 EXPECT_TRUE(query.ToString().length() == 0);
463 std::vector<double> vectDouble{1.23, 2.23, 3.23};
464 query.In("", vectDouble);
465 EXPECT_TRUE(query.ToString().length() == 0);
466 query.In("$.^test_field_name", vectDouble);
467 EXPECT_TRUE(query.ToString().length() == 0);
468 std::vector<std::string> vectString{ "value 1", "value 2", "value 3" };
469 query.In("", vectString);
470 EXPECT_TRUE(query.ToString().length() == 0);
471 query.In("$.test_field_^name^", vectString);
472 EXPECT_TRUE(query.ToString().length() == 0);
473 }
474
475 /**
476 * @tc.name: DataQuery
477 * @tc.desc: the predicate is in, the field is valid.
478 * @tc.type: FUNC
479 * @tc.require:
480 * @tc.author: zuojiangjiang
481 */
482 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryInValidField, TestSize.Level1)
483 {
484 DataQuery query;
485 std::vector<int> vectInt{ 10, 20, 30 };
486 query.In("$.test_field_name", vectInt);
487 EXPECT_TRUE(query.ToString().length() > 0);
488 query.Reset();
489 std::vector<int64_t> vectLong{ (int64_t) 100, (int64_t) 200, (int64_t) 300 };
490 query.In("$.test_field_name", vectLong);
491 EXPECT_TRUE(query.ToString().length() > 0);
492 query.Reset();
493 std::vector<double> vectDouble{1.23, 2.23, 3.23};
494 query.In("$.test_field_name", vectDouble);
495 EXPECT_TRUE(query.ToString().length() > 0);
496 query.Reset();
497 std::vector<std::string> vectString{ "value 1", "value 2", "value 3" };
498 query.In("$.test_field_name", vectString);
499 EXPECT_TRUE(query.ToString().length() > 0);
500 }
501
502 /**
503 * @tc.name: DataQuery
504 * @tc.desc: the predicate is notIn, the field is invalid.
505 * @tc.type: FUNC
506 * @tc.require:
507 * @tc.author: zuojiangjiang
508 */
509 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryNotInInvalidField, TestSize.Level1)
510 {
511 DataQuery query;
512 std::vector<int> vectInt{ 10, 20, 30 };
513 query.NotIn("", vectInt);
514 EXPECT_TRUE(query.ToString().length() == 0);
515 query.NotIn("$.^", vectInt);
516 EXPECT_TRUE(query.ToString().length() == 0);
517 std::vector<int64_t> vectLong{ (int64_t) 100, (int64_t) 200, (int64_t) 300 };
518 query.NotIn("", vectLong);
519 EXPECT_TRUE(query.ToString().length() == 0);
520 query.NotIn("^^", vectLong);
521 EXPECT_TRUE(query.ToString().length() == 0);
522 std::vector<double> vectDouble{ 1.23, 2.23, 3.23 };
523 query.NotIn("", vectDouble);
524 EXPECT_TRUE(query.ToString().length() == 0);
525 query.NotIn("^$.test_field_name", vectDouble);
526 EXPECT_TRUE(query.ToString().length() == 0);
527 std::vector<std::string> vectString{ "value 1", "value 2", "value 3" };
528 query.NotIn("", vectString);
529 EXPECT_TRUE(query.ToString().length() == 0);
530 query.NotIn("$.^", vectString);
531 EXPECT_TRUE(query.ToString().length() == 0);
532 }
533
534 /**
535 * @tc.name: DataQuery
536 * @tc.desc: the predicate is notIn, the field is valid.
537 * @tc.type: FUNC
538 * @tc.require:
539 * @tc.author: zuojiangjiang
540 */
541 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryNotInValidField, TestSize.Level1)
542 {
543 DataQuery query;
544 std::vector<int> vectInt{ 10, 20, 30 };
545 query.NotIn("$.test_field_name", vectInt);
546 EXPECT_TRUE(query.ToString().length() > 0);
547 query.Reset();
548 std::vector<int64_t> vectLong{ (int64_t) 100, (int64_t) 200, (int64_t) 300 };
549 query.NotIn("$.test_field_name", vectLong);
550 EXPECT_TRUE(query.ToString().length() > 0);
551 query.Reset();
552 std::vector<double> vectDouble{ 1.23, 2.23, 3.23 };
553 query.NotIn("$.test_field_name", vectDouble);
554 EXPECT_TRUE(query.ToString().length() > 0);
555 query.Reset();
556 std::vector<std::string> vectString{ "value 1", "value 2", "value 3" };
557 query.NotIn("$.test_field_name", vectString);
558 EXPECT_TRUE(query.ToString().length() > 0);
559 }
560
561 /**
562 * @tc.name: DataQuery
563 * @tc.desc: the predicate is like, the field is invalid.
564 * @tc.type: FUNC
565 * @tc.require:
566 * @tc.author: zuojiangjiang
567 */
568 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLikeInvalidField, TestSize.Level1)
569 {
570 DataQuery query;
571 query.Like("", "test value");
572 EXPECT_TRUE(query.ToString().length() == 0);
573 query.Like("$.test_fi^eld_name", "test value");
574 EXPECT_TRUE(query.ToString().length() == 0);
575 }
576
577 /**
578 * @tc.name: DataQuery
579 * @tc.desc: the predicate is like, the field is valid.
580 * @tc.type: FUNC
581 * @tc.require:
582 * @tc.author: zuojiangjiang
583 */
584 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLikeValidField, TestSize.Level1)
585 {
586 DataQuery query;
587 query.Like("$.test_field_name", "test value");
588 EXPECT_TRUE(query.ToString().length() > 0);
589 }
590
591 /**
592 * @tc.name: DataQuery
593 * @tc.desc: the predicate is unlike, the field is invalid.
594 * @tc.type: FUNC
595 * @tc.require:
596 * @tc.author: zuojiangjiang
597 */
598 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryUnlikeInvalidField, TestSize.Level1)
599 {
600 DataQuery query;
601 query.Unlike("", "test value");
602 EXPECT_TRUE(query.ToString().length() == 0);
603 query.Unlike("$.^", "test value");
604 EXPECT_TRUE(query.ToString().length() == 0);
605 }
606
607 /**
608 * @tc.name: DataQuery
609 * @tc.desc: the predicate is unlike, the field is valid.
610 * @tc.type: FUNC
611 * @tc.require:
612 * @tc.author: zuojiangjiang
613 */
614 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryUnlikeValidField, TestSize.Level1)
615 {
616 DataQuery query;
617 query.Unlike("$.test_field_name", "test value");
618 EXPECT_TRUE(query.ToString().length() > 0);
619 }
620
621 /**
622 * @tc.name: DataQuery
623 * @tc.desc: the predicate is and
624 * @tc.type: FUNC
625 * @tc.require:
626 * @tc.author: zuojiangjiang
627 */
628 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryAnd, TestSize.Level1)
629 {
630 DataQuery query;
631 query.Like("$.test_field_name1", "test value1");
632 query.And();
633 query.Like("$.test_field_name2", "test value2");
634 EXPECT_TRUE(query.ToString().length() > 0);
635 }
636
637 /**
638 * @tc.name: DataQuery
639 * @tc.desc: the predicate is or
640 * @tc.type: FUNC
641 * @tc.require:
642 * @tc.author: zuojiangjiang
643 */
644 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryOr, TestSize.Level1)
645 {
646 DataQuery query;
647 query.Like("$.test_field_name1", "test value1");
648 query.Or();
649 query.Like("$.test_field_name2", "test value2");
650 EXPECT_TRUE(query.ToString().length() > 0);
651 }
652
653 /**
654 * @tc.name: DataQuery
655 * @tc.desc: the predicate is orderByAsc, the field is invalid.
656 * @tc.type: FUNC
657 * @tc.require:
658 * @tc.author: zuojiangjiang
659 */
660 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryOrderByAscInvalidField, TestSize.Level1)
661 {
662 DataQuery query;
663 query.OrderByAsc("");
664 EXPECT_TRUE(query.ToString().length() == 0);
665 query.OrderByAsc("$.^");
666 EXPECT_TRUE(query.ToString().length() == 0);
667 }
668
669 /**
670 * @tc.name: DataQuery
671 * @tc.desc: the predicate is orderByAsc, the field is valid.
672 * @tc.type: FUNC
673 * @tc.require:
674 * @tc.author: zuojiangjiang
675 */
676 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryOrderByAscValidField, TestSize.Level1)
677 {
678 DataQuery query;
679 query.OrderByAsc("$.test_field_name1");
680 EXPECT_TRUE(query.ToString().length() > 0);
681 }
682
683 /**
684 * @tc.name: DataQuery
685 * @tc.desc: the predicate is orderByDesc, the field is invalid.
686 * @tc.type: FUNC
687 * @tc.require:
688 * @tc.author: zuojiangjiang
689 */
690 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryOrderByDescInvalidField, TestSize.Level1)
691 {
692 DataQuery query;
693 query.OrderByDesc("");
694 EXPECT_TRUE(query.ToString().length() == 0);
695 query.OrderByDesc("$.test^_field_name1");
696 EXPECT_TRUE(query.ToString().length() == 0);
697 }
698
699 /**
700 * @tc.name: DataQuery
701 * @tc.desc: the predicate is orderByDesc, the field is valid.
702 * @tc.type: FUNC
703 * @tc.require:
704 * @tc.author: zuojiangjiang
705 */
706 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryOrderByDescValidField, TestSize.Level1)
707 {
708 DataQuery query;
709 query.OrderByDesc("$.test_field_name1");
710 EXPECT_TRUE(query.ToString().length() > 0);
711 }
712
713 /**
714 * @tc.name: DataQuery
715 * @tc.desc: the predicate is limit, the field is invalid.
716 * @tc.type: FUNC
717 * @tc.require:
718 * @tc.author: zuojiangjiang
719 */
720 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLimitInvalidField, TestSize.Level1)
721 {
722 DataQuery query;
723 query.Limit(INVALID_NUMBER, 100);
724 EXPECT_TRUE(query.ToString().length() == 0);
725 query.Limit(10, INVALID_NUMBER);
726 EXPECT_TRUE(query.ToString().length() == 0);
727 }
728
729 /**
730 * @tc.name: DataQuery
731 * @tc.desc: the predicate is limit, the field is valid.
732 * @tc.type: FUNC
733 * @tc.require:
734 * @tc.author: zuojiangjiang
735 */
736 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryLimitValidField, TestSize.Level1)
737 {
738 DataQuery query;
739 query.Limit(10, 100);
740 EXPECT_TRUE(query.ToString().length() > 0);
741 }
742
743 /**
744 * @tc.name: DataQuery
745 * @tc.desc: query single kvStore by dataQuery, the predicate is notEqualTo
746 * @tc.type: FUNC
747 * @tc.require:
748 * @tc.author: zuojiangjiang
749 */
750 HWTEST_F(SingleKvStoreClientQueryTest, SingleKvStoreQueryNotEqualTo, TestSize.Level1)
751 {
752 DistributedKvDataManager manager;
753 Options options = { .createIfMissing = true, .encrypt = true, .autoSync = true,
754 .kvStoreType = KvStoreType::SINGLE_VERSION, .schema = VALID_SCHEMA_STRICT_DEFINE };
755 options.area = EL1;
756 options.securityLevel = S1;
757 options.baseDir = "/data/service/el1/public/database/SingleKvStoreClientQueryTest";
758 AppId appId = { "SingleKvStoreClientQueryTest" };
759 StoreId storeId = { "SingleKvStoreClientQueryTestStoreId1" };
760 statusGetKvStore = manager.GetSingleKvStore(options, appId, storeId, singleKvStore);
761 EXPECT_NE(singleKvStore, nullptr) << "kvStorePtr is null.";
762 singleKvStore->Put("test_key_1", "{\"name\":1}");
763 singleKvStore->Put("test_key_2", "{\"name\":2}");
764 singleKvStore->Put("test_key_3", "{\"name\":3}");
765
766 DataQuery query;
767 query.NotEqualTo("$.name", 3);
768 std::vector<Entry> results;
769 Status status1 = singleKvStore->GetEntries(query, results);
770 ASSERT_EQ(status1, Status::SUCCESS);
771 EXPECT_TRUE(results.size() == 2);
772 results.clear();
773 Status status2 = singleKvStore->GetEntries(query, results);
774 ASSERT_EQ(status2, Status::SUCCESS);
775 EXPECT_TRUE(results.size() == 2);
776
777 std::shared_ptr<KvStoreResultSet> resultSet;
778 Status status3 = singleKvStore->GetResultSet(query, resultSet);
779 ASSERT_EQ(status3, Status::SUCCESS);
780 EXPECT_TRUE(resultSet->GetCount() == 2);
781 auto closeResultSetStatus = singleKvStore->CloseResultSet(resultSet);
782 ASSERT_EQ(closeResultSetStatus, Status::SUCCESS);
783 Status status4 = singleKvStore->GetResultSet(query, resultSet);
784 ASSERT_EQ(status4, Status::SUCCESS);
785 EXPECT_TRUE(resultSet->GetCount() == 2);
786
787 closeResultSetStatus = singleKvStore->CloseResultSet(resultSet);
788 ASSERT_EQ(closeResultSetStatus, Status::SUCCESS);
789
790 int resultSize1;
791 Status status5 = singleKvStore->GetCount(query, resultSize1);
792 ASSERT_EQ(status5, Status::SUCCESS);
793 EXPECT_TRUE(resultSize1 == 2);
794 int resultSize2;
795 Status status6 = singleKvStore->GetCount(query, resultSize2);
796 ASSERT_EQ(status6, Status::SUCCESS);
797 EXPECT_TRUE(resultSize2 == 2);
798
799 singleKvStore->Delete("test_key_1");
800 singleKvStore->Delete("test_key_2");
801 singleKvStore->Delete("test_key_3");
802 Status status = manager.CloseAllKvStore(appId);
803 EXPECT_EQ(status, Status::SUCCESS);
804 status = manager.DeleteAllKvStore(appId, options.baseDir);
805 EXPECT_EQ(status, Status::SUCCESS);
806 }
807
808 /**
809 * @tc.name: DataQuery
810 * @tc.desc: query single kvStore by dataQuery, the predicate is notEqualTo and equalTo
811 * @tc.type: FUNC
812 * @tc.require:
813 * @tc.author: zuojiangjiang
814 */
815 HWTEST_F(SingleKvStoreClientQueryTest, SingleKvStoreQueryNotEqualToAndEqualTo, TestSize.Level1)
816 {
817 DistributedKvDataManager manager;
818 Options options = { .createIfMissing = true, .encrypt = true, .autoSync = true,
819 .kvStoreType = KvStoreType::SINGLE_VERSION, .schema = VALID_SCHEMA_STRICT_DEFINE };
820 options.area = EL1;
821 options.securityLevel = S1;
822 options.baseDir = "/data/service/el1/public/database/SingleKvStoreClientQueryTest";
823 AppId appId = { "SingleKvStoreClientQueryTest" };
824 StoreId storeId = { "SingleKvStoreClientQueryTestStoreId2" };
825 statusGetKvStore = manager.GetSingleKvStore(options, appId, storeId, singleKvStore);
826 EXPECT_NE(singleKvStore, nullptr) << "kvStorePtr is null.";
827 singleKvStore->Put("test_key_1", "{\"name\":1}");
828 singleKvStore->Put("test_key_2", "{\"name\":2}");
829 singleKvStore->Put("test_key_3", "{\"name\":3}");
830
831 DataQuery query;
832 query.NotEqualTo("$.name", 3);
833 query.And();
834 query.EqualTo("$.name", 1);
835 std::vector<Entry> results1;
836 Status status1 = singleKvStore->GetEntries(query, results1);
837 ASSERT_EQ(status1, Status::SUCCESS);
838 EXPECT_TRUE(results1.size() == 1);
839 std::vector<Entry> results2;
840 Status status2 = singleKvStore->GetEntries(query, results2);
841 ASSERT_EQ(status2, Status::SUCCESS);
842 EXPECT_TRUE(results2.size() == 1);
843
844 std::shared_ptr<KvStoreResultSet> resultSet;
845 Status status3 = singleKvStore->GetResultSet(query, resultSet);
846 ASSERT_EQ(status3, Status::SUCCESS);
847 EXPECT_TRUE(resultSet->GetCount() == 1);
848 auto closeResultSetStatus = singleKvStore->CloseResultSet(resultSet);
849 ASSERT_EQ(closeResultSetStatus, Status::SUCCESS);
850 Status status4 = singleKvStore->GetResultSet(query, resultSet);
851 ASSERT_EQ(status4, Status::SUCCESS);
852 EXPECT_TRUE(resultSet->GetCount() == 1);
853
854 closeResultSetStatus = singleKvStore->CloseResultSet(resultSet);
855 ASSERT_EQ(closeResultSetStatus, Status::SUCCESS);
856
857 int resultSize1;
858 Status status5 = singleKvStore->GetCount(query, resultSize1);
859 ASSERT_EQ(status5, Status::SUCCESS);
860 EXPECT_TRUE(resultSize1 == 1);
861 int resultSize2;
862 Status status6 = singleKvStore->GetCount(query, resultSize2);
863 ASSERT_EQ(status6, Status::SUCCESS);
864 EXPECT_TRUE(resultSize2 == 1);
865
866 singleKvStore->Delete("test_key_1");
867 singleKvStore->Delete("test_key_2");
868 singleKvStore->Delete("test_key_3");
869 Status status = manager.CloseAllKvStore(appId);
870 EXPECT_EQ(status, Status::SUCCESS);
871 status = manager.DeleteAllKvStore(appId, options.baseDir);
872 EXPECT_EQ(status, Status::SUCCESS);
873 }
874
875 /**
876 * @tc.name: DataQuery
877 * @tc.desc: query group, the predicate is prefix, isNotNull, but field is invalid
878 * @tc.type: FUNC
879 * @tc.require:
880 * @tc.author: zuojiangjiang
881 */
882 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryGroupAbnormal, TestSize.Level1)
883 {
884 DataQuery query;
885 query.KeyPrefix("");
886 EXPECT_TRUE(query.ToString().length() == 0);
887 query.KeyPrefix("prefix^");
888 EXPECT_TRUE(query.ToString().length() == 0);
889 query.Reset();
890 query.BeginGroup();
891 query.IsNotNull("");
892 EXPECT_TRUE(query.ToString().length() > 0);
893 query.IsNotNull("^$.name");
894 EXPECT_TRUE(query.ToString().length() > 0);
895 query.EndGroup();
896 EXPECT_TRUE(query.ToString().length() > 0);
897 }
898
899 /**
900 * @tc.name: DataQuery
901 * @tc.desc: query group, the predicate is prefix, isNotNull.
902 * @tc.type: FUNC
903 * @tc.require:
904 * @tc.author: zuojiangjiang
905 */
906 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryByGroupNormal, TestSize.Level1)
907 {
908 DataQuery query;
909 query.KeyPrefix("prefix");
910 EXPECT_TRUE(query.ToString().length() > 0);
911 query.Reset();
912 query.BeginGroup();
913 query.IsNotNull("$.name");
914 query.EndGroup();
915 EXPECT_TRUE(query.ToString().length() > 0);
916 }
917
918 /**
919 * @tc.name: DataQuery
920 * @tc.desc: the predicate is setSuggestIndex, the field is invalid.
921 * @tc.type: FUNC
922 * @tc.require:
923 * @tc.author: liuwenhui
924 */
925 HWTEST_F(SingleKvStoreClientQueryTest, DataQuerySetSuggestIndexInvalidField, TestSize.Level1)
926 {
927 DataQuery query;
928 query.SetSuggestIndex("");
929 EXPECT_TRUE(query.ToString().length() == 0);
930 query.SetSuggestIndex("test_field^_name");
931 EXPECT_TRUE(query.ToString().length() == 0);
932 }
933
934 /**
935 * @tc.name: DataQuery
936 * @tc.desc: the predicate is setSuggestIndex, the field is valid.
937 * @tc.type: FUNC
938 * @tc.require:
939 * @tc.author: liuwenhui
940 */
941 HWTEST_F(SingleKvStoreClientQueryTest, DataQuerySetSuggestIndexValidField, TestSize.Level1)
942 {
943 DataQuery query;
944 query.SetSuggestIndex("test_field_name");
945 EXPECT_TRUE(query.ToString().length() > 0);
946 }
947
948 /**
949 * @tc.name: DataQuery
950 * @tc.desc: the predicate is inKeys
951 * @tc.type: FUNC
952 * @tc.require:
953 * @tc.author: taoyuxin
954 */
955 HWTEST_F(SingleKvStoreClientQueryTest, DataQuerySetInKeys, TestSize.Level1)
956 {
957 DataQuery query;
958 query.InKeys({});
959 EXPECT_TRUE(query.ToString().length() == 0);
960 query.InKeys({"test_field_name"});
961 EXPECT_TRUE(query.ToString().length() > 0);
962 query.InKeys({"test_field_name_hasKey"});
963 EXPECT_TRUE(query.ToString().length() > 0);
964 query.Reset();
965 std::vector<std::string> keys { "test_field", "", "^test_field", "^", "test_field_name" };
966 query.InKeys(keys);
967 EXPECT_TRUE(query.ToString().length() > 0);
968 }
969
970 /**
971 * @tc.name: DataQuery
972 * @tc.desc:the predicate is deviceId, the field is invalid
973 * @tc.type: FUNC
974 * @tc.require:
975 * @tc.author: zuojiangjiang
976 */
977 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryDeviceIdInvalidField, TestSize.Level1)
978 {
979 DataQuery query;
980 query.DeviceId("");
981 EXPECT_TRUE(query.ToString().length() == 0);
982 query.DeviceId("$$^");
983 EXPECT_TRUE(query.ToString().length() == 0);
984 query.DeviceId("device_id^");
985 EXPECT_TRUE(query.ToString().length() == 0);
986 }
987
988 /**
989 * @tc.name: DataQuery
990 * @tc.desc: the predicate is valid deviceId, the field is valid
991 * @tc.type: FUNC
992 * @tc.require:
993 * @tc.author: zuojiangjiang
994 */
995 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryDeviceIdValidField, TestSize.Level1)
996 {
997 DataQuery query;
998 query.DeviceId("device_id");
999 EXPECT_TRUE(query.ToString().length() > 0);
1000 query.Reset();
1001 std::string deviceId = "";
1002 uint32_t i = 0;
1003 while (i < MAX_QUERY_LENGTH) {
1004 deviceId += "device";
1005 i++;
1006 }
1007 query.DeviceId(deviceId);
1008 EXPECT_TRUE(query.ToString().length() == 0);
1009 }
1010
1011 /**
1012 * @tc.name: DataQuery
1013 * @tc.desc: the predicate is between, the value is invalid.
1014 * @tc.type: FUNC
1015 * @tc.require:
1016 * @tc.author: SQL
1017 */
1018 HWTEST_F(SingleKvStoreClientQueryTest, DataQueryBetweenInvalid, TestSize.Level1)
1019 {
1020 DistributedKvDataManager manager;
1021 Options options = { .createIfMissing = true, .encrypt = true, .autoSync = true,
1022 .kvStoreType = KvStoreType::SINGLE_VERSION, .schema = VALID_SCHEMA_STRICT_DEFINE };
1023 options.area = EL1;
1024 options.securityLevel = S1;
1025 options.baseDir = "/data/service/el1/public/database/SingleKvStoreClientQueryTest";
1026 AppId appId = { "SingleKvStoreClientQueryTest" };
1027 StoreId storeId = { "SingleKvStoreClientQueryTestStoreId3" };
1028 statusGetKvStore = manager.GetSingleKvStore(options, appId, storeId, singleKvStore);
1029 EXPECT_NE(singleKvStore, nullptr) << "kvStorePtr is null.";
1030 singleKvStore->Put("test_key_1", "{\"name\":1}");
1031 singleKvStore->Put("test_key_2", "{\"name\":2}");
1032 singleKvStore->Put("test_key_3", "{\"name\":3}");
1033
1034 DataQuery query;
1035 query.Between({}, {});
1036 std::vector<Entry> results1;
1037 Status status = singleKvStore->GetEntries(query, results1);
1038 EXPECT_EQ(status, NOT_SUPPORT);
1039
1040 singleKvStore->Delete("test_key_1");
1041 singleKvStore->Delete("test_key_2");
1042 singleKvStore->Delete("test_key_3");
1043 status = manager.CloseAllKvStore(appId);
1044 EXPECT_EQ(status, Status::SUCCESS);
1045 status = manager.DeleteAllKvStore(appId, options.baseDir);
1046 EXPECT_EQ(status, Status::SUCCESS);
1047 }
1048 } // namespace
1049