@@ -47,67 +47,138 @@ func TestGridFS(x *testing.T) {
47
47
findIndex (findCtx , mt , mt .DB .Collection ("fs.chunks" ), true , "key" , "files_id" )
48
48
})
49
49
// should not create a new index if index is numerically the same
50
- mt .Run ("indexes not created if equivalent indexes exist" , 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
- }},
50
+ mt .Run ("equivalent indexes" , func (mt * mtest.T ) {
51
+ tests := []struct {
52
+ name string
53
+ filesIndex bson.D
54
+ chunksIndex bson.D
55
+ newIndexes bool
56
+ }{
57
+ {
58
+ "numerically equal" ,
59
+ bson.D {
60
+ {"key" , bson.D {{"filename" , float64 (1.0 )}, {"uploadDate" , float64 (1.0 )}}},
61
+ {"name" , "filename_1_uploadDate_1" },
62
+ },
63
+ bson.D {
64
+ {"key" , bson.D {{"files_id" , float64 (1.0 )}, {"n" , float64 (1.0 )}}},
65
+ {"name" , "files_id_1_n_1" },
66
+ {"unique" , true },
67
+ },
68
+ false ,
61
69
},
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
- }},
70
+ {
71
+ "numerically inequal" ,
72
+ bson.D {
73
+ {"key" , bson.D {{"filename" , float64 (- 1.0 )}, {"uploadDate" , float64 (1.0 )}}},
74
+ {"name" , "filename_-1_uploadDate_1" },
75
+ },
76
+ bson.D {
77
+ {"key" , bson.D {{"files_id" , float64 (1.0 )}, {"n" , float64 (- 1.0 )}}},
78
+ {"name" , "files_id_1_n_-1" },
79
+ {"unique" , true },
80
+ },
81
+ true ,
75
82
},
76
- )
77
- assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
83
+ }
84
+ for _ , test := range tests {
85
+ mt .Run (test .name , func (mt * mtest.T ) {
86
+ mt .Run ("OpenUploadStream" , func (mt * mtest.T ) {
87
+ // add indexes with floats to collections manually
88
+ res := mt .DB .RunCommand (context .Background (),
89
+ bson.D {
90
+ {"createIndexes" , "fs.files" },
91
+ {"indexes" , bson.A {
92
+ test .filesIndex ,
93
+ }},
94
+ },
95
+ )
96
+ assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
78
97
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 )
98
+ res = mt .DB .RunCommand (context .Background (),
99
+ bson.D {
100
+ {"createIndexes" , "fs.chunks" },
101
+ {"indexes" , bson.A {
102
+ test .chunksIndex ,
103
+ }},
104
+ },
105
+ )
106
+ assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
83
107
84
- _ , err = bucket .OpenUploadStream ("filename" )
85
- assert .Nil (mt , err , "OpenUploadStream error: %v" , err )
108
+ mt .ClearEvents ()
86
109
87
- mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
88
- return evt .CommandName == "createIndexes"
89
- })
90
- evt := mt .GetStartedEvent ()
91
- if evt != nil {
92
- mt .Fatalf ("expected no createIndexes events but got %v" , evt .Command )
93
- }
94
- })
95
- mt .Run ("UploadFromStream" , func (mt * mtest.T ) {
96
- var fileContent []byte
97
- bucket , err := gridfs .NewBucket (mt .DB )
98
- assert .Nil (mt , err , "NewBucket error: %v" , err )
110
+ bucket , err := gridfs .NewBucket (mt .DB )
111
+ assert .Nil (mt , err , "NewBucket error: %v" , err )
112
+ defer func () {
113
+ bucket .Drop ()
114
+ } ()
99
115
100
- _ , err = bucket .UploadFromStream ("filename" , bytes . NewBuffer ( fileContent ) )
101
- assert .Nil (mt , err , "UploadFromStream error: %v" , err )
116
+ _ , err = bucket .OpenUploadStream ("filename" )
117
+ assert .Nil (mt , err , "OpenUploadStream error: %v" , err )
102
118
103
- mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
104
- return evt .CommandName == "createIndexes"
119
+ mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
120
+ return evt .CommandName == "createIndexes"
121
+ })
122
+ evt := mt .GetStartedEvent ()
123
+ if test .newIndexes {
124
+ if evt == nil {
125
+ mt .Fatalf ("expected createIndexes events but got none" )
126
+ }
127
+ } else {
128
+ if evt != nil {
129
+ mt .Fatalf ("expected no createIndexes events but got %v" , evt .Command )
130
+ }
131
+ }
132
+ })
133
+ mt .Run ("UploadFromStream" , func (mt * mtest.T ) {
134
+ // add indexes with floats to collections manually
135
+ res := mt .DB .RunCommand (context .Background (),
136
+ bson.D {
137
+ {"createIndexes" , "fs.files" },
138
+ {"indexes" , bson.A {
139
+ test .filesIndex ,
140
+ }},
141
+ },
142
+ )
143
+ assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
144
+
145
+ res = mt .DB .RunCommand (context .Background (),
146
+ bson.D {
147
+ {"createIndexes" , "fs.chunks" },
148
+ {"indexes" , bson.A {
149
+ test .chunksIndex ,
150
+ }},
151
+ },
152
+ )
153
+ assert .Nil (mt , res .Err (), "createIndexes error: %v" , res .Err ())
154
+
155
+ mt .ClearEvents ()
156
+ var fileContent []byte
157
+ bucket , err := gridfs .NewBucket (mt .DB )
158
+ assert .Nil (mt , err , "NewBucket error: %v" , err )
159
+ defer func () {
160
+ bucket .Drop ()
161
+ } ()
162
+
163
+ _ , err = bucket .UploadFromStream ("filename" , bytes .NewBuffer (fileContent ))
164
+ assert .Nil (mt , err , "UploadFromStream error: %v" , err )
165
+
166
+ mt .FilterStartedEvents (func (evt * event.CommandStartedEvent ) bool {
167
+ return evt .CommandName == "createIndexes"
168
+ })
169
+ evt := mt .GetStartedEvent ()
170
+ if test .newIndexes {
171
+ if evt == nil {
172
+ mt .Fatalf ("expected createIndexes events but got none" )
173
+ }
174
+ } else {
175
+ if evt != nil {
176
+ mt .Fatalf ("expected no createIndexes events but got %v" , evt .Command )
177
+ }
178
+ }
179
+ })
105
180
})
106
- evt := mt .GetStartedEvent ()
107
- if evt != nil {
108
- mt .Fatalf ("expected no createIndexes events but got %v" , evt .Command )
109
- }
110
- })
181
+ }
111
182
})
112
183
113
184
mt .RunOpts ("round trip" , mtest .NewOptions ().MaxServerVersion ("3.6" ), func (mt * mtest.T ) {
0 commit comments