Skip to content

Commit

Permalink
Add tests for collections create and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Apr 5, 2022
1 parent c56f3ec commit dcb0ecf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 65 deletions.
74 changes: 74 additions & 0 deletions integration/commands_administration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
65 changes: 0 additions & 65 deletions internal/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit dcb0ecf

Please sign in to comment.