Skip to content

Commit

Permalink
Add tests for tailable cursors (#3833)
Browse files Browse the repository at this point in the history
  • Loading branch information
noisersup authored Dec 13, 2023
1 parent b26d394 commit c020139
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 50 deletions.
51 changes: 3 additions & 48 deletions integration/cursors/getmore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,22 +335,7 @@ func TestCursorsGetMoreCommand(t *testing.T) {
err := collection.Database().RunCommand(ctx, command).Decode(&res)
require.NoError(t, err)

doc := integration.ConvertDocument(t, res)

v, _ := doc.Get("cursor")
require.NotNil(t, v)

cursor, ok := v.(*types.Document)
require.True(t, ok)

cursorID, _ := cursor.Get("id")
assert.NotNil(t, cursorID)

v, _ = cursor.Get("firstBatch")
require.NotNil(t, v)

firstBatch, ok := v.(*types.Array)
require.True(t, ok)
firstBatch, cursorID := getFirstBatch(t, res)

require.Equal(t, len(tc.firstBatch), firstBatch.Len(), "expected: %v, got: %v", tc.firstBatch, firstBatch)
for i, elem := range tc.firstBatch {
Expand Down Expand Up @@ -382,22 +367,7 @@ func TestCursorsGetMoreCommand(t *testing.T) {
integration.AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)

// upon error response contains firstBatch field.
doc = integration.ConvertDocument(t, res)

v, _ = doc.Get("cursor")
require.NotNil(t, v)

cursor, ok = v.(*types.Document)
require.True(t, ok)

cursorID, _ = cursor.Get("id")
assert.NotNil(t, cursorID)

v, _ = cursor.Get("firstBatch")
require.NotNil(t, v)

firstBatch, ok = v.(*types.Array)
require.True(t, ok)
firstBatch, _ = getFirstBatch(t, res)

require.Equal(t, len(tc.firstBatch), firstBatch.Len(), "expected: %v, got: %v", tc.firstBatch, firstBatch)
for i, elem := range tc.firstBatch {
Expand All @@ -409,22 +379,7 @@ func TestCursorsGetMoreCommand(t *testing.T) {

require.NoError(t, err)

doc = integration.ConvertDocument(t, res)

v, _ = doc.Get("cursor")
require.NotNil(t, v)

cursor, ok = v.(*types.Document)
require.True(t, ok)

cursorID, _ = cursor.Get("id")
assert.NotNil(t, cursorID)

v, _ = cursor.Get("nextBatch")
require.NotNil(t, v)

nextBatch, ok := v.(*types.Array)
require.True(t, ok)
nextBatch, _ := getNextBatch(t, res)

require.Equal(t, len(tc.nextBatch), nextBatch.Len(), "expected: %v, got: %v", tc.nextBatch, nextBatch)
for i, elem := range tc.nextBatch {
Expand Down
75 changes: 75 additions & 0 deletions integration/cursors/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 cursors

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"

"github.com/FerretDB/FerretDB/integration"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)

// getFirstBatch takes the response from the query that generates the cursors,
// validates if it contains cursor.firstBatch, and cursor ID, and returns those.
func getFirstBatch(t testtb.TB, res bson.D) (*types.Array, any) {
t.Helper()

doc := integration.ConvertDocument(t, res)

v, _ := doc.Get("cursor")
require.NotNil(t, v)

cursor, ok := v.(*types.Document)
require.True(t, ok)

cursorID, _ := cursor.Get("id")
assert.NotNil(t, cursorID)

v, _ = cursor.Get("firstBatch")
require.NotNil(t, v)

firstBatch, ok := v.(*types.Array)
require.True(t, ok)

return firstBatch, cursorID
}

// getNextBatch takes the response from the getMore query,
// validates if it contains cursor.nextBatch, and cursor ID, and returns those.
func getNextBatch(t testtb.TB, res bson.D) (*types.Array, any) {
t.Helper()

doc := integration.ConvertDocument(t, res)

v, _ := doc.Get("cursor")
require.NotNil(t, v)

cursor, ok := v.(*types.Document)
require.True(t, ok)

cursorID, _ := cursor.Get("id")
assert.NotNil(t, cursorID)

v, _ = cursor.Get("nextBatch")
require.NotNil(t, v)

firstBatch, ok := v.(*types.Array)
require.True(t, ok)

return firstBatch, cursorID
}
Loading

0 comments on commit c020139

Please sign in to comment.