...

Source file src/go.mongodb.org/mongo-driver/mongo/single_result_test.go

Documentation: go.mongodb.org/mongo-driver/mongo

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package mongo
     8  
     9  import (
    10  	"context"
    11  	"errors"
    12  	"fmt"
    13  	"testing"
    14  
    15  	"go.mongodb.org/mongo-driver/bson"
    16  	"go.mongodb.org/mongo-driver/internal/assert"
    17  	"go.mongodb.org/mongo-driver/internal/require"
    18  	"go.mongodb.org/mongo-driver/mongo/options"
    19  )
    20  
    21  func TestSingleResult(t *testing.T) {
    22  	t.Run("Decode", func(t *testing.T) {
    23  		t.Run("decode twice", func(t *testing.T) {
    24  			// Test that Decode and Raw can be called more than once
    25  			c, err := newCursor(newTestBatchCursor(1, 1), nil, bson.DefaultRegistry)
    26  			assert.Nil(t, err, "newCursor error: %v", err)
    27  
    28  			sr := &SingleResult{cur: c, reg: bson.DefaultRegistry}
    29  			var firstDecode, secondDecode bson.Raw
    30  			err = sr.Decode(&firstDecode)
    31  			assert.Nil(t, err, "Decode error: %v", err)
    32  			err = sr.Decode(&secondDecode)
    33  			assert.Nil(t, err, "Decode error: %v", err)
    34  
    35  			rawBytes, err := sr.Raw()
    36  			assert.Nil(t, err, "Raw error: %v", err)
    37  
    38  			assert.Equal(t, firstDecode, secondDecode, "expected contents %v, got %v", firstDecode, secondDecode)
    39  			assert.Equal(t, firstDecode, rawBytes, "expected contents %v, got %v", firstDecode, rawBytes)
    40  		})
    41  		t.Run("decode with error", func(t *testing.T) {
    42  			r := []byte("foo")
    43  			sr := &SingleResult{rdr: r, err: errors.New("Raw error")}
    44  			res, err := sr.Raw()
    45  			resBytes := []byte(res)
    46  			assert.Equal(t, r, resBytes, "expected contents %v, got %v", r, resBytes)
    47  			assert.Equal(t, sr.err, err, "expected error %v, got %v", sr.err, err)
    48  		})
    49  		t.Run("with BSONOptions", func(t *testing.T) {
    50  			c, err := newCursor(newTestBatchCursor(1, 1), nil, bson.DefaultRegistry)
    51  			require.NoError(t, err, "newCursor error")
    52  
    53  			sr := &SingleResult{
    54  				cur: c,
    55  				bsonOpts: &options.BSONOptions{
    56  					UseJSONStructTags: true,
    57  				},
    58  				reg: bson.DefaultRegistry,
    59  			}
    60  
    61  			type myDocument struct {
    62  				A *int32 `json:"foo"`
    63  			}
    64  
    65  			var got myDocument
    66  			err = sr.Decode(&got)
    67  			require.NoError(t, err, "Decode error")
    68  
    69  			i := int32(0)
    70  			want := myDocument{A: &i}
    71  
    72  			assert.Equal(t, want, got, "expected and actual Decode results are different")
    73  		})
    74  	})
    75  
    76  	t.Run("Err", func(t *testing.T) {
    77  		sr := &SingleResult{}
    78  		assert.Equal(t, ErrNoDocuments, sr.Err(), "expected error %v, got %v", ErrNoDocuments, sr.Err())
    79  	})
    80  }
    81  
    82  func TestNewSingleResultFromDocument(t *testing.T) {
    83  	// Mock a document returned by FindOne in SingleResult.
    84  	t.Run("mock FindOne", func(t *testing.T) {
    85  		findOneResult := bson.D{{"_id", 2}, {"foo", "bar"}}
    86  		res := NewSingleResultFromDocument(findOneResult, nil, nil)
    87  
    88  		// Assert that first, decoded document is as expected.
    89  		findOneResultBytes, err := bson.Marshal(findOneResult)
    90  		assert.Nil(t, err, "Marshal error: %v", err)
    91  		expectedRawBytes := bson.Raw(findOneResultBytes)
    92  		rawBytes, err := res.Raw()
    93  		assert.Nil(t, err, "Raw error: %v", err)
    94  		assert.Equal(t, expectedRawBytes, rawBytes,
    95  			"expected decoded SingleResult to be %v, got %v", expectedRawBytes, rawBytes)
    96  
    97  		// Assert that RDR contents are set correctly after Decode.
    98  		assert.NotNil(t, res.rdr, "expected non-nil rdr contents")
    99  		assert.Equal(t, expectedRawBytes, res.rdr,
   100  			"expected RDR contents to be %v, got %v", expectedRawBytes, res.rdr)
   101  
   102  		// Assert that a call to cur.Next will return false, as there was only one document in
   103  		// the slice passed to NewSingleResultFromDocument.
   104  		next := res.cur.Next(context.Background())
   105  		assert.False(t, next, "expected call to Next to return false, got true")
   106  
   107  		// Check for error on SingleResult.
   108  		assert.Nil(t, res.Err(), "SingleResult error: %v", res.Err())
   109  
   110  		// Assert that a call to cur.Close will not fail.
   111  		err = res.cur.Close(context.Background())
   112  		assert.Nil(t, err, "Close error: %v", err)
   113  	})
   114  
   115  	// Mock an error in SingleResult.
   116  	t.Run("mock FindOne with error", func(t *testing.T) {
   117  		mockErr := fmt.Errorf("mock error")
   118  		res := NewSingleResultFromDocument(bson.D{}, mockErr, nil)
   119  
   120  		// Assert that the raw bytes returns the mocked error.
   121  		_, err := res.Raw()
   122  		assert.NotNil(t, err, "expected Raw error, got nil")
   123  		assert.Equal(t, mockErr, err, "expected error %v, got %v", mockErr, err)
   124  
   125  		// Check for error on SingleResult.
   126  		assert.NotNil(t, res.Err(), "expected SingleResult error, got nil")
   127  		assert.Equal(t, mockErr, res.Err(), "expected SingleResult error %v, got %v",
   128  			mockErr, res.Err())
   129  
   130  		// Assert that error is propagated to underlying cursor.
   131  		assert.NotNil(t, res.cur.err, "expected underlying cursor, got nil")
   132  		assert.Equal(t, mockErr, res.cur.err, "expected underlying cursor %v, got %v",
   133  			mockErr, res.cur.err)
   134  	})
   135  }
   136  

View as plain text