@@ -14,13 +14,14 @@ import (
14
14
"testing"
15
15
"time"
16
16
17
+ "go.mongodb.org/mongo-driver/bson"
18
+ "go.mongodb.org/mongo-driver/event"
17
19
"go.mongodb.org/mongo-driver/internal/testutil/assert"
18
20
"go.mongodb.org/mongo-driver/internal/testutil/israce"
19
21
"go.mongodb.org/mongo-driver/mongo"
20
22
"go.mongodb.org/mongo-driver/mongo/gridfs"
21
23
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
22
24
"go.mongodb.org/mongo-driver/mongo/options"
23
- "go.mongodb.org/mongo-driver/x/bsonx"
24
25
)
25
26
26
27
func TestGridFS (x * testing.T ) {
@@ -45,6 +46,66 @@ func TestGridFS(x *testing.T) {
45
46
findIndex (findCtx , mt , mt .DB .Collection ("fs.files" ), false , "key" , "filename" )
46
47
findIndex (findCtx , mt , mt .DB .Collection ("fs.chunks" ), true , "key" , "files_id" )
47
48
})
49
+ // should not create a new index if index is numerically the same
50
+ mt .Run ("numericallyEqualIndexes" , func (mt * mtest.T ) {
51
+ //add indexes with floats to collections manually
52
+ res := mt .DB .RunCommand (context .Background (),
53
+ bson.D {
54
+ {"createIndexes" , "fs.files" },
55
+ {"indexes" , bson.A {
56
+ bson.D {
57
+ {"key" , bson.D {{"filename" , float64 (1.0 )}, {"uploadDate" , float64 (1.0 )}}},
58
+ {"name" , "filename_1_uploadDate_1" },
59
+ },
60
+ }},
61
+ },
62
+ )
63
+ assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
64
+
65
+ res = mt .DB .RunCommand (context .Background (),
66
+ bson.D {
67
+ {"createIndexes" , "fs.chunks" },
68
+ {"indexes" , bson.A {
69
+ bson.D {
70
+ {"key" , bson.D {{"files_id" , float64 (1.0 )}, {"n" , float64 (1.0 )}}},
71
+ {"name" , "files_id_1_n_1" },
72
+ {"unique" , true },
73
+ },
74
+ }},
75
+ },
76
+ )
77
+ assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
78
+
79
+ mt .ClearEvents ()
80
+ mt .Run ("OpenUploadStream" , func (mt * mtest.T ) {
81
+ bucket , err := gridfs .NewBucket (mt .DB )
82
+ assert .Nil (mt , err , "NewBucket error: %v" , err )
83
+
84
+ _ , err = bucket .OpenUploadStream ("filename" )
85
+ assert .Nil (mt , err , "OpenUploadStream error: %v" , err )
86
+
87
+ mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
88
+ return evt .CommandName == "createIndexes"
89
+ })
90
+ evt := mt .GetStartedEvent ()
91
+ assert .Nil (mt , evt , "Expected that createIndexes wasn't called: %v" , evt )
92
+ })
93
+ mt .Run ("UploadFromStream" , func (mt * mtest.T ) {
94
+ var fileContent []byte
95
+ bucket , err := gridfs .NewBucket (mt .DB )
96
+ assert .Nil (mt , err , "NewBucket error: %v" , err )
97
+
98
+ _ , err = bucket .UploadFromStream ("filename" , bytes .NewBuffer (fileContent ))
99
+ assert .Nil (mt , err , "UploadFromStream error: %v" , err )
100
+
101
+ mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
102
+ return evt .CommandName == "createIndexes"
103
+ })
104
+ evt := mt .GetStartedEvent ()
105
+ assert .Nil (mt , evt , "Expected that createIndexes wasn't called: %v" , evt )
106
+ })
107
+ })
108
+
48
109
mt .RunOpts ("round trip" , mtest .NewOptions ().MaxServerVersion ("3.6" ), func (mt * mtest.T ) {
49
110
skipRoundTripTest (mt )
50
111
oneK := 1024
@@ -130,15 +191,27 @@ func findIndex(ctx context.Context, mt *mtest.T, coll *mongo.Collection, unique
130
191
assert .True (mt , foundIndex , "index %v not found" , keys )
131
192
}
132
193
194
+ func countIndexes (ctx context.Context , mt * mtest.T , coll * mongo.Collection ) int {
195
+ mt .Helper ()
196
+ cur , err := coll .Indexes ().List (ctx )
197
+ assert .Nil (mt , err , "Indexes List error: %v" , err )
198
+
199
+ var count int
200
+ for cur .Next (ctx ) {
201
+ count ++
202
+ }
203
+ return count
204
+ }
205
+
133
206
func skipRoundTripTest (mt * mtest.T ) {
134
207
if runtime .GOOS != "darwin" {
135
208
return
136
209
}
137
210
138
- var serverStatus bsonx. Doc
211
+ var serverStatus bson. Raw
139
212
err := mt .DB .RunCommand (
140
213
context .Background (),
141
- bsonx. Doc {{"serverStatus" , bsonx . Int32 ( 1 ) }},
214
+ bson. D {{"serverStatus" , 1 }},
142
215
).Decode (& serverStatus )
143
216
assert .Nil (mt , err , "serverStatus error %v" , err )
144
217
0 commit comments