Skip to content

Commit

Permalink
test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Sep 17, 2024
1 parent a9774d1 commit 3b4d7ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions mongo/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ func (c *Cursor) Close(ctx context.Context) error {

// All iterates the cursor and decodes each document into results. The results parameter must be a pointer to a slice.
// The slice pointed to by results will be completely overwritten. A nil slice pointer will not be modified if the cursor
// has been closed or exhausted. This method will close the cursor after retrieving all documents. If the cursor has been
// iterated, any previously iterated documents will not be included in results.
// has been closed, exhausted, or is empty. This method will close the cursor after retrieving all documents. If the
// cursor has been iterated, any previously iterated documents will not be included in results.
//
// This method requires driver version >= 1.1.0.
func (c *Cursor) All(ctx context.Context, results interface{}) error {
Expand Down
34 changes: 23 additions & 11 deletions mongo/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestCursor(t *testing.T) {

t.Run("fills slice with all documents", func(t *testing.T) {
cursor, err := newCursor(newTestBatchCursor(1, 5), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

var docs []bson.D
err = cursor.All(context.Background(), &docs)
Expand All @@ -124,7 +124,7 @@ func TestCursor(t *testing.T) {

t.Run("nil slice", func(t *testing.T) {
cursor, err := newCursor(newTestBatchCursor(0, 0), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

var docs []bson.D
err = cursor.All(context.Background(), &docs)
Expand All @@ -134,17 +134,29 @@ func TestCursor(t *testing.T) {

t.Run("empty slice", func(t *testing.T) {
cursor, err := newCursor(newTestBatchCursor(0, 0), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

docs := []bson.D{}
err = cursor.All(context.Background(), &docs)
assert.NoError(t, err, "All error: %v", err)
assert.NotNil(t, docs, "expected non-nil docs")
assert.Len(t, docs, 0, "expected 0 docs, got %v", len(docs))
})

t.Run("empty slice overwritten", func(t *testing.T) {
cursor, err := newCursor(newTestBatchCursor(0, 0), nil, nil)
require.NoError(t, err, "newCursor error: %v", err)

docs := []bson.D{{{"foo", "bar"}}, {{"hello", "world"}, {"pi", 3.14159}}}
err = cursor.All(context.Background(), &docs)
assert.NoError(t, err, "All error: %v", err)
assert.NotNil(t, docs, "expected non-nil docs")
assert.Len(t, docs, 0, "expected 0 docs, got %v", len(docs))
})

t.Run("decodes each document into slice type", func(t *testing.T) {
cursor, err := newCursor(newTestBatchCursor(1, 5), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

type Document struct {
Foo int32 `bson:"foo"`
Expand All @@ -162,7 +174,7 @@ func TestCursor(t *testing.T) {

t.Run("multiple batches are included", func(t *testing.T) {
cursor, err := newCursor(newTestBatchCursor(2, 5), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)
var docs []bson.D
err = cursor.All(context.Background(), &docs)
assert.NoError(t, err, "All error: %v", err)
Expand All @@ -179,7 +191,7 @@ func TestCursor(t *testing.T) {

tbc := newTestBatchCursor(1, 5)
cursor, err := newCursor(tbc, nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

err = cursor.All(context.Background(), &docs)
assert.NoError(t, err, "All error: %v", err)
Expand All @@ -190,7 +202,7 @@ func TestCursor(t *testing.T) {
var docs interface{} = []bson.D{}

cursor, err := newCursor(newTestBatchCursor(1, 5), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

err = cursor.All(context.Background(), &docs)
assert.NoError(t, err, "All error: %v", err)
Expand All @@ -200,7 +212,7 @@ func TestCursor(t *testing.T) {
var docs interface{} = "test"

cursor, err := newCursor(newTestBatchCursor(1, 5), nil, nil)
assert.NoError(t, err, "newCursor error: %v", err)
require.NoError(t, err, "newCursor error: %v", err)

err = cursor.All(context.Background(), &docs)
assert.Error(t, err, "expected error, got: %v", err)
Expand Down Expand Up @@ -238,13 +250,13 @@ func TestNewCursorFromDocuments(t *testing.T) {
bson.D{{"_id", 2}, {"quux", "quuz"}},
}
cur, err := NewCursorFromDocuments(findResult, nil, nil)
assert.NoError(t, err, "NewCursorFromDocuments error: %v", err)
require.NoError(t, err, "NewCursorFromDocuments error: %v", err)

// Assert that decoded documents are as expected.
var i int
for cur.Next(context.Background()) {
docBytes, err := bson.Marshal(findResult[i])
assert.NoError(t, err, "Marshal error: %v", err)
require.NoError(t, err, "Marshal error: %v", err)
expectedDecoded := bson.Raw(docBytes)

var decoded bson.Raw
Expand All @@ -270,7 +282,7 @@ func TestNewCursorFromDocuments(t *testing.T) {
mockErr := fmt.Errorf("mock error")
findResult := []interface{}{bson.D{{"_id", 0}, {"foo", "bar"}}}
cur, err := NewCursorFromDocuments(findResult, mockErr, nil)
assert.NoError(t, err, "NewCursorFromDocuments error: %v", err)
require.NoError(t, err, "NewCursorFromDocuments error: %v", err)

// Assert that a call to Next will return false because of existing error.
next := cur.Next(context.Background())
Expand Down

0 comments on commit 3b4d7ab

Please sign in to comment.