mongo 1.10.2 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +43 -9
  5. data/VERSION +1 -1
  6. data/lib/mongo.rb +1 -0
  7. data/lib/mongo/collection.rb +36 -21
  8. data/lib/mongo/connection/pool.rb +14 -22
  9. data/lib/mongo/cursor.rb +13 -0
  10. data/lib/mongo/db.rb +18 -13
  11. data/lib/mongo/functional.rb +0 -2
  12. data/lib/mongo/functional/authentication.rb +35 -25
  13. data/lib/mongo/legacy.rb +4 -4
  14. data/mongo.gemspec +0 -5
  15. data/test/functional/authentication_test.rb +3 -2
  16. data/test/functional/bulk_write_collection_view_test.rb +9 -14
  17. data/test/functional/client_test.rb +42 -43
  18. data/test/functional/collection_test.rb +1073 -995
  19. data/test/functional/collection_writer_test.rb +1 -1
  20. data/test/functional/cursor_fail_test.rb +3 -9
  21. data/test/functional/cursor_message_test.rb +14 -15
  22. data/test/functional/cursor_test.rb +224 -166
  23. data/test/functional/db_api_test.rb +262 -261
  24. data/test/functional/db_connection_test.rb +1 -3
  25. data/test/functional/db_test.rb +116 -115
  26. data/test/functional/grid_file_system_test.rb +108 -108
  27. data/test/functional/pool_test.rb +73 -0
  28. data/test/functional/timeout_test.rb +2 -0
  29. data/test/helpers/test_unit.rb +146 -11
  30. data/test/replica_set/authentication_test.rb +4 -2
  31. data/test/replica_set/basic_test.rb +5 -13
  32. data/test/replica_set/client_test.rb +8 -6
  33. data/test/replica_set/complex_connect_test.rb +3 -0
  34. data/test/replica_set/count_test.rb +2 -0
  35. data/test/replica_set/cursor_test.rb +5 -0
  36. data/test/replica_set/insert_test.rb +1 -1
  37. data/test/replica_set/max_values_test.rb +1 -1
  38. data/test/replica_set/pinning_test.rb +1 -1
  39. data/test/replica_set/query_test.rb +1 -1
  40. data/test/replica_set/read_preference_test.rb +7 -1
  41. data/test/replica_set/refresh_test.rb +11 -8
  42. data/test/replica_set/replication_ack_test.rb +2 -1
  43. data/test/sharded_cluster/basic_test.rb +17 -11
  44. data/test/shared/authentication/basic_auth_shared.rb +59 -98
  45. data/test/shared/authentication/bulk_api_auth_shared.rb +11 -21
  46. data/test/shared/authentication/gssapi_shared.rb +28 -21
  47. data/test/test_helper.rb +5 -0
  48. data/test/tools/mongo_config.rb +96 -11
  49. metadata +4 -5
  50. metadata.gz.sig +0 -0
  51. data/lib/mongo/functional/sasl_java.rb +0 -48
@@ -29,7 +29,7 @@ class CollectionWriterTest < Test::Unit::TestCase
29
29
  COLLECTION_NAME = 'test'
30
30
 
31
31
  def default_setup
32
- @client = MongoClient.new
32
+ @client = MongoClient.from_uri(TEST_URI)
33
33
  @db = @client[DATABASE_NAME]
34
34
  @collection = @db[COLLECTION_NAME]
35
35
  @collection.drop
@@ -19,19 +19,13 @@ class CursorFailTest < Test::Unit::TestCase
19
19
 
20
20
  include Mongo
21
21
 
22
- @@connection = standard_connection
23
- @@db = @@connection.db(TEST_DB)
24
- @@coll = @@db.collection('test')
25
- @@version = @@connection.server_version
26
-
27
22
  def setup
28
- @@coll.remove({})
29
- @@coll.insert({'a' => 1}) # collection not created until it's used
30
- @@coll_full_name = "#{TEST_DB}.test"
23
+ @connection = standard_connection
24
+ @db = @connection[TEST_DB]
31
25
  end
32
26
 
33
27
  def test_refill_via_get_more_alt_coll
34
- coll = @@db.collection('test-alt-coll')
28
+ coll = @db.collection('test-alt-coll')
35
29
  coll.remove
36
30
  coll.insert('a' => 1) # collection not created until it's used
37
31
  assert_equal 1, coll.count
@@ -19,39 +19,38 @@ class CursorMessageTest < Test::Unit::TestCase
19
19
 
20
20
  include Mongo
21
21
 
22
- @@connection = standard_connection
23
- @@db = @@connection.db(TEST_DB)
24
- @@coll = @@db.collection('test')
25
- @@version = @@connection.server_version
26
-
27
22
  def setup
28
- @@coll.remove
29
- @@coll.insert('a' => 1) # collection not created until it's used
30
- @@coll_full_name = "#{TEST_DB}.test"
23
+ @connection = standard_connection
24
+ @db = @connection.db(TEST_DB)
25
+ @coll = @db.collection('test')
26
+ @version = @connection.server_version
27
+ @coll.remove
28
+ @coll.insert('a' => 1) # collection not created until it's used
29
+ @coll_full_name = "#{TEST_DB}.test"
31
30
  end
32
31
 
33
32
  def test_valid_batch_sizes
34
33
  assert_raise ArgumentError do
35
- @@coll.find({}, :batch_size => 1, :limit => 5)
34
+ @coll.find({}, :batch_size => 1, :limit => 5)
36
35
  end
37
36
 
38
37
  assert_raise ArgumentError do
39
- @@coll.find({}, :batch_size => -1, :limit => 5)
38
+ @coll.find({}, :batch_size => -1, :limit => 5)
40
39
  end
41
40
 
42
- assert @@coll.find({}, :batch_size => 0, :limit => 5)
41
+ assert @coll.find({}, :batch_size => 0, :limit => 5)
43
42
  end
44
43
 
45
44
  def test_batch_size
46
- @@coll.remove
45
+ @coll.remove
47
46
  200.times do |n|
48
- @@coll.insert({:a => n})
47
+ @coll.insert({:a => n})
49
48
  end
50
49
 
51
- list = @@coll.find({}, :batch_size => 2, :limit => 6).to_a
50
+ list = @coll.find({}, :batch_size => 2, :limit => 6).to_a
52
51
  assert_equal 6, list.length
53
52
 
54
- list = @@coll.find({}, :batch_size => 100, :limit => 101).to_a
53
+ list = @coll.find({}, :batch_size => 100, :limit => 101).to_a
55
54
  assert_equal 101, list.length
56
55
  end
57
56
  end
@@ -19,15 +19,14 @@ class CursorTest < Test::Unit::TestCase
19
19
  include Mongo
20
20
  include Mongo::Constants
21
21
 
22
- @@connection = standard_connection
23
- @@db = @@connection.db(TEST_DB)
24
- @@coll = @@db.collection('test')
25
- @@version = @@connection.server_version
26
-
27
22
  def setup
28
- @@coll.remove
29
- @@coll.insert('a' => 1) # collection not created until it's used
30
- @@coll_full_name = "#{TEST_DB}.test"
23
+ @connection = standard_connection
24
+ @db = @connection.db(TEST_DB)
25
+ @coll = @db.collection('test')
26
+ @version = @connection.server_version
27
+ @coll.remove
28
+ @coll.insert('a' => 1) # collection not created until it's used
29
+ @coll_full_name = "#{TEST_DB}.test"
31
30
  end
32
31
 
33
32
  def test_alive
@@ -36,18 +35,18 @@ class CursorTest < Test::Unit::TestCase
36
35
  batch << {:a => n}
37
36
  end
38
37
 
39
- @@coll.insert(batch)
40
- cursor = @@coll.find
38
+ @coll.insert(batch)
39
+ cursor = @coll.find
41
40
  assert !cursor.alive?
42
41
  cursor.next
43
42
  assert cursor.alive?
44
43
  cursor.close
45
44
  assert !cursor.alive?
46
- @@coll.remove
45
+ @coll.remove
47
46
  end
48
47
 
49
48
  def test_add_and_remove_options
50
- c = @@coll.find
49
+ c = @coll.find
51
50
  assert_equal 0, c.options & OP_QUERY_EXHAUST
52
51
  c.add_option(OP_QUERY_EXHAUST)
53
52
  assert_equal OP_QUERY_EXHAUST, c.options & OP_QUERY_EXHAUST
@@ -65,19 +64,19 @@ class CursorTest < Test::Unit::TestCase
65
64
  end
66
65
 
67
66
  def test_exhaust
68
- if @@version >= "2.0"
69
- @@coll.remove
67
+ if @version >= "2.0"
68
+ @coll.remove
70
69
  data = "1" * 10_000
71
70
  5000.times do |n|
72
- @@coll.insert({:n => n, :data => data})
71
+ @coll.insert({:n => n, :data => data})
73
72
  end
74
73
 
75
- c = Cursor.new(@@coll)
74
+ c = Cursor.new(@coll)
76
75
  c.add_option(OP_QUERY_EXHAUST)
77
- assert_equal @@coll.count, c.to_a.size
76
+ assert_equal @coll.count, c.to_a.size
78
77
  assert c.closed?
79
78
 
80
- c = Cursor.new(@@coll)
79
+ c = Cursor.new(@coll)
81
80
  c.add_option(OP_QUERY_EXHAUST)
82
81
  4999.times do
83
82
  c.next
@@ -87,16 +86,16 @@ class CursorTest < Test::Unit::TestCase
87
86
  assert !c.has_next?
88
87
  assert c.closed?
89
88
 
90
- @@coll.remove
89
+ @coll.remove
91
90
  end
92
91
  end
93
92
 
94
93
  def test_compile_regex_get_more
95
94
  return unless defined?(BSON::BSON_RUBY) && BSON::BSON_CODER == BSON::BSON_RUBY
96
- @@coll.remove
95
+ @coll.remove
97
96
  n_docs = 3
98
- n_docs.times { |n| @@coll.insert({ 'n' => /.*/ }) }
99
- cursor = @@coll.find({}, :batch_size => (n_docs-1), :compile_regex => false)
97
+ n_docs.times { |n| @coll.insert({ 'n' => /.*/ }) }
98
+ cursor = @coll.find({}, :batch_size => (n_docs-1), :compile_regex => false)
100
99
  cursor.expects(:send_get_more)
101
100
  cursor.to_a.each do |doc|
102
101
  assert_kind_of BSON::Regex, doc['n']
@@ -104,7 +103,7 @@ class CursorTest < Test::Unit::TestCase
104
103
  end
105
104
 
106
105
  def test_max_time_ms_error
107
- cursor = @@coll.find
106
+ cursor = @coll.find
108
107
  cursor.stubs(:send_initial_query).returns(true)
109
108
 
110
109
  cursor.instance_variable_set(:@cache, [{
@@ -118,16 +117,16 @@ class CursorTest < Test::Unit::TestCase
118
117
  end
119
118
 
120
119
  def test_max_time_ms
121
- with_forced_timeout(@@connection) do
120
+ with_forced_timeout(@connection) do
122
121
  assert_raise ExecutionTimeout do
123
- cursor = @@coll.find.max_time_ms(100)
122
+ cursor = @coll.find.max_time_ms(100)
124
123
  cursor.to_a
125
124
  end
126
125
  end
127
126
  end
128
127
 
129
128
  def test_exhaust_after_limit_error
130
- c = Cursor.new(@@coll, :limit => 17)
129
+ c = Cursor.new(@coll, :limit => 17)
131
130
  assert_raise MongoArgumentError do
132
131
  c.add_option(OP_QUERY_EXHAUST)
133
132
  end
@@ -138,7 +137,7 @@ class CursorTest < Test::Unit::TestCase
138
137
  end
139
138
 
140
139
  def test_limit_after_exhaust_error
141
- c = Cursor.new(@@coll)
140
+ c = Cursor.new(@coll)
142
141
  c.add_option(OP_QUERY_EXHAUST)
143
142
  assert_raise MongoArgumentError do
144
143
  c.limit(17)
@@ -146,8 +145,8 @@ class CursorTest < Test::Unit::TestCase
146
145
  end
147
146
 
148
147
  def test_exhaust_with_mongos
149
- @@connection.expects(:mongos?).returns(:true)
150
- c = Cursor.new(@@coll)
148
+ @connection.expects(:mongos?).returns(:true)
149
+ c = Cursor.new(@coll)
151
150
 
152
151
  assert_raise MongoArgumentError do
153
152
  c.add_option(OP_QUERY_EXHAUST)
@@ -156,110 +155,116 @@ class CursorTest < Test::Unit::TestCase
156
155
 
157
156
  def test_inspect
158
157
  selector = {:a => 1}
159
- cursor = @@coll.find(selector)
160
- assert_equal "<Mongo::Cursor:0x#{cursor.object_id.to_s(16)} namespace='#{@@db.name}.#{@@coll.name}' " +
161
- "@selector=#{selector.inspect} @cursor_id=#{cursor.cursor_id}>", cursor.inspect
158
+ cursor = @coll.find(selector)
159
+ assert_equal "<Mongo::Cursor:0x#{cursor.object_id.to_s(16)} namespace='#{@db.name}.#{@coll.name}' " +
160
+ "@selector=#{selector.inspect} @cursor_id=#{cursor.cursor_id}>", cursor.inspect
162
161
  end
163
162
 
164
163
  def test_explain
165
- cursor = @@coll.find('a' => 1)
166
- explaination = cursor.explain
167
- assert_not_nil explaination['cursor']
168
- assert_kind_of Numeric, explaination['n']
169
- assert_kind_of Numeric, explaination['millis']
170
- assert_kind_of Numeric, explaination['nscanned']
164
+ cursor = @coll.find('a' => 1)
165
+ explanation = cursor.explain
166
+ if @version < '2.7'
167
+ assert_not_nil explanation['cursor']
168
+ assert_kind_of Numeric, explanation['n']
169
+ assert_kind_of Numeric, explanation['millis']
170
+ assert_kind_of Numeric, explanation['nscanned']
171
+ else
172
+ cursor = @coll.find('a' => 1)
173
+ assert_not_nil explanation
174
+ assert explanation.keys.include?('executionStats')
175
+ end
171
176
  end
172
177
 
173
178
  def test_each_with_no_block
174
- assert_kind_of(Enumerator, @@coll.find().each) if defined? Enumerator
179
+ assert_kind_of(Enumerator, @coll.find().each) if defined? Enumerator
175
180
  end
176
181
 
177
182
  def test_count
178
- @@coll.remove
183
+ @coll.remove
179
184
 
180
- assert_equal 0, @@coll.find().count()
185
+ assert_equal 0, @coll.find().count()
181
186
 
182
187
  10.times do |i|
183
- @@coll.save("x" => i)
188
+ @coll.save("x" => i)
184
189
  end
185
190
 
186
- assert_equal 10, @@coll.find().count()
187
- assert_kind_of Integer, @@coll.find().count()
188
- assert_equal 10, @@coll.find({}, :limit => 5).count()
189
- assert_equal 10, @@coll.find({}, :skip => 5).count()
191
+ assert_equal 10, @coll.find().count()
192
+ assert_kind_of Integer, @coll.find().count()
193
+ assert_equal 10, @coll.find({}, :limit => 5).count()
194
+ assert_equal 10, @coll.find({}, :skip => 5).count()
190
195
 
191
- assert_equal 5, @@coll.find({}, :limit => 5).count(true)
192
- assert_equal 5, @@coll.find({}, :skip => 5).count(true)
193
- assert_equal 2, @@coll.find({}, :skip => 5, :limit => 2).count(true)
196
+ assert_equal 5, @coll.find({}, :limit => 5).count(true)
197
+ assert_equal 5, @coll.find({}, :skip => 5).count(true)
198
+ assert_equal 2, @coll.find({}, :skip => 5, :limit => 2).count(true)
194
199
 
195
- assert_equal 1, @@coll.find({"x" => 1}).count()
196
- assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
200
+ assert_equal 1, @coll.find({"x" => 1}).count()
201
+ assert_equal 5, @coll.find({"x" => {"$lt" => 5}}).count()
197
202
 
198
- a = @@coll.find()
203
+ a = @coll.find()
199
204
  b = a.count()
200
205
  a.each do |doc|
201
206
  break
202
207
  end
203
208
  assert_equal b, a.count()
204
209
 
205
- assert_equal 0, @@db['acollectionthatdoesn'].count()
210
+ assert_equal 0, @db['acollectionthatdoesn'].count()
206
211
  end
207
212
 
208
213
  def test_sort
209
- @@coll.remove
210
- 5.times{|x| @@coll.insert({"age" => x}) }
214
+ @coll.remove
215
+ 5.times{|x| @coll.insert({"age" => x}) }
211
216
 
212
- assert_kind_of Cursor, @@coll.find().sort(:age, 1)
217
+ assert_kind_of Cursor, @coll.find().sort(:age, 1)
213
218
 
214
- assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"]
215
- assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"]
216
- assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"]
219
+ assert_equal 0, @coll.find().sort(:age, 1).next_document["age"]
220
+ assert_equal 4, @coll.find().sort(:age, -1).next_document["age"]
221
+ assert_equal 0, @coll.find().sort([["age", :asc]]).next_document["age"]
217
222
 
218
- assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]])
223
+ assert_kind_of Cursor, @coll.find().sort([[:age, -1], [:b, 1]])
219
224
 
220
- assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
221
- assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
225
+ assert_equal 4, @coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
226
+ assert_equal 0, @coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
222
227
 
223
- assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
224
- assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
228
+ assert_equal 4, @coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
229
+ assert_equal 0, @coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
225
230
 
226
- cursor = @@coll.find()
231
+ cursor = @coll.find()
227
232
  cursor.next_document
228
233
  assert_raise InvalidOperation do
229
234
  cursor.sort(["age"])
230
235
  end
231
236
 
232
237
  assert_raise InvalidSortValueError do
233
- @@coll.find().sort(:age, 25).next_document
238
+ @coll.find().sort(:age, 25).next_document
234
239
  end
235
240
 
236
241
  assert_raise InvalidSortValueError do
237
- @@coll.find().sort(25).next_document
242
+ @coll.find().sort(25).next_document
238
243
  end
239
244
  end
240
245
 
241
246
  def test_sort_date
242
- @@coll.remove
243
- 5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) }
247
+ @coll.remove
248
+ 5.times{|x| @coll.insert({"created_at" => Time.utc(2000 + x)}) }
244
249
 
245
- assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year
246
- assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year
250
+ assert_equal 2000, @coll.find().sort(:created_at, :asc).next_document["created_at"].year
251
+ assert_equal 2004, @coll.find().sort(:created_at, :desc).next_document["created_at"].year
247
252
 
248
- assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year
249
- assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year
253
+ assert_equal 2000, @coll.find().sort([:created_at, :asc]).next_document["created_at"].year
254
+ assert_equal 2004, @coll.find().sort([:created_at, :desc]).next_document["created_at"].year
250
255
 
251
- assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
252
- assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
256
+ assert_equal 2000, @coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
257
+ assert_equal 2004, @coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
253
258
  end
254
259
 
255
260
  def test_sort_min_max_keys
256
- @@coll.remove
257
- @@coll.insert({"n" => 1000000})
258
- @@coll.insert({"n" => -1000000})
259
- @@coll.insert({"n" => MaxKey.new})
260
- @@coll.insert({"n" => MinKey.new})
261
+ @coll.remove
262
+ @coll.insert({"n" => 1000000})
263
+ @coll.insert({"n" => -1000000})
264
+ @coll.insert({"n" => MaxKey.new})
265
+ @coll.insert({"n" => MinKey.new})
261
266
 
262
- results = @@coll.find.sort([:n, :asc]).to_a
267
+ results = @coll.find.sort([:n, :asc]).to_a
263
268
 
264
269
  assert_equal MinKey.new, results[0]['n']
265
270
  assert_equal(-1000000, results[1]['n'])
@@ -268,73 +273,73 @@ class CursorTest < Test::Unit::TestCase
268
273
  end
269
274
 
270
275
  def test_id_range_queries
271
- @@coll.remove
276
+ @coll.remove
272
277
 
273
278
  t1 = Time.now
274
279
  t1_id = ObjectId.from_time(t1)
275
- @@coll.save({:t => 't1'})
276
- @@coll.save({:t => 't1'})
277
- @@coll.save({:t => 't1'})
280
+ @coll.save({:t => 't1'})
281
+ @coll.save({:t => 't1'})
282
+ @coll.save({:t => 't1'})
278
283
  sleep(1)
279
284
  t2 = Time.now
280
285
  t2_id = ObjectId.from_time(t2)
281
- @@coll.save({:t => 't2'})
282
- @@coll.save({:t => 't2'})
283
- @@coll.save({:t => 't2'})
286
+ @coll.save({:t => 't2'})
287
+ @coll.save({:t => 't2'})
288
+ @coll.save({:t => 't2'})
284
289
 
285
- assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id, '$lt' => t2_id}}).count
286
- @@coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
290
+ assert_equal 3, @coll.find({'_id' => {'$gt' => t1_id, '$lt' => t2_id}}).count
291
+ @coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
287
292
  assert_equal 't2', doc['t']
288
293
  end
289
294
  end
290
295
 
291
296
  def test_limit
292
- @@coll.remove
297
+ @coll.remove
293
298
 
294
299
  10.times do |i|
295
- @@coll.save("x" => i)
300
+ @coll.save("x" => i)
296
301
  end
297
- assert_equal 10, @@coll.find().count()
302
+ assert_equal 10, @coll.find().count()
298
303
 
299
- results = @@coll.find().limit(5).to_a
304
+ results = @coll.find().limit(5).to_a
300
305
  assert_equal 5, results.length
301
306
  end
302
307
 
303
308
  def test_timeout_options
304
- cursor = Cursor.new(@@coll)
309
+ cursor = Cursor.new(@coll)
305
310
  assert_equal true, cursor.timeout
306
311
 
307
- cursor = @@coll.find
312
+ cursor = @coll.find
308
313
  assert_equal true, cursor.timeout
309
314
 
310
- cursor = @@coll.find({}, :timeout => nil)
315
+ cursor = @coll.find({}, :timeout => nil)
311
316
  assert_equal true, cursor.timeout
312
317
 
313
- cursor = Cursor.new(@@coll, :timeout => false)
318
+ cursor = Cursor.new(@coll, :timeout => false)
314
319
  assert_equal false, cursor.timeout
315
320
 
316
- @@coll.find({}, :timeout => false) do |c|
321
+ @coll.find({}, :timeout => false) do |c|
317
322
  assert_equal false, c.timeout
318
323
  end
319
324
  end
320
325
 
321
326
  def test_timeout
322
- opts = Cursor.new(@@coll).options
327
+ opts = Cursor.new(@coll).options
323
328
  assert_equal 0, opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
324
329
 
325
- opts = Cursor.new(@@coll, :timeout => false).options
330
+ opts = Cursor.new(@coll, :timeout => false).options
326
331
  assert_equal Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT,
327
- opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
332
+ opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
328
333
  end
329
334
 
330
335
  def test_limit_exceptions
331
- cursor = @@coll.find()
336
+ cursor = @coll.find()
332
337
  cursor.next_document
333
338
  assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
334
339
  cursor.limit(1)
335
340
  end
336
341
 
337
- cursor = @@coll.find()
342
+ cursor = @coll.find()
338
343
  cursor.close
339
344
  assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
340
345
  cursor.limit(1)
@@ -342,15 +347,15 @@ class CursorTest < Test::Unit::TestCase
342
347
  end
343
348
 
344
349
  def test_skip
345
- @@coll.remove
350
+ @coll.remove
346
351
 
347
352
  10.times do |i|
348
- @@coll.save("x" => i)
353
+ @coll.save("x" => i)
349
354
  end
350
- assert_equal 10, @@coll.find().count()
355
+ assert_equal 10, @coll.find().count()
351
356
 
352
- all_results = @@coll.find().to_a
353
- skip_results = @@coll.find().skip(2).to_a
357
+ all_results = @coll.find().to_a
358
+ skip_results = @coll.find().skip(2).to_a
354
359
  assert_equal 10, all_results.length
355
360
  assert_equal 8, skip_results.length
356
361
 
@@ -358,13 +363,13 @@ class CursorTest < Test::Unit::TestCase
358
363
  end
359
364
 
360
365
  def test_skip_exceptions
361
- cursor = @@coll.find()
366
+ cursor = @coll.find()
362
367
  cursor.next_document
363
368
  assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
364
369
  cursor.skip(1)
365
370
  end
366
371
 
367
- cursor = @@coll.find()
372
+ cursor = @coll.find()
368
373
  cursor.close
369
374
  assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
370
375
  cursor.skip(1)
@@ -372,20 +377,20 @@ class CursorTest < Test::Unit::TestCase
372
377
  end
373
378
 
374
379
  def test_limit_skip_chaining
375
- @@coll.remove
380
+ @coll.remove
376
381
  10.times do |i|
377
- @@coll.save("x" => i)
382
+ @coll.save("x" => i)
378
383
  end
379
384
 
380
- all_results = @@coll.find().to_a
381
- limited_skip_results = @@coll.find().limit(5).skip(3).to_a
385
+ all_results = @coll.find().to_a
386
+ limited_skip_results = @coll.find().limit(5).skip(3).to_a
382
387
 
383
388
  assert_equal all_results.slice(3...8), limited_skip_results
384
389
  end
385
390
 
386
391
  def test_close_no_query_sent
387
392
  begin
388
- cursor = @@coll.find('a' => 1)
393
+ cursor = @coll.find('a' => 1)
389
394
  cursor.close
390
395
  assert cursor.closed?
391
396
  rescue => ex
@@ -394,33 +399,33 @@ class CursorTest < Test::Unit::TestCase
394
399
  end
395
400
 
396
401
  def test_refill_via_get_more
397
- assert_equal 1, @@coll.count
402
+ assert_equal 1, @coll.count
398
403
  1000.times { |i|
399
- assert_equal 1 + i, @@coll.count
400
- @@coll.insert('a' => i)
404
+ assert_equal 1 + i, @coll.count
405
+ @coll.insert('a' => i)
401
406
  }
402
407
 
403
- assert_equal 1001, @@coll.count
408
+ assert_equal 1001, @coll.count
404
409
  count = 0
405
- @@coll.find.each { |obj|
410
+ @coll.find.each { |obj|
406
411
  count += obj['a']
407
412
  }
408
- assert_equal 1001, @@coll.count
413
+ assert_equal 1001, @coll.count
409
414
 
410
415
  # do the same thing again for debugging
411
- assert_equal 1001, @@coll.count
416
+ assert_equal 1001, @coll.count
412
417
  count2 = 0
413
- @@coll.find.each { |obj|
418
+ @coll.find.each { |obj|
414
419
  count2 += obj['a']
415
420
  }
416
- assert_equal 1001, @@coll.count
421
+ assert_equal 1001, @coll.count
417
422
 
418
423
  assert_equal count, count2
419
424
  assert_equal 499501, count
420
425
  end
421
426
 
422
427
  def test_refill_via_get_more_alt_coll
423
- coll = @@db.collection('test-alt-coll')
428
+ coll = @db.collection('test-alt-coll')
424
429
  coll.remove
425
430
  coll.insert('a' => 1) # collection not created until it's used
426
431
  assert_equal 1, coll.count
@@ -451,7 +456,7 @@ class CursorTest < Test::Unit::TestCase
451
456
 
452
457
  def test_close_after_query_sent
453
458
  begin
454
- cursor = @@coll.find('a' => 1)
459
+ cursor = @coll.find('a' => 1)
455
460
  cursor.next_document
456
461
  cursor.close
457
462
  assert cursor.closed?
@@ -461,82 +466,135 @@ class CursorTest < Test::Unit::TestCase
461
466
  end
462
467
 
463
468
  def test_kill_cursors
464
- @@coll.drop
469
+ @coll.drop
465
470
 
466
- client_cursors = @@db.command("cursorInfo" => 1)["clientCursors_size"]
471
+ client_cursors = @db.command("cursorInfo" => 1)["clientCursors_size"]
467
472
 
468
473
  10000.times do |i|
469
- @@coll.insert("i" => i)
474
+ @coll.insert("i" => i)
470
475
  end
471
476
 
472
477
  assert_equal(client_cursors,
473
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
478
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
474
479
 
475
480
  10.times do |i|
476
- @@coll.find_one()
481
+ @coll.find_one()
477
482
  end
478
483
 
479
484
  assert_equal(client_cursors,
480
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
485
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
481
486
 
482
487
  10.times do |i|
483
- a = @@coll.find()
488
+ a = @coll.find()
484
489
  a.next_document
485
490
  a.close()
486
491
  end
487
492
 
488
493
  assert_equal(client_cursors,
489
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
494
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
490
495
 
491
- a = @@coll.find()
496
+ a = @coll.find()
492
497
  a.next_document
493
498
 
494
499
  assert_not_equal(client_cursors,
495
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
500
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
496
501
 
497
502
  a.close()
498
503
 
499
504
  assert_equal(client_cursors,
500
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
505
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
501
506
 
502
- a = @@coll.find({}, :limit => 10).next_document
507
+ a = @coll.find({}, :limit => 10).next_document
503
508
 
504
509
  assert_equal(client_cursors,
505
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
510
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
506
511
 
507
- @@coll.find() do |cursor|
512
+ @coll.find() do |cursor|
508
513
  cursor.next_document
509
514
  end
510
515
 
511
516
  assert_equal(client_cursors,
512
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
517
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
513
518
 
514
- @@coll.find() { |cursor|
519
+ @coll.find() { |cursor|
515
520
  cursor.next_document
516
521
  }
517
522
 
518
523
  assert_equal(client_cursors,
519
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
524
+ @db.command("cursorInfo" => 1)["clientCursors_size"])
520
525
  end
521
526
 
522
527
  def test_count_with_fields
523
- @@coll.remove
524
- @@coll.save("x" => 1)
528
+ @coll.remove
529
+ @coll.save("x" => 1)
525
530
 
526
- if @@version < "1.1.3"
527
- assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
531
+ if @version < "1.1.3"
532
+ assert_equal(0, @coll.find({}, :fields => ["a"]).count())
533
+ else
534
+ assert_equal(1, @coll.find({}, :fields => ["a"]).count())
535
+ end
536
+ end
537
+
538
+ def test_count_with_hint
539
+ @coll.drop
540
+ @coll.save(:i => 1)
541
+ @coll.save(:i => 2)
542
+ assert_equal 2, @coll.find.count
543
+
544
+ @coll.ensure_index(BSON::OrderedHash[:i, Mongo::ASCENDING])
545
+
546
+ # Check that a named_hint can be specified
547
+ assert_equal 1, @coll.find({ :i => 1 }, :named_hint => '_id_').count
548
+ assert_equal 2, @coll.find({ }, :named_hint => '_id_').count
549
+
550
+ # Verify that the hint is being sent to the server by providing a bad hint
551
+ if @version > '2.6'
552
+ assert_raise Mongo::OperationFailure do
553
+ @coll.find({ :i => 1 }, :hint => 'bad_hint').count
554
+ end
528
555
  else
529
- assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
556
+ assert_equal 1, @coll.find({ :i => 1 }, :hint => 'bad_hint').count
530
557
  end
558
+
559
+ # Verify that the named_hint is being sent to the server by providing a bad hint
560
+ if @version > '2.6'
561
+ assert_raise Mongo::OperationFailure do
562
+ @coll.find({ :i => 1 }, :named_hint => 'bad_hint').count
563
+ end
564
+ else
565
+ assert_equal 1, @coll.find({ :i => 1 }, :named_hint => 'bad_hint').count
566
+ end
567
+
568
+ @coll.ensure_index(BSON::OrderedHash[:x, Mongo::ASCENDING], :sparse => true)
569
+
570
+ # The sparse index won't have any entries.
571
+ # Check that count returns 0 when using the hint.
572
+ expected = @version > '2.6' ? 0 : 1
573
+ assert_equal expected, @coll.find({ :i => 1 }, :hint => { 'x' => 1 }).count
574
+ assert_equal expected, @coll.find({ :i => 1 }, :hint => 'x').count
575
+ assert_equal expected, @coll.find({ :i => 1 }, :named_hint => 'x_1').count
576
+
577
+ # Verify that the hint / named hint set on the collection is used.
578
+ @coll.hint = { 'x' => 1 }
579
+ assert_equal expected, @coll.find(:i => 1).count
580
+
581
+ @coll.hint = 'x'
582
+ assert_equal expected, @coll.find(:i => 1).count
583
+
584
+ @coll.named_hint = 'x_1'
585
+ assert_equal expected, @coll.find(:i => 1).count
586
+
587
+ assert_equal 2, @coll.find({ }, :hint => 'x').count
588
+ assert_equal 2, @coll.find({ }, :named_hint => 'x_1').count
531
589
  end
532
590
 
533
591
  def test_has_next
534
- @@coll.remove
592
+ @coll.remove
535
593
  200.times do |n|
536
- @@coll.save("x" => n)
594
+ @coll.save("x" => n)
537
595
  end
538
596
 
539
- cursor = @@coll.find
597
+ cursor = @coll.find
540
598
  n = 0
541
599
  while cursor.has_next?
542
600
  assert cursor.next
@@ -548,12 +606,12 @@ class CursorTest < Test::Unit::TestCase
548
606
  end
549
607
 
550
608
  def test_cursor_invalid
551
- @@coll.remove
609
+ @coll.remove
552
610
  10000.times do |n|
553
- @@coll.insert({:a => n})
611
+ @coll.insert({:a => n})
554
612
  end
555
613
 
556
- cursor = @@coll.find({})
614
+ cursor = @coll.find({})
557
615
 
558
616
  assert_raise_error Mongo::OperationFailure, "CURSOR_NOT_FOUND" do
559
617
  9999.times do
@@ -564,26 +622,26 @@ class CursorTest < Test::Unit::TestCase
564
622
  end
565
623
 
566
624
  def test_enumberables
567
- @@coll.remove
625
+ @coll.remove
568
626
  100.times do |n|
569
- @@coll.insert({:a => n})
627
+ @coll.insert({:a => n})
570
628
  end
571
629
 
572
- assert_equal 100, @@coll.find.to_a.length
573
- assert_equal 100, @@coll.find.to_set.length
630
+ assert_equal 100, @coll.find.to_a.length
631
+ assert_equal 100, @coll.find.to_set.length
574
632
 
575
- cursor = @@coll.find
633
+ cursor = @coll.find
576
634
  50.times { |n| cursor.next_document }
577
635
  assert_equal 50, cursor.to_a.length
578
636
  end
579
637
 
580
638
  def test_rewind
581
- @@coll.remove
639
+ @coll.remove
582
640
  100.times do |n|
583
- @@coll.insert({:a => n})
641
+ @coll.insert({:a => n})
584
642
  end
585
643
 
586
- cursor = @@coll.find
644
+ cursor = @coll.find
587
645
  cursor.to_a
588
646
  assert_equal [], cursor.map {|doc| doc }
589
647
 
@@ -598,14 +656,14 @@ class CursorTest < Test::Unit::TestCase
598
656
 
599
657
  def test_transformer
600
658
  transformer = Proc.new { |doc| doc }
601
- cursor = Cursor.new(@@coll, :transformer => transformer)
659
+ cursor = Cursor.new(@coll, :transformer => transformer)
602
660
  assert_equal(transformer, cursor.transformer)
603
661
  end
604
662
 
605
663
  def test_instance_transformation_with_next
606
664
  klass = Struct.new(:id, :a)
607
665
  transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
608
- cursor = Cursor.new(@@coll, :transformer => transformer)
666
+ cursor = Cursor.new(@coll, :transformer => transformer)
609
667
  instance = cursor.next
610
668
 
611
669
  assert_instance_of(klass, instance)
@@ -616,7 +674,7 @@ class CursorTest < Test::Unit::TestCase
616
674
  def test_instance_transformation_with_each
617
675
  klass = Struct.new(:id, :a)
618
676
  transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
619
- cursor = Cursor.new(@@coll, :transformer => transformer)
677
+ cursor = Cursor.new(@coll, :transformer => transformer)
620
678
 
621
679
  cursor.each do |instance|
622
680
  assert_instance_of(klass, instance)