Skip to content

Commit aa7785b

Browse files
author
Divjot Arora
committed
Add test cases for ListCollections
GODRIVER-31 Change-Id: I6087b16dc2429af5f5fc228138e67d45e53632a2
1 parent de03a35 commit aa7785b

File tree

1 file changed

+133
-56
lines changed

1 file changed

+133
-56
lines changed

mongo/database_internal_test.go

Lines changed: 133 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import (
1010
"context"
1111
"testing"
1212

13+
"fmt"
14+
"os"
15+
1316
"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"
1518
"github.com/mongodb/mongo-go-driver/internal/testutil"
1619
"github.com/stretchr/testify/require"
1720
)
@@ -72,78 +75,152 @@ func TestDatabase_Drop(t *testing.T) {
7275

7376
}
7477

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))
8599

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
87118

88119
for cursor.Next(context.Background()) {
120+
next := bson.NewDocument()
89121
err = cursor.Decode(next)
90-
require.NoError(t, err)
122+
if err != nil {
123+
return err
124+
}
91125

92126
elem, err := next.LookupErr("name")
93-
require.NoError(t, err)
127+
if err != nil {
128+
return err
129+
}
130+
94131
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)
97140
}
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
101149
}
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
108154
}
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)
109166
}
110-
defer func() {
111-
err := db.Drop(context.Background())
112-
require.NoError(t, err)
113-
}()
167+
168+
return nil
114169
}
115170

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+
}
126183

127-
next := bson.NewDocument()
184+
cursor, err := db.ListCollections(context.Background(), filter)
185+
if err != nil {
186+
return err
187+
}
128188

129-
for cursor.Next(context.Background()) {
130-
err = cursor.Decode(next)
131-
require.NoError(t, err)
189+
return verifyListCollections(cursor, uncappedName, cappedName, cappedOnly)
190+
}
132191

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()
135195

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+
}
140208

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+
})
144225
}
145-
defer func() {
146-
err := db.Drop(context.Background())
147-
require.NoError(t, err)
148-
}()
149226
}

0 commit comments

Comments
 (0)