4
4
// not use this file except in compliance with the License. You may obtain
5
5
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
6
7
- package testhelpers // import "go.mongodb.org/mongo-driver/internal/testutil/ helpers"
7
+ package helpers
8
8
9
9
import (
10
10
"fmt"
11
11
"io/ioutil"
12
- "math"
13
12
"path"
14
- "strings"
15
- "time"
16
-
17
13
"testing"
18
14
19
- "io"
20
-
21
- "reflect"
22
-
23
15
"github.com/stretchr/testify/require"
24
16
"go.mongodb.org/mongo-driver/bson"
25
- "go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
26
17
)
27
18
28
- // Test helpers
29
-
30
- // IsNil returns true if the object is nil
31
- func IsNil (object interface {}) bool {
32
- if object == nil {
33
- return true
34
- }
35
-
36
- value := reflect .ValueOf (object )
37
- kind := value .Kind ()
38
-
39
- // checking to see if type is Chan, Func, Interface, Map, Ptr, or Slice
40
- if kind >= reflect .Chan && kind <= reflect .Slice && value .IsNil () {
41
- return true
42
- }
43
-
44
- return false
45
- }
46
-
47
- // RequireNotNil throws an error if var is nil
48
- func RequireNotNil (t * testing.T , variable interface {}, msgFormat string , msgVars ... interface {}) {
49
- if IsNil (variable ) {
50
- t .Fatalf (msgFormat , msgVars ... )
51
- }
52
- }
53
-
54
- // RequireNil throws an error if var is not nil
55
- func RequireNil (t * testing.T , variable interface {}, msgFormat string , msgVars ... interface {}) {
56
- t .Helper ()
57
- if ! IsNil (variable ) {
58
- t .Fatalf (msgFormat , msgVars ... )
59
- }
60
- }
61
-
62
19
// FindJSONFilesInDir finds the JSON files in a directory.
63
20
func FindJSONFilesInDir (t * testing.T , dir string ) []string {
21
+ t .Helper ()
22
+
64
23
files := make ([]string , 0 )
65
24
66
25
entries , err := ioutil .ReadDir (dir )
@@ -77,202 +36,26 @@ func FindJSONFilesInDir(t *testing.T, dir string) []string {
77
36
return files
78
37
}
79
38
80
- // RequireNoErrorOnClose ensures there is not an error when calling Close.
81
- func RequireNoErrorOnClose (t * testing.T , c io.Closer ) {
82
- require .NoError (t , c .Close ())
83
- }
84
-
85
- // VerifyConnStringOptions verifies the options on the connection string.
86
- func VerifyConnStringOptions (t * testing.T , cs connstring.ConnString , options map [string ]interface {}) {
87
- // Check that all options are present.
88
- for key , value := range options {
89
-
90
- key = strings .ToLower (key )
91
- switch key {
92
- case "appname" :
93
- require .Equal (t , value , cs .AppName )
94
- case "authsource" :
95
- require .Equal (t , value , cs .AuthSource )
96
- case "authmechanism" :
97
- require .Equal (t , value , cs .AuthMechanism )
98
- case "authmechanismproperties" :
99
- convertedMap := value .(map [string ]interface {})
100
- require .Equal (t ,
101
- mapInterfaceToString (convertedMap ),
102
- cs .AuthMechanismProperties )
103
- case "compressors" :
104
- require .Equal (t , convertToStringSlice (value ), cs .Compressors )
105
- case "connecttimeoutms" :
106
- require .Equal (t , value , float64 (cs .ConnectTimeout / time .Millisecond ))
107
- case "directconnection" :
108
- require .True (t , cs .DirectConnectionSet )
109
- require .Equal (t , value , cs .DirectConnection )
110
- case "heartbeatfrequencyms" :
111
- require .Equal (t , value , float64 (cs .HeartbeatInterval / time .Millisecond ))
112
- case "journal" :
113
- require .True (t , cs .JSet )
114
- require .Equal (t , value , cs .J )
115
- case "loadbalanced" :
116
- require .True (t , cs .LoadBalancedSet )
117
- require .Equal (t , value , cs .LoadBalanced )
118
- case "localthresholdms" :
119
- require .True (t , cs .LocalThresholdSet )
120
- require .Equal (t , value , float64 (cs .LocalThreshold / time .Millisecond ))
121
- case "maxidletimems" :
122
- require .Equal (t , value , float64 (cs .MaxConnIdleTime / time .Millisecond ))
123
- case "maxpoolsize" :
124
- require .True (t , cs .MaxPoolSizeSet )
125
- require .Equal (t , value , cs .MaxPoolSize )
126
- case "maxstalenessseconds" :
127
- require .True (t , cs .MaxStalenessSet )
128
- require .Equal (t , value , float64 (cs .MaxStaleness / time .Second ))
129
- case "minpoolsize" :
130
- require .True (t , cs .MinPoolSizeSet )
131
- require .Equal (t , value , int64 (cs .MinPoolSize ))
132
- case "readpreference" :
133
- require .Equal (t , value , cs .ReadPreference )
134
- case "readpreferencetags" :
135
- sm , ok := value .([]interface {})
136
- require .True (t , ok )
137
- tags := make ([]map [string ]string , 0 , len (sm ))
138
- for _ , i := range sm {
139
- m , ok := i .(map [string ]interface {})
140
- require .True (t , ok )
141
- tags = append (tags , mapInterfaceToString (m ))
142
- }
143
- require .Equal (t , tags , cs .ReadPreferenceTagSets )
144
- case "readconcernlevel" :
145
- require .Equal (t , value , cs .ReadConcernLevel )
146
- case "replicaset" :
147
- require .Equal (t , value , cs .ReplicaSet )
148
- case "retrywrites" :
149
- require .True (t , cs .RetryWritesSet )
150
- require .Equal (t , value , cs .RetryWrites )
151
- case "serverselectiontimeoutms" :
152
- require .Equal (t , value , float64 (cs .ServerSelectionTimeout / time .Millisecond ))
153
- case "srvmaxhosts" :
154
- require .Equal (t , value , float64 (cs .SRVMaxHosts ))
155
- case "srvservicename" :
156
- require .Equal (t , value , cs .SRVServiceName )
157
- case "ssl" , "tls" :
158
- require .Equal (t , value , cs .SSL )
159
- case "sockettimeoutms" :
160
- require .Equal (t , value , float64 (cs .SocketTimeout / time .Millisecond ))
161
- case "tlsallowinvalidcertificates" , "tlsallowinvalidhostnames" , "tlsinsecure" :
162
- require .True (t , cs .SSLInsecureSet )
163
- require .Equal (t , value , cs .SSLInsecure )
164
- case "tlscafile" :
165
- require .True (t , cs .SSLCaFileSet )
166
- require .Equal (t , value , cs .SSLCaFile )
167
- case "tlscertificatekeyfile" :
168
- require .True (t , cs .SSLClientCertificateKeyFileSet )
169
- require .Equal (t , value , cs .SSLClientCertificateKeyFile )
170
- case "tlscertificatekeyfilepassword" :
171
- require .True (t , cs .SSLClientCertificateKeyPasswordSet )
172
- require .Equal (t , value , cs .SSLClientCertificateKeyPassword ())
173
- case "w" :
174
- if cs .WNumberSet {
175
- valueInt := GetIntFromInterface (value )
176
- require .NotNil (t , valueInt )
177
- require .Equal (t , * valueInt , int64 (cs .WNumber ))
178
- } else {
179
- require .Equal (t , value , cs .WString )
180
- }
181
- case "wtimeoutms" :
182
- require .Equal (t , value , float64 (cs .WTimeout / time .Millisecond ))
183
- case "waitqueuetimeoutms" :
184
- case "zlibcompressionlevel" :
185
- require .Equal (t , value , float64 (cs .ZlibLevel ))
186
- case "zstdcompressionlevel" :
187
- require .Equal (t , value , float64 (cs .ZstdLevel ))
188
- case "tlsdisableocspendpointcheck" :
189
- require .Equal (t , value , cs .SSLDisableOCSPEndpointCheck )
190
- default :
191
- opt , ok := cs .UnknownOptions [key ]
192
- require .True (t , ok )
193
- require .Contains (t , opt , fmt .Sprint (value ))
194
- }
39
+ // RawToDocuments converts a bson.Raw that is internally an array of documents to []bson.Raw.
40
+ func RawToDocuments (doc bson.Raw ) []bson.Raw {
41
+ values , err := doc .Values ()
42
+ if err != nil {
43
+ panic (fmt .Sprintf ("error converting BSON document to values: %v" , err ))
195
44
}
196
- }
197
45
198
- // RawSliceToInterfaceSlice converts a []bson.Raw to []interface{}.
199
- func RawSliceToInterfaceSlice (elems []bson.Raw ) []interface {} {
200
- out := make ([]interface {}, 0 , len (elems ))
201
- for _ , elem := range elems {
202
- out = append (out , elem )
203
- }
204
- return out
205
- }
206
-
207
- // RawToInterfaceSlice converts a bson.Raw that is internally an array to []interface{}.
208
- func RawToInterfaceSlice (doc bson.Raw ) []interface {} {
209
- values , _ := doc .Values ()
210
-
211
- out := make ([]interface {}, 0 , len (values ))
212
- for _ , val := range values {
213
- out = append (out , val .Document ())
46
+ out := make ([]bson.Raw , len (values ))
47
+ for i := range values {
48
+ out [i ] = values [i ].Document ()
214
49
}
215
50
216
51
return out
217
52
}
218
53
219
- // Convert each interface{} value in the map to a string.
220
- func mapInterfaceToString (m map [string ]interface {}) map [string ]string {
221
- out := make (map [string ]string )
222
-
223
- for key , value := range m {
224
- out [key ] = fmt .Sprint (value )
54
+ // RawToInterfaces takes one or many bson.Raw documents and returns them as a []interface{}.
55
+ func RawToInterfaces (docs ... bson.Raw ) []interface {} {
56
+ out := make ([]interface {}, len (docs ))
57
+ for i := range docs {
58
+ out [i ] = docs [i ]
225
59
}
226
-
227
60
return out
228
61
}
229
-
230
- func convertToStringSlice (i interface {}) []string {
231
- s , ok := i .([]interface {})
232
- if ! ok {
233
- return nil
234
- }
235
- ret := make ([]string , 0 , len (s ))
236
- for _ , v := range s {
237
- str , ok := v .(string )
238
- if ! ok {
239
- continue
240
- }
241
- ret = append (ret , str )
242
- }
243
- return ret
244
- }
245
-
246
- // GetIntFromInterface attempts to convert an empty interface value to an integer.
247
- //
248
- // Returns nil if it is not possible.
249
- func GetIntFromInterface (i interface {}) * int64 {
250
- var out int64
251
-
252
- switch v := i .(type ) {
253
- case int :
254
- out = int64 (v )
255
- case int32 :
256
- out = int64 (v )
257
- case int64 :
258
- out = v
259
- case float32 :
260
- f := float64 (v )
261
- if math .Floor (f ) != f || f > float64 (math .MaxInt64 ) {
262
- break
263
- }
264
-
265
- out = int64 (f )
266
-
267
- case float64 :
268
- if math .Floor (v ) != v || v > float64 (math .MaxInt64 ) {
269
- break
270
- }
271
-
272
- out = int64 (v )
273
- default :
274
- return nil
275
- }
276
-
277
- return & out
278
- }
0 commit comments