@@ -3,7 +3,6 @@ package gomongo_test
33import (
44 "context"
55 "fmt"
6- "slices"
76 "strings"
87 "testing"
98
@@ -13,6 +12,24 @@ import (
1312 "go.mongodb.org/mongo-driver/v2/bson"
1413)
1514
15+ // containsCollectionName checks if the rows contain a JSON object with the given collection name.
16+ func containsCollectionName (rows []string , name string ) bool {
17+ for _ , row := range rows {
18+ var doc bson.M
19+ if err := bson .UnmarshalExtJSON ([]byte (row ), false , & doc ); err == nil {
20+ if doc ["name" ] == name {
21+ return true
22+ }
23+ }
24+ }
25+ return false
26+ }
27+
28+ // containsDatabaseName checks if the rows contain a JSON object with the given database name.
29+ func containsDatabaseName (rows []string , name string ) bool {
30+ return containsCollectionName (rows , name ) // Same logic
31+ }
32+
1633func TestCreateIndex (t * testing.T ) {
1734 testutil .RunOnAllDBs (t , func (t * testing.T , db testutil.TestDB ) {
1835 dbName := fmt .Sprintf ("testdb_create_idx_%s" , db .Name )
@@ -163,7 +180,7 @@ func TestCreateCollection(t *testing.T) {
163180 collResult , err := gc .Execute (ctx , dbName , `show collections` )
164181 require .NoError (t , err )
165182 require .Equal (t , 1 , collResult .RowCount )
166- require .Equal (t , "newcollection" , collResult . Rows [ 0 ] )
183+ require .True (t , containsCollectionName ( collResult . Rows , "newcollection" ), "expected 'newcollection' in result" )
167184 })
168185}
169186
@@ -183,7 +200,7 @@ func TestDropDatabase(t *testing.T) {
183200 // Verify database exists
184201 result , err := gc .Execute (ctx , dbName , `show dbs` )
185202 require .NoError (t , err )
186- require .True (t , slices . Contains (result .Rows , dbName ), "database should exist before drop" )
203+ require .True (t , containsDatabaseName (result .Rows , dbName ), "database should exist before drop" )
187204
188205 // Drop the database
189206 result , err = gc .Execute (ctx , dbName , `db.dropDatabase()` )
@@ -216,7 +233,7 @@ func TestRenameCollection(t *testing.T) {
216233 collResult , err := gc .Execute (ctx , dbName , `show collections` )
217234 require .NoError (t , err )
218235 require .Equal (t , 1 , collResult .RowCount )
219- require .Equal (t , "newname" , collResult . Rows [ 0 ] )
236+ require .True (t , containsCollectionName ( collResult . Rows , "newname" ), "expected 'newname' in result" )
220237
221238 // Verify data is preserved
222239 findResult , err := gc .Execute (ctx , dbName , `db.newname.find()` )
@@ -252,7 +269,7 @@ func TestRenameCollectionWithDropTarget(t *testing.T) {
252269 collResult , err := gc .Execute (ctx , dbName , `show collections` )
253270 require .NoError (t , err )
254271 require .Equal (t , 1 , collResult .RowCount )
255- require .Equal (t , "target" , collResult . Rows [ 0 ] )
272+ require .True (t , containsCollectionName ( collResult . Rows , "target" ), "expected 'target' in result" )
256273
257274 // Verify it has source data, not old target data
258275 findResult , err := gc .Execute (ctx , dbName , `db.target.find()` )
@@ -280,7 +297,7 @@ func TestCreateCollectionWithOptions(t *testing.T) {
280297 collResult , err := gc .Execute (ctx , dbName , `show collections` )
281298 require .NoError (t , err )
282299 require .Equal (t , 1 , collResult .RowCount )
283- require .Equal (t , "cappedcoll" , collResult . Rows [ 0 ] )
300+ require .True (t , containsCollectionName ( collResult . Rows , "cappedcoll" ), "expected 'cappedcoll' in result" )
284301 })
285302}
286303
0 commit comments