@@ -15,18 +15,22 @@ import (
15
15
16
16
"github.com/mongodb/mongo-go-driver/bson"
17
17
"github.com/mongodb/mongo-go-driver/bson/objectid"
18
+ "github.com/mongodb/mongo-go-driver/core/readconcern"
19
+ "github.com/mongodb/mongo-go-driver/core/readpref"
20
+ "github.com/mongodb/mongo-go-driver/core/writeconcern"
18
21
"github.com/mongodb/mongo-go-driver/internal/testutil"
22
+ "github.com/mongodb/mongo-go-driver/mongo/dbopt"
19
23
"github.com/stretchr/testify/require"
20
24
)
21
25
22
- func createTestDatabase (t * testing.T , name * string ) * Database {
26
+ func createTestDatabase (t * testing.T , name * string , opts ... dbopt. Option ) * Database {
23
27
if name == nil {
24
28
db := testutil .DBName (t )
25
29
name = & db
26
30
}
27
31
28
32
client := createTestClient (t )
29
- return client .Database (* name )
33
+ return client .Database (* name , opts ... )
30
34
}
31
35
32
36
func TestDatabase_initialize (t * testing.T ) {
@@ -39,6 +43,70 @@ func TestDatabase_initialize(t *testing.T) {
39
43
require .NotNil (t , db .client )
40
44
}
41
45
46
+ func compareDbs (t * testing.T , expected * Database , got * Database ) {
47
+ switch {
48
+ case expected .readPreference != got .readPreference :
49
+ t .Errorf ("expected read preference %#v. got %#v" , expected .readPreference , got .readPreference )
50
+ case expected .readConcern != got .readConcern :
51
+ t .Errorf ("expected read concern %#v. got %#v" , expected .readConcern , got .readConcern )
52
+ case expected .writeConcern != got .writeConcern :
53
+ t .Errorf ("expected write concern %#v. got %#v" , expected .writeConcern , got .writeConcern )
54
+ }
55
+ }
56
+
57
+ func TestDatabase_Options (t * testing.T ) {
58
+ name := "testDb_options"
59
+ rpPrimary := readpref .Primary ()
60
+ rpSecondary := readpref .Secondary ()
61
+ wc1 := writeconcern .New (writeconcern .W (5 ))
62
+ wc2 := writeconcern .New (writeconcern .W (10 ))
63
+ rcLocal := readconcern .Local ()
64
+ rcMajority := readconcern .Majority ()
65
+
66
+ opts := []dbopt.Option {dbopt .ReadPreference (rpPrimary ), dbopt .ReadConcern (rcLocal ), dbopt .WriteConcern (wc1 ),
67
+ dbopt .ReadPreference (rpSecondary ), dbopt .ReadConcern (rcMajority ), dbopt .WriteConcern (wc2 )}
68
+
69
+ expectedDb := & Database {
70
+ readConcern : rcMajority ,
71
+ readPreference : rpSecondary ,
72
+ writeConcern : wc2 ,
73
+ }
74
+
75
+ t .Run ("IndividualOptions" , func (t * testing.T ) {
76
+ // if options specified multiple times, last instance should take precedence
77
+ db := createTestDatabase (t , & name , opts ... )
78
+ compareDbs (t , expectedDb , db )
79
+ })
80
+
81
+ t .Run ("Bundle" , func (t * testing.T ) {
82
+ db := createTestDatabase (t , & name , dbopt .BundleDatabase (opts ... ))
83
+ compareDbs (t , expectedDb , db )
84
+ })
85
+ }
86
+
87
+ func TestDatabase_InheritOptions (t * testing.T ) {
88
+ name := "testDb_options_inherit"
89
+ client := createTestClient (t )
90
+
91
+ rpPrimary := readpref .Primary ()
92
+ rcLocal := readconcern .Local ()
93
+ client .readPreference = rpPrimary
94
+ client .readConcern = rcLocal
95
+
96
+ wc1 := writeconcern .New (writeconcern .W (10 ))
97
+ db := client .Database (name , dbopt .WriteConcern (wc1 ))
98
+
99
+ // db should inherit read preference and read concern from client
100
+ switch {
101
+ case db .readPreference != rpPrimary :
102
+ t .Errorf ("expected read preference primary. got %#v" , db .readPreference )
103
+ case db .readConcern != rcLocal :
104
+ t .Errorf ("expected read concern local. got %#v" , db .readConcern )
105
+ case db .writeConcern != wc1 :
106
+ t .Errorf ("expected write concern %#v. got %#v" , wc1 , db .writeConcern )
107
+ }
108
+ }
109
+
42
110
func TestDatabase_RunCommand (t * testing.T ) {
43
111
t .Parallel ()
44
112
@@ -190,20 +258,24 @@ func listCollectionsTest(db *Database, cappedOnly bool) error {
190
258
}
191
259
192
260
func TestDatabase_ListCollections (t * testing.T ) {
193
- // TODO(GODRIVER-272): Add tests for the replica_set topology using the secondary read preference
194
261
t .Parallel ()
262
+ rpPrimary := readpref .Primary ()
263
+ rpSecondary := readpref .Secondary ()
195
264
196
265
var listCollectionsTable = []struct {
197
266
name string
198
267
expectedTopology string
199
268
cappedOnly bool
269
+ rp * readpref.ReadPref
200
270
}{
201
- {"standalone_nofilter" , "server" , false },
202
- {"standalone_filter" , "server" , true },
203
- {"replicaset_nofilter" , "replica_set" , false },
204
- {"replicaset_filter" , "replica_set" , true },
205
- {"sharded_nofilter" , "sharded_cluster" , false },
206
- {"sharded_filter" , "sharded_cluster" , true },
271
+ {"standalone_nofilter" , "server" , false , rpPrimary },
272
+ {"standalone_filter" , "server" , true , rpPrimary },
273
+ {"replicaset_nofilter" , "replica_set" , false , rpPrimary },
274
+ {"replicaset_filter" , "replica_set" , true , rpPrimary },
275
+ {"replicaset_secondary_nofilter" , "replica_set" , false , rpSecondary },
276
+ {"replicaset_secondary_filter" , "replica_set" , true , rpSecondary },
277
+ {"sharded_nofilter" , "sharded_cluster" , false , rpPrimary },
278
+ {"sharded_filter" , "sharded_cluster" , true , rpPrimary },
207
279
}
208
280
209
281
for _ , tt := range listCollectionsTable {
@@ -212,7 +284,7 @@ func TestDatabase_ListCollections(t *testing.T) {
212
284
t .Skip ()
213
285
}
214
286
dbName := "db_list_collections"
215
- db := createTestDatabase (t , & dbName )
287
+ db := createTestDatabase (t , & dbName , dbopt . ReadPreference ( tt . rp ) )
216
288
217
289
defer func () {
218
290
err := db .Drop (context .Background ())
0 commit comments