Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gomongo_test
import (
"context"
"fmt"
"slices"
"strings"
"testing"

Expand All @@ -13,6 +12,24 @@ import (
"go.mongodb.org/mongo-driver/v2/bson"
)

// containsCollectionName checks if the rows contain a JSON object with the given collection name.
func containsCollectionName(rows []string, name string) bool {
for _, row := range rows {
var doc bson.M
if err := bson.UnmarshalExtJSON([]byte(row), false, &doc); err == nil {
if doc["name"] == name {
return true
}
}
}
return false
}

// containsDatabaseName checks if the rows contain a JSON object with the given database name.
func containsDatabaseName(rows []string, name string) bool {
return containsCollectionName(rows, name) // Same logic
}

func TestCreateIndex(t *testing.T) {
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_create_idx_%s", db.Name)
Expand Down Expand Up @@ -163,7 +180,7 @@ func TestCreateCollection(t *testing.T) {
collResult, err := gc.Execute(ctx, dbName, `show collections`)
require.NoError(t, err)
require.Equal(t, 1, collResult.RowCount)
require.Equal(t, "newcollection", collResult.Rows[0])
require.True(t, containsCollectionName(collResult.Rows, "newcollection"), "expected 'newcollection' in result")
})
}

Expand All @@ -183,7 +200,7 @@ func TestDropDatabase(t *testing.T) {
// Verify database exists
result, err := gc.Execute(ctx, dbName, `show dbs`)
require.NoError(t, err)
require.True(t, slices.Contains(result.Rows, dbName), "database should exist before drop")
require.True(t, containsDatabaseName(result.Rows, dbName), "database should exist before drop")

// Drop the database
result, err = gc.Execute(ctx, dbName, `db.dropDatabase()`)
Expand Down Expand Up @@ -216,7 +233,7 @@ func TestRenameCollection(t *testing.T) {
collResult, err := gc.Execute(ctx, dbName, `show collections`)
require.NoError(t, err)
require.Equal(t, 1, collResult.RowCount)
require.Equal(t, "newname", collResult.Rows[0])
require.True(t, containsCollectionName(collResult.Rows, "newname"), "expected 'newname' in result")

// Verify data is preserved
findResult, err := gc.Execute(ctx, dbName, `db.newname.find()`)
Expand Down Expand Up @@ -252,7 +269,7 @@ func TestRenameCollectionWithDropTarget(t *testing.T) {
collResult, err := gc.Execute(ctx, dbName, `show collections`)
require.NoError(t, err)
require.Equal(t, 1, collResult.RowCount)
require.Equal(t, "target", collResult.Rows[0])
require.True(t, containsCollectionName(collResult.Rows, "target"), "expected 'target' in result")

// Verify it has source data, not old target data
findResult, err := gc.Execute(ctx, dbName, `db.target.find()`)
Expand Down Expand Up @@ -280,7 +297,7 @@ func TestCreateCollectionWithOptions(t *testing.T) {
collResult, err := gc.Execute(ctx, dbName, `show collections`)
require.NoError(t, err)
require.Equal(t, 1, collResult.RowCount)
require.Equal(t, "cappedcoll", collResult.Rows[0])
require.True(t, containsCollectionName(collResult.Rows, "cappedcoll"), "expected 'cappedcoll' in result")
})
}

Expand Down
32 changes: 25 additions & 7 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gomongo_test
import (
"context"
"fmt"
"slices"
"testing"

"github.com/bytebase/gomongo"
Expand Down Expand Up @@ -40,8 +39,17 @@ func TestShowDatabases(t *testing.T) {
require.NotNil(t, result)
require.GreaterOrEqual(t, result.RowCount, 1)

// Check that dbName is in the result
require.True(t, slices.Contains(result.Rows, dbName), "expected '%s' in database list, got: %v", dbName, result.Rows)
// Check that dbName is in the result (as JSON object with "name" field)
found := false
for _, row := range result.Rows {
var doc bson.M
err := bson.UnmarshalExtJSON([]byte(row), false, &doc)
if err == nil && doc["name"] == dbName {
found = true
break
}
}
require.True(t, found, "expected database '%s' in result, got: %v", dbName, result.Rows)
})
}
})
Expand All @@ -67,10 +75,15 @@ func TestShowCollections(t *testing.T) {
require.NotNil(t, result)
require.Equal(t, 2, result.RowCount)

// Check that both collections are in the result
// Check that both collections are in the result (as JSON objects with "name" field)
collectionSet := make(map[string]bool)
for _, row := range result.Rows {
collectionSet[row] = true
var doc bson.M
err := bson.UnmarshalExtJSON([]byte(row), false, &doc)
require.NoError(t, err, "row should be valid JSON: %s", row)
if name, ok := doc["name"].(string); ok {
collectionSet[name] = true
}
}
require.True(t, collectionSet["users"], "expected 'users' collection")
require.True(t, collectionSet["orders"], "expected 'orders' collection")
Expand All @@ -97,10 +110,15 @@ func TestGetCollectionNames(t *testing.T) {
require.NotNil(t, result)
require.Equal(t, 2, result.RowCount)

// Check that both collections are in the result
// Check that both collections are in the result (as JSON objects with "name" field)
collectionSet := make(map[string]bool)
for _, row := range result.Rows {
collectionSet[row] = true
var doc bson.M
err := bson.UnmarshalExtJSON([]byte(row), false, &doc)
require.NoError(t, err, "row should be valid JSON: %s", row)
if name, ok := doc["name"].(string); ok {
collectionSet[name] = true
}
}
require.True(t, collectionSet["products"], "expected 'products' collection")
require.True(t, collectionSet["categories"], "expected 'categories' collection")
Expand Down
11 changes: 9 additions & 2 deletions internal/executor/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ func executeShowCollections(ctx context.Context, client *mongo.Client, database
return nil, fmt.Errorf("list collections failed: %w", err)
}

rows := make([]string, len(names))
copy(rows, names)
rows := make([]string, 0, len(names))
for _, name := range names {
doc := bson.M{"name": name}
jsonBytes, err := bson.MarshalExtJSONIndent(doc, false, false, "", " ")
if err != nil {
return nil, fmt.Errorf("marshal failed: %w", err)
}
rows = append(rows, string(jsonBytes))
}

return &Result{
Rows: rows,
Expand Down
11 changes: 9 additions & 2 deletions internal/executor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ func executeShowDatabases(ctx context.Context, client *mongo.Client) (*Result, e
return nil, fmt.Errorf("list databases failed: %w", err)
}

rows := make([]string, len(names))
copy(rows, names)
rows := make([]string, 0, len(names))
for _, name := range names {
doc := bson.M{"name": name}
jsonBytes, err := bson.MarshalExtJSONIndent(doc, false, false, "", " ")
if err != nil {
return nil, fmt.Errorf("marshal failed: %w", err)
}
rows = append(rows, string(jsonBytes))
}

return &Result{
Rows: rows,
Expand Down
Loading