1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <variant>
23
24 #include "gdb_errors.h"
25 #include "db_store_impl.h"
26 #include "edge.h"
27 #include "grd_adapter.h"
28 #include "grd_adapter_manager.h"
29 #include "gdb_helper.h"
30 #include "gdb_store.h"
31 #include "gdb_utils.h"
32 #include "graph_statement.h"
33 #include "graph_connection.h"
34 #include "path.h"
35 #include "result.h"
36 #include "vertex.h"
37
38 using namespace testing::ext;
39 using namespace OHOS::DistributedDataAip;
40 class GdbExecuteTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 void VerifyPersonInfo(const GraphValue &person, const std::string &name, const int32_t &age);
47 void MatchAndVerifyPerson(const std::string &name, const int32_t &age);
48
49 static const std::string databaseName;
50 static const std::string databasePath;
51 static std::shared_ptr<DBStore> store_;
52 static const std::string createGraphGql;
53 static const std::shared_ptr<StoreConfig> databaseConfig;
54 static const std::string pathJsonString;
55 };
56 std::shared_ptr<DBStore> GdbExecuteTest::store_;
57 const std::string GdbExecuteTest::databaseName = "execute_test";
58 const std::string GdbExecuteTest::databasePath = "/data";
59 const std::string GdbExecuteTest::createGraphGql = "CREATE GRAPH test { "
60 "(person:Person {name STRING, age INT, sex BOOL DEFAULT false}),"
61 "(dog:Dog {name STRING, age INT}), "
62 "(person) -[:Friend]-> (person) "
63 "};";
64 const std::string GdbExecuteTest::pathJsonString = R"({
65 "start": {
66 "label": "PERSON",
67 "identity": 1,
68 "properties": {
69 "AGE": 32,
70 "SALARY": 75.35,
71 "NAME": "Alice",
72 "GENDER": "Female",
73 "PHONENUMBERS": false,
74 "EMAILS": null
75 }
76 },
77 "end": {
78 "label": "PERSON",
79 "identity": 2,
80 "properties": {
81 "AGE": 28,
82 "SALARY": 65000,
83 "NAME": "Bob",
84 "GENDER": "Male",
85 "PHONENUMBERS": "123456789",
86 "EMAILS": "bob@example.com"
87 }
88 },
89 "relationship": {
90 "label": "鐩寸郴浜插睘",
91 "identity": 3,
92 "start": 1,
93 "end": 2,
94 "properties": {
95 "NUM": 4,
96 "PINYIN": "zhixiqinshu"
97 }
98 }
99 })";
100
SetUpTestCase()101 void GdbExecuteTest::SetUpTestCase()
102 {
103 if (!IsSupportArkDataDb()) {
104 GTEST_SKIP() << "Current testcase is not compatible from current gdb";
105 return;
106 }
107 int errCode = E_OK;
108 auto config = StoreConfig(databaseName, databasePath);
109 GDBHelper::DeleteDBStore(config);
110
111 GdbExecuteTest::store_ = GDBHelper::GetDBStore(config, errCode);
112 }
113
TearDownTestCase()114 void GdbExecuteTest::TearDownTestCase()
115 {
116 GDBHelper::DeleteDBStore(StoreConfig(databaseName, databasePath));
117 store_ = nullptr;
118 }
119
SetUp()120 void GdbExecuteTest::SetUp()
121 {
122 if (!IsSupportArkDataDb()) {
123 GTEST_SKIP() << "Current testcase is not compatible from current gdb";
124 return;
125 }
126 auto result = store_->ExecuteGql(createGraphGql);
127 }
128
TearDown()129 void GdbExecuteTest::TearDown()
130 {
131 if (store_ != nullptr) {
132 auto result = store_->ExecuteGql("DROP GRAPH test");
133 }
134 }
135
136 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore, TestSize.Level1)
137 {
138 int errCode = E_OK;
139 std::string dbName = "success";
140 std::string dbPath = databasePath;
141 auto config = StoreConfig(dbName, dbPath);
142
143 auto store = GDBHelper::GetDBStore(config, errCode);
144 EXPECT_NE(store, nullptr);
145 EXPECT_EQ(errCode, E_OK);
146 GDBHelper::DeleteDBStore(config);
147 }
148
149 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStoreVector, TestSize.Level1)
150 {
151 int errCode = E_OK;
152 std::string dbName = "success";
153 std::string dbPath = databasePath;
154 auto config = StoreConfig(dbName, dbPath, DBType::DB_VECTOR);
155
156 auto store = GDBHelper::GetDBStore(config, errCode);
157 EXPECT_EQ(store, nullptr);
158 EXPECT_EQ(errCode, E_NOT_SUPPORT);
159 GDBHelper::DeleteDBStore(config);
160 }
161
162 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStoreButt, TestSize.Level1)
163 {
164 int errCode = E_OK;
165 std::string dbName = "success";
166 std::string dbPath = databasePath;
167 auto config = StoreConfig(dbName, dbPath, DBType::DB_BUTT);
168
169 auto store = GDBHelper::GetDBStore(config, errCode);
170 EXPECT_EQ(store, nullptr);
171 EXPECT_EQ(errCode, E_NOT_SUPPORT);
172 GDBHelper::DeleteDBStore(config);
173 }
174
175 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStoreReadConSize, TestSize.Level1)
176 {
177 int errCode = E_OK;
178 std::string dbName = "success";
179 std::string dbPath = databasePath;
180 auto config = StoreConfig(dbName, dbPath);
181 config.SetReadConSize(0);
182
183 auto store = GDBHelper::GetDBStore(config, errCode);
184 EXPECT_NE(store, nullptr);
185 EXPECT_EQ(errCode, E_OK);
186 GDBHelper::DeleteDBStore(config);
187 }
188
189 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStoreReadConSizeMax, TestSize.Level1)
190 {
191 int errCode = E_OK;
192 std::string dbName = "success";
193 std::string dbPath = databasePath;
194 auto config = StoreConfig(dbName, dbPath);
195 config.SetReadConSize(500);
196
197 auto store = GDBHelper::GetDBStore(config, errCode);
198 EXPECT_EQ(store, nullptr);
199 EXPECT_EQ(errCode, E_ARGS_READ_CON_OVERLOAD);
200 GDBHelper::DeleteDBStore(config);
201 }
202
203 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_NameHasdbSuffix, TestSize.Level1)
204 {
205 int errCode = E_OK;
206 std::string dbName = "success.db";
207 std::string dbPath = databasePath;
208 auto config = StoreConfig(dbName, dbPath);
209
210 auto store = GDBHelper::GetDBStore(config, errCode);
211 EXPECT_EQ(store, nullptr);
212 EXPECT_NE(errCode, E_OK);
213 }
214
215 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_NameHasDBSuffix, TestSize.Level1)
216 {
217 int errCode = E_OK;
218 std::string dbName = "success.DB";
219 std::string dbPath = databasePath;
220 auto config = StoreConfig(dbName, dbPath);
221
222 auto store = GDBHelper::GetDBStore(config, errCode);
223 EXPECT_EQ(store, nullptr);
224 EXPECT_NE(errCode, E_OK);
225 }
226
227 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_NameHasSpecialChar, TestSize.Level1)
228 {
229 int errCode = E_OK;
230 std::string dbName = "suc@!#$*(cess.";
231 std::string dbPath = databasePath;
232 auto config = StoreConfig(dbName, dbPath);
233
234 auto store = GDBHelper::GetDBStore(config, errCode);
235 EXPECT_EQ(store, nullptr);
236 EXPECT_NE(errCode, E_OK);
237 }
238
239 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_PathOkEmptyName, TestSize.Level1)
240 {
241 int errCode = E_OK;
242 std::string dbName = "";
243 std::string dbPath = databasePath;
244 auto config = StoreConfig(dbName, dbPath);
245
246 auto store = GDBHelper::GetDBStore(config, errCode);
247 EXPECT_EQ(store, nullptr);
248 EXPECT_NE(errCode, E_OK);
249 }
250
251 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_NotFoundPath, TestSize.Level1)
252 {
253 int errCode = E_OK;
254 std::string dbName = "success";
255 std::string dbPath = "/test/path1/";
256 auto config = StoreConfig(dbName, dbPath);
257
258 auto store = GDBHelper::GetDBStore(config, errCode);
259 EXPECT_EQ(store, nullptr);
260 EXPECT_EQ(errCode, E_GRD_FAILED_FILE_OPERATION);
261 }
262
263 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_PathErrorEmptyName, TestSize.Level1)
264 {
265 int errCode = E_OK;
266 std::string dbName = "";
267 std::string dbPath = "/test/path2";
268 auto config = StoreConfig(dbName, dbPath);
269
270 auto store = GDBHelper::GetDBStore(config, errCode);
271 EXPECT_EQ(store, nullptr);
272 EXPECT_EQ(errCode, E_GRD_INVAILD_NAME_ERR);
273 }
274
275 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_Empty_Path, TestSize.Level1)
276 {
277 int errCode = E_OK;
278 std::string dbName = databaseName;
279 std::string dbPath = "";
280 auto config = StoreConfig(dbName, dbPath);
281
282 auto store = GDBHelper::GetDBStore(config, errCode);
283 EXPECT_EQ(store, nullptr);
284 EXPECT_EQ(errCode, E_GRD_FAILED_FILE_OPERATION);
285 }
286
287 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_Empty_NameAndPath, TestSize.Level1)
288 {
289 int errCode = E_OK;
290 std::string dbName = "";
291 std::string dbPath = "";
292 auto config = StoreConfig(dbName, dbPath);
293
294 auto store = GDBHelper::GetDBStore(config, errCode);
295 EXPECT_EQ(store, nullptr);
296 EXPECT_EQ(errCode, E_GRD_INVAILD_NAME_ERR);
297 }
298
299 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_OK, TestSize.Level1)
300 {
301 int errCode = E_OK;
302 std::string dbName = "execute_test_ok";
303 std::string dbPath = "/data";
304 auto config = StoreConfig(dbName, dbPath);
305
306 auto store = GDBHelper::GetDBStore(config, errCode);
307 EXPECT_NE(store, nullptr);
308 EXPECT_EQ(errCode, E_OK);
309 GDBHelper::DeleteDBStore(config);
310 }
311
312 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_OK_PathRepeat, TestSize.Level1)
313 {
314 int errCode = E_OK;
315 std::string dbName = "execute_test_ok_2";
316 std::string dbPath = "/data";
317 auto config = StoreConfig(dbName, dbPath);
318
319 auto store = GDBHelper::GetDBStore(config, errCode);
320 EXPECT_NE(store, nullptr);
321 EXPECT_EQ(errCode, E_OK);
322 GDBHelper::DeleteDBStore(config);
323 }
324
325 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_LongName, TestSize.Level1)
326 {
327 int errCode = E_OK;
328 std::string dbName = "";
329 for (int32_t i = 0; i < 1000000; i++) {
330 dbName += "A";
331 }
332 std::string dbPath = "/test/path2";
333 auto config = StoreConfig(dbName, dbPath);
334
335 auto store = GDBHelper::GetDBStore(config, errCode);
336 EXPECT_EQ(store, nullptr);
337 EXPECT_EQ(errCode, E_GRD_FAILED_FILE_OPERATION);
338 }
339
340 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_LongNamePathOk, TestSize.Level1)
341 {
342 int errCode = E_OK;
343 std::string dbName = "";
344 for (int32_t i = 0; i < 1000000; i++) {
345 dbName += "A";
346 }
347 std::string dbPath = "/data";
348 auto config = StoreConfig(dbName, dbPath);
349
350 auto store = GDBHelper::GetDBStore(config, errCode);
351 EXPECT_EQ(store, nullptr);
352 EXPECT_EQ(errCode, E_GRD_SEMANTIC_ERROR);
353 }
354
355 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_LongPath, TestSize.Level1)
356 {
357 int errCode = E_OK;
358 std::string dbName = "";
359 for (int32_t i = 0; i < 30; i++) {
360 dbName += "A";
361 }
362 std::string dbPath = "/test/path2";
363 for (int32_t i = 0; i < 3000000; i++) {
364 dbPath += "A";
365 }
366 auto config = StoreConfig(dbName, dbPath);
367
368 auto store = GDBHelper::GetDBStore(config, errCode);
369 EXPECT_EQ(store, nullptr);
370 EXPECT_EQ(errCode, E_GRD_SEMANTIC_ERROR);
371 }
372
373 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_InVaildSecurityLevel, TestSize.Level1)
374 {
375 int errCode = E_OK;
376 std::string dbName = "tttt";
377 std::string dbPath = "/data";
378
379 auto config = StoreConfig(dbName, dbPath);
380 config.SetSecurityLevel(-3);
381
382 auto store = GDBHelper::GetDBStore(config, errCode);
383 EXPECT_EQ(store, nullptr);
384 EXPECT_EQ(errCode, E_INVALID_ARGS);
385 }
386
387 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_InVaildSecurityLevel02, TestSize.Level1)
388 {
389 int errCode = E_OK;
390 std::string dbName = "tttt";
391 std::string dbPath = "/data";
392
393 auto config = StoreConfig(dbName, dbPath);
394 config.SetSecurityLevel(0);
395
396 auto store = GDBHelper::GetDBStore(config, errCode);
397 EXPECT_EQ(store, nullptr);
398 EXPECT_EQ(errCode, E_INVALID_ARGS);
399 }
400
401 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_InVaildSecurityLevel03, TestSize.Level1)
402 {
403 int errCode = E_OK;
404 std::string dbName = "tttt";
405 std::string dbPath = "/data";
406
407 auto config = StoreConfig(dbName, dbPath);
408 config.SetSecurityLevel(500);
409
410 auto store = GDBHelper::GetDBStore(config, errCode);
411 EXPECT_EQ(store, nullptr);
412 EXPECT_EQ(errCode, E_INVALID_ARGS);
413 }
414
415 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_SecurityLevel, TestSize.Level1)
416 {
417 int errCode = E_OK;
418 std::string dbName = "tttt";
419 std::string dbPath = "/data";
420
421 auto config = StoreConfig(dbName, dbPath);
422 config.SetSecurityLevel(3);
423
424 auto store = GDBHelper::GetDBStore(config, errCode);
425 EXPECT_NE(store, nullptr);
426 EXPECT_EQ(errCode, E_OK);
427 }
428
429 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_SecurityLevelLast, TestSize.Level1)
430 {
431 int errCode = E_OK;
432 std::string dbName = "tttt";
433 std::string dbPath = "/data";
434
435 auto config = StoreConfig(dbName, dbPath);
436 config.SetSecurityLevel(SecurityLevel::LAST);
437
438 auto store = GDBHelper::GetDBStore(config, errCode);
439 EXPECT_EQ(store, nullptr);
440 EXPECT_EQ(errCode, E_INVALID_ARGS);
441 store = nullptr;
442 GDBHelper::DeleteDBStore(config);
443 }
444
445 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_GetSecurityLeve, TestSize.Level1)
446 {
447 int errCode = E_OK;
448 std::string dbName = "tttt03";
449 std::string dbPath = "/data";
450 auto config = StoreConfig(dbName, dbPath);
451 config.SetSecurityLevel(SecurityLevel::S2);
452 auto level = config.GetSecurityLevel();
453 EXPECT_EQ(level, SecurityLevel::S2);
454 auto store = GDBHelper::GetDBStore(config, errCode);
455 EXPECT_NE(store, nullptr);
456 EXPECT_EQ(errCode, E_OK);
457
458 store = nullptr;
459 GDBHelper::DeleteDBStore(config);
460
461 config.SetSecurityLevel(SecurityLevel::LAST);
462 level = config.GetSecurityLevel();
463 EXPECT_EQ(level, SecurityLevel::LAST);
464 store = GDBHelper::GetDBStore(config, errCode);
465 EXPECT_EQ(errCode, E_INVALID_ARGS);
466 EXPECT_EQ(store, nullptr);
467 store = nullptr;
468 GDBHelper::DeleteDBStore(config);
469 }
470
471 /**
472 * @tc.name: GdbStore_GetDBStore_SecurityLevel03
473 * @tc.desc: test StoreConfig SetSecurityLevel S2->S1
474 * @tc.type: FUNC
475 */
476 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_SecurityLevel02, TestSize.Level1)
477 {
478 int errCode = E_OK;
479 std::string dbName = "tttt02";
480 std::string dbPath = "/data";
481 auto config = StoreConfig(dbName, dbPath);
482 config.SetSecurityLevel(SecurityLevel::S2);
483 auto store = GDBHelper::GetDBStore(config, errCode);
484 EXPECT_NE(store, nullptr);
485 EXPECT_EQ(errCode, E_OK);
486 store = nullptr;
487
488 auto invalidConfig = config;
489 invalidConfig.SetSecurityLevel(SecurityLevel::S1);
490 store = GDBHelper::GetDBStore(invalidConfig, errCode);
491 EXPECT_EQ(errCode, E_CONFIG_INVALID_CHANGE);
492 EXPECT_EQ(store, nullptr);
493 store = nullptr;
494 GDBHelper::DeleteDBStore(config);
495 }
496
497 /**
498 * @tc.name: GdbStore_GetDBStore_SecurityLevel03
499 * @tc.desc: test StoreConfig SetSecurityLevel S2->S3
500 * @tc.type: FUNC
501 */
502 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_SecurityLevel03, TestSize.Level1)
503 {
504 int errCode = E_OK;
505 std::string dbName = "tttt02";
506 std::string dbPath = "/data";
507 auto config = StoreConfig(dbName, dbPath);
508 config.SetSecurityLevel(SecurityLevel::S2);
509 auto store = GDBHelper::GetDBStore(config, errCode);
510 EXPECT_NE(store, nullptr);
511 EXPECT_EQ(errCode, E_OK);
512 store = nullptr;
513
514 auto invalidConfig = config;
515 invalidConfig.SetSecurityLevel(SecurityLevel::S3);
516 store = GDBHelper::GetDBStore(invalidConfig, errCode);
517 EXPECT_NE(store, nullptr);
518 EXPECT_EQ(errCode, E_OK);
519 store = nullptr;
520 GDBHelper::DeleteDBStore(config);
521 }
522
523 HWTEST_F(GdbExecuteTest, GdbStore_Execute_NoInsertQuery, TestSize.Level1)
524 {
525 ASSERT_NE(store_, nullptr);
526 auto result = store_->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
527 ASSERT_EQ(result.first, E_OK);
528 ASSERT_NE(result.second, nullptr);
529 EXPECT_EQ(result.second->GetAllData().size(), 0);
530 }
531
532 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_AfterClose, TestSize.Level1)
533 {
534 ASSERT_NE(store_, nullptr);
535 int errCode = E_OK;
536 std::string dbName = "ttttclose";
537 std::string dbPath = "/data";
538
539 auto config = StoreConfig(dbName, dbPath);
540 config.SetSecurityLevel(SecurityLevel::S3);
541
542 auto store = GDBHelper::GetDBStore(config, errCode);
543 ASSERT_NE(store, nullptr);
544 EXPECT_EQ(errCode, E_OK);
545 auto ret = store->Close();
546 EXPECT_EQ(E_OK, ret);
547 EXPECT_NE(store, nullptr);
548 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
549 EXPECT_EQ(result.first, E_OK);
550 result = store_->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
551 ASSERT_EQ(result.first, E_OK);
552 ASSERT_NE(result.second, nullptr);
553 EXPECT_EQ(result.second->GetAllData().size(), 1);
554 GDBHelper::DeleteDBStore(config);
555 }
556
557 /**
558 * @tc.name: GdbStore_GetDBStore_AfterDrop
559 * @tc.desc: test GdbStore AfterDrop Insert
560 * @tc.type: FUNC
561 */
562 HWTEST_F(GdbExecuteTest, GdbStore_GetDBStore_AfterDrop, TestSize.Level1)
563 {
564 ASSERT_NE(store_, nullptr);
565 int errCode = E_OK;
566 std::string dbName = "ttttdrop";
567 std::string dbPath = "/data";
568
569 auto config = StoreConfig(dbName, dbPath);
570 config.SetSecurityLevel(SecurityLevel::S3);
571
572 auto store = GDBHelper::GetDBStore(config, errCode);
573 EXPECT_NE(store, nullptr);
574 EXPECT_EQ(errCode, E_OK);
575
576 auto result = store_->ExecuteGql("DROP GRAPH test;");
577 EXPECT_NE(result.second, nullptr);
578 EXPECT_EQ(result.first, E_OK);
579
580 result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
581 EXPECT_EQ(result.first, E_GRD_UNDEFINED_PARAM);
582 result = store_->ExecuteGql(createGraphGql);
583 EXPECT_NE(result.second, nullptr);
584 EXPECT_EQ(result.first, E_OK);
585 GDBHelper::DeleteDBStore(config);
586 }
587
588 /**
589 * @tc.name: GdbStore_Execute_001
590 * @tc.desc: test GdbStore Execute
591 * @tc.type: FUNC
592 */
593 HWTEST_F(GdbExecuteTest, GdbStore_Execute_LongName, TestSize.Level1)
594 {
595 ASSERT_NE(store_, nullptr);
596 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
597 EXPECT_EQ(result.first, E_OK);
598 result = store_->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
599 ASSERT_EQ(result.first, E_OK);
600 ASSERT_NE(result.second, nullptr);
601 EXPECT_EQ(result.second->GetAllData().size(), 1);
602 std::string name = "name2";
603 for (int32_t i = 0; i < 300; i++) {
604 name += "A";
605 }
606 result = store_->ExecuteGql("INSERT (:Person {name: '" + name + "', age: 11});");
607 ASSERT_EQ(result.first, E_OK);
608 result = store_->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
609 ASSERT_EQ(result.first, E_OK);
610 ASSERT_NE(result.second, nullptr);
611 EXPECT_EQ(result.second->GetAllData().size(), 2);
612
613 std::string name2 = "name2";
614 for (int32_t i = 0; i < 3000000; i++) {
615 name2 += "A";
616 }
617 result = store_->ExecuteGql("INSERT (:Person {name: '" + name2 + "', age: 11});");
618 ASSERT_EQ(result.first, E_INVALID_ARGS);
619 result = store_->QueryGql("MATCH (person:Person {age: 11}) RETURN person;");
620 ASSERT_EQ(result.first, E_OK);
621 ASSERT_NE(result.second, nullptr);
622 EXPECT_EQ(result.second->GetAllData().size(), 2);
623 }
624
625 /**
626 * @tc.name: GdbStore_Execute_001
627 * @tc.desc: test GdbStore Execute
628 * @tc.type: FUNC
629 */
630 HWTEST_F(GdbExecuteTest, GdbStore_Execute_001, TestSize.Level1)
631 {
632 ASSERT_NE(store_, nullptr);
633 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
634 EXPECT_EQ(result.first, E_OK);
635 MatchAndVerifyPerson("name_1", 11);
636 result = store_->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
637 EXPECT_EQ(result.first, E_OK);
638 MatchAndVerifyPerson("name_2", 22);
639 result = store_->ExecuteGql("INSERT (:Person {name: 'name_3', age: 33});");
640 EXPECT_EQ(result.first, E_OK);
641 MatchAndVerifyPerson("name_3", 33);
642 result = store_->ExecuteGql(
643 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_2'}) INSERT (p1)-[:Friend]->(p2);");
644 EXPECT_EQ(result.first, E_OK);
645 result = store_->ExecuteGql(
646 "MATCH (p1:Person {name: 'name_2'}), (p2:Person {name: 'name_3'}) INSERT (p1)-[:Friend]->(p2);");
647 EXPECT_EQ(result.first, E_OK);
648
649 result = store_->QueryGql("MATCH (person:Person)-[relation:Friend]->() RETURN person, relation;");
650 ASSERT_EQ(result.first, E_OK);
651 EXPECT_EQ(result.second->GetAllData().size(), 2);
652 auto data = result.second->GetAllData();
653 GraphValue person = data[0]["person"];
654 VerifyPersonInfo(person, "name_1", 11);
655
656 auto result2 =
657 store_->QueryGql("MATCH path=(a:Person {name: 'name_1'})-[]->{2, 2}(b:Person {name: 'name_3'}) RETURN path;");
658 ASSERT_EQ(result2.first, E_OK);
659 EXPECT_EQ(result2.second->GetAllData().size(), 1);
660
661 GraphValue path = result2.second->GetAllData()[0]["path"];
662 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Path>>(path));
663
664 auto pathPath = std::get<std::shared_ptr<Path>>(path);
665 EXPECT_EQ(pathPath->GetPathLength(), 2);
666 }
667
668 /**
669 * @tc.name: GdbStore_Execute_001
670 * @tc.desc: test GdbStore Execute
671 * @tc.type: FUNC
672 */
673 HWTEST_F(GdbExecuteTest, GdbStore_Execute_002, TestSize.Level1)
674 {
675 ASSERT_NE(store_, nullptr);
676 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
677 EXPECT_EQ(result.first, E_OK);
678 result = store_->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
679 EXPECT_EQ(result.first, E_OK);
680 result = store_->ExecuteGql("INSERT (:Person {name: 'name_3', age: 33});");
681 EXPECT_EQ(result.first, E_OK);
682 result = store_->ExecuteGql(
683 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_2'}) INSERT (p1)-[:Friend]->(p2);");
684 EXPECT_EQ(result.first, E_OK);
685 result = store_->ExecuteGql(
686 "MATCH (p1:Person {name: 'name_2'}), (p2:Person {name: 'name_3'}) INSERT (p1)-[:Friend]->(p2);");
687 EXPECT_EQ(result.first, E_OK);
688 result = store_->ExecuteGql(
689 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_3'}) INSERT (p1)-[:Friend]->(p2);");
690 EXPECT_EQ(result.first, E_OK);
691
692 result = store_->QueryGql(
693 "MATCH (person:Person {name: 'name_1'})-[relation:Friend]->(d) where d.age > 25 RETURN person, relation;");
694 ASSERT_EQ(result.first, E_OK);
695 EXPECT_EQ(result.second->GetAllData().size(), 1);
696 GraphValue person = result.second->GetAllData()[0]["person"];
697 VerifyPersonInfo(person, "name_1", 11);
698
699 GraphValue relation = result.second->GetAllData()[0]["relation"];
700 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Edge>>(relation));
701 auto relationGraphEdge = std::get<std::shared_ptr<Edge>>(relation);
702 EXPECT_EQ(relationGraphEdge->GetLabel(), "Friend");
703 }
704
MatchAndVerifyPerson(const std::string & name,const int32_t & age)705 void GdbExecuteTest::MatchAndVerifyPerson(const std::string &name, const int32_t &age)
706 {
707 ASSERT_NE(store_, nullptr);
708 auto gql = "MATCH (person:Person {name: '" + name + "'}) RETURN person;";
709 auto result = store_->QueryGql(gql);
710 ASSERT_EQ(result.first, E_OK);
711 ASSERT_NE(result.second, nullptr);
712 EXPECT_EQ(result.second->GetAllData().size(), 1);
713 GraphValue person = result.second->GetAllData()[0]["person"];
714 VerifyPersonInfo(person, name, age);
715 }
716
VerifyPersonInfo(const GraphValue & person,const std::string & name,const int32_t & age)717 void GdbExecuteTest::VerifyPersonInfo(const GraphValue &person, const std::string &name, const int32_t &age)
718 {
719 auto expectSize = 3;
720 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
721 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
722 EXPECT_EQ(personVertex->GetLabel(), "Person");
723 ASSERT_EQ(personVertex->GetProperties().size(), expectSize);
724
725 auto nameDb = personVertex->GetProperties().find("name");
726 ASSERT_NE(nameDb, personVertex->GetProperties().end());
727 ASSERT_TRUE(std::holds_alternative<std::string>(nameDb->second));
728 EXPECT_EQ(std::get<std::string>(nameDb->second), name);
729
730 auto ageDb = personVertex->GetProperties().find("age");
731 ASSERT_NE(ageDb, personVertex->GetProperties().end());
732 ASSERT_TRUE(std::holds_alternative<int64_t>(ageDb->second));
733 EXPECT_EQ(std::get<int64_t>(ageDb->second), age);
734
735 auto sex = personVertex->GetProperties().find("sex");
736 ASSERT_NE(sex, personVertex->GetProperties().end());
737 ASSERT_TRUE(std::holds_alternative<int64_t>(sex->second));
738 EXPECT_EQ(std::get<int64_t>(sex->second), 0);
739 }
740
741 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Updata, TestSize.Level1)
742 {
743 ASSERT_NE(store_, nullptr);
744 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
745 EXPECT_EQ(result.first, E_OK);
746 MatchAndVerifyPerson("name_1", 11);
747
748 result = store_->ExecuteGql("MATCH (p1:Person {name: 'name_1'}) SET p1.name = 'name_1_modify';");
749 EXPECT_EQ(result.first, E_OK);
750 MatchAndVerifyPerson("name_1_modify", 11);
751
752 result = store_->ExecuteGql(
753 "MATCH (p1:Person {name: 'name_1_modify'}) SET p1.name = 'name_1_modify2', p1.age=100 + 11;");
754 EXPECT_EQ(result.first, E_OK);
755 MatchAndVerifyPerson("name_1_modify2", 111);
756
757 result = store_->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
758 EXPECT_EQ(result.first, E_OK);
759 MatchAndVerifyPerson("name_2", 22);
760 result = store_->ExecuteGql("MATCH (p2:Person {name: 'name_2'}) SET p2 = {name: 'name_2_modify_all', age: 99};");
761 EXPECT_EQ(result.first, E_OK);
762 MatchAndVerifyPerson("name_2_modify_all", 99);
763
764 result = store_->ExecuteGql("MATCH (p1:Person {name: 'name_1_modify2'}), (p2:Person {name: 'name_2_modify_all'}) "
765 "INSERT (p1)-[:Friend]->(p2);");
766 EXPECT_EQ(result.first, E_OK);
767 result =
768 store_->ExecuteGql("MATCH (n:Person {name: 'name_1_modify2'})-[r:Friend]->(m:Person ) SET m.name = 'name_3';");
769 MatchAndVerifyPerson("name_3", 99);
770 EXPECT_EQ(result.first, E_OK);
771 }
772
773 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UpdataNull, TestSize.Level1)
774 {
775 ASSERT_NE(store_, nullptr);
776 auto result = store_->ExecuteGql("INSERT (:Person {name: 'hahaha', age: 987});");
777 EXPECT_EQ(result.first, E_OK);
778 MatchAndVerifyPerson("hahaha", 987);
779 // update name = null
780 result = store_->ExecuteGql("MATCH (p1:Person {name: 'hahaha'}) SET p1 = {age: 666};");
781 EXPECT_EQ(result.first, E_OK);
782 result = store_->QueryGql("MATCH (person:Person {age: 666}) RETURN person;");
783 ASSERT_EQ(result.first, E_OK);
784 ASSERT_NE(result.second, nullptr);
785 EXPECT_EQ(result.second->GetAllData().size(), 1);
786 GraphValue person = result.second->GetAllData()[0]["person"];
787 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
788 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
789 EXPECT_EQ(personVertex->GetLabel(), "Person");
790 ASSERT_EQ(personVertex->GetProperties().size(), 2);
791
792 auto nameDb = personVertex->GetProperties().find("name");
793 ASSERT_EQ(nameDb, personVertex->GetProperties().end());
794
795 auto ageDb = personVertex->GetProperties().find("age");
796 ASSERT_NE(ageDb, personVertex->GetProperties().end());
797 ASSERT_TRUE(std::holds_alternative<int64_t>(ageDb->second));
798 EXPECT_EQ(std::get<int64_t>(ageDb->second), 666);
799 }
800
801 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UpdataNull02, TestSize.Level1)
802 {
803 ASSERT_NE(store_, nullptr);
804 auto result = store_->ExecuteGql("INSERT (:Person {name: 'hahaha', age: 987});");
805 EXPECT_EQ(result.first, E_OK);
806 MatchAndVerifyPerson("hahaha", 987);
807 // update name = null
808 result = store_->ExecuteGql("MATCH (p1:Person {name: 'hahaha'}) SET p1.age = 666, p1.name = null;");
809 EXPECT_EQ(result.first, E_OK);
810 result = store_->QueryGql("MATCH (person:Person {age: 666}) RETURN person;");
811 ASSERT_EQ(result.first, E_OK);
812 ASSERT_NE(result.second, nullptr);
813 EXPECT_EQ(result.second->GetAllData().size(), 1);
814 GraphValue person = result.second->GetAllData()[0]["person"];
815 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
816 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
817 EXPECT_EQ(personVertex->GetLabel(), "Person");
818 ASSERT_EQ(personVertex->GetProperties().size(), 2);
819
820 auto nameDb = personVertex->GetProperties().find("name");
821 ASSERT_EQ(nameDb, personVertex->GetProperties().end());
822
823 auto ageDb = personVertex->GetProperties().find("age");
824 ASSERT_NE(ageDb, personVertex->GetProperties().end());
825 ASSERT_TRUE(std::holds_alternative<int64_t>(ageDb->second));
826 EXPECT_EQ(std::get<int64_t>(ageDb->second), 666);
827 }
828
829 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Updata_MatchFail, TestSize.Level1)
830 {
831 ASSERT_NE(store_, nullptr);
832 auto result = store_->ExecuteGql("INSERT (:Person {name: 'zhangsan001', age: 10});");
833 EXPECT_EQ(result.first, E_OK);
834 MatchAndVerifyPerson("zhangsan001", 10);
835
836 result = store_->ExecuteGql("MATCH (p1:Person {name: 'notFound'}) SET p1.name = 'name_1_modify';");
837 EXPECT_EQ(result.first, E_OK);
838 }
839
840 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Updata_AgeError, TestSize.Level1)
841 {
842 ASSERT_NE(store_, nullptr);
843
844 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_4', age: 44});");
845 EXPECT_EQ(result.first, E_OK);
846 MatchAndVerifyPerson("name_4", 44);
847
848 result = store_->ExecuteGql("MATCH (p1:Person {name: 'name_4'}) SET p1.age = 'string_age';");
849 EXPECT_EQ(result.first, E_GRD_SEMANTIC_ERROR);
850 // update failed, no change
851 MatchAndVerifyPerson("name_4", 44);
852 }
853
854 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Updata_ColumnError, TestSize.Level1)
855 {
856 ASSERT_NE(store_, nullptr);
857
858 auto result = store_->ExecuteGql("INSERT (:PersonErr {name: true, age: 44});");
859 EXPECT_EQ(result.first, E_GRD_UNDEFINED_PARAM);
860
861 auto gql = "MATCH (person:Person) RETURN person;";
862 result = store_->QueryGql(gql);
863 ASSERT_EQ(result.first, E_OK);
864 EXPECT_EQ(result.second->GetAllData().size(), 0);
865
866 result = store_->ExecuteGql("INSERT (:Person {name: true, age: 44});");
867 EXPECT_EQ(result.first, E_GRD_SEMANTIC_ERROR);
868 gql = "MATCH (person:Person) RETURN person;";
869 result = store_->QueryGql(gql);
870 ASSERT_EQ(result.first, E_OK);
871 EXPECT_EQ(result.second->GetAllData().size(), 0);
872
873 result = store_->ExecuteGql("INSERT (:Person {name: 'zhangsan', age: 'error'});");
874 EXPECT_EQ(result.first, E_GRD_SEMANTIC_ERROR);
875 gql = "MATCH (person:Person) RETURN person;";
876 result = store_->QueryGql(gql);
877 ASSERT_EQ(result.first, E_OK);
878 EXPECT_EQ(result.second->GetAllData().size(), 0);
879 }
880
881 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Delete, TestSize.Level1)
882 {
883 ASSERT_NE(store_, nullptr);
884
885 auto result = store_->ExecuteGql("INSERT (:Person {name: 'zhangsan_delete', age: 10});");
886 EXPECT_EQ(result.first, E_OK);
887 MatchAndVerifyPerson("zhangsan_delete", 10);
888
889 result = store_->ExecuteGql("MATCH (p:Person {name: 'zhangsan_delete'}) DETACH DELETE p;");
890 EXPECT_EQ(result.first, E_OK);
891 auto gql = "MATCH (person:Person {name: 'zhangsan_delete'}) RETURN person;";
892 result = store_->QueryGql(gql);
893 ASSERT_EQ(result.first, E_OK);
894 EXPECT_EQ(result.second->GetAllData().size(), 0);
895 // Double Delete
896 result = store_->ExecuteGql("MATCH (p:Person {name: 'zhangsan_delete'}) DETACH DELETE p;");
897 EXPECT_EQ(result.first, E_OK);
898 gql = "MATCH (person:Person {name: 'zhangsan_delete'}) RETURN person;";
899 result = store_->QueryGql(gql);
900 ASSERT_EQ(result.first, E_OK);
901 EXPECT_EQ(result.second->GetAllData().size(), 0);
902 }
903
904 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Delete_NotFound, TestSize.Level1)
905 {
906 ASSERT_NE(store_, nullptr);
907 auto result = store_->ExecuteGql("INSERT (:Person {name: 'zhangsan_delete', age: 10});");
908 EXPECT_EQ(result.first, E_OK);
909 MatchAndVerifyPerson("zhangsan_delete", 10);
910
911 result = store_->ExecuteGql("MATCH (p:Person {name: 'notFound'}) DETACH DELETE p;");
912 EXPECT_EQ(result.first, E_OK);
913 MatchAndVerifyPerson("zhangsan_delete", 10);
914 }
915
916 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Delete_PError, TestSize.Level1)
917 {
918 ASSERT_NE(store_, nullptr);
919 auto result = store_->ExecuteGql("INSERT (:Person {name: 'delete_error', age: 11});");
920 EXPECT_EQ(result.first, E_OK);
921 MatchAndVerifyPerson("delete_error", 11);
922
923 result = store_->ExecuteGql("MATCH (p:Person {name: 'delete_error'}) DETACH DELETE p_error;");
924 EXPECT_EQ(result.first, E_GRD_UNDEFINED_PARAM);
925 MatchAndVerifyPerson("delete_error", 11);
926 }
927
928 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Delete_Related, TestSize.Level1)
929 {
930 ASSERT_NE(store_, nullptr);
931
932 auto result = store_->ExecuteGql("INSERT (:Person {name: 'delete_1', age: 11});");
933 EXPECT_EQ(result.first, E_OK);
934 MatchAndVerifyPerson("delete_1", 11);
935 result = store_->ExecuteGql("INSERT (:Person {name: 'delete_2', age: 22});");
936 MatchAndVerifyPerson("delete_2", 22);
937 EXPECT_EQ(result.first, E_OK);
938
939 result = store_->ExecuteGql(
940 "MATCH (p1:Person {name: 'delete_1'}), (p2:Person {name: 'delete_2'}) INSERT (p1)-[:Friend]->(p2);");
941 EXPECT_EQ(result.first, E_OK);
942 result =
943 store_->QueryGql("MATCH (person:Person {name: 'delete_1'})-[relation:Friend]->() RETURN person, relation;");
944 ASSERT_EQ(result.first, E_OK);
945 EXPECT_EQ(result.second->GetAllData().size(), 1);
946 GraphValue person = result.second->GetAllData()[0]["person"];
947 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
948 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
949 EXPECT_EQ(personVertex->GetLabel(), "Person");
950 ASSERT_EQ(personVertex->GetProperties().size(), 3);
951 auto name = personVertex->GetProperties().find("name");
952 ASSERT_NE(name, personVertex->GetProperties().end());
953 ASSERT_TRUE(std::holds_alternative<std::string>(name->second));
954 EXPECT_EQ(std::get<std::string>(name->second), "delete_1");
955
956 auto age = personVertex->GetProperties().find("age");
957 ASSERT_NE(age, personVertex->GetProperties().end());
958 ASSERT_TRUE(std::holds_alternative<int64_t>(age->second));
959 EXPECT_EQ(std::get<int64_t>(age->second), 11);
960
961 result = store_->ExecuteGql("MATCH (p:Person)-[:Friend]->(relatedPerson:Person) DETACH DELETE p, relatedPerson;");
962 EXPECT_EQ(result.first, E_OK);
963 result =
964 store_->QueryGql("MATCH (person:Person {name: 'delete_1'})-[relation:Friend]->() RETURN person, relation;");
965 ASSERT_EQ(result.first, E_OK);
966 EXPECT_EQ(result.second->GetAllData().size(), 0);
967 }
968
969 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Delete_Related_Error, TestSize.Level1)
970 {
971 ASSERT_NE(store_, nullptr);
972
973 auto result = store_->ExecuteGql("INSERT (:Person {name: 'delete_3', age: 11});");
974 EXPECT_EQ(result.first, E_OK);
975 MatchAndVerifyPerson("delete_3", 11);
976 result = store_->ExecuteGql("INSERT (:Person {name: 'delete_4', age: 22});");
977 EXPECT_EQ(result.first, E_OK);
978 MatchAndVerifyPerson("delete_4", 22);
979
980 result = store_->ExecuteGql(
981 "MATCH (p1:Person {name: 'delete_3'}), (p2:Person {name: 'delete_4'}) INSERT (p1)-[:Friend]->(p2);");
982 EXPECT_EQ(result.first, E_OK);
983 result =
984 store_->QueryGql("MATCH (person:Person {name: 'delete_3'})-[relation:Friend]->() RETURN person, relation;");
985 ASSERT_EQ(result.first, E_OK);
986 EXPECT_EQ(result.second->GetAllData().size(), 1);
987 GraphValue person = result.second->GetAllData()[0]["person"];
988 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
989 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
990 EXPECT_EQ(personVertex->GetLabel(), "Person");
991 ASSERT_EQ(personVertex->GetProperties().size(), 3);
992 auto name = personVertex->GetProperties().find("name");
993 ASSERT_NE(name, personVertex->GetProperties().end());
994 ASSERT_TRUE(std::holds_alternative<std::string>(name->second));
995 EXPECT_EQ(std::get<std::string>(name->second), "delete_3");
996
997 // No Friend_Error, delete Eror
998 result =
999 store_->ExecuteGql("MATCH (p:Person)-[:Friend_Error]->(relatedPerson:Person) DETACH DELETE p, relatedPerson;");
1000 EXPECT_EQ(result.first, E_GRD_UNDEFINED_PARAM);
1001 // p:Person, but delete p_error
1002 result =
1003 store_->ExecuteGql("MATCH (p:Person)-[:Friend]->(relatedPerson:Person) DETACH DELETE p_error, relatedPerson;");
1004 EXPECT_EQ(result.first, E_GRD_UNDEFINED_PARAM);
1005 //relatedPerson:Person, but delete relatedPerson_error
1006 result =
1007 store_->ExecuteGql("MATCH (p:Person)-[:Friend]->(relatedPerson:Person) DETACH DELETE p, relatedPerson_error;");
1008 EXPECT_EQ(result.first, E_GRD_UNDEFINED_PARAM);
1009
1010 result = store_->ExecuteGql("MATCH (p:Person)-[:Friend]->(relatedPerson:Person) DETACH DELETE relatedPerson, p;");
1011 EXPECT_EQ(result.first, E_OK);
1012 // delete success, data.size == 0
1013 result =
1014 store_->QueryGql("MATCH (person:Person {name: 'delete_3'})-[relation:Friend]->() RETURN person, relation;");
1015 ASSERT_EQ(result.first, E_OK);
1016 EXPECT_EQ(result.second->GetAllData().size(), 0);
1017 }
1018
1019 HWTEST_F(GdbExecuteTest, GdbStore_Execute_QueryGql, TestSize.Level1)
1020 {
1021 ASSERT_NE(store_, nullptr);
1022 auto result = store_->ExecuteGql("INSERT (:Person {name: 'zhangsan_111', age: 11, sex: true});");
1023 EXPECT_EQ(result.first, E_OK);
1024
1025 result = store_->QueryGql("MATCH (person:Person {name: 'zhangsan_111'}) RETURN person;");
1026 EXPECT_EQ(result.first, E_OK);
1027
1028 EXPECT_EQ(result.second->GetAllData().size(), 1);
1029 GraphValue personErr = result.second->GetAllData()[0]["personErr"];
1030 // No personErr
1031 ASSERT_TRUE(std::holds_alternative<std::monostate>(personErr));
1032
1033 GraphValue person = result.second->GetAllData()[0]["person"];
1034 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
1035 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
1036 ASSERT_NE(personVertex->GetLabel(), "error_label");
1037 EXPECT_EQ(personVertex->GetLabel(), "Person");
1038 // size = 4 {name, age, sex, identity}
1039 ASSERT_EQ(personVertex->GetProperties().size(), 3);
1040
1041 auto name = personVertex->GetProperties().find("name");
1042 ASSERT_NE(name, personVertex->GetProperties().end());
1043 ASSERT_TRUE(std::holds_alternative<std::string>(name->second));
1044 EXPECT_EQ(std::get<std::string>(name->second), "zhangsan_111");
1045
1046 auto age = personVertex->GetProperties().find("age");
1047 ASSERT_NE(age, personVertex->GetProperties().end());
1048 ASSERT_TRUE(std::holds_alternative<int64_t>(age->second));
1049 EXPECT_EQ(std::get<int64_t>(age->second), 11);
1050
1051 auto sex = personVertex->GetProperties().find("sex");
1052 ASSERT_NE(sex, personVertex->GetProperties().end());
1053 ASSERT_TRUE(std::holds_alternative<int64_t>(sex->second));
1054 EXPECT_EQ(std::get<int64_t>(sex->second), 1);
1055 // No Propertie is OTHER, equal End
1056 auto other = personVertex->GetProperties().find("OTHER");
1057 EXPECT_EQ(other, personVertex->GetProperties().end());
1058 }
1059
1060 HWTEST_F(GdbExecuteTest, GdbStore_Execute_QueryGql_2, TestSize.Level1)
1061 {
1062 ASSERT_NE(store_, nullptr);
1063
1064 auto result = store_->ExecuteGql("INSERT (:Person {name: 'lisi_1', age: 66});");
1065 EXPECT_EQ(result.first, E_OK);
1066 result = store_->ExecuteGql("INSERT (:Person {name: 'lisi_2', age: 66, sex: true});");
1067 EXPECT_EQ(result.first, E_OK);
1068 result = store_->ExecuteGql("INSERT (:Dog {name: 'xiaohuang1', age: 66});");
1069 EXPECT_EQ(result.first, E_OK);
1070 result = store_->QueryGql("MATCH (person:Person{age: 66}) RETURN person;");
1071 EXPECT_EQ(result.first, E_OK);
1072 EXPECT_EQ(result.second->GetAllData().size(), 2);
1073
1074 GraphValue person = result.second->GetAllData()[0]["person"];
1075 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
1076 auto personVertex = std::get<std::shared_ptr<Vertex>>(person);
1077 EXPECT_EQ(personVertex->GetLabel(), "Person");
1078 ASSERT_EQ(personVertex->GetProperties().size(), 3);
1079 auto name = personVertex->GetProperties().find("name");
1080 ASSERT_NE(name, personVertex->GetProperties().end());
1081 ASSERT_TRUE(std::holds_alternative<std::string>(name->second));
1082 EXPECT_EQ(std::get<std::string>(name->second), "lisi_1");
1083 auto age = personVertex->GetProperties().find("age");
1084 ASSERT_NE(age, personVertex->GetProperties().end());
1085 ASSERT_TRUE(std::holds_alternative<int64_t>(age->second));
1086 EXPECT_EQ(std::get<int64_t>(age->second), 66);
1087 auto sex = personVertex->GetProperties().find("sex");
1088 ASSERT_NE(sex, personVertex->GetProperties().end());
1089 ASSERT_TRUE(std::holds_alternative<int64_t>(age->second));
1090 EXPECT_EQ(std::get<int64_t>(sex->second), 0);
1091
1092 person = result.second->GetAllData()[1]["person"];
1093 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Vertex>>(person));
1094 personVertex = std::get<std::shared_ptr<Vertex>>(person);
1095 EXPECT_EQ(personVertex->GetLabel(), "Person");
1096 ASSERT_EQ(personVertex->GetProperties().size(), 3);
1097 name = personVertex->GetProperties().find("name");
1098 ASSERT_NE(name, personVertex->GetProperties().end());
1099 ASSERT_TRUE(std::holds_alternative<std::string>(name->second));
1100 EXPECT_EQ(std::get<std::string>(name->second), "lisi_2");
1101 age = personVertex->GetProperties().find("age");
1102 ASSERT_NE(age, personVertex->GetProperties().end());
1103 ASSERT_TRUE(std::holds_alternative<int64_t>(age->second));
1104 EXPECT_EQ(std::get<int64_t>(age->second), 66);
1105 sex = personVertex->GetProperties().find("sex");
1106 ASSERT_NE(sex, personVertex->GetProperties().end());
1107 ASSERT_TRUE(std::holds_alternative<int64_t>(age->second));
1108 EXPECT_EQ(std::get<int64_t>(sex->second), 1);
1109 }
1110
1111 HWTEST_F(GdbExecuteTest, GdbStore_Execute_ParseEdge, TestSize.Level1)
1112 {
1113 ASSERT_NE(store_, nullptr);
1114 int errCode = E_ERROR;
1115 // no start, no end
1116 nlohmann::json json = nlohmann::json::parse("{\"name\" : \"zhangsan\"}", nullptr, false);
1117 ASSERT_FALSE(json.is_discarded());
1118 ASSERT_FALSE(json.is_null());
1119 Edge::Parse(json, errCode);
1120 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1121 // no identity
1122 std::string jsonStr = "{\"start\" : 1, \"end\" : 2}";
1123 json = nlohmann::json::parse(jsonStr, nullptr, false);
1124 Edge::Parse(json, errCode);
1125 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1126 // ok
1127 jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":3,"
1128 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1129 json = nlohmann::json::parse(jsonStr, nullptr, false);
1130 Edge::Parse(json, errCode);
1131 EXPECT_EQ(errCode, E_OK);
1132 // start is AA
1133 jsonStr = "{\"start\" : \"AA\", \"end\" : 2, \"label\":\"COMPANY\",\"identity\":3,"
1134 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1135 json = nlohmann::json::parse(jsonStr, nullptr, false);
1136 Edge::Parse(json, errCode);
1137 EXPECT_EQ(errCode, E_OK);
1138 // end is B
1139 jsonStr = "{\"start\" : 1, \"end\" : \"B\", \"label\":\"COMPANY\",\"identity\":3,"
1140 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1141 json = nlohmann::json::parse(jsonStr, nullptr, false);
1142 Edge::Parse(json, errCode);
1143 EXPECT_EQ(errCode, E_OK);
1144 // identity is C
1145 jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":\"C\","
1146 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1147 json = nlohmann::json::parse(jsonStr, nullptr, false);
1148 Edge::Parse(json, errCode);
1149 EXPECT_EQ(errCode, E_OK);
1150 // label is 222
1151 jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":222,'identity':3,\"properties\":{\"NAME\":\"myCompany3\","
1152 "\"FOUNDED\":2011}}";
1153 json = nlohmann::json::parse(jsonStr, nullptr, false);
1154 Edge::Parse(json, errCode);
1155 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1156 // key4 is null
1157 jsonStr =
1158 "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":2,"
1159 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":4.5,\"SEX\":true,\"key1\":true,\"key2\":[], \"key3\":{}, "
1160 "\"key4\": null}}";
1161 json = nlohmann::json::parse(jsonStr, nullptr, false);
1162 Edge::Parse(json, errCode);
1163 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1164 }
1165
1166 HWTEST_F(GdbExecuteTest, GdbStore_Execute_PathSegment, TestSize.Level1)
1167 {
1168 ASSERT_NE(store_, nullptr);
1169 int errCode = E_ERROR;
1170 // NO start and end
1171 nlohmann::json json = nlohmann::json::parse("{\"name\" : \"zhangsan\"}", nullptr, false);
1172 ASSERT_FALSE(json.is_discarded());
1173 ASSERT_FALSE(json.is_null());
1174 PathSegment::Parse(json, errCode);
1175 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1176 // no relationship
1177 std::string jsonStr = "{\"start\" : {}, \"end\" : {}}";
1178 json = nlohmann::json::parse(jsonStr, nullptr, false);
1179 PathSegment::Parse(json, errCode);
1180 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1181
1182 jsonStr = "{\"start\" : {}, \"end\" : {}, \"relationship\":{}, \"label\":\"COMPANY\",\"identity\":3,"
1183 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1184 json = nlohmann::json::parse(jsonStr, nullptr, false);
1185 PathSegment::Parse(json, errCode);
1186 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1187
1188 jsonStr = "{\"start\" : {}, \"end\" : {}, \"relationship\":{}, \"label\":\"COMPANY\",\"identity\":3,"
1189 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1190 json = nlohmann::json::parse(jsonStr, nullptr, false);
1191 PathSegment::Parse(json, errCode);
1192 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1193
1194 jsonStr = "{\"start\" : {}, \"end\" : {}, \"relationship\":{}, \"label\":\"COMPANY\",\"identity\":\"C\","
1195 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1196 json = nlohmann::json::parse(jsonStr, nullptr, false);
1197 PathSegment::Parse(json, errCode);
1198 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1199
1200 jsonStr = "{\"start\" : {}, \"end\" : {}, \"relationship\":{}, \"label\":222,\"identity\":2,"
1201 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1202 json = nlohmann::json::parse(jsonStr, nullptr, false);
1203 PathSegment::Parse(json, errCode);
1204 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1205
1206 jsonStr = "{\"start\" : {}, \"end\" : {}, \"relationship\":{}, \"label\":\"COMPANY\",\"identity\":2,"
1207 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":4.5,\"SEX\":true,\"key1\":true,\"key2\":[], "
1208 "\"key3\":{}, \"key4\": null}}";
1209 json = nlohmann::json::parse(jsonStr, nullptr, false);
1210 PathSegment::Parse(json, errCode);
1211 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1212
1213 json = nlohmann::json::parse(pathJsonString, nullptr, false);
1214 PathSegment::Parse(json, errCode);
1215 ASSERT_EQ(errCode, E_OK);
1216 }
1217
1218 HWTEST_F(GdbExecuteTest, GdbStore_Execute_PathSegment02, TestSize.Level1)
1219 {
1220 ASSERT_NE(store_, nullptr);
1221 int errCode = E_ERROR;
1222 nlohmann::json json = nlohmann::json::parse(pathJsonString, nullptr, false);
1223 PathSegment::Parse(json, errCode);
1224 EXPECT_EQ(errCode, E_OK);
1225 // identity:A, E_OK
1226 std::string jsonStr =
1227 "{\"start\":{\"label\":\"PERSON\",\"identity\":\"A\",\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1228 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1229 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1230 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1231 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":2,\"properties\":{\"NUM\":4,"
1232 "\"PINYIN\":\"zhixiqinshu\"}}}";
1233 json = nlohmann::json::parse(jsonStr, nullptr, false);
1234 PathSegment::Parse(json, errCode);
1235 EXPECT_EQ(errCode, E_OK);
1236
1237 // label:2, E_PARSE_JSON_FAILED
1238 jsonStr =
1239 "{\"start\":{\"label\":2,\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1240 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1241 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1242 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1243 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":2,\"properties\":{\"NUM\":4,"
1244 "\"PINYIN\":\"zhixiqinshu\"}}}";
1245 json = nlohmann::json::parse(jsonStr, nullptr, false);
1246 PathSegment::Parse(json, errCode);
1247 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1248
1249 // relationship->start:B, E_OK
1250 jsonStr =
1251 "{\"start\":{\"label\":\"PERSON\",\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1252 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1253 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1254 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1255 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":\"B\",\"end\":2,\"properties\":{\"NUM\":4,"
1256 "\"PINYIN\":\"zhixiqinshu\"}}}";
1257 json = nlohmann::json::parse(jsonStr, nullptr, false);
1258 PathSegment::Parse(json, errCode);
1259 EXPECT_EQ(errCode, E_OK);
1260
1261 // relationship->end:C, E_OK
1262 jsonStr =
1263 "{\"start\":{\"label\":\"PERSON\",\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1264 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1265 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1266 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1267 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":\"C\",\"properties\":{\"NUM\":4,"
1268 "\"PINYIN\":\"zhixiqinshu\"}}}";
1269 json = nlohmann::json::parse(jsonStr, nullptr, false);
1270 PathSegment::Parse(json, errCode);
1271 ASSERT_EQ(errCode, E_OK);
1272 }
1273
1274 HWTEST_F(GdbExecuteTest, GdbStore_Execute_PathSegment03, TestSize.Level1)
1275 {
1276 ASSERT_NE(store_, nullptr);
1277 int errCode = E_ERROR;
1278 nlohmann::json json = nlohmann::json::parse(pathJsonString, nullptr, false);
1279 PathSegment::Parse(json, errCode);
1280 ASSERT_EQ(errCode, E_OK);
1281 // end no label:PERSON and identity: A
1282 std::string jsonStr =
1283 "{\"start\":{\"label\":\"PERSON\",\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1284 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1285 "\"end\":{\"identity\":\"A\",\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1286 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1287 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":2,\"properties\":{\"NUM\":4,"
1288 "\"PINYIN\":\"zhixiqinshu\"}}}";
1289 json = nlohmann::json::parse(jsonStr, nullptr, false);
1290 PathSegment::Parse(json, errCode);
1291 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1292
1293 jsonStr = "{\"start\" : 1, \"end\" : {}}";
1294 json = nlohmann::json::parse(jsonStr, nullptr, false);
1295 PathSegment::Parse(json, errCode);
1296 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1297
1298 jsonStr = "{\"start\" : {}, \"relationship\" : 1}";
1299 json = nlohmann::json::parse(jsonStr, nullptr, false);
1300 PathSegment::Parse(json, errCode);
1301 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1302
1303 jsonStr = "{\"start\" : {}, \"relationship\" : {}}";
1304 json = nlohmann::json::parse(jsonStr, nullptr, false);
1305 PathSegment::Parse(json, errCode);
1306 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1307
1308 jsonStr = "{\"start\" : {}, \"relationship\" : {}, \"end\" : 1}";
1309 json = nlohmann::json::parse(jsonStr, nullptr, false);
1310 PathSegment::Parse(json, errCode);
1311 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1312
1313 jsonStr = "{\"start\" : {}, \"relationship\" : {}, \"end\" : {}}";
1314 json = nlohmann::json::parse(jsonStr, nullptr, false);
1315 PathSegment::Parse(json, errCode);
1316 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1317 }
1318
1319 HWTEST_F(GdbExecuteTest, GdbStore_Execute_PathSegment04, TestSize.Level1)
1320 {
1321 int errCode = E_ERROR;
1322 nlohmann::json json = nlohmann::json::parse(pathJsonString, nullptr, false);
1323 PathSegment::Parse(json, errCode);
1324 EXPECT_EQ(errCode, E_OK);
1325 // identity:A, E_OK
1326 std::string jsonStr =
1327 "{\"start\":{\"label\":\"PERSON\",\"identity\":{},\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1328 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1329 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1330 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1331 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":2,\"properties\":{\"NUM\":4,"
1332 "\"PINYIN\":\"zhixiqinshu\"}}}";
1333 json = nlohmann::json::parse(jsonStr, nullptr, false);
1334 PathSegment::Parse(json, errCode);
1335 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1336 std::shared_ptr<Vertex> sourceVertex;
1337 std::shared_ptr<Vertex> targetVertex;
1338 std::shared_ptr<Edge> edge;
1339 auto paths = std::make_shared<PathSegment>(sourceVertex, targetVertex, edge);
1340 EXPECT_NE(paths, nullptr);
1341 EXPECT_EQ(sourceVertex, paths->GetSourceVertex());
1342 EXPECT_EQ(edge, paths->GetEdge());
1343 EXPECT_EQ(targetVertex, paths->GetTargetVertex());
1344 jsonStr =
1345 "{\"start\":{\"label\":\"PERSON\",\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1346 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1347 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1348 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1349 "\"relationship\":{}}";
1350 json = nlohmann::json::parse(jsonStr, nullptr, false);
1351 PathSegment::Parse(json, errCode);
1352 ASSERT_EQ(errCode, E_PARSE_JSON_FAILED);
1353 }
1354
1355 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Path01, TestSize.Level1)
1356 {
1357 std::shared_ptr<Vertex> start = std::make_shared<Vertex>();
1358 std::shared_ptr<Vertex> end = std::make_shared<Vertex>();
1359 auto path = std::make_shared<Path>();
1360 path = std::make_shared<Path>(start, end);
1361 EXPECT_NE(path, nullptr);
1362 path->SetPathLength(1);
1363 EXPECT_EQ(1, path->GetPathLength());
1364 path->SetStart(std::make_shared<Vertex>("1", "HAHA"));
1365 EXPECT_EQ("1", path->GetStart()->GetId());
1366 EXPECT_EQ("HAHA", path->GetStart()->GetLabel());
1367 auto labels = path->GetStart()->GetLabels();
1368 EXPECT_EQ(1, labels.size());
1369
1370 path->SetEnd(std::make_shared<Vertex>("2", "HAHA2"));
1371 EXPECT_EQ("2", path->GetEnd()->GetId());
1372 EXPECT_EQ("HAHA2", path->GetEnd()->GetLabel());
1373 auto segment = path->GetSegments();
1374 EXPECT_EQ(segment.size(), 0);
1375 }
1376
1377 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Path02, TestSize.Level1)
1378 {
1379 ASSERT_NE(store_, nullptr);
1380 int errCode = E_ERROR;
1381 nlohmann::json json = nlohmann::json::parse(pathJsonString, nullptr, false);
1382 Path::Parse(json, errCode);
1383 // no length
1384 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1385 // identity:A, E_OK
1386 std::string jsonStr =
1387 "{\"length\": 1,\"start\":{\"label\":\"PERSON\",\"identity\":3,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1388 "\"NAME\":\"Alice\",\"GENDER\":\"Female\"}},"
1389 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1390 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1391 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":2,\"properties\":{\"NUM\":4,"
1392 "\"PINYIN\":\"zhixiqinshu\"}},\"segments\":[{\"start\":{\"label\":\"PERSON\","
1393 "\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1394 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1395 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1396 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1397 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":\"C\",\"properties\":{\"NUM\":4,"
1398 "\"PINYIN\":\"zhixiqinshu\"}}}]}";
1399 json = nlohmann::json::parse(jsonStr, nullptr, false);
1400 Path::Parse(json, errCode);
1401 EXPECT_EQ(errCode, E_OK);
1402
1403 // label:2, E_PARSE_JSON_FAILED
1404 jsonStr =
1405 "{\"length\": 1,\"start\":{\"label\":2,\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1406 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1407 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1408 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1409 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":2,\"properties\":{\"NUM\":4,"
1410 "\"PINYIN\":\"zhixiqinshu\"}},\"segments\":[{}]}";
1411 json = nlohmann::json::parse(jsonStr, nullptr, false);
1412 Path::Parse(json, errCode);
1413 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1414 }
1415
1416 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Path03, TestSize.Level1)
1417 {
1418 ASSERT_NE(store_, nullptr);
1419 int errCode = E_ERROR;
1420 nlohmann::json json = nlohmann::json::parse(pathJsonString, nullptr, false);
1421 Path::Parse(json, errCode);
1422 // no length
1423 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1424 // relationship->start:B, E_OK
1425 auto jsonStr =
1426 "{\"length\": 1,\"start\":{\"label\":\"PERSON\",\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1427 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1428 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1429 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1430 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":\"B\",\"end\":2,\"properties\":{\"NUM\":4,"
1431 "\"PINYIN\":\"zhixiqinshu\"}},\"segments\":[{}]}";
1432 json = nlohmann::json::parse(jsonStr, nullptr, false);
1433 Path::Parse(json, errCode);
1434 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1435
1436 // relationship->end:C, E_OK
1437 jsonStr =
1438 "{\"length\": 1,\"start\":{\"label\":\"PERSON\",\"identity\":1,\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1439 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1440 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1441 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1442 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":\"C\",\"properties\":{\"NUM\":4,"
1443 "\"PINYIN\":\"zhixiqinshu\"}},\"segments\":[{\"start\":{\"label\":\"PERSON\",\"identity\":1,"
1444 "\"properties\":{\"AGE\":32,\"SALARY\":75.35,"
1445 "\"NAME\":\"Alice\",\"GENDER\":\"Female\",\"PHONENUMBERS\":false,\"EMAILS\":null}},"
1446 "\"end\":{\"label\":\"PERSON\",\"identity\":2,\"properties\":{\"AGE\":28,\"SALARY\":65000,"
1447 "\"NAME\":\"Bob\",\"GENDER\":\"Male\",\"PHONENUMBERS\":\"123456789\",\"EMAILS\":\" bob@example.com\"}},"
1448 "\"relationship\":{\"label\":\"tttt\",\"identity\":3,\"start\":1,\"end\":\"C\",\"properties\":{\"NUM\":4,"
1449 "\"PINYIN\":\"zhixiqinshu\"}}}]}";
1450 json = nlohmann::json::parse(jsonStr, nullptr, false);
1451 Path::Parse(json, errCode);
1452 ASSERT_EQ(errCode, E_OK);
1453 }
1454
1455 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Vertex, TestSize.Level1)
1456 {
1457 ASSERT_NE(store_, nullptr);
1458 int errCode = E_ERROR;
1459 // no start, no end
1460 nlohmann::json json = nlohmann::json::parse("{\"name\" : \"zhangsan\"}", nullptr, false);
1461 ASSERT_FALSE(json.is_discarded());
1462 ASSERT_FALSE(json.is_null());
1463 Vertex::Parse(json, errCode);
1464 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1465 // no identity
1466 std::string jsonStr = "{\"start\" : 1, \"end\" : 2}";
1467 json = nlohmann::json::parse(jsonStr, nullptr, false);
1468 Vertex::Parse(json, errCode);
1469 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1470 // ok
1471 jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":3,"
1472 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1473 json = nlohmann::json::parse(jsonStr, nullptr, false);
1474 Vertex::Parse(json, errCode);
1475 EXPECT_EQ(errCode, E_OK);
1476 // start is AA
1477 jsonStr = "{\"start\" : \"AA\", \"end\" : 2, \"label\":\"COMPANY\",\"identity\":3,"
1478 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1479 json = nlohmann::json::parse(jsonStr, nullptr, false);
1480 Vertex::Parse(json, errCode);
1481 EXPECT_EQ(errCode, E_OK);
1482 // end is B
1483 jsonStr = "{\"start\" : 1, \"end\" : \"B\", \"label\":\"COMPANY\",\"identity\":3,"
1484 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1485 json = nlohmann::json::parse(jsonStr, nullptr, false);
1486 Vertex::Parse(json, errCode);
1487 EXPECT_EQ(errCode, E_OK);
1488 // identity is C
1489 jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":\"C\","
1490 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1491 json = nlohmann::json::parse(jsonStr, nullptr, false);
1492 Vertex::Parse(json, errCode);
1493 EXPECT_EQ(errCode, E_OK);
1494 // label is 222
1495 jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":222,'identity':3,\"properties\":{\"NAME\":\"myCompany3\","
1496 "\"FOUNDED\":2011}}";
1497 json = nlohmann::json::parse(jsonStr, nullptr, false);
1498 Vertex::Parse(json, errCode);
1499 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1500 // key4 is null
1501 jsonStr =
1502 "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":2,"
1503 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":4.5,\"SEX\":true,\"key1\":true,\"key2\":[], \"key3\":{}, "
1504 "\"key4\": null}}";
1505 json = nlohmann::json::parse(jsonStr, nullptr, false);
1506 Vertex::Parse(json, errCode);
1507 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1508 }
1509
1510 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Vertex02, TestSize.Level1)
1511 {
1512 Vertex vertex;
1513 Vertex vertex2("1", "PERSON");
1514 ASSERT_EQ(vertex2.GetLabel(), "PERSON");
1515 vertex2.SetLabel("PERSON1");
1516 ASSERT_EQ(vertex2.GetLabel(), "PERSON1");
1517 std::unordered_map<std::string, PropType> properties;
1518 Vertex vertex3("1", "PERSON2", properties);
1519 ASSERT_EQ(vertex3.GetLabel(), "PERSON2");
1520 vertex3.SetLabel("PERSON3");
1521 ASSERT_EQ(vertex3.GetLabel(), "PERSON3");
1522 }
1523
1524 /**
1525 * @tc.name: GdbStore_Execute_PathChange
1526 * @tc.desc: test Path Change
1527 * @tc.type: FUNC
1528 */
1529 HWTEST_F(GdbExecuteTest, GdbStore_Execute_PathChange, TestSize.Level1)
1530 {
1531 ASSERT_NE(store_, nullptr);
1532 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
1533 MatchAndVerifyPerson("name_1", 11);
1534 result = store_->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
1535 MatchAndVerifyPerson("name_2", 22);
1536 result = store_->ExecuteGql("INSERT (:Person {name: 'name_3', age: 33});");
1537 MatchAndVerifyPerson("name_3", 33);
1538 result = store_->ExecuteGql("INSERT (:Person {name: 'name_4', age: 44});");
1539 MatchAndVerifyPerson("name_4", 44);
1540 result = store_->ExecuteGql(
1541 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_2'}) INSERT (p1)-[:Friend]->(p2);");
1542 EXPECT_EQ(result.first, E_OK);
1543 result = store_->ExecuteGql(
1544 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_3'}) INSERT (p1)-[:Friend]->(p2);");
1545 EXPECT_EQ(result.first, E_OK);
1546 result = store_->ExecuteGql(
1547 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_4'}) INSERT (p1)-[:Friend]->(p2);");
1548 EXPECT_EQ(result.first, E_OK);
1549 result = store_->ExecuteGql(
1550 "MATCH (p1:Person {name: 'name_2'}), (p2:Person {name: 'name_3'}) INSERT (p1)-[:Friend]->(p2);");
1551 EXPECT_EQ(result.first, E_OK);
1552 result = store_->ExecuteGql(
1553 "MATCH (p1:Person {name: 'name_2'}), (p2:Person {name: 'name_4'}) INSERT (p1)-[:Friend]->(p2);");
1554 EXPECT_EQ(result.first, E_OK);
1555 result = store_->ExecuteGql(
1556 "MATCH (p1:Person {name: 'name_3'}), (p2:Person {name: 'name_4'}) INSERT (p1)-[:Friend]->(p2);");
1557 EXPECT_EQ(result.first, E_OK);
1558
1559 result =
1560 store_->QueryGql("MATCH path=(a:Person {name: 'name_1'})-[]->{0, 3}(b:Person) RETURN path;");
1561 ASSERT_EQ(result.first, E_OK);
1562 EXPECT_EQ(result.second->GetAllData().size(), 8);
1563
1564 GraphValue path = result.second->GetAllData()[0]["path"];
1565 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Path>>(path));
1566 }
1567
1568 /**
1569 * @tc.name: GdbStore_Execute_PathChangeRing
1570 * @tc.desc: Querying a Trail with a Ring
1571 * @tc.type: FUNC
1572 */
1573 HWTEST_F(GdbExecuteTest, GdbStore_Execute_PathChangeRing, TestSize.Level1)
1574 {
1575 ASSERT_NE(store_, nullptr);
1576 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_1', age: 11});");
1577 MatchAndVerifyPerson("name_1", 11);
1578 result = store_->ExecuteGql("INSERT (:Person {name: 'name_2', age: 22});");
1579 MatchAndVerifyPerson("name_2", 22);
1580 result = store_->ExecuteGql("INSERT (:Person {name: 'name_3', age: 33});");
1581 MatchAndVerifyPerson("name_3", 33);
1582 result = store_->ExecuteGql(
1583 "MATCH (p1:Person {name: 'name_1'}), (p2:Person {name: 'name_2'}) INSERT (p1)-[:Friend]->(p2);");
1584 EXPECT_EQ(result.first, E_OK);
1585 result = store_->ExecuteGql(
1586 "MATCH (p1:Person {name: 'name_2'}), (p2:Person {name: 'name_3'}) INSERT (p1)-[:Friend]->(p2);");
1587 EXPECT_EQ(result.first, E_OK);
1588 result = store_->ExecuteGql(
1589 "MATCH (p1:Person {name: 'name_3'}), (p2:Person {name: 'name_1'}) INSERT (p1)-[:Friend]->(p2);");
1590 EXPECT_EQ(result.first, E_OK);
1591
1592 result =
1593 store_->QueryGql("MATCH path=(a:Person {name: 'name_1'})-[]->{0, 3}(b:Person) RETURN path;");
1594 ASSERT_EQ(result.first, E_OK);
1595 EXPECT_EQ(result.second->GetAllData().size(), 4);
1596
1597 GraphValue path = result.second->GetAllData()[0]["path"];
1598 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Path>>(path));
1599 }
1600
1601 /**
1602 * @tc.name: GdbStore_Execute_AllGraph
1603 * @tc.desc: test All Graph
1604 * @tc.type: FUNC
1605 */
1606 HWTEST_F(GdbExecuteTest, GdbStore_Execute_AllGraph, TestSize.Level1)
1607 {
1608 ASSERT_NE(store_, nullptr);
1609 std::string name = "zhangsanfeng";
1610 for (int32_t i = 0; i < 50; i++) {
1611 auto nameInner = name + "_" + std::to_string(i);
1612 auto result = store_->ExecuteGql("INSERT (:Person {name: '" + nameInner + "', age: 11});");
1613 MatchAndVerifyPerson(nameInner, 11);
1614 for (int32_t j = 0; j < 100; j++) {
1615 auto nameOut = nameInner + "_" + std::to_string(j);
1616 result = store_->ExecuteGql("INSERT (:Person {name: '" + nameOut + "', age: 22});");
1617 MatchAndVerifyPerson(nameOut, 22);
1618 result = store_->ExecuteGql(
1619 "MATCH (p1:Person {name: '" + nameInner + "'}), (p2:Person {name: '" + nameOut + "'}) "
1620 "INSERT (p1)-[:Friend]->(p2);");
1621 EXPECT_EQ(result.first, E_OK);
1622 }
1623 }
1624 auto result =
1625 store_->QueryGql("MATCH path=(a:Person)-[]->{0, 3}(b:Person) RETURN path;");
1626 ASSERT_EQ(result.first, E_OK);
1627 EXPECT_EQ(result.second->GetAllData().size(), 10050);
1628 GraphValue path = result.second->GetAllData()[0]["path"];
1629 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Path>>(path));
1630
1631 auto gql = "MATCH (person:Person) RETURN person;";
1632 result = store_->QueryGql(gql);
1633 ASSERT_EQ(result.first, E_OK);
1634 ASSERT_NE(result.second, nullptr);
1635 EXPECT_EQ(result.second->GetAllData().size(), 5050);
1636 }
1637
1638 /**
1639 * @tc.name: GdbStore_Execute_SelfPath
1640 * @tc.desc: test Self Path
1641 * @tc.type: FUNC
1642 */
1643 HWTEST_F(GdbExecuteTest, GdbStore_Execute_SelfPath, TestSize.Level1)
1644 {
1645 ASSERT_NE(store_, nullptr);
1646 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_111', age: 11});");
1647 MatchAndVerifyPerson("name_111", 11);
1648
1649 result = store_->ExecuteGql(
1650 "MATCH (p1:Person {name: 'name_111'}), (p2:Person {name: 'name_111'}) INSERT (p1)-[:Friend]->(p2);");
1651 EXPECT_EQ(result.first, E_OK);
1652
1653 result = store_->QueryGql("MATCH path=(a:Person {name: 'name_111'})-[]->{0, 3}(b:Person) RETURN path;");
1654 ASSERT_EQ(result.first, E_OK);
1655 EXPECT_EQ(result.second->GetAllData().size(), 2);
1656
1657 GraphValue path = result.second->GetAllData()[0]["path"];
1658 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Path>>(path));
1659 }
1660
1661 /**
1662 * @tc.name: GdbStore_Execute_SelfPath
1663 * @tc.desc: test Self Path
1664 * @tc.type: FUNC
1665 */
1666 HWTEST_F(GdbExecuteTest, GdbStore_Execute_SelfPath02, TestSize.Level1)
1667 {
1668 ASSERT_NE(store_, nullptr);
1669 auto result = store_->ExecuteGql("INSERT (:Person {name: 'name_test_001', age: 11});");
1670 MatchAndVerifyPerson("name_test_001", 11);
1671
1672 result = store_->ExecuteGql("INSERT (:Person {name: 'name_test_001', age: 11});");
1673
1674 result = store_->ExecuteGql(
1675 "MATCH (p1:Person {name: 'name_test_001'}), (p2:Person {name: 'name_test_001'}) INSERT (p1)-[:Friend]->(p2);");
1676 EXPECT_EQ(result.first, E_OK);
1677
1678 result = store_->ExecuteGql(
1679 "MATCH (p1:Person {name: 'name_test_001'}), (p2:Person {name: 'name_test_001'}) INSERT (p1)-[:Friend]->(p2);");
1680 EXPECT_EQ(result.first, E_OK);
1681
1682 result =
1683 store_->QueryGql("MATCH path=(a:Person {name: 'name_test_001'})-[]->{0, 3}(b:Person) RETURN path;");
1684 ASSERT_EQ(result.first, E_OK);
1685 EXPECT_EQ(result.second->GetAllData().size(), 126);
1686
1687 GraphValue path = result.second->GetAllData()[0]["path"];
1688 ASSERT_TRUE(std::holds_alternative<std::shared_ptr<Path>>(path));
1689 }
1690
1691 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StoreConfigSetName, TestSize.Level1)
1692 {
1693 int errCode = E_OK;
1694 std::string dbName = "success01";
1695 std::string dbPath = databasePath;
1696 auto config = StoreConfig(dbName, dbPath);
1697 config.SetName("success01_update");
1698 EXPECT_EQ("success01_update", config.GetName());
1699
1700 auto store = GDBHelper::GetDBStore(config, errCode);
1701 EXPECT_NE(store, nullptr);
1702 EXPECT_EQ(errCode, E_OK);
1703 GDBHelper::DeleteDBStore(StoreConfig("success01_update", databasePath));
1704 }
1705
1706 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StoreConfigSetPath, TestSize.Level1)
1707 {
1708 int errCode = E_OK;
1709 std::string dbName = "success01";
1710 std::string dbPath = "/test/test";
1711 auto config = StoreConfig(dbName, dbPath);
1712 config.SetPath(databasePath);
1713 EXPECT_EQ(databasePath, config.GetPath());
1714
1715 auto store = GDBHelper::GetDBStore(config, errCode);
1716 EXPECT_NE(store, nullptr);
1717 EXPECT_EQ(errCode, E_OK);
1718 GDBHelper::DeleteDBStore(StoreConfig(dbName, databasePath));
1719 }
1720
1721 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StoreConfigSetDbType, TestSize.Level1)
1722 {
1723 int errCode = E_OK;
1724 std::string dbName = "success01";
1725 std::string dbPath = databasePath;
1726 auto config = StoreConfig(dbName, dbPath);
1727 config.SetDbType(DBType::DB_GRAPH);
1728 EXPECT_EQ(config.GetDbType(), DBType::DB_GRAPH);
1729
1730 auto store = GDBHelper::GetDBStore(config, errCode);
1731 EXPECT_NE(store, nullptr);
1732 EXPECT_EQ(errCode, E_OK);
1733 GDBHelper::DeleteDBStore(StoreConfig(dbName, databasePath));
1734 }
1735
1736 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StoreConfigSetEncryptStatus, TestSize.Level1)
1737 {
1738 int errCode = E_OK;
1739 std::string dbName = "success01";
1740 std::string dbPath = databasePath;
1741 auto config = StoreConfig(dbName, dbPath);
1742 config.SetEncryptStatus(true);
1743 ASSERT_TRUE(config.IsEncrypt());
1744 config.SetEncryptStatus(false);
1745 ASSERT_TRUE(!config.IsEncrypt());
1746 auto store = GDBHelper::GetDBStore(config, errCode);
1747 EXPECT_NE(store, nullptr);
1748 EXPECT_EQ(errCode, E_OK);
1749 GDBHelper::DeleteDBStore(StoreConfig(dbName, databasePath));
1750 }
1751
1752 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StoreConfigSetReadTime, TestSize.Level1)
1753 {
1754 int errCode = E_OK;
1755 std::string dbName = "success01";
1756 std::string dbPath = databasePath;
1757 std::vector<uint8_t> encryptKey = std::vector<uint8_t>();
1758 auto config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, true, encryptKey);
1759 config = StoreConfig(dbName, dbPath, DBType::DB_GRAPH, false, encryptKey);
1760 config.SetReadTime(3);
1761 EXPECT_EQ(config.GetReadTime(), 3);
1762
1763 auto store = GDBHelper::GetDBStore(config, errCode);
1764 EXPECT_NE(store, nullptr);
1765 EXPECT_EQ(errCode, E_OK);
1766 GDBHelper::DeleteDBStore(StoreConfig(dbName, databasePath));
1767 }
1768
1769 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UtilsTest, TestSize.Level1)
1770 {
1771 int errCode = E_OK;
1772 errCode = GdbUtils::CreateDirectory("");
1773 EXPECT_EQ(errCode, E_OK);
1774 errCode = GdbUtils::CreateDirectory("dir1");
1775 EXPECT_EQ(errCode, E_CREATE_FOLDER_FAIT);
1776 GdbUtils::CreateDirectory("dir1/dir2");
1777 EXPECT_EQ(errCode, E_CREATE_FOLDER_FAIT);
1778 GdbUtils::CreateDirectory("dir1/dir2/dir3");
1779 EXPECT_EQ(errCode, E_CREATE_FOLDER_FAIT);
1780 GdbUtils::CreateDirectory("/dir1/dir2/dir3");
1781 EXPECT_EQ(errCode, E_CREATE_FOLDER_FAIT);
1782 GdbUtils::CreateDirectory("/data/");
1783 EXPECT_EQ(errCode, E_CREATE_FOLDER_FAIT);
1784 GdbUtils::CreateDirectory("/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2"
1785 "/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2"
1786 "/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2"
1787 "/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2"
1788 "/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2"
1789 "/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2"
1790 "/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2/data/dir1/dir2");
1791 EXPECT_EQ(errCode, E_CREATE_FOLDER_FAIT);
1792 }
1793
1794 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UtilsAnonymousTest, TestSize.Level1)
1795 {
1796 auto ret = GdbUtils::Anonymous("fileName");
1797 EXPECT_EQ(ret, "fileName");
1798 ret = GdbUtils::Anonymous("dir1/shortPath");
1799 EXPECT_EQ(ret, "dir1/shortPa");
1800 ret = GdbUtils::Anonymous("dir1/el1/longPath");
1801 EXPECT_EQ(ret, "dir1/el1/longPa");
1802 ret = GdbUtils::Anonymous("el1/l");
1803 EXPECT_EQ(ret, "el1");
1804 ret = GdbUtils::Anonymous(
1805 "dir1/el1/longPath/longPath/longPath/longPath/longPath/longPath/longPath/longPath/longPath/longPath/longPath");
1806 EXPECT_EQ(ret, "dir1/***/el1/***/longPa");
1807 ret = GdbUtils::Anonymous("/data/dir1/dir2/dir3");
1808 EXPECT_EQ(ret, "/***/dir3");
1809 ret = GdbUtils::Anonymous("/data/el1/dir2");
1810 EXPECT_EQ(ret, "/***/el1/dir2");
1811 ret = GdbUtils::Anonymous("/data/app/el1/0/base/com.my.hmos.arkwebcore");
1812 EXPECT_EQ(ret, "/***/el1/***/com.my.hmos.arkwebcore");
1813 }
1814
1815 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StatementTest, TestSize.Level1)
1816 {
1817 auto errCode = E_OK;
1818 std::string gql = "INSERT (:Person {name: 'name_test_001', age: 11});";
1819 GraphStatement statement(nullptr, "", nullptr, errCode);
1820 EXPECT_EQ(errCode, E_PREPARE_CHECK_FAILED);
1821
1822 GRD_DB *dbHandle;
1823 auto ret = std::make_shared<GraphStatement>(dbHandle, gql, nullptr, errCode);
1824 EXPECT_NE(ret, nullptr);
1825 errCode = ret->Prepare();
1826 EXPECT_EQ(errCode, E_PREPARE_CHECK_FAILED);
1827
1828 errCode = ret->Step();
1829 EXPECT_EQ(errCode, E_STEP_CHECK_FAILED);
1830
1831 errCode = ret->Finalize();
1832 EXPECT_EQ(errCode, E_OK);
1833
1834 errCode = ret->GetColumnCount();
1835 EXPECT_EQ(errCode, E_STATEMENT_EMPTY);
1836
1837 auto pair = ret->GetColumnName(0);
1838 EXPECT_EQ(pair.first, E_STATEMENT_EMPTY);
1839 pair = ret->GetColumnName(0);
1840 EXPECT_EQ(pair.first, E_STATEMENT_EMPTY);
1841 pair = ret->GetColumnName(3);
1842 EXPECT_EQ(pair.first, E_STATEMENT_EMPTY);
1843 pair = ret->GetColumnName(9);
1844 EXPECT_EQ(pair.first, E_STATEMENT_EMPTY);
1845
1846 auto pair1 = ret->GetColumnValue(0);
1847 EXPECT_EQ(pair1.first, E_STATEMENT_EMPTY);
1848 pair1 = ret->GetColumnValue(3);
1849 EXPECT_EQ(pair1.first, E_STATEMENT_EMPTY);
1850 pair1 = ret->GetColumnValue(100);
1851 EXPECT_EQ(pair1.first, E_STATEMENT_EMPTY);
1852
1853 errCode = ret->IsReady();
1854 EXPECT_EQ(errCode, E_OK);
1855 }
1856
1857 HWTEST_F(GdbExecuteTest, GdbStore_Execute_StatementTest02, TestSize.Level1)
1858 {
1859 auto errCode = E_OK;
1860 std::string gql = "INSERT (:Person {name: 'name_test_001', age: 11});";
1861
1862 auto ret = std::make_shared<GraphStatement>(nullptr, gql, nullptr, errCode);
1863 EXPECT_NE(ret, nullptr);
1864 errCode = ret->Prepare();
1865 EXPECT_EQ(errCode, E_PREPARE_CHECK_FAILED);
1866
1867 errCode = ret->Step();
1868 EXPECT_EQ(errCode, E_STEP_CHECK_FAILED);
1869
1870 errCode = ret->Finalize();
1871 EXPECT_EQ(errCode, E_OK);
1872
1873 errCode = ret->GetColumnCount();
1874 EXPECT_EQ(errCode, E_STATEMENT_EMPTY);
1875
1876 auto pair = ret->GetColumnName(0);
1877 EXPECT_EQ(pair.first, E_STATEMENT_EMPTY);
1878
1879 auto pair1 = ret->GetColumnValue(0);
1880 EXPECT_EQ(pair1.first, E_STATEMENT_EMPTY);
1881
1882 errCode = ret->IsReady();
1883 EXPECT_EQ(errCode, E_OK);
1884 }
1885
1886 HWTEST_F(GdbExecuteTest, GdbStore_Execute_FullResult, TestSize.Level1)
1887 {
1888 auto fullResult = std::make_shared<FullResult>();
1889 EXPECT_NE(fullResult, nullptr);
1890 auto errCode = fullResult->InitData(nullptr);
1891 EXPECT_EQ(errCode, E_STATEMENT_EMPTY);
1892 fullResult = std::make_shared<FullResult>();
1893 EXPECT_NE(fullResult, nullptr);
1894 auto statement = std::make_shared<GraphStatement>(nullptr, "", nullptr, errCode);
1895 errCode = fullResult->InitData(statement);
1896 EXPECT_EQ(errCode, E_STEP_CHECK_FAILED);
1897 }
1898
1899 HWTEST_F(GdbExecuteTest, GdbStore_Execute_EdgeTest, TestSize.Level1)
1900 {
1901 auto errCode = E_ERROR;
1902 auto edge = std::make_shared<Edge>();
1903 EXPECT_NE(edge, nullptr);
1904 edge = std::make_shared<Edge>(std::make_shared<Vertex>(), "2", "3");
1905 EXPECT_NE(edge, nullptr);
1906 edge = std::make_shared<Edge>(nullptr, "2", "3");
1907 EXPECT_NE(edge, nullptr);
1908 edge = std::make_shared<Edge>("1", "PERSON", "2", "3");
1909 EXPECT_NE(edge, nullptr);
1910 edge->SetSourceId("22");
1911 EXPECT_EQ("22", edge->GetSourceId());
1912 edge->SetTargetId("33");
1913 EXPECT_EQ("33", edge->GetTargetId());
1914
1915 auto jsonStr = "{\"start\" : 1, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":3,"
1916 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1917 nlohmann::json json = nlohmann::json::parse(jsonStr, nullptr, false);
1918 Edge::Parse(json, errCode);
1919 EXPECT_EQ(errCode, E_OK);
1920
1921 jsonStr = "{\"start\" : {}, \"end\" : 2, \"label\":\"COMPANY\",\"identity\":3,"
1922 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1923 json = nlohmann::json::parse(jsonStr, nullptr, false);
1924 Edge::Parse(json, errCode);
1925 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1926
1927 jsonStr = "{\"start\" : 1, \"end\" : {}, \"label\":\"COMPANY\",\"identity\":3,"
1928 "\"properties\":{\"NAME\":\"myCompany3\",\"FOUNDED\":2011}}";
1929 json = nlohmann::json::parse(jsonStr, nullptr, false);
1930 Edge::Parse(json, errCode);
1931 EXPECT_EQ(errCode, E_PARSE_JSON_FAILED);
1932 }
1933
1934 HWTEST_F(GdbExecuteTest, GdbStore_Execute_Connect, TestSize.Level1)
1935 {
1936 auto errCode = E_ERROR;
1937 errCode = Connection::RegisterCreator(DBType::DB_BUTT, nullptr);
1938 EXPECT_EQ(errCode, E_NOT_SUPPORT);
1939 errCode = Connection::RegisterCreator(static_cast<DBType>(-1), nullptr);
1940 EXPECT_EQ(errCode, E_NOT_SUPPORT);
__anon3d5958e60102(const StoreConfig& config, bool isWriter) 1941 auto func = [](const StoreConfig& config, bool isWriter) {
1942 std::pair<int32_t, std::shared_ptr<Connection>> ret = std::make_pair(
1943 static_cast<int32_t>(DBType::DB_GRAPH), nullptr);
1944 return ret;
1945 };
1946 errCode = Connection::RegisterCreator(DBType::DB_GRAPH, func);
1947 EXPECT_EQ(errCode, E_OK);
1948 }
1949
1950 HWTEST_F(GdbExecuteTest, GdbStore_DBStore01, TestSize.Level1)
1951 {
1952 int errCode = E_OK;
1953 std::string dbName = "execute_test_ok_2";
1954 std::string dbPath = "/data";
1955 auto config = StoreConfig(dbName, dbPath);
1956
1957 auto store = std::make_shared<DBStoreImpl>(config);
1958 EXPECT_NE(store, nullptr);
1959 errCode = store->InitConn();
1960 EXPECT_EQ(errCode, E_OK);
1961 errCode = store->InitConn();
1962 EXPECT_EQ(errCode, E_OK);
1963 const std::string gql = "INSERT (:Person {name: 'name_1', age: 11});";
1964 auto [err, result] = store->QueryGql(gql);
1965 EXPECT_EQ(err, E_GRD_UNDEFINED_PARAM);
1966 auto [err1, result1] = store->ExecuteGql(gql);
1967 EXPECT_EQ(err1, E_GRD_UNDEFINED_PARAM);
1968 GDBHelper::DeleteDBStore(config);
1969 }
1970
1971 HWTEST_F(GdbExecuteTest, GdbStore_NameErre01, TestSize.Level1)
1972 {
1973 ASSERT_NE(store_, nullptr);
1974 auto result = store_->ExecuteGql("DROP GRAPH test");
1975 EXPECT_EQ(result.first, E_OK);
1976
1977 std::string createGql = "CREATE GRAPH test {(person:";
1978 for (int i = 0; i < 129; ++i) {
1979 createGql += "i";
1980 }
1981 createGql += " {name STRING, age INT}), (person) -[:FRIEND]-> (person)};";
1982 result = store_->ExecuteGql(createGql);
1983 EXPECT_EQ(result.first, E_GRD_INVALID_NAME);
1984 }
1985
1986 HWTEST_F(GdbExecuteTest, GdbStore_NameErre02, TestSize.Level1)
1987 {
1988 ASSERT_NE(store_, nullptr);
1989 auto result = store_->ExecuteGql("DROP GRAPH test");
1990 EXPECT_EQ(result.first, E_OK);
1991
1992 std::string createGql =
1993 "CREATE GRAPH test {(anon_person: Person {name STRING, age INT}), (person) -[:Friend]-> (person)};";
1994 result = store_->ExecuteGql(createGql);
1995 EXPECT_EQ(result.first, E_GRD_SEMANTIC_ERROR);
1996 }
1997
1998 HWTEST_F(GdbExecuteTest, GdbStore_Conflict01, TestSize.Level1)
1999 {
2000 ASSERT_NE(store_, nullptr);
2001 auto result = store_->ExecuteGql("DROP GRAPH test");
2002 EXPECT_EQ(result.first, E_OK);
2003 result = store_->ExecuteGql(
2004 "CREATE GRAPH test {(person: Person {id INT, name STRING}),(person) -[:Friend]-> (person)};");
2005 EXPECT_EQ(result.first, E_OK);
2006
2007 result = store_->ExecuteGql("CREATE UNIQUE INDEX index_person_id ON person(id);");
2008 EXPECT_EQ(result.first, E_OK);
2009 result = store_->ExecuteGql("INSERT (:Person {id: 1, name: 'name_1'});");
2010 EXPECT_EQ(result.first, E_OK);
2011 result = store_->ExecuteGql("INSERT (:Person {id: 1, name: 'name_2'});");
2012 EXPECT_EQ(result.first, E_GRD_DATA_CONFLICT);
2013 }
2014
2015 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UtilsStringTest, TestSize.Level1)
2016 {
2017 std::string str = "abcd";
2018 GdbUtils::ClearAndZeroString(str);
2019 EXPECT_EQ(str, "");
2020 }
2021
2022 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UtilsConfigTest, TestSize.Level1)
2023 {
2024 std::vector<uint8_t> keys = {1, 2, 3, 4, 5, 6, 7, 8};
2025 auto ret = GdbUtils::GetConfigStr(keys, false);
2026 EXPECT_EQ(ret,
2027 "{\"pageSize\": 4, \"crcCheckEnable\": 0, \"defaultIsolationLevel\": 3, \"redoFlushByTrx\": 1, "
2028 "\"metaInfoBak\": 1}");
2029
2030 ret = GdbUtils::GetConfigStr(keys, true);
2031 EXPECT_EQ(ret,
2032 "{\"isEncrypted\": 1, \"hexPassword\": \"0102030405060708\", "
2033 "\"pageSize\": 4, \"crcCheckEnable\": 0, \"defaultIsolationLevel\": 3, \"redoFlushByTrx\": 1, "
2034 "\"metaInfoBak\": 1}");
2035 }
2036
2037 HWTEST_F(GdbExecuteTest, GdbStore_Execute_UtilsKeyTest, TestSize.Level1)
2038 {
2039 std::vector<uint8_t> keys = {1, 2, 3, 4, 5, 6, 7, 8};
2040 const size_t keyBuffSize = keys.size() * 2 + 1; // 2 hex number can represent a uint8_t, 1 is for '\0'
2041 std::vector<char> keyBuff(keyBuffSize);
2042 auto ret = GdbUtils::GetEncryptKey(keys, keyBuff.data(), keyBuffSize);
2043 std::string hexString(ret, ret + keyBuffSize - 1);
2044 EXPECT_EQ(hexString, "0102030405060708");
2045 }