diff --git a/integration/commands_administration_test.go b/integration/commands_administration_test.go new file mode 100644 index 000000000000..5bd93ade6dde --- /dev/null +++ b/integration/commands_administration_test.go @@ -0,0 +1,74 @@ +// Copyright 2021 FerretDB Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package integration + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" +) + +func TestCommandsAdministrationCreateDropList(t *testing.T) { + t.Parallel() + ctx, collection := setup(t) + db := collection.Database() + name := collection.Name() + + names, err := db.ListCollectionNames(ctx, bson.D{}) + require.NoError(t, err) + assert.Contains(t, names, name) + + err = collection.Drop(ctx) + require.NoError(t, err) + + // error is consumed by the driver + err = collection.Drop(ctx) + require.NoError(t, err) + err = db.Collection(name).Drop(ctx) + require.NoError(t, err) + + // drop manually to check error + var actual bson.D + err = db.RunCommand(ctx, bson.D{{"drop", name}}).Decode(&actual) + expectedErr := mongo.CommandError{ + Code: 26, + Name: "NamespaceNotFound", + Message: `ns not found`, + } + require.Equal(t, expectedErr, err) + + names, err = db.ListCollectionNames(ctx, bson.D{}) + require.NoError(t, err) + assert.NotContains(t, names, name) + + err = db.CreateCollection(ctx, name) + require.NoError(t, err) + + err = db.CreateCollection(ctx, name) + expectedErr = mongo.CommandError{ + Code: 48, + Name: "NamespaceExists", + Message: `Collection already exists. ` + + `NS: testcommandsadministrationcreatedroplist.testcommandsadministrationcreatedroplist`, + } + require.Equal(t, expectedErr, err) + + names, err = db.ListCollectionNames(ctx, bson.D{}) + require.NoError(t, err) + assert.Contains(t, names, name) +} diff --git a/internal/handlers/handler_test.go b/internal/handlers/handler_test.go index b340158b7fd0..9c633e72ddca 100644 --- a/internal/handlers/handler_test.go +++ b/internal/handlers/handler_test.go @@ -2548,68 +2548,3 @@ func TestListDropDatabase(t *testing.T) { testutil.AssertEqual(t, expected, actual) }) } - -//nolint:paralleltest // we test a global list of collections -func TestCreateListDropCollection(t *testing.T) { - ctx, handler, pool := setup(t, nil) - db := testutil.Schema(ctx, t, pool) - - t.Run("nonexisting", func(t *testing.T) { - collection := testutil.TableName(t) - - actual := handle(ctx, t, handler, types.MustNewDocument( - "create", collection, - "$db", db, - )) - expected := types.MustNewDocument( - "ok", float64(1), - ) - testutil.AssertEqual(t, expected, actual) - - // TODO test listCollections command once we have better cursor support - // https://github.com/FerretDB/FerretDB/issues/79 - - tables, err := pool.Tables(ctx, db) - require.NoError(t, err) - assert.Equal(t, []string{collection}, tables) - - actual = handle(ctx, t, handler, types.MustNewDocument( - "drop", collection, - "$db", db, - )) - expected = types.MustNewDocument( - "nIndexesWas", int32(1), - "ns", db+"."+collection, - "ok", float64(1), - ) - testutil.AssertEqual(t, expected, actual) - - actual = handle(ctx, t, handler, types.MustNewDocument( - "drop", collection, - "$db", db, - )) - expected = types.MustNewDocument( - "ok", float64(0), - "errmsg", "ns not found", - "code", int32(26), - "codeName", "NamespaceNotFound", - ) - testutil.AssertEqual(t, expected, actual) - }) - - t.Run("existing", func(t *testing.T) { - collection := testutil.Table(ctx, t, pool, db) - - actual := handle(ctx, t, handler, types.MustNewDocument( - "create", collection, - "$db", db, - )) - expected := types.MustNewDocument( - "ok", float64(0), - "errmsg", "Collection already exists. NS: testcreatelistdropcollection.testcreatelistdropcollection-existing", - "code", int32(48), - "codeName", "NamespaceExists", - ) - testutil.AssertEqual(t, expected, actual) - }) -}