...

Source file src/go.mongodb.org/mongo-driver/mongo/results_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  	"testing"
    11  
    12  	"go.mongodb.org/mongo-driver/bson"
    13  	"go.mongodb.org/mongo-driver/internal/assert"
    14  )
    15  
    16  func TestResults(t *testing.T) {
    17  	t.Run("delete result", func(t *testing.T) {
    18  		// TODO(GODRIVER-2367): Do not test the ability to unmarshal BSON directly to a DeleteResult.
    19  		t.Run("unmarshal into", func(t *testing.T) {
    20  			doc := bson.D{
    21  				{"n", int64(2)},
    22  				{"ok", int64(1)},
    23  			}
    24  
    25  			b, err := bson.Marshal(doc)
    26  			assert.Nil(t, err, "Marshal error: %v", err)
    27  
    28  			var result DeleteResult
    29  			err = bson.Unmarshal(b, &result)
    30  			assert.Nil(t, err, "Unmarshal error: %v", err)
    31  			assert.Equal(t, int64(2), result.DeletedCount, "expected DeletedCount 2, got %v", result.DeletedCount)
    32  		})
    33  		// TODO(GODRIVER-2367): Do not test the ability to marshal a DeleteResult to BSON.
    34  		t.Run("marshal from", func(t *testing.T) {
    35  			result := DeleteResult{DeletedCount: 1}
    36  			buf, err := bson.Marshal(result)
    37  			assert.Nil(t, err, "Marshal error: %v", err)
    38  
    39  			var doc bson.D
    40  			err = bson.Unmarshal(buf, &doc)
    41  			assert.Nil(t, err, "Unmarshal error: %v", err)
    42  
    43  			assert.Equal(t, 1, len(doc), "expected document length 1, got %v", len(doc))
    44  			for _, elem := range doc {
    45  				if elem.Key != "n" {
    46  					continue
    47  				}
    48  
    49  				n, ok := elem.Value.(int64)
    50  				assert.True(t, ok, "expected n type %T, got %T", int64(0), elem.Value)
    51  				assert.Equal(t, int64(1), n, "expected n 1, got %v", n)
    52  				return
    53  			}
    54  			t.Fatal("key n not found in document")
    55  		})
    56  	})
    57  	t.Run("update result", func(t *testing.T) {
    58  		// TODO(GODRIVER-2367): Do not test the ability to unmarshal BSON directly to an UpdateResult.
    59  		t.Run("unmarshal into", func(t *testing.T) {
    60  			doc := bson.D{
    61  				{"n", 1},
    62  				{"nModified", 2},
    63  				{"upserted", bson.A{
    64  					bson.D{
    65  						{"index", 0},
    66  						{"_id", 3},
    67  					},
    68  				}},
    69  			}
    70  			b, err := bson.Marshal(doc)
    71  			assert.Nil(t, err, "Marshal error: %v", err)
    72  
    73  			var result UpdateResult
    74  			err = bson.Unmarshal(b, &result)
    75  			assert.Nil(t, err, "Unmarshal error: %v", err)
    76  			assert.Equal(t, int64(1), result.MatchedCount, "expected MatchedCount 1, got %v", result.MatchedCount)
    77  			assert.Equal(t, int64(2), result.ModifiedCount, "expected ModifiedCount 2, got %v", result.ModifiedCount)
    78  			upsertedID := result.UpsertedID.(int32)
    79  			assert.Equal(t, int32(3), upsertedID, "expected upsertedID 3, got %v", upsertedID)
    80  		})
    81  	})
    82  }
    83  

View as plain text