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
3,885 changes: 2,069 additions & 1,816 deletions collection_test.go

Large diffs are not rendered by default.

448 changes: 230 additions & 218 deletions database_test.go

Large diffs are not rendered by default.

190 changes: 100 additions & 90 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gomongo_test

import (
"context"
"fmt"
"testing"

"github.com/bytebase/gomongo"
Expand All @@ -10,69 +11,73 @@ import (
)

func TestParseError(t *testing.T) {
client := testutil.GetClient(t)
dbName := "testdb_parse_error"
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_parse_error_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

gc := gomongo.NewClient(client)
ctx := context.Background()
gc := gomongo.NewClient(db.Client)
ctx := context.Background()

_, err := gc.Execute(ctx, dbName, "db.users.find({ name: })")
require.Error(t, err)
_, err := gc.Execute(ctx, dbName, "db.users.find({ name: })")
require.Error(t, err)

var parseErr *gomongo.ParseError
require.ErrorAs(t, err, &parseErr)
var parseErr *gomongo.ParseError
require.ErrorAs(t, err, &parseErr)
})
}

func TestPlannedOperation(t *testing.T) {
client := testutil.GetClient(t)
dbName := "testdb_planned_op"
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_planned_op_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

gc := gomongo.NewClient(client)
ctx := context.Background()
gc := gomongo.NewClient(db.Client)
ctx := context.Background()

// createIndex is a planned M3 operation - should return PlannedOperationError
_, err := gc.Execute(ctx, dbName, "db.users.createIndex({ name: 1 })")
require.Error(t, err)
// createIndex is a planned M3 operation - should return PlannedOperationError
_, err := gc.Execute(ctx, dbName, "db.users.createIndex({ name: 1 })")
require.Error(t, err)

var plannedErr *gomongo.PlannedOperationError
require.ErrorAs(t, err, &plannedErr)
require.Equal(t, "createIndex()", plannedErr.Operation)
var plannedErr *gomongo.PlannedOperationError
require.ErrorAs(t, err, &plannedErr)
require.Equal(t, "createIndex()", plannedErr.Operation)
})
}

func TestUnsupportedOperation(t *testing.T) {
client := testutil.GetClient(t)
dbName := "testdb_unsup_op"
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_unsup_op_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

gc := gomongo.NewClient(client)
ctx := context.Background()
gc := gomongo.NewClient(db.Client)
ctx := context.Background()

// createSearchIndex is NOT in the registry - should return UnsupportedOperationError
_, err := gc.Execute(ctx, dbName, `db.movies.createSearchIndex({ name: "default", definition: { mappings: { dynamic: true } } })`)
require.Error(t, err)
// createSearchIndex is NOT in the registry - should return UnsupportedOperationError
_, err := gc.Execute(ctx, dbName, `db.movies.createSearchIndex({ name: "default", definition: { mappings: { dynamic: true } } })`)
require.Error(t, err)

var unsupportedErr *gomongo.UnsupportedOperationError
require.ErrorAs(t, err, &unsupportedErr)
require.Equal(t, "createSearchIndex()", unsupportedErr.Operation)
var unsupportedErr *gomongo.UnsupportedOperationError
require.ErrorAs(t, err, &unsupportedErr)
require.Equal(t, "createSearchIndex()", unsupportedErr.Operation)
})
}

func TestUnsupportedOptionError(t *testing.T) {
client := testutil.GetClient(t)
dbName := "testdb_unsup_opt_err"
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_unsup_opt_err_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

ctx := context.Background()
ctx := context.Background()

gc := gomongo.NewClient(client)
gc := gomongo.NewClient(db.Client)

// find() with unsupported option 'collation'
_, err := gc.Execute(ctx, dbName, `db.users.find({}, {}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "find()", optErr.Method)
require.Equal(t, "collation", optErr.Option)
// find() with unsupported option 'collation'
_, err := gc.Execute(ctx, dbName, `db.users.find({}, {}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "find()", optErr.Method)
require.Equal(t, "collation", optErr.Option)
})
}

func TestMethodRegistryStats(t *testing.T) {
Expand All @@ -87,79 +92,84 @@ func TestMethodRegistryStats(t *testing.T) {
}

func TestFindOneUnsupportedOption(t *testing.T) {
client := testutil.GetClient(t)
dbName := "testdb_findone_unsup_opt"
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_findone_unsup_opt_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

ctx := context.Background()
ctx := context.Background()

gc := gomongo.NewClient(client)
gc := gomongo.NewClient(db.Client)

_, err := gc.Execute(ctx, dbName, `db.users.findOne({}, {}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "findOne()", optErr.Method)
require.Equal(t, "collation", optErr.Option)
_, err := gc.Execute(ctx, dbName, `db.users.findOne({}, {}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "findOne()", optErr.Method)
require.Equal(t, "collation", optErr.Option)
})
}

func TestAggregateUnsupportedOption(t *testing.T) {
client := testutil.GetClient(t)
dbName := "testdb_agg_unsup_opt"
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_agg_unsup_opt_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

ctx := context.Background()
ctx := context.Background()

gc := gomongo.NewClient(client)
gc := gomongo.NewClient(db.Client)

_, err := gc.Execute(ctx, dbName, `db.users.aggregate([], { allowDiskUse: true })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "aggregate()", optErr.Method)
require.Equal(t, "allowDiskUse", optErr.Option)
_, err := gc.Execute(ctx, dbName, `db.users.aggregate([], { allowDiskUse: true })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "aggregate()", optErr.Method)
require.Equal(t, "allowDiskUse", optErr.Option)
})
}

func TestCountDocumentsUnsupportedOption(t *testing.T) {
dbName := "testdb_count_unsup"
client := testutil.GetClient(t)
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_count_unsup_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

ctx := context.Background()
ctx := context.Background()

gc := gomongo.NewClient(client)
gc := gomongo.NewClient(db.Client)

_, err := gc.Execute(ctx, dbName, `db.users.countDocuments({}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "countDocuments()", optErr.Method)
_, err := gc.Execute(ctx, dbName, `db.users.countDocuments({}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "countDocuments()", optErr.Method)
})
}

func TestDistinctUnsupportedOption(t *testing.T) {
dbName := "testdb_distinct_unsup"
client := testutil.GetClient(t)
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_distinct_unsup_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

ctx := context.Background()
ctx := context.Background()

gc := gomongo.NewClient(client)
gc := gomongo.NewClient(db.Client)

_, err := gc.Execute(ctx, dbName, `db.users.distinct("city", {}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "distinct()", optErr.Method)
_, err := gc.Execute(ctx, dbName, `db.users.distinct("city", {}, { collation: { locale: "en" } })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "distinct()", optErr.Method)
})
}

func TestEstimatedDocumentCountUnsupportedOption(t *testing.T) {
dbName := "testdb_est_count_unsup"
client := testutil.GetClient(t)
defer testutil.CleanupDatabase(t, client, dbName)
testutil.RunOnAllDBs(t, func(t *testing.T, db testutil.TestDB) {
dbName := fmt.Sprintf("testdb_est_count_unsup_%s", db.Name)
defer testutil.CleanupDatabase(t, db.Client, dbName)

ctx := context.Background()
ctx := context.Background()

gc := gomongo.NewClient(client)
gc := gomongo.NewClient(db.Client)

_, err := gc.Execute(ctx, dbName, `db.users.estimatedDocumentCount({ comment: "test" })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "estimatedDocumentCount()", optErr.Method)
require.Equal(t, "comment", optErr.Option)
_, err := gc.Execute(ctx, dbName, `db.users.estimatedDocumentCount({ comment: "test" })`)
var optErr *gomongo.UnsupportedOptionError
require.ErrorAs(t, err, &optErr)
require.Equal(t, "estimatedDocumentCount()", optErr.Method)
require.Equal(t, "comment", optErr.Option)
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/bytebase/parser v0.0.0-20260121030202-698704919f24
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.11.1
github.com/testcontainers/testcontainers-go v0.40.0
github.com/testcontainers/testcontainers-go/modules/mongodb v0.40.0
go.mongodb.org/mongo-driver/v2 v2.4.1
)
Expand Down Expand Up @@ -51,7 +52,6 @@ require (
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v4 v4.25.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/testcontainers/testcontainers-go v0.40.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand Down
Loading