yangdx commited on
Commit
4265a8d
·
1 Parent(s): cd7baaa

Update graph db test

Browse files
Files changed (1) hide show
  1. tests/test_graph_storage.py +39 -39
tests/test_graph_storage.py CHANGED
@@ -133,10 +133,6 @@ async def test_graph_basic(storage):
133
  4. 使用 get_edge 读取一条边
134
  """
135
  try:
136
- # 清理之前的测试数据
137
- print("清理之前的测试数据...")
138
- await storage.drop()
139
-
140
  # 1. 插入第一个节点
141
  node1_id = "人工智能"
142
  node1_data = {
@@ -251,10 +247,6 @@ async def test_graph_advanced(storage):
251
  9. 使用 drop 清理数据
252
  """
253
  try:
254
- # 清理之前的测试数据
255
- print("清理之前的测试数据...\n")
256
- await storage.drop()
257
-
258
  # 1. 插入测试数据
259
  # 插入节点1: 人工智能
260
  node1_id = "人工智能"
@@ -445,10 +437,6 @@ async def test_graph_batch_operations(storage):
445
  5. 使用 get_nodes_edges_batch 批量获取多个节点的所有边
446
  """
447
  try:
448
- # 清理之前的测试数据
449
- print("清理之前的测试数据...\n")
450
- await storage.drop()
451
-
452
  # 1. 插入测试数据
453
  # 插入节点1: 人工智能
454
  node1_id = "人工智能"
@@ -776,10 +764,6 @@ async def test_graph_special_characters(storage):
776
  3. 验证特殊字符是否被正确保存和检索
777
  """
778
  try:
779
- # 清理之前的测试数据
780
- print("清理之前的测试数据...\n")
781
- await storage.drop()
782
-
783
  # 1. 测试节点名称中的特殊字符
784
  node1_id = "包含'单引号'的节点"
785
  node1_data = {
@@ -792,7 +776,7 @@ async def test_graph_special_characters(storage):
792
  await storage.upsert_node(node1_id, node1_data)
793
 
794
  # 2. 测试节点名称中的双引号
795
- node2_id = "包含\"双引号\"的节点"
796
  node2_data = {
797
  "entity_id": node2_id,
798
  "description": "这个描述同时包含'单引号'和\"双引号\"以及\\反斜杠\\路径",
@@ -824,7 +808,7 @@ async def test_graph_special_characters(storage):
824
 
825
  # 5. 测试边描述中的更复杂特殊字符组合
826
  edge2_data = {
827
- "relationship": "复杂\"关系\"\\类型",
828
  "weight": 0.8,
829
  "description": "包含SQL注入尝试: SELECT * FROM users WHERE name='admin'--",
830
  }
@@ -842,13 +826,17 @@ async def test_graph_special_characters(storage):
842
  if node_props:
843
  print(f"成功读取节点: {node_id}")
844
  print(f"节点描述: {node_props.get('description', '无描述')}")
845
-
846
  # 验证节点ID是否正确保存
847
- assert node_props.get("entity_id") == node_id, f"节点ID不匹配: 期望 {node_id}, 实际 {node_props.get('entity_id')}"
848
-
 
 
849
  # 验证描述是否正确保存
850
- assert node_props.get("description") == original_data["description"], f"节点描述不匹配: 期望 {original_data['description']}, 实际 {node_props.get('description')}"
851
-
 
 
852
  print(f"节点 {node_id} 特殊字符验证成功")
853
  else:
854
  print(f"读取节点属性失败: {node_id}")
@@ -861,13 +849,17 @@ async def test_graph_special_characters(storage):
861
  print(f"成功读取边: {node1_id} -> {node2_id}")
862
  print(f"边关系: {edge1_props.get('relationship', '无关系')}")
863
  print(f"边描述: {edge1_props.get('description', '无描述')}")
864
-
865
  # 验证边关系是否正确保存
866
- assert edge1_props.get("relationship") == edge1_data["relationship"], f"边关系不匹配: 期望 {edge1_data['relationship']}, 实际 {edge1_props.get('relationship')}"
867
-
 
 
868
  # 验证边描述是否正确保存
869
- assert edge1_props.get("description") == edge1_data["description"], f"边描述不匹配: 期望 {edge1_data['description']}, 实际 {edge1_props.get('description')}"
870
-
 
 
871
  print(f"边 {node1_id} -> {node2_id} 特殊字符验证成功")
872
  else:
873
  print(f"读取边属性失败: {node1_id} -> {node2_id}")
@@ -878,13 +870,17 @@ async def test_graph_special_characters(storage):
878
  print(f"成功读取边: {node2_id} -> {node3_id}")
879
  print(f"边关系: {edge2_props.get('relationship', '无关系')}")
880
  print(f"边描述: {edge2_props.get('description', '无描述')}")
881
-
882
  # 验证边关系是否正确保存
883
- assert edge2_props.get("relationship") == edge2_data["relationship"], f"边关系不匹配: 期望 {edge2_data['relationship']}, 实际 {edge2_props.get('relationship')}"
884
-
 
 
885
  # 验证边描述是否正确保存
886
- assert edge2_props.get("description") == edge2_data["description"], f"边描述不匹配: 期望 {edge2_data['description']}, 实际 {edge2_props.get('description')}"
887
-
 
 
888
  print(f"边 {node2_id} -> {node3_id} 特殊字符验证成功")
889
  else:
890
  print(f"读取边属性失败: {node2_id} -> {node3_id}")
@@ -907,10 +903,6 @@ async def test_graph_undirected_property(storage):
907
  4. 验证批量操作中的无向图特性
908
  """
909
  try:
910
- # 清理之前的测试数据
911
- print("清理之前的测试数据...\n")
912
- await storage.drop()
913
-
914
  # 1. 插入测试数据
915
  # 插入节点1: 计算机科学
916
  node1_id = "计算机科学"
@@ -1131,6 +1123,12 @@ async def main():
1131
 
1132
  choice = input("\n请输入选项 (1/2/3/4/5/6): ")
1133
 
 
 
 
 
 
 
1134
  if choice == "1":
1135
  await test_graph_basic(storage)
1136
  elif choice == "2":
@@ -1155,8 +1153,10 @@ async def main():
1155
 
1156
  if batch_result:
1157
  ASCIIColors.cyan("\n=== 开始无向图特性测试 ===")
1158
- undirected_result = await test_graph_undirected_property(storage)
1159
-
 
 
1160
  if undirected_result:
1161
  ASCIIColors.cyan("\n=== 开始特殊字符测试 ===")
1162
  await test_graph_special_characters(storage)
 
133
  4. 使用 get_edge 读取一条边
134
  """
135
  try:
 
 
 
 
136
  # 1. 插入第一个节点
137
  node1_id = "人工智能"
138
  node1_data = {
 
247
  9. 使用 drop 清理数据
248
  """
249
  try:
 
 
 
 
250
  # 1. 插入测试数据
251
  # 插入节点1: 人工智能
252
  node1_id = "人工智能"
 
437
  5. 使用 get_nodes_edges_batch 批量获取多个节点的所有边
438
  """
439
  try:
 
 
 
 
440
  # 1. 插入测试数据
441
  # 插入节点1: 人工智能
442
  node1_id = "人工智能"
 
764
  3. 验证特殊字符是否被正确保存和检索
765
  """
766
  try:
 
 
 
 
767
  # 1. 测试节点名称中的特殊字符
768
  node1_id = "包含'单引号'的节点"
769
  node1_data = {
 
776
  await storage.upsert_node(node1_id, node1_data)
777
 
778
  # 2. 测试节点名称中的双引号
779
+ node2_id = '包含"双引号"的节点'
780
  node2_data = {
781
  "entity_id": node2_id,
782
  "description": "这个描述同时包含'单引号'和\"双引号\"以及\\反斜杠\\路径",
 
808
 
809
  # 5. 测试边描述中的更复杂特殊字符组合
810
  edge2_data = {
811
+ "relationship": '复杂"关系"\\类型',
812
  "weight": 0.8,
813
  "description": "包含SQL注入尝试: SELECT * FROM users WHERE name='admin'--",
814
  }
 
826
  if node_props:
827
  print(f"成功读取节点: {node_id}")
828
  print(f"节点描述: {node_props.get('description', '无描述')}")
829
+
830
  # 验证节点ID是否正确保存
831
+ assert (
832
+ node_props.get("entity_id") == node_id
833
+ ), f"节点ID不匹配: 期望 {node_id}, 实际 {node_props.get('entity_id')}"
834
+
835
  # 验证描述是否正确保存
836
+ assert (
837
+ node_props.get("description") == original_data["description"]
838
+ ), f"节点描述不匹配: 期望 {original_data['description']}, 实际 {node_props.get('description')}"
839
+
840
  print(f"节点 {node_id} 特殊字符验证成功")
841
  else:
842
  print(f"读取节点属性失败: {node_id}")
 
849
  print(f"成功读取边: {node1_id} -> {node2_id}")
850
  print(f"边关系: {edge1_props.get('relationship', '无关系')}")
851
  print(f"边描述: {edge1_props.get('description', '无描述')}")
852
+
853
  # 验证边关系是否正确保存
854
+ assert (
855
+ edge1_props.get("relationship") == edge1_data["relationship"]
856
+ ), f"边关系不匹配: 期望 {edge1_data['relationship']}, 实际 {edge1_props.get('relationship')}"
857
+
858
  # 验证边描述是否正确保存
859
+ assert (
860
+ edge1_props.get("description") == edge1_data["description"]
861
+ ), f"边描述不匹配: 期望 {edge1_data['description']}, 实际 {edge1_props.get('description')}"
862
+
863
  print(f"边 {node1_id} -> {node2_id} 特殊字符验证成功")
864
  else:
865
  print(f"读取边属性失败: {node1_id} -> {node2_id}")
 
870
  print(f"成功读取边: {node2_id} -> {node3_id}")
871
  print(f"边关系: {edge2_props.get('relationship', '无关系')}")
872
  print(f"边描述: {edge2_props.get('description', '无描述')}")
873
+
874
  # 验证边关系是否正确保存
875
+ assert (
876
+ edge2_props.get("relationship") == edge2_data["relationship"]
877
+ ), f"边关系不匹配: 期望 {edge2_data['relationship']}, 实际 {edge2_props.get('relationship')}"
878
+
879
  # 验证边描述是否正确保存
880
+ assert (
881
+ edge2_props.get("description") == edge2_data["description"]
882
+ ), f"边描述不匹配: 期望 {edge2_data['description']}, 实际 {edge2_props.get('description')}"
883
+
884
  print(f"边 {node2_id} -> {node3_id} 特殊字符验证成功")
885
  else:
886
  print(f"读取边属性失败: {node2_id} -> {node3_id}")
 
903
  4. 验证批量操作中的无向图特性
904
  """
905
  try:
 
 
 
 
906
  # 1. 插入测试数据
907
  # 插入节点1: 计算机科学
908
  node1_id = "计算机科学"
 
1123
 
1124
  choice = input("\n请输入选项 (1/2/3/4/5/6): ")
1125
 
1126
+ # 在执行测试前清理数据
1127
+ if choice in ["1", "2", "3", "4", "5", "6"]:
1128
+ ASCIIColors.yellow("\n执行测试前清理数据...")
1129
+ await storage.drop()
1130
+ ASCIIColors.green("数据清理完成\n")
1131
+
1132
  if choice == "1":
1133
  await test_graph_basic(storage)
1134
  elif choice == "2":
 
1153
 
1154
  if batch_result:
1155
  ASCIIColors.cyan("\n=== 开始无向图特性测试 ===")
1156
+ undirected_result = await test_graph_undirected_property(
1157
+ storage
1158
+ )
1159
+
1160
  if undirected_result:
1161
  ASCIIColors.cyan("\n=== 开始特殊字符测试 ===")
1162
  await test_graph_special_characters(storage)