@@ -46,20 +46,22 @@ func ensureGraph(ctx context.Context, db driver.Database, name string, options *
46
46
47
47
// TestCreateGraph creates a graph and then checks that it exists.
48
48
func TestCreateGraph (t * testing.T ) {
49
+ ctx := context .Background ()
49
50
c := createClientFromEnv (t , true )
50
- db := ensureDatabase (nil , c , "graph_test" , nil , t )
51
+ db := ensureDatabase (ctx , c , "graph_test" , nil , t )
51
52
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 {
53
55
t .Fatalf ("Failed to create graph '%s': %s" , name , describe (err ))
54
56
}
55
57
// Graph must exist now
56
- if found , err := db .GraphExists (nil , name ); err != nil {
58
+ if found , err := db .GraphExists (ctx , name ); err != nil {
57
59
t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
58
60
} else if ! found {
59
61
t .Errorf ("GraphExists('%s') return false, expected true" , name )
60
62
}
61
63
// Graph must be listed
62
- if list , err := db .Graphs (nil ); err != nil {
64
+ if list , err := db .Graphs (ctx ); err != nil {
63
65
t .Errorf ("Graphs failed: %s" , describe (err ))
64
66
} else {
65
67
found := false
@@ -74,34 +76,116 @@ func TestCreateGraph(t *testing.T) {
74
76
}
75
77
}
76
78
// Open graph
77
- if g , err := db .Graph (nil , name ); err != nil {
79
+ if g , err := db .Graph (ctx , name ); err != nil {
78
80
t .Errorf ("Graph('%s') failed: %s" , name , describe (err ))
79
81
} else if g .Name () != name {
80
82
t .Errorf ("Graph.Name wrong. Expected '%s', got '%s'" , name , g .Name ())
81
83
}
82
84
}
83
85
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
+
84
167
// TestRemoveGraph creates a graph and then removes it.
85
168
func TestRemoveGraph (t * testing.T ) {
169
+ ctx := context .Background ()
86
170
c := createClientFromEnv (t , true )
87
- db := ensureDatabase (nil , c , "graph_test" , nil , t )
171
+ db := ensureDatabase (ctx , c , "graph_test" , nil , t )
88
172
name := "test_remove_graph"
89
- g , err := db .CreateGraphV2 (nil , name , nil )
173
+ g , err := db .CreateGraphV2 (ctx , name , nil )
90
174
if err != nil {
91
175
t .Fatalf ("Failed to create graph '%s': %s" , name , describe (err ))
92
176
}
93
177
// Graph must exist now
94
- if found , err := db .GraphExists (nil , name ); err != nil {
178
+ if found , err := db .GraphExists (ctx , name ); err != nil {
95
179
t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
96
180
} else if ! found {
97
181
t .Errorf ("GraphExists('%s') return false, expected true" , name )
98
182
}
99
183
// Now remove it
100
- if err := g .Remove (nil ); err != nil {
184
+ if err := g .Remove (ctx ); err != nil {
101
185
t .Fatalf ("Failed to remove graph '%s': %s" , name , describe (err ))
102
186
}
103
187
// 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 {
105
189
t .Errorf ("GraphExists('%s') failed: %s" , name , describe (err ))
106
190
} else if found {
107
191
t .Errorf ("GraphExists('%s') return true, expected false" , name )
0 commit comments