...

Source file src/go.mongodb.org/mongo-driver/mongo/description/server_test.go

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

     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 description
     8  
     9  import (
    10  	"errors"
    11  	"testing"
    12  	"time"
    13  
    14  	"go.mongodb.org/mongo-driver/bson/primitive"
    15  	"go.mongodb.org/mongo-driver/internal/assert"
    16  	"go.mongodb.org/mongo-driver/mongo/address"
    17  	"go.mongodb.org/mongo-driver/tag"
    18  )
    19  
    20  func TestServer(t *testing.T) {
    21  	int64ToPtr := func(i64 int64) *int64 { return &i64 }
    22  
    23  	t.Run("equals", func(t *testing.T) {
    24  		defaultServer := Server{}
    25  		// Only some of the Server fields affect equality
    26  		testCases := []struct {
    27  			name   string
    28  			server Server
    29  			equal  bool
    30  		}{
    31  			{"empty", Server{}, true},
    32  			{"address", Server{Addr: address.Address("foo")}, true},
    33  			{"arbiters", Server{Arbiters: []string{"foo"}}, false},
    34  			{"rtt", Server{AverageRTT: time.Second}, true},
    35  			{"compression", Server{Compression: []string{"foo"}}, true},
    36  			{"canonicalAddr", Server{CanonicalAddr: address.Address("foo")}, false},
    37  			{"electionID", Server{ElectionID: primitive.NewObjectID()}, false},
    38  			{"heartbeatInterval", Server{HeartbeatInterval: time.Second}, true},
    39  			{"hosts", Server{Hosts: []string{"foo"}}, false},
    40  			{"lastError", Server{LastError: errors.New("foo")}, false},
    41  			{"lastUpdateTime", Server{LastUpdateTime: time.Now()}, true},
    42  			{"lastWriteTime", Server{LastWriteTime: time.Now()}, true},
    43  			{"maxBatchCount", Server{MaxBatchCount: 1}, true},
    44  			{"maxDocumentSize", Server{MaxDocumentSize: 1}, true},
    45  			{"maxMessageSize", Server{MaxMessageSize: 1}, true},
    46  			{"members", Server{Members: []address.Address{address.Address("foo")}}, true},
    47  			{"passives", Server{Passives: []string{"foo"}}, false},
    48  			{"passive", Server{Passive: true}, true},
    49  			{"primary", Server{Primary: address.Address("foo")}, false},
    50  			{"readOnly", Server{ReadOnly: true}, true},
    51  			{
    52  				"sessionTimeoutMinutes",
    53  				Server{
    54  					SessionTimeoutMinutesPtr: int64ToPtr(1),
    55  					SessionTimeoutMinutes:    1,
    56  				},
    57  				false,
    58  			},
    59  			{"setName", Server{SetName: "foo"}, false},
    60  			{"setVersion", Server{SetVersion: 1}, false},
    61  			{"tags", Server{Tags: tag.Set{tag.Tag{"foo", "bar"}}}, false},
    62  			{"topologyVersion", Server{TopologyVersion: &TopologyVersion{primitive.NewObjectID(), 0}}, false},
    63  			{"kind", Server{Kind: Standalone}, false},
    64  			{"wireVersion", Server{WireVersion: &VersionRange{1, 2}}, false},
    65  		}
    66  		for _, tc := range testCases {
    67  			t.Run(tc.name, func(t *testing.T) {
    68  				actual := defaultServer.Equal(tc.server)
    69  				assert.Equal(t, actual, tc.equal, "expected %v, got %v", tc.equal, actual)
    70  			})
    71  		}
    72  	})
    73  }
    74  

View as plain text