17
17
package com.mongodb
18
18
19
19
import com.mongodb.client.model.Collation
20
+ import com.mongodb.client.model.DBCollectionFindOptions
20
21
import com.mongodb.operation.BatchCursor
21
22
import com.mongodb.operation.CountOperation
22
23
import com.mongodb.operation.FindOperation
23
24
import org.bson.BsonDocument
24
25
import spock.lang.Specification
25
26
27
+ import java.util.concurrent.TimeUnit
28
+
26
29
import static com.mongodb.CustomMatchers.isTheSameAs
27
30
import static com.mongodb.Fixture.getMongoClient
28
31
import static spock.util.matcher.HamcrestSupport.expect
@@ -109,59 +112,34 @@ class DBCursorSpecification extends Specification {
109
112
110
113
def ' find should create the correct FindOperation' () {
111
114
given :
112
- def dbObject = new BasicDBObject (' _id' , 1 )
113
- def executor = new TestOperationExecutor ([Stub (BatchCursor ) {
114
- def count = 0
115
- next() >> {
116
- count++
117
- [dbObject]
118
- }
119
- hasNext() >> {
120
- count == 0
121
- }
122
- getServerCursor() >> {
123
- null
124
- }
125
- }]);
115
+ def executor = new TestOperationExecutor ([stubBatchCursor()]);
126
116
def collection = new DB(getMongoClient(), ' myDatabase' , executor). getCollection(' test' )
127
117
def cursor = new DBCursor (collection, new BasicDBObject (), new BasicDBObject (), ReadPreference . primary())
128
118
cursor. setReadConcern(ReadConcern . MAJORITY )
129
119
130
120
when :
131
- def results = cursor. toArray()
121
+ cursor. toArray()
132
122
133
123
then :
134
- results == [dbObject]
135
124
expect executor. getReadOperation(), isTheSameAs(new FindOperation (collection. getNamespace(), collection. getObjectCodec())
136
125
.readConcern(ReadConcern . MAJORITY )
137
126
.filter(new BsonDocument ())
138
127
.projection(new BsonDocument ())
139
128
.modifiers(new BsonDocument ()))
140
129
}
141
130
131
+
142
132
def ' one should create the correct FindOperation' () {
143
133
given :
144
- def dbObject = new BasicDBObject (' _id' , 1 )
145
- def executor = new TestOperationExecutor ([Stub (BatchCursor ) {
146
- def count = 0
147
- next() >> {
148
- count++
149
- [dbObject]
150
- }
151
- hasNext() >> {
152
- count == 0
153
- }
154
- getServerCursor() >> new ServerCursor (12L , new ServerAddress ())
155
- }]);
134
+ def executor = new TestOperationExecutor ([stubBatchCursor()]);
156
135
def collection = new DB(getMongoClient(), ' myDatabase' , executor). getCollection(' test' )
157
136
def cursor = new DBCursor (collection, new BasicDBObject (), new BasicDBObject (), ReadPreference . primary())
158
137
cursor. setReadConcern(ReadConcern . MAJORITY )
159
138
160
139
when :
161
- def result = cursor. one()
140
+ cursor. one()
162
141
163
142
then :
164
- result == dbObject
165
143
expect executor. getReadOperation(), isTheSameAs(new FindOperation (collection. getNamespace(), collection. getObjectCodec())
166
144
.readConcern(ReadConcern . MAJORITY )
167
145
.limit(-1 )
@@ -170,6 +148,104 @@ class DBCursorSpecification extends Specification {
170
148
.modifiers(new BsonDocument ()))
171
149
}
172
150
151
+ def ' DBCollectionFindOptions should be used to create the expected operation' () {
152
+ given :
153
+ def executor = new TestOperationExecutor ([stubBatchCursor()]);
154
+ def collection = new DB(getMongoClient(), ' myDatabase' , executor). getCollection(' test' )
155
+ def collation = Collation . builder(). locale(' en' ). build()
156
+ def cursorType = CursorType.NonTailable
157
+ def filter = new BasicDBObject ()
158
+ def projection = BasicDBObject . parse(' {a: 1, _id: 0}' )
159
+ def sort = BasicDBObject . parse(' {a: 1}' )
160
+ def modifiers = BasicDBObject . parse(' {$comment: 1}' )
161
+ def bsonFilter = new BsonDocument ()
162
+ def bsonModifiers = BsonDocument . parse(modifiers. toJson())
163
+ def bsonProjection = BsonDocument . parse(projection. toJson())
164
+ def bsonSort = BsonDocument . parse(sort. toJson())
165
+ def readConcern = ReadConcern . LOCAL
166
+ def readPreference = ReadPreference . nearest()
167
+ def findOptions = new DBCollectionFindOptions ()
168
+ .batchSize(1 )
169
+ .collation(collation)
170
+ .cursorType(cursorType)
171
+ .limit(1 )
172
+ .maxAwaitTime(1 , TimeUnit . MILLISECONDS )
173
+ .maxTime(1 , TimeUnit . MILLISECONDS )
174
+ .modifiers(modifiers)
175
+ .noCursorTimeout(true )
176
+ .oplogReplay(true )
177
+ .partial(true )
178
+ .projection(projection)
179
+ .readConcern(readConcern)
180
+ .readPreference(readPreference)
181
+ .skip(1 )
182
+ .sort(sort)
183
+ def cursor = new DBCursor (collection, filter, findOptions)
184
+
185
+ when :
186
+ cursor. toArray()
187
+
188
+ then :
189
+ expect executor. getReadOperation(), isTheSameAs(new FindOperation (collection. getNamespace(), collection. getObjectCodec())
190
+ .batchSize(1 )
191
+ .collation(collation)
192
+ .cursorType(cursorType)
193
+ .filter(bsonFilter)
194
+ .limit(1 )
195
+ .maxAwaitTime(1 , TimeUnit . MILLISECONDS )
196
+ .maxTime(1 , TimeUnit . MILLISECONDS )
197
+ .modifiers(bsonModifiers)
198
+ .noCursorTimeout(true )
199
+ .oplogReplay(true )
200
+ .partial(true )
201
+ .projection(bsonProjection)
202
+ .readConcern(readConcern)
203
+ .skip(1 )
204
+ .sort(bsonSort)
205
+ )
206
+
207
+ executor. getReadPreference() == findOptions. getReadPreference()
208
+ }
209
+
210
+ def ' DBCursor options should override DBCollectionFindOptions' () {
211
+ given :
212
+ def executor = new TestOperationExecutor ([stubBatchCursor()]);
213
+ def collection = new DB(getMongoClient(), ' myDatabase' , executor). getCollection(' test' )
214
+ def cursorType = CursorType.NonTailable
215
+ def readPreference = ReadPreference . primary()
216
+ def filter = new BasicDBObject ()
217
+ def bsonFilter = new BsonDocument ()
218
+ def findOptions = new DBCollectionFindOptions ()
219
+ .cursorType(cursorType)
220
+ .noCursorTimeout(false )
221
+ .oplogReplay(false )
222
+ .partial(false )
223
+ .readPreference(readPreference)
224
+
225
+ def cursor = new DBCursor (collection, filter , findOptions)
226
+ .addOption(Bytes . QUERYOPTION_AWAITDATA )
227
+ .addOption(Bytes . QUERYOPTION_NOTIMEOUT )
228
+ .addOption(Bytes . QUERYOPTION_OPLOGREPLAY )
229
+ .addOption(Bytes . QUERYOPTION_PARTIAL )
230
+ .addOption(Bytes . QUERYOPTION_SLAVEOK )
231
+ .addOption(Bytes . QUERYOPTION_TAILABLE )
232
+
233
+ when :
234
+ cursor. toArray()
235
+
236
+ then :
237
+ expect executor. getReadOperation(), isTheSameAs(new FindOperation (collection. getNamespace(), collection. getObjectCodec())
238
+ .modifiers(new BsonDocument ())
239
+ .cursorType(CursorType.TailableAwait )
240
+ .filter(bsonFilter)
241
+ .noCursorTimeout(true )
242
+ .oplogReplay(true )
243
+ .partial(true )
244
+ )
245
+
246
+ executor. getReadPreference() == ReadPreference . secondaryPreferred()
247
+ }
248
+
173
249
def ' count should create the correct CountOperation' () {
174
250
def executor = new TestOperationExecutor ([42L ]);
175
251
def collection = new DB(getMongoClient(), ' myDatabase' , executor). getCollection(' test' )
@@ -201,4 +277,18 @@ class DBCursorSpecification extends Specification {
201
277
.readConcern(ReadConcern . MAJORITY )
202
278
.filter(new BsonDocument ()))
203
279
}
280
+
281
+ private stubBatchCursor () {
282
+ Stub (BatchCursor ) {
283
+ def count = 0
284
+ next() >> {
285
+ count++
286
+ [new BasicDBObject (' _id' , 1 )]
287
+ }
288
+ hasNext() >> {
289
+ count == 0
290
+ }
291
+ getServerCursor() >> new ServerCursor (12L , new ServerAddress ())
292
+ };
293
+ }
204
294
}
0 commit comments