@@ -10,8 +10,11 @@ import (
10
10
"context"
11
11
"testing"
12
12
13
+ "fmt"
14
+ "os"
15
+
13
16
"github.com/mongodb/mongo-go-driver/bson"
14
- "github.com/mongodb/mongo-go-driver/core/option "
17
+ "github.com/mongodb/mongo-go-driver/bson/objectid "
15
18
"github.com/mongodb/mongo-go-driver/internal/testutil"
16
19
"github.com/stretchr/testify/require"
17
20
)
@@ -72,78 +75,152 @@ func TestDatabase_Drop(t *testing.T) {
72
75
73
76
}
74
77
75
- func TestDatabase_ListCollections (t * testing.T ) {
76
- t .Parallel ()
77
- dbName := "db_list_collection"
78
- db := createTestDatabase (t , & dbName )
79
- collName := "list_collections_name"
80
- coll := db .Collection (collName )
81
- require .Equal (t , coll .Name (), collName )
82
- require .NotNil (t , coll )
83
- cursor , err := db .ListCollections (context .Background (), nil )
84
- require .NoError (t , err )
78
+ // creates 1 normal collection and 1 capped collection of size 64*1024
79
+ func setupListCollectionsDb (db * Database ) (uncappedName string , cappedName string , err error ) {
80
+ uncappedName , cappedName = "listcoll_uncapped" , "listcoll_capped"
81
+ uncappedColl := db .Collection (uncappedName )
82
+
83
+ _ , err = db .RunCommand (
84
+ context .Background (),
85
+ bson .NewDocument (
86
+ bson .EC .String ("create" , cappedName ),
87
+ bson .EC .Boolean ("capped" , true ),
88
+ bson .EC .Int32 ("size" , 64 * 1024 ),
89
+ ),
90
+ )
91
+ if err != nil {
92
+ return "" , "" , err
93
+ }
94
+ cappedColl := db .Collection (cappedName )
95
+
96
+ id := objectid .New ()
97
+ want := bson .EC .ObjectID ("_id" , id )
98
+ doc := bson .NewDocument (want , bson .EC .Int32 ("x" , 1 ))
85
99
86
- next := bson .NewDocument ()
100
+ _ , err = uncappedColl .InsertOne (context .Background (), doc )
101
+ if err != nil {
102
+ return "" , "" , err
103
+ }
104
+
105
+ _ , err = cappedColl .InsertOne (context .Background (), doc )
106
+ if err != nil {
107
+ return "" , "" , err
108
+ }
109
+
110
+ return uncappedName , cappedName , nil
111
+ }
112
+
113
+ // verifies both collection names are found in cursor, cursor does not have extra collections, and cursor has no
114
+ // duplicates
115
+ func verifyListCollections (cursor Cursor , uncappedName string , cappedName string , cappedOnly bool ) (err error ) {
116
+ var uncappedFound bool
117
+ var cappedFound bool
87
118
88
119
for cursor .Next (context .Background ()) {
120
+ next := bson .NewDocument ()
89
121
err = cursor .Decode (next )
90
- require .NoError (t , err )
122
+ if err != nil {
123
+ return err
124
+ }
91
125
92
126
elem , err := next .LookupErr ("name" )
93
- require .NoError (t , err )
127
+ if err != nil {
128
+ return err
129
+ }
130
+
94
131
if elem .Type () != bson .TypeString {
95
- t .Errorf ("Incorrect type for 'name'. got %v; want %v" , elem .Type (), bson .TypeString )
96
- t .FailNow ()
132
+ return fmt .Errorf ("incorrect type for 'name'. got %v. want %v" , elem .Type (), bson .TypeString )
133
+ }
134
+
135
+ elemName := elem .StringValue ()
136
+
137
+ if elemName != uncappedName && elemName != cappedName {
138
+ return fmt .Errorf ("incorrect collection name. got: %s. wanted: %s or %s" , elemName , uncappedName ,
139
+ cappedName )
97
140
}
98
- if elem .StringValue () != collName {
99
- t .Errorf ("Incorrect collection name. got %s: want %s" , elem .StringValue (), collName )
100
- t .FailNow ()
141
+
142
+ if elemName == uncappedName && ! uncappedFound {
143
+ if cappedOnly {
144
+ return fmt .Errorf ("found uncapped collection %s. expected only capped collections" , uncappedName )
145
+ }
146
+
147
+ uncappedFound = true
148
+ continue
101
149
}
102
- //Because we run it without nameOnly parameter we should check if another parameter is exist
103
- docType , err := next .LookupErr ("type" )
104
- require .NoError (t , err )
105
- if docType .StringValue () != "collections" {
106
- t .Errorf ("Incorrect cursor type. got %s: want %s" , docType .StringValue (), "collections" )
107
- t .FailNow ()
150
+
151
+ if elemName == cappedName && ! cappedFound {
152
+ cappedFound = true
153
+ continue
108
154
}
155
+
156
+ // duplicate found
157
+ return fmt .Errorf ("found duplicate collection %s" , elemName )
158
+ }
159
+
160
+ if ! cappedFound {
161
+ return fmt .Errorf ("did not find collection %s" , cappedName )
162
+ }
163
+
164
+ if ! cappedOnly && ! uncappedFound {
165
+ return fmt .Errorf ("did not find collection %s" , uncappedName )
109
166
}
110
- defer func () {
111
- err := db .Drop (context .Background ())
112
- require .NoError (t , err )
113
- }()
167
+
168
+ return nil
114
169
}
115
170
116
- func TestDatabase_ListCollectionsOptions (t * testing.T ) {
117
- t .Parallel ()
118
- dbName := "db_list_collection_options"
119
- db := createTestDatabase (t , & dbName )
120
- collName := "list_collections_options"
121
- coll := db .Collection (collName )
122
- require .Equal (t , coll .Name (), collName )
123
- require .NotNil (t , coll )
124
- cursor , err := db .ListCollections (context .Background (), nil , option .OptNameOnly (true ))
125
- require .NoError (t , err )
171
+ func listCollectionsTest (db * Database , cappedOnly bool ) error {
172
+ uncappedName , cappedName , err := setupListCollectionsDb (db )
173
+ if err != nil {
174
+ return err
175
+ }
176
+
177
+ var filter * bson.Document
178
+ if cappedOnly {
179
+ filter = bson .NewDocument (
180
+ bson .EC .Boolean ("options.capped" , true ),
181
+ )
182
+ }
126
183
127
- next := bson .NewDocument ()
184
+ cursor , err := db .ListCollections (context .Background (), filter )
185
+ if err != nil {
186
+ return err
187
+ }
128
188
129
- for cursor .Next (context .Background ()) {
130
- err = cursor .Decode (next )
131
- require .NoError (t , err )
189
+ return verifyListCollections (cursor , uncappedName , cappedName , cappedOnly )
190
+ }
132
191
133
- elem , err := next .LookupErr ("name" )
134
- require .NoError (t , err )
192
+ func TestDatabase_ListCollections (t * testing.T ) {
193
+ // TODO(GODRIVER-272): Add tests for the replica_set topology using the secondary read preference
194
+ t .Parallel ()
135
195
136
- if elem .StringValue () != collName {
137
- t .Errorf ("Incorrect collection name. got %s: want %s" , elem .StringValue (), collName )
138
- t .FailNow ()
139
- }
196
+ var listCollectionsTable = []struct {
197
+ name string
198
+ expectedTopology string
199
+ cappedOnly bool
200
+ }{
201
+ {"standalone_nofilter" , "server" , false },
202
+ {"standalone_filter" , "server" , true },
203
+ {"replicaset_nofilter" , "replica_set" , false },
204
+ {"replicaset_filter" , "replica_set" , true },
205
+ {"sharded_nofilter" , "sharded_cluster" , false },
206
+ {"sharded_filter" , "sharded_cluster" , true },
207
+ }
140
208
141
- //Because we run it with name only parameter we should check that there are no other parameters
142
- _ , err = next .LookupErr ("type" )
143
- require .Error (t , err )
209
+ for _ , tt := range listCollectionsTable {
210
+ t .Run (tt .name , func (t * testing.T ) {
211
+ if os .Getenv ("topology" ) != tt .expectedTopology {
212
+ t .Skip ()
213
+ }
214
+ dbName := "db_list_collections"
215
+ db := createTestDatabase (t , & dbName )
216
+
217
+ defer func () {
218
+ err := db .Drop (context .Background ())
219
+ require .NoError (t , err )
220
+ }()
221
+
222
+ err := listCollectionsTest (db , tt .cappedOnly )
223
+ require .NoError (t , err )
224
+ })
144
225
}
145
- defer func () {
146
- err := db .Drop (context .Background ())
147
- require .NoError (t , err )
148
- }()
149
226
}
0 commit comments