@@ -46,20 +46,22 @@ func ensureGraph(ctx context.Context, db driver.Database, name string, options *
4646
4747// TestCreateGraph creates a graph and then checks that it exists.
4848func TestCreateGraph (t * testing.T ) {
49+ ctx := context .Background ()
4950 c := createClientFromEnv (t , true )
50- db := ensureDatabase (nil , c , "graph_test" , nil , t )
51+ db := ensureDatabase (ctx , c , "graph_test" , nil , t )
5152 name := "test_create_graph"
52- if _ , err := db .CreateGraphV2 (nil , name , nil ); err != nil {
53+
54+ if _ , err := db .CreateGraphV2 (ctx , name , nil ); err != nil {
5355 t .Fatalf ("Failed to create graph '%s': %s" , name , describe (err ))
5456 }
5557 // Graph must exist now
56- if found , err := db .GraphExists (nil , name ); err != nil {
58+ if found , err := db .GraphExists (ctx , name ); err != nil {
5759 t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
5860 } else if ! found {
5961 t .Errorf ("GraphExists('%s') return false, expected true" , name )
6062 }
6163 // Graph must be listed
62- if list , err := db .Graphs (nil ); err != nil {
64+ if list , err := db .Graphs (ctx ); err != nil {
6365 t .Errorf ("Graphs failed: %s" , describe (err ))
6466 } else {
6567 found := false
@@ -74,34 +76,116 @@ func TestCreateGraph(t *testing.T) {
7476 }
7577 }
7678 // Open graph
77- if g , err := db .Graph (nil , name ); err != nil {
79+ if g , err := db .Graph (ctx , name ); err != nil {
7880 t .Errorf ("Graph('%s') failed: %s" , name , describe (err ))
7981 } else if g .Name () != name {
8082 t .Errorf ("Graph.Name wrong. Expected '%s', got '%s'" , name , g .Name ())
8183 }
8284}
8385
86+ // TestCreateGraphWithOptions creates a graph with options then checks if each options is set correctly.
87+ func TestCreateGraphWithOptions (t * testing.T ) {
88+ ctx := context .Background ()
89+ c := createClientFromEnv (t , true )
90+ skipBelowVersion (c , "3.6" , t )
91+ skipNoCluster (c , t )
92+
93+ db := ensureDatabase (ctx , c , "graph_test" , nil , t )
94+ name := "test_create_graph_2"
95+
96+ options := & driver.CreateGraphOptions {
97+ OrphanVertexCollections : []string {"orphan1" , "orphan2" },
98+ EdgeDefinitions : []driver.EdgeDefinition {
99+ {
100+ Collection : "coll" ,
101+ To : []string {"to-coll1" },
102+ From : []string {"from-coll1" },
103+ },
104+ },
105+ NumberOfShards : 2 ,
106+ ReplicationFactor : 3 ,
107+ WriteConcern : 2 ,
108+ SmartGraphAttribute : "orphan1" ,
109+ }
110+
111+ if _ , err := db .CreateGraphV2 (ctx , name , options ); err != nil {
112+ t .Fatalf ("Failed to create graph '%s': %s" , name , describe (err ))
113+ }
114+ // Graph must exist now
115+ if found , err := db .GraphExists (ctx , name ); err != nil {
116+ t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
117+ } else if ! found {
118+ t .Errorf ("GraphExists('%s') return false, expected true" , name )
119+ }
120+ // Graph must be listed
121+ if list , err := db .Graphs (ctx ); err != nil {
122+ t .Errorf ("Graphs failed: %s" , describe (err ))
123+ } else {
124+ found := false
125+ for _ , g := range list {
126+ if g .Name () == name {
127+ found = true
128+ break
129+ }
130+ }
131+ if ! found {
132+ t .Errorf ("Graph '%s' not found in list" , name )
133+ }
134+ }
135+
136+ // Open graph
137+ g , err := db .Graph (ctx , name )
138+ if err != nil {
139+ t .Errorf ("Graph('%s') failed: %s" , name , describe (err ))
140+ } else if g .Name () != name {
141+ t .Errorf ("Graph.Name wrong. Expected '%s', got '%s'" , name , g .Name ())
142+ }
143+
144+ if g .NumberOfShards () != options .NumberOfShards {
145+ t .Errorf ("Graph.NumberOfShards wrong. Expected '%d', got '%d'" , options .NumberOfShards , g .NumberOfShards ())
146+ }
147+ if g .ReplicationFactor () != options .ReplicationFactor {
148+ t .Errorf ("Graph.ReplicationFactor wrong. Expected '%d', got '%d'" , options .ReplicationFactor , g .ReplicationFactor ())
149+ }
150+ if g .WriteConcern () != options .WriteConcern {
151+ t .Errorf ("Graph.WriteConcern wrong. Expected '%d', got '%d'" , options .WriteConcern , g .WriteConcern ())
152+ }
153+ if g .EdgeDefinitions ()[0 ].Collection != options .EdgeDefinitions [0 ].Collection {
154+ t .Errorf ("Graph.EdgeDefinitions.collection wrong. Expected '%s', got '%s'" , options .EdgeDefinitions [0 ].Collection , g .EdgeDefinitions ()[0 ].Collection )
155+ }
156+ if g .EdgeDefinitions ()[0 ].From [0 ] != options .EdgeDefinitions [0 ].From [0 ] {
157+ t .Errorf ("Graph.EdgeDefinitions.from wrong. Expected '%s', got '%s'" , options .EdgeDefinitions [0 ].From [0 ], g .EdgeDefinitions ()[0 ].From [0 ])
158+ }
159+ if g .EdgeDefinitions ()[0 ].To [0 ] != options .EdgeDefinitions [0 ].To [0 ] {
160+ t .Errorf ("Graph.EdgeDefinitions.to wrong. Expected '%s', got '%s'" , options .EdgeDefinitions [0 ].To [0 ], g .EdgeDefinitions ()[0 ].To [0 ])
161+ }
162+ if g .OrphanCollections ()[0 ] != options .OrphanVertexCollections [0 ] && g .OrphanCollections ()[1 ] != options .OrphanVertexCollections [1 ] {
163+ t .Errorf ("Graph.IsSmart wrong. Expected '%v', got '%v'" , options .OrphanVertexCollections , g .OrphanCollections ())
164+ }
165+ }
166+
84167// TestRemoveGraph creates a graph and then removes it.
85168func TestRemoveGraph (t * testing.T ) {
169+ ctx := context .Background ()
86170 c := createClientFromEnv (t , true )
87- db := ensureDatabase (nil , c , "graph_test" , nil , t )
171+ db := ensureDatabase (ctx , c , "graph_test" , nil , t )
88172 name := "test_remove_graph"
89- g , err := db .CreateGraphV2 (nil , name , nil )
173+ g , err := db .CreateGraphV2 (ctx , name , nil )
90174 if err != nil {
91175 t .Fatalf ("Failed to create graph '%s': %s" , name , describe (err ))
92176 }
93177 // Graph must exist now
94- if found , err := db .GraphExists (nil , name ); err != nil {
178+ if found , err := db .GraphExists (ctx , name ); err != nil {
95179 t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
96180 } else if ! found {
97181 t .Errorf ("GraphExists('%s') return false, expected true" , name )
98182 }
99183 // Now remove it
100- if err := g .Remove (nil ); err != nil {
184+ if err := g .Remove (ctx ); err != nil {
101185 t .Fatalf ("Failed to remove graph '%s': %s" , name , describe (err ))
102186 }
103187 // Graph must not exist now
104- if found , err := db .GraphExists (nil , name ); err != nil {
188+ if found , err := db .GraphExists (ctx , name ); err != nil {
105189 t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
106190 } else if found {
107191 t .Errorf ("GraphExists('%s') return true, expected false" , name )
0 commit comments