@@ -28,22 +28,30 @@ def assert_same_dict_values(
28
28
29
29
30
30
def assert_bc (d1 : dict [str | int , float ], d2 : dict [str | int , float ]) -> None :
31
+ assert d1
32
+ assert d2
31
33
assert_same_dict_values (d1 , d2 , 14 )
32
34
33
35
34
36
def assert_pagerank (d1 : dict [str | int , float ], d2 : dict [str | int , float ]) -> None :
37
+ assert d1
38
+ assert d2
35
39
assert_same_dict_values (d1 , d2 , 15 )
36
40
37
41
38
42
def assert_louvain (l1 : list [set [Any ]], l2 : list [set [Any ]]) -> None :
39
43
# TODO: Implement some kind of comparison
40
44
# Reason: Louvain returns different results on different runs
45
+ assert l1
46
+ assert l2
41
47
pass
42
48
43
49
44
50
def assert_k_components (
45
51
d1 : dict [int , list [set [Any ]]], d2 : dict [int , list [set [Any ]]]
46
52
) -> None :
53
+ assert d1
54
+ assert d2
47
55
assert d1 .keys () == d2 .keys (), "Dictionaries have different keys"
48
56
assert d1 == d2
49
57
@@ -91,6 +99,8 @@ def test_algorithm(
91
99
G_4 = nxadb .DiGraph (graph_name = "KarateGraph" , symmetrize_edges = True )
92
100
G_5 = nxadb .DiGraph (graph_name = "KarateGraph" , symmetrize_edges = False )
93
101
G_6 = nxadb .MultiGraph (graph_name = "KarateGraph" )
102
+ G_7 = nxadb .MultiDiGraph (graph_name = "KarateGraph" , symmetrize_edges = True )
103
+ G_8 = nxadb .MultiDiGraph (graph_name = "KarateGraph" , symmetrize_edges = False )
94
104
95
105
r_1 = algorithm_func (G_1 )
96
106
r_2 = algorithm_func (G_2 )
@@ -121,7 +131,12 @@ def test_algorithm(
121
131
r_11 = algorithm_func (G_6 )
122
132
r_11_orig = algorithm_func .orig_func (G_6 ) # type: ignore
123
133
124
- assert all ([r_7 , r_7_orig , r_8 , r_8_orig , r_9 , r_9_orig , r_10 , r_11 , r_11_orig ])
134
+ r_12 = algorithm_func (G_7 )
135
+ r_12_orig = algorithm_func .orig_func (G_7 ) # type: ignore
136
+
137
+ r_13 = algorithm_func (G_8 )
138
+ r_13_orig = algorithm_func .orig_func (G_8 ) # type: ignore
139
+
125
140
assert_func (r_7 , r_7_orig )
126
141
assert_func (r_8 , r_8_orig )
127
142
assert_func (r_9 , r_9_orig )
@@ -134,6 +149,14 @@ def test_algorithm(
134
149
assert_func (r_7 , r_11 )
135
150
assert_func (r_8 , r_11 )
136
151
assert_func (r_11 , r_11_orig )
152
+ assert_func (r_12 , r_12_orig )
153
+ assert_func (r_13 , r_13_orig )
154
+ assert r_12 != r_13
155
+ assert r_12_orig != r_13_orig
156
+ assert_func (r_8 , r_12 )
157
+ assert_func (r_8_orig , r_12_orig )
158
+ assert_func (r_9 , r_13 )
159
+ assert_func (r_9_orig , r_13_orig )
137
160
138
161
139
162
def test_shortest_path_remote_algorithm (load_graph : Any ) -> None :
@@ -157,6 +180,7 @@ def test_shortest_path_remote_algorithm(load_graph: Any) -> None:
157
180
(nxadb .Graph ),
158
181
(nxadb .DiGraph ),
159
182
(nxadb .MultiGraph ),
183
+ (nxadb .MultiDiGraph ),
160
184
],
161
185
)
162
186
def test_nodes_crud (load_graph : Any , graph_cls : type [nxadb .Graph ]) -> None :
@@ -741,6 +765,176 @@ def test_multigraph_edges_crud(load_graph: Any) -> None:
741
765
assert db .document (edge_id )["object" ]["sub_object" ]["foo" ] == "baz"
742
766
743
767
768
+ def test_multidigraph_edges_crud (load_graph : Any ) -> None :
769
+ G_1 = nxadb .MultiDiGraph (graph_name = "KarateGraph" )
770
+ G_2 = G_NX
771
+
772
+ assert len (G_1 .adj ) == len (G_2 .adj )
773
+ assert len (G_1 .edges ) == len (G_2 .edges )
774
+ assert G_1 .number_of_edges () == G_2 .number_of_edges ()
775
+
776
+ for src , dst , w in G_1 .edges .data ("weight" ):
777
+ assert G_1 .adj [src ][dst ][0 ]["weight" ] == w
778
+
779
+ for src , dst , w in G_1 .edges .data ("bad_key" , default = "boom!" ):
780
+ assert "bad_key" not in G_1 .adj [src ][dst ][0 ]
781
+ assert w == "boom!"
782
+
783
+ for k , edge_key_dict in G_1 .adj ["person/1" ].items ():
784
+ assert db .has_document (k )
785
+ assert db .has_document (edge_key_dict [0 ]["_id" ])
786
+
787
+ G_1 .add_edge ("person/1" , "person/1" , foo = "bar" , _edge_type = "knows" )
788
+ edge_id = G_1 .adj ["person/1" ]["person/1" ][0 ]["_id" ]
789
+ doc = db .document (edge_id )
790
+ assert doc ["foo" ] == "bar"
791
+ assert G_1 .adj ["person/1" ]["person/1" ][0 ]["foo" ] == "bar"
792
+
793
+ del G_1 .adj ["person/1" ]["person/1" ][0 ]["foo" ]
794
+ doc = db .document (edge_id )
795
+ assert "foo" not in doc
796
+
797
+ G_1 .adj ["person/1" ]["person/1" ][0 ].update ({"bar" : "foo" })
798
+ doc = db .document (edge_id )
799
+ assert doc ["bar" ] == "foo"
800
+
801
+ assert len (G_1 .adj ["person/1" ]["person/1" ][0 ]) == len (doc )
802
+ adj_count = len (G_1 .adj ["person/1" ])
803
+ G_1 .remove_edge ("person/1" , "person/1" )
804
+ assert len (G_1 .adj ["person/1" ]) == adj_count - 1
805
+ assert not db .has_document (edge_id )
806
+ assert "person/1" in G_1
807
+
808
+ assert not db .has_document ("person/new_node_1" )
809
+ col_count = db .collection ("knows" ).count ()
810
+
811
+ G_1 .add_edge ("new_node_1" , "new_node_2" , foo = "bar" )
812
+ assert db .document (G_1 ["new_node_1" ]["new_node_2" ][0 ]["_id" ])["foo" ] == "bar"
813
+ G_1 .add_edge ("new_node_1" , "new_node_2" , foo = "bar" , bar = "foo" )
814
+ doc = db .document (G_1 ["new_node_1" ]["new_node_2" ][1 ]["_id" ])
815
+ assert doc ["foo" ] == "bar"
816
+ assert doc ["bar" ] == "foo"
817
+
818
+ bind_vars = {
819
+ "src" : f"{ G_1 .default_node_type } /new_node_1" ,
820
+ "dst" : f"{ G_1 .default_node_type } /new_node_2" ,
821
+ }
822
+
823
+ result = list (
824
+ db .aql .execute (
825
+ f"FOR e IN knows FILTER e._from == @src AND e._to == @dst RETURN e" , # noqa
826
+ bind_vars = bind_vars ,
827
+ )
828
+ )
829
+
830
+ assert len (result ) == 2
831
+
832
+ result = list (
833
+ db .aql .execute (
834
+ f"FOR e IN knows FILTER e._from == @dst AND e._to == @src RETURN e" , # noqa
835
+ bind_vars = bind_vars ,
836
+ )
837
+ )
838
+
839
+ assert len (result ) == 0
840
+
841
+ assert db .collection ("knows" ).count () == col_count + 2
842
+ assert G_1 .adj ["new_node_1" ]["new_node_2" ][0 ]
843
+ assert G_1 .adj ["new_node_1" ]["new_node_2" ][0 ]["foo" ] == "bar"
844
+ assert G_1 .pred ["new_node_2" ]["new_node_1" ][0 ]
845
+ assert "new_node_1" not in G_1 .adj ["new_node_2" ]
846
+ assert (
847
+ G_1 .adj ["new_node_1" ]["new_node_2" ][0 ]["_id" ]
848
+ == G_1 .pred ["new_node_2" ]["new_node_1" ][0 ]["_id" ]
849
+ )
850
+ edge_id = G_1 .adj ["new_node_1" ]["new_node_2" ][0 ]["_id" ]
851
+ doc = db .document (edge_id )
852
+ assert db .has_document (doc ["_from" ])
853
+ assert db .has_document (doc ["_to" ])
854
+ assert G_1 .nodes ["new_node_1" ]
855
+ assert G_1 .nodes ["new_node_2" ]
856
+
857
+ assert len (G_1 .adj ["new_node_1" ]["new_node_2" ]) == 2
858
+ G_1 .remove_edge ("new_node_1" , "new_node_2" )
859
+ G_1 .clear ()
860
+ assert "new_node_1" in G_1
861
+ assert "new_node_2" in G_1
862
+ assert "new_node_2" in G_1 .adj ["new_node_1" ]
863
+ assert len (G_1 .adj ["new_node_1" ]["new_node_2" ]) == 1
864
+
865
+ G_1 .add_edges_from (
866
+ [("new_node_1" , "new_node_2" ), ("new_node_1" , "new_node_3" )], foo = "bar"
867
+ )
868
+ G_1 .clear ()
869
+ assert "new_node_1" in G_1
870
+ assert "new_node_2" in G_1
871
+ assert "new_node_3" in G_1
872
+
873
+ for k in G_1 .adj ["new_node_1" ]["new_node_2" ]:
874
+ assert G_1 .adj ["new_node_1" ]["new_node_2" ][k ]["foo" ] == "bar"
875
+ assert G_1 .pred ["new_node_2" ]["new_node_1" ][k ]["foo" ] == "bar"
876
+
877
+ for k in G_1 .adj ["new_node_1" ]["new_node_3" ]:
878
+ assert G_1 .adj ["new_node_1" ]["new_node_3" ][k ]["foo" ] == "bar"
879
+ assert G_1 .pred ["new_node_3" ]["new_node_1" ][k ]["foo" ] == "bar"
880
+
881
+ assert len (G_1 .adj ["new_node_1" ]["new_node_2" ]) == 2
882
+ assert len (G_1 .adj ["new_node_1" ]["new_node_3" ]) == 1
883
+ G_1 .remove_edges_from ([("new_node_1" , "new_node_2" ), ("new_node_1" , "new_node_3" )])
884
+ assert len (G_1 .adj ["new_node_1" ]["new_node_2" ]) == 1
885
+
886
+ assert "new_node_1" in G_1
887
+ assert "new_node_2" in G_1
888
+ assert "new_node_3" in G_1
889
+ assert "new_node_2" in G_1 .adj ["new_node_1" ]
890
+ assert "new_node_3" not in G_1 .adj ["new_node_1" ]
891
+
892
+ edge_id = "knows/1"
893
+ assert "person/1" not in G_1 ["person/2" ]
894
+ assert (
895
+ G_1 .succ ["person/1" ]["person/2" ][edge_id ]
896
+ == G_1 .pred ["person/2" ]["person/1" ][edge_id ]
897
+ )
898
+ new_weight = 1000
899
+ G_1 ["person/1" ]["person/2" ][edge_id ]["weight" ] = new_weight
900
+ assert G_1 .succ ["person/1" ]["person/2" ][edge_id ]["weight" ] == new_weight
901
+ assert G_1 .pred ["person/2" ]["person/1" ][edge_id ]["weight" ] == new_weight
902
+ G_1 .clear ()
903
+ assert G_1 .succ ["person/1" ]["person/2" ][edge_id ]["weight" ] == new_weight
904
+ G_1 .clear ()
905
+ assert G_1 .pred ["person/2" ]["person/1" ][edge_id ]["weight" ] == new_weight
906
+
907
+ edge_id = G_1 ["person/1" ]["person/2" ][edge_id ]["_id" ]
908
+ G_1 ["person/1" ]["person/2" ][edge_id ]["object" ] = {"foo" : "bar" , "bar" : "foo" }
909
+ assert "_rev" not in G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]
910
+ assert isinstance (G_1 ["person/1" ]["person/2" ][edge_id ]["object" ], EdgeAttrDict )
911
+ assert db .document (edge_id )["object" ] == {"foo" : "bar" , "bar" : "foo" }
912
+
913
+ G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["foo" ] = "baz"
914
+ assert db .document (edge_id )["object" ]["foo" ] == "baz"
915
+
916
+ del G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["foo" ]
917
+ assert "_rev" not in G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]
918
+ assert isinstance (G_1 ["person/1" ]["person/2" ][edge_id ]["object" ], EdgeAttrDict )
919
+ assert "foo" not in db .document (edge_id )["object" ]
920
+
921
+ G_1 ["person/1" ]["person/2" ][edge_id ]["object" ].update (
922
+ {"sub_object" : {"foo" : "bar" }}
923
+ )
924
+ assert "_rev" not in G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["sub_object" ]
925
+ assert isinstance (
926
+ G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["sub_object" ], EdgeAttrDict
927
+ )
928
+ assert db .document (edge_id )["object" ]["sub_object" ]["foo" ] == "bar"
929
+
930
+ G_1 .clear ()
931
+
932
+ assert G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["sub_object" ]["foo" ] == "bar"
933
+ G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["sub_object" ]["foo" ] = "baz"
934
+ assert "_rev" not in G_1 ["person/1" ]["person/2" ][edge_id ]["object" ]["sub_object" ]
935
+ assert db .document (edge_id )["object" ]["sub_object" ]["foo" ] == "baz"
936
+
937
+
744
938
def test_graph_dict_init (load_graph : Any ) -> None :
745
939
G = nxadb .Graph (graph_name = "KarateGraph" , default_node_type = "person" )
746
940
assert db .collection ("_graphs" ).has ("KarateGraph" )
0 commit comments