@@ -11,6 +11,7 @@ import (
11
11
"testing"
12
12
13
13
"github.com/mongodb/mongo-go-driver/bson"
14
+ "github.com/mongodb/mongo-go-driver/core/option"
14
15
"github.com/mongodb/mongo-go-driver/internal/testutil"
15
16
"github.com/stretchr/testify/require"
16
17
)
@@ -70,3 +71,79 @@ func TestDatabase_Drop(t *testing.T) {
70
71
require .NotContains (t , list , name )
71
72
72
73
}
74
+
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 )
85
+
86
+ next := bson .NewDocument ()
87
+
88
+ for cursor .Next (context .Background ()) {
89
+ err = cursor .Decode (next )
90
+ require .NoError (t , err )
91
+
92
+ elem , err := next .LookupErr ("name" )
93
+ require .NoError (t , err )
94
+ if elem .Type () != bson .TypeString {
95
+ t .Errorf ("Incorrect type for 'name'. got %v; want %v" , elem .Type (), bson .TypeString )
96
+ t .FailNow ()
97
+ }
98
+ if elem .StringValue () != collName {
99
+ t .Errorf ("Incorrect collection name. got %s: want %s" , elem .StringValue (), collName )
100
+ t .FailNow ()
101
+ }
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 ()
108
+ }
109
+ }
110
+ defer func () {
111
+ err := db .Drop (context .Background ())
112
+ require .NoError (t , err )
113
+ }()
114
+ }
115
+
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 )
126
+
127
+ next := bson .NewDocument ()
128
+
129
+ for cursor .Next (context .Background ()) {
130
+ err = cursor .Decode (next )
131
+ require .NoError (t , err )
132
+
133
+ elem , err := next .LookupErr ("name" )
134
+ require .NoError (t , err )
135
+
136
+ if elem .StringValue () != collName {
137
+ t .Errorf ("Incorrect collection name. got %s: want %s" , elem .StringValue (), collName )
138
+ t .FailNow ()
139
+ }
140
+
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 )
144
+ }
145
+ defer func () {
146
+ err := db .Drop (context .Background ())
147
+ require .NoError (t , err )
148
+ }()
149
+ }
0 commit comments