@@ -15,9 +15,12 @@ import (
15
15
"github.com/mongodb/mongo-go-driver/bson"
16
16
"github.com/mongodb/mongo-go-driver/bson/objectid"
17
17
"github.com/mongodb/mongo-go-driver/core/option"
18
+ "github.com/mongodb/mongo-go-driver/core/readconcern"
19
+ "github.com/mongodb/mongo-go-driver/core/readpref"
18
20
"github.com/mongodb/mongo-go-driver/core/writeconcern"
19
21
"github.com/mongodb/mongo-go-driver/internal/testutil"
20
22
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
23
+ "github.com/mongodb/mongo-go-driver/mongo/collectionopt"
21
24
"github.com/mongodb/mongo-go-driver/mongo/countopt"
22
25
"github.com/mongodb/mongo-go-driver/mongo/deleteopt"
23
26
"github.com/mongodb/mongo-go-driver/mongo/distinctopt"
@@ -29,15 +32,15 @@ import (
29
32
"github.com/stretchr/testify/require"
30
33
)
31
34
32
- func createTestCollection (t * testing.T , dbName * string , collName * string ) * Collection {
35
+ func createTestCollection (t * testing.T , dbName * string , collName * string , opts ... collectionopt. Option ) * Collection {
33
36
if collName == nil {
34
37
coll := testutil .ColName (t )
35
38
collName = & coll
36
39
}
37
40
38
41
db := createTestDatabase (t , dbName )
39
42
40
- return db .Collection (* collName )
43
+ return db .Collection (* collName , opts ... )
41
44
}
42
45
43
46
func initCollection (t * testing.T , coll * Collection ) {
@@ -76,6 +79,73 @@ func TestCollection_initialize(t *testing.T) {
76
79
require .NotNil (t , coll .db )
77
80
}
78
81
82
+ func compareColls (t * testing.T , expected * Collection , got * Collection ) {
83
+ switch {
84
+ case expected .readPreference != got .readPreference :
85
+ t .Errorf ("expected read preference %#v. got %#v" , expected .readPreference , got .readPreference )
86
+ case expected .readConcern != got .readConcern :
87
+ t .Errorf ("expected read concern %#v. got %#v" , expected .readConcern , got .readConcern )
88
+ case expected .writeConcern != got .writeConcern :
89
+ t .Errorf ("expected write concern %#v. got %#v" , expected .writeConcern , got .writeConcern )
90
+ }
91
+ }
92
+
93
+ func TestCollection_Options (t * testing.T ) {
94
+ name := "testDb_options"
95
+ rpPrimary := readpref .Primary ()
96
+ rpSecondary := readpref .Secondary ()
97
+ wc1 := writeconcern .New (writeconcern .W (5 ))
98
+ wc2 := writeconcern .New (writeconcern .W (10 ))
99
+ rcLocal := readconcern .Local ()
100
+ rcMajority := readconcern .Majority ()
101
+
102
+ opts := []collectionopt.Option {collectionopt .ReadPreference (rpPrimary ), collectionopt .ReadConcern (rcLocal ), collectionopt .WriteConcern (wc1 ),
103
+ collectionopt .ReadPreference (rpSecondary ), collectionopt .ReadConcern (rcMajority ), collectionopt .WriteConcern (wc2 )}
104
+
105
+ dbName := "collection_internal_test_db1"
106
+
107
+ expectedColl := & Collection {
108
+ readConcern : rcMajority ,
109
+ readPreference : rpSecondary ,
110
+ writeConcern : wc2 ,
111
+ }
112
+
113
+ t .Run ("IndividualOptions" , func (t * testing.T ) {
114
+ // if options specified multiple times, last instance should take precedence
115
+ coll := createTestCollection (t , & dbName , & name , opts ... )
116
+ compareColls (t , expectedColl , coll )
117
+ })
118
+
119
+ t .Run ("Bundle" , func (t * testing.T ) {
120
+ coll := createTestCollection (t , & dbName , & name , collectionopt .BundleCollection (opts ... ))
121
+ compareColls (t , expectedColl , coll )
122
+ })
123
+ }
124
+
125
+ func TestCollection_InheritOptions (t * testing.T ) {
126
+ name := "testDb_options_inherit"
127
+ client := createTestClient (t )
128
+
129
+ rpPrimary := readpref .Primary ()
130
+ rcLocal := readconcern .Local ()
131
+ wc1 := writeconcern .New (writeconcern .W (10 ))
132
+
133
+ db := client .Database ("collection_internal_test_db2" )
134
+ db .readPreference = rpPrimary
135
+ db .readConcern = rcLocal
136
+ coll := db .Collection (name , collectionopt .WriteConcern (wc1 ))
137
+
138
+ // coll should inherit read preference and read concern from client
139
+ switch {
140
+ case coll .readPreference != rpPrimary :
141
+ t .Errorf ("expected read preference primary. got %#v" , coll .readPreference )
142
+ case coll .readConcern != rcLocal :
143
+ t .Errorf ("expected read concern local. got %#v" , coll .readConcern )
144
+ case coll .writeConcern != wc1 :
145
+ t .Errorf ("expected write concern %#v. got %#v" , wc1 , coll .writeConcern )
146
+ }
147
+ }
148
+
79
149
func TestCollection_namespace (t * testing.T ) {
80
150
t .Parallel ()
81
151
0 commit comments