...

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

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

     1  // Copyright (C) MongoDB, Inc. 2022-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  	"go.mongodb.org/mongo-driver/internal/require"
    15  )
    16  
    17  func TestErrorMessages(t *testing.T) {
    18  	details, err := bson.Marshal(bson.D{{"details", bson.D{{"operatorName", "$jsonSchema"}}}})
    19  	require.Nil(t, err, "unexpected error marshaling BSON")
    20  
    21  	cases := []struct {
    22  		desc     string
    23  		err      error
    24  		expected string
    25  	}{
    26  		{
    27  			desc: "WriteException error message should contain the WriteError Message and Details",
    28  			err: WriteException{
    29  				WriteErrors: WriteErrors{
    30  					{
    31  						Message: "test message 1",
    32  						Details: details,
    33  					},
    34  					{
    35  						Message: "test message 2",
    36  						Details: details,
    37  					},
    38  				},
    39  			},
    40  			expected: `write exception: write errors: [test message 1: {"details": {"operatorName": "$jsonSchema"}}, test message 2: {"details": {"operatorName": "$jsonSchema"}}]`,
    41  		},
    42  		{
    43  			desc: "BulkWriteException error message should contain the WriteError Message and Details",
    44  			err: BulkWriteException{
    45  				WriteErrors: []BulkWriteError{
    46  					{
    47  						WriteError: WriteError{
    48  							Message: "test message 1",
    49  							Details: details,
    50  						},
    51  					},
    52  					{
    53  						WriteError: WriteError{
    54  							Message: "test message 2",
    55  							Details: details,
    56  						},
    57  					},
    58  				},
    59  			},
    60  			expected: `bulk write exception: write errors: [test message 1: {"details": {"operatorName": "$jsonSchema"}}, test message 2: {"details": {"operatorName": "$jsonSchema"}}]`,
    61  		},
    62  	}
    63  
    64  	for _, tc := range cases {
    65  		tc := tc
    66  		t.Run(tc.desc, func(t *testing.T) {
    67  			t.Parallel()
    68  
    69  			assert.Equal(t, tc.expected, tc.err.Error())
    70  		})
    71  	}
    72  }
    73  

View as plain text