mongo-find_replace 0.18.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/LICENSE.txt +202 -0
  2. data/README.rdoc +358 -0
  3. data/Rakefile +133 -0
  4. data/bin/bson_benchmark.rb +59 -0
  5. data/bin/fail_if_no_c.rb +11 -0
  6. data/examples/admin.rb +42 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +88 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +70 -0
  13. data/examples/simple.rb +24 -0
  14. data/examples/strict.rb +35 -0
  15. data/examples/types.rb +36 -0
  16. data/lib/mongo.rb +61 -0
  17. data/lib/mongo/admin.rb +95 -0
  18. data/lib/mongo/collection.rb +664 -0
  19. data/lib/mongo/connection.rb +555 -0
  20. data/lib/mongo/cursor.rb +393 -0
  21. data/lib/mongo/db.rb +527 -0
  22. data/lib/mongo/exceptions.rb +60 -0
  23. data/lib/mongo/gridfs.rb +22 -0
  24. data/lib/mongo/gridfs/chunk.rb +90 -0
  25. data/lib/mongo/gridfs/grid_store.rb +555 -0
  26. data/lib/mongo/types/binary.rb +48 -0
  27. data/lib/mongo/types/code.rb +36 -0
  28. data/lib/mongo/types/dbref.rb +38 -0
  29. data/lib/mongo/types/min_max_keys.rb +58 -0
  30. data/lib/mongo/types/objectid.rb +219 -0
  31. data/lib/mongo/types/regexp_of_holding.rb +45 -0
  32. data/lib/mongo/util/bson_c.rb +18 -0
  33. data/lib/mongo/util/bson_ruby.rb +595 -0
  34. data/lib/mongo/util/byte_buffer.rb +222 -0
  35. data/lib/mongo/util/conversions.rb +97 -0
  36. data/lib/mongo/util/ordered_hash.rb +135 -0
  37. data/lib/mongo/util/server_version.rb +69 -0
  38. data/lib/mongo/util/support.rb +26 -0
  39. data/lib/mongo/util/xml_to_ruby.rb +112 -0
  40. data/mongo-ruby-driver.gemspec +28 -0
  41. data/test/replica/count_test.rb +34 -0
  42. data/test/replica/insert_test.rb +50 -0
  43. data/test/replica/pooled_insert_test.rb +54 -0
  44. data/test/replica/query_test.rb +39 -0
  45. data/test/test_admin.rb +67 -0
  46. data/test/test_bson.rb +397 -0
  47. data/test/test_byte_buffer.rb +81 -0
  48. data/test/test_chunk.rb +82 -0
  49. data/test/test_collection.rb +534 -0
  50. data/test/test_connection.rb +160 -0
  51. data/test/test_conversions.rb +120 -0
  52. data/test/test_cursor.rb +386 -0
  53. data/test/test_db.rb +254 -0
  54. data/test/test_db_api.rb +783 -0
  55. data/test/test_db_connection.rb +16 -0
  56. data/test/test_grid_store.rb +306 -0
  57. data/test/test_helper.rb +42 -0
  58. data/test/test_objectid.rb +156 -0
  59. data/test/test_ordered_hash.rb +168 -0
  60. data/test/test_round_trip.rb +114 -0
  61. data/test/test_slave_connection.rb +36 -0
  62. data/test/test_threading.rb +87 -0
  63. data/test/threading/test_threading_large_pool.rb +90 -0
  64. data/test/unit/collection_test.rb +52 -0
  65. data/test/unit/connection_test.rb +59 -0
  66. data/test/unit/cursor_test.rb +94 -0
  67. data/test/unit/db_test.rb +97 -0
  68. metadata +123 -0
@@ -0,0 +1,160 @@
1
+ require 'test/test_helper'
2
+ require 'logger'
3
+ require 'stringio'
4
+ require 'thread'
5
+
6
+ # NOTE: assumes Mongo is running
7
+ class TestConnection < Test::Unit::TestCase
8
+
9
+ include Mongo
10
+
11
+ def setup
12
+ @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
13
+ @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
14
+ @mongo = Connection.new(@host, @port)
15
+ end
16
+
17
+ def teardown
18
+ @mongo.db('ruby-mongo-test').error
19
+ end
20
+
21
+ def test_server_info
22
+ server_info = @mongo.server_info
23
+ assert server_info.keys.include?("version")
24
+ assert_equal 1.0, server_info["ok"]
25
+ end
26
+
27
+ def test_server_version
28
+ assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s
29
+ end
30
+
31
+ def test_invalid_database_names
32
+ assert_raise TypeError do @mongo.db(4) end
33
+
34
+ assert_raise InvalidName do @mongo.db('') end
35
+ assert_raise InvalidName do @mongo.db('te$t') end
36
+ assert_raise InvalidName do @mongo.db('te.t') end
37
+ assert_raise InvalidName do @mongo.db('te\\t') end
38
+ assert_raise InvalidName do @mongo.db('te/t') end
39
+ assert_raise InvalidName do @mongo.db('te st') end
40
+ end
41
+
42
+ def test_database_info
43
+ @mongo.drop_database('ruby-mongo-info-test')
44
+ @mongo.db('ruby-mongo-info-test').collection('info-test').insert('a' => 1)
45
+
46
+ info = @mongo.database_info
47
+ assert_not_nil info
48
+ assert_kind_of Hash, info
49
+ assert_not_nil info['ruby-mongo-info-test']
50
+ assert info['ruby-mongo-info-test'] > 0
51
+
52
+ @mongo.drop_database('ruby-mongo-info-test')
53
+ end
54
+
55
+ def test_copy_database
56
+ @mongo.db('old').collection('copy-test').insert('a' => 1)
57
+ @mongo.copy_database('old', 'new')
58
+ old_object = @mongo.db('old').collection('copy-test').find.next_document
59
+ new_object = @mongo.db('new').collection('copy-test').find.next_document
60
+ assert_equal old_object, new_object
61
+ end
62
+
63
+ def test_database_names
64
+ @mongo.drop_database('ruby-mongo-info-test')
65
+ @mongo.db('ruby-mongo-info-test').collection('info-test').insert('a' => 1)
66
+
67
+ names = @mongo.database_names
68
+ assert_not_nil names
69
+ assert_kind_of Array, names
70
+ assert names.length >= 1
71
+ assert names.include?('ruby-mongo-info-test')
72
+
73
+ @mongo.drop_database('ruby-mongo-info-test')
74
+ end
75
+
76
+ def test_logging
77
+ output = StringIO.new
78
+ logger = Logger.new(output)
79
+ logger.level = Logger::DEBUG
80
+ db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test')
81
+ assert output.string.include?("admin.$cmd.find")
82
+ end
83
+
84
+ def test_connection_logger
85
+ output = StringIO.new
86
+ logger = Logger.new(output)
87
+ logger.level = Logger::DEBUG
88
+ connection = Connection.new(@host, @port, :logger => logger)
89
+ assert_equal logger, connection.logger
90
+
91
+ connection.logger.debug 'testing'
92
+ assert output.string.include?('testing')
93
+ end
94
+
95
+ def test_drop_database
96
+ db = @mongo.db('ruby-mongo-will-be-deleted')
97
+ coll = db.collection('temp')
98
+ coll.remove
99
+ coll.insert(:name => 'temp')
100
+ assert_equal 1, coll.count()
101
+ assert @mongo.database_names.include?('ruby-mongo-will-be-deleted')
102
+
103
+ @mongo.drop_database('ruby-mongo-will-be-deleted')
104
+ assert !@mongo.database_names.include?('ruby-mongo-will-be-deleted')
105
+ end
106
+
107
+ def test_nodes
108
+ db = Connection.new({:left => ['foo', 123]}, nil, :connect => false)
109
+ nodes = db.nodes
110
+ assert_equal 2, db.nodes.length
111
+ assert_equal ['foo', 123], nodes[0]
112
+ assert_equal ['localhost', Connection::DEFAULT_PORT], nodes[1]
113
+
114
+ db = Connection.new({:right => 'bar'}, nil, :connect => false)
115
+ nodes = db.nodes
116
+ assert_equal 2, nodes.length
117
+ assert_equal ['localhost', Connection::DEFAULT_PORT], nodes[0]
118
+ assert_equal ['bar', Connection::DEFAULT_PORT], nodes[1]
119
+
120
+ db = Connection.new({:right => ['foo', 123], :left => 'bar'}, nil, :connect => false)
121
+ nodes = db.nodes
122
+ assert_equal 2, nodes.length
123
+ assert_equal ['bar', Connection::DEFAULT_PORT], nodes[0]
124
+ assert_equal ['foo', 123], nodes[1]
125
+ end
126
+
127
+ context "Connection exceptions" do
128
+ setup do
129
+ @conn = Mongo::Connection.new('localhost', 27017, :pool_size => 10, :timeout => 10)
130
+ @coll = @conn['mongo-ruby-test']['test-connection-exceptions']
131
+ end
132
+
133
+ should "release connection if an exception is raised on send_message" do
134
+ @conn.stubs(:send_message_on_socket).raises(ConnectionFailure)
135
+ assert_equal 0, @conn.checked_out.size
136
+ assert_raise ConnectionFailure do
137
+ @coll.insert({:test => "insert"})
138
+ end
139
+ assert_equal 0, @conn.checked_out.size
140
+ end
141
+
142
+ should "release connection if an exception is raised on send_with_safe_check" do
143
+ @conn.stubs(:receive).raises(ConnectionFailure)
144
+ assert_equal 0, @conn.checked_out.size
145
+ assert_raise ConnectionFailure do
146
+ @coll.insert({:test => "insert"}, :safe => true)
147
+ end
148
+ assert_equal 0, @conn.checked_out.size
149
+ end
150
+
151
+ should "release connection if an exception is raised on receive_message" do
152
+ @conn.stubs(:receive).raises(ConnectionFailure)
153
+ assert_equal 0, @conn.checked_out.size
154
+ assert_raise ConnectionFailure do
155
+ @coll.find.to_a
156
+ end
157
+ assert_equal 0, @conn.checked_out.size
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,120 @@
1
+ require 'test/test_helper'
2
+ require 'mongo/exceptions'
3
+ require 'mongo/util/conversions'
4
+ require 'mongo/util/ordered_hash'
5
+
6
+ class ConversionsTest < Test::Unit::TestCase
7
+ include Mongo::Conversions
8
+
9
+ def test_array_as_sort_parameters_with_array_of_key_and_value
10
+ params = array_as_sort_parameters(["field1", "asc"])
11
+ assert_equal({"field1" => 1}, params)
12
+ end
13
+
14
+ def test_array_as_sort_parameters_with_array_of_string_and_values
15
+ params = array_as_sort_parameters([["field1", :asc], ["field2", :desc]])
16
+ assert_equal({ "field1" => 1, "field2" => -1 }, params)
17
+ end
18
+
19
+ def test_string_as_sort_parameters_with_string
20
+ params = string_as_sort_parameters("field")
21
+ assert_equal({ "field" => 1 }, params)
22
+ end
23
+
24
+ def test_string_as_sort_parameters_with_empty_string
25
+ params = string_as_sort_parameters("")
26
+ assert_equal({}, params)
27
+ end
28
+
29
+ def test_symbol_as_sort_parameters
30
+ params = string_as_sort_parameters(:field)
31
+ assert_equal({ "field" => 1 }, params)
32
+ end
33
+
34
+ def test_sort_value_when_value_is_one
35
+ assert_equal 1, sort_value(1)
36
+ end
37
+
38
+ def test_sort_value_when_value_is_one_as_a_string
39
+ assert_equal 1, sort_value("1")
40
+ end
41
+
42
+ def test_sort_value_when_value_is_negative_one
43
+ assert_equal -1, sort_value(-1)
44
+ end
45
+
46
+ def test_sort_value_when_value_is_negative_one_as_a_string
47
+ assert_equal -1, sort_value("-1")
48
+ end
49
+
50
+ def test_sort_value_when_value_is_ascending
51
+ assert_equal 1, sort_value("ascending")
52
+ end
53
+
54
+ def test_sort_value_when_value_is_asc
55
+ assert_equal 1, sort_value("asc")
56
+ end
57
+
58
+ def test_sort_value_when_value_is_uppercase_ascending
59
+ assert_equal 1, sort_value("ASCENDING")
60
+ end
61
+
62
+ def test_sort_value_when_value_is_uppercase_asc
63
+ assert_equal 1, sort_value("ASC")
64
+ end
65
+
66
+ def test_sort_value_when_value_is_symbol_ascending
67
+ assert_equal 1, sort_value(:ascending)
68
+ end
69
+
70
+ def test_sort_value_when_value_is_symbol_asc
71
+ assert_equal 1, sort_value(:asc)
72
+ end
73
+
74
+ def test_sort_value_when_value_is_symbol_uppercase_ascending
75
+ assert_equal 1, sort_value(:ASCENDING)
76
+ end
77
+
78
+ def test_sort_value_when_value_is_symbol_uppercase_asc
79
+ assert_equal 1, sort_value(:ASC)
80
+ end
81
+
82
+ def test_sort_value_when_value_is_descending
83
+ assert_equal -1, sort_value("descending")
84
+ end
85
+
86
+ def test_sort_value_when_value_is_desc
87
+ assert_equal -1, sort_value("desc")
88
+ end
89
+
90
+ def test_sort_value_when_value_is_uppercase_descending
91
+ assert_equal -1, sort_value("DESCENDING")
92
+ end
93
+
94
+ def test_sort_value_when_value_is_uppercase_desc
95
+ assert_equal -1, sort_value("DESC")
96
+ end
97
+
98
+ def test_sort_value_when_value_is_symbol_descending
99
+ assert_equal -1, sort_value(:descending)
100
+ end
101
+
102
+ def test_sort_value_when_value_is_symbol_desc
103
+ assert_equal -1, sort_value(:desc)
104
+ end
105
+
106
+ def test_sort_value_when_value_is_uppercase_symbol_descending
107
+ assert_equal -1, sort_value(:DESCENDING)
108
+ end
109
+
110
+ def test_sort_value_when_value_is_uppercase_symbol_desc
111
+ assert_equal -1, sort_value(:DESC)
112
+ end
113
+
114
+ def test_sort_value_when_value_is_invalid
115
+ assert_raise Mongo::InvalidSortValueError do
116
+ sort_value(2)
117
+ end
118
+ end
119
+
120
+ end
@@ -0,0 +1,386 @@
1
+ require 'test/test_helper'
2
+ require 'logger'
3
+
4
+ # NOTE: assumes Mongo is running
5
+ class CursorTest < Test::Unit::TestCase
6
+
7
+ include Mongo
8
+
9
+ @@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
10
+ ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
11
+ @@db = @@connection.db('ruby-mongo-test')
12
+ @@coll = @@db.collection('test')
13
+ @@version = @@connection.server_version
14
+
15
+ def setup
16
+ @@coll.remove
17
+ @@coll.insert('a' => 1) # collection not created until it's used
18
+ @@coll_full_name = 'ruby-mongo-test.test'
19
+ end
20
+
21
+ def test_explain
22
+ cursor = @@coll.find('a' => 1)
23
+ explaination = cursor.explain
24
+ assert_not_nil explaination['cursor']
25
+ assert_kind_of Numeric, explaination['n']
26
+ assert_kind_of Numeric, explaination['millis']
27
+ assert_kind_of Numeric, explaination['nscanned']
28
+ end
29
+
30
+ def test_count
31
+ @@coll.remove
32
+
33
+ assert_equal 0, @@coll.find().count()
34
+
35
+ 10.times do |i|
36
+ @@coll.save("x" => i)
37
+ end
38
+
39
+ assert_equal 10, @@coll.find().count()
40
+ assert_kind_of Integer, @@coll.find().count()
41
+ assert_equal 10, @@coll.find({}, :limit => 5).count()
42
+ assert_equal 10, @@coll.find({}, :skip => 5).count()
43
+
44
+ assert_equal 1, @@coll.find({"x" => 1}).count()
45
+ assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
46
+
47
+ a = @@coll.find()
48
+ b = a.count()
49
+ a.each do |doc|
50
+ break
51
+ end
52
+ assert_equal b, a.count()
53
+
54
+ assert_equal 0, @@db['acollectionthatdoesn'].count()
55
+ end
56
+
57
+ def test_next_object_deprecation
58
+ @@coll.remove
59
+ @@coll.insert({"a" => 1})
60
+
61
+ assert_equal 1, @@coll.find().next_object["a"]
62
+ end
63
+
64
+ def test_sort
65
+ @@coll.remove
66
+ 5.times{|x| @@coll.insert({"age" => x}) }
67
+
68
+ assert_kind_of Cursor, @@coll.find().sort(:age, 1)
69
+
70
+ assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"]
71
+ assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"]
72
+ assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"]
73
+
74
+ assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]])
75
+
76
+ assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
77
+ assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
78
+
79
+ assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
80
+ assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
81
+
82
+ cursor = @@coll.find()
83
+ cursor.next_document
84
+ assert_raise InvalidOperation do
85
+ cursor.sort(["age"])
86
+ end
87
+
88
+ assert_raise InvalidSortValueError do
89
+ @@coll.find().sort(:age, 25).next_document
90
+ end
91
+
92
+ assert_raise InvalidSortValueError do
93
+ @@coll.find().sort(25).next_document
94
+ end
95
+ end
96
+
97
+ def test_sort_date
98
+ @@coll.remove
99
+ 5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) }
100
+
101
+ assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year
102
+ assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year
103
+
104
+ assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year
105
+ assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year
106
+
107
+ assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
108
+ assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
109
+ end
110
+
111
+ def test_sort_min_max_keys
112
+ @@coll.remove
113
+ @@coll.insert({"n" => 1000000})
114
+ @@coll.insert({"n" => -1000000})
115
+ @@coll.insert({"n" => MaxKey.new})
116
+ @@coll.insert({"n" => MinKey.new})
117
+
118
+ results = @@coll.find.sort([:n, :asc]).to_a
119
+
120
+ assert_equal MinKey.new, results[0]['n']
121
+ assert_equal -1000000, results[1]['n']
122
+ assert_equal 1000000, results[2]['n']
123
+ assert_equal MaxKey.new, results[3]['n']
124
+ end
125
+
126
+ def test_id_range_queries
127
+ @@coll.remove
128
+
129
+ t1 = Time.now
130
+ t1_id = ObjectID.from_time(t1)
131
+ @@coll.save({:t => 't1'})
132
+ @@coll.save({:t => 't1'})
133
+ @@coll.save({:t => 't1'})
134
+ sleep(2)
135
+ t2 = Time.now
136
+ t2_id = ObjectID.from_time(t2)
137
+ @@coll.save({:t => 't2'})
138
+ @@coll.save({:t => 't2'})
139
+ @@coll.save({:t => 't2'})
140
+
141
+ assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id}, '_id' => {'$lt' => t2_id}}).count
142
+ @@coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
143
+ assert_equal 't2', doc['t']
144
+ end
145
+ end
146
+
147
+ def test_limit
148
+ @@coll.remove
149
+
150
+ 10.times do |i|
151
+ @@coll.save("x" => i)
152
+ end
153
+ assert_equal 10, @@coll.find().count()
154
+
155
+ results = @@coll.find().limit(5).to_a
156
+ assert_equal 5, results.length
157
+ end
158
+
159
+ def test_limit_exceptions
160
+ assert_raise ArgumentError do
161
+ cursor = @@coll.find().limit('not-an-integer')
162
+ end
163
+
164
+ cursor = @@coll.find()
165
+ firstResult = cursor.next_document
166
+ assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
167
+ cursor.limit(1)
168
+ end
169
+
170
+ cursor = @@coll.find()
171
+ cursor.close
172
+ assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
173
+ cursor.limit(1)
174
+ end
175
+ end
176
+
177
+ def test_skip
178
+ @@coll.remove
179
+
180
+ 10.times do |i|
181
+ @@coll.save("x" => i)
182
+ end
183
+ assert_equal 10, @@coll.find().count()
184
+
185
+ all_results = @@coll.find().to_a
186
+ skip_results = @@coll.find().skip(2).to_a
187
+ assert_equal 10, all_results.length
188
+ assert_equal 8, skip_results.length
189
+
190
+ assert_equal all_results.slice(2...10), skip_results
191
+ end
192
+
193
+ def test_skip_exceptions
194
+ assert_raise ArgumentError do
195
+ cursor = @@coll.find().skip('not-an-integer')
196
+ end
197
+
198
+ cursor = @@coll.find()
199
+ firstResult = cursor.next_document
200
+ assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
201
+ cursor.skip(1)
202
+ end
203
+
204
+ cursor = @@coll.find()
205
+ cursor.close
206
+ assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
207
+ cursor.skip(1)
208
+ end
209
+ end
210
+
211
+ def test_limit_skip_chaining
212
+ @@coll.remove
213
+ 10.times do |i|
214
+ @@coll.save("x" => i)
215
+ end
216
+
217
+ all_results = @@coll.find().to_a
218
+ limited_skip_results = @@coll.find().limit(5).skip(3).to_a
219
+
220
+ assert_equal all_results.slice(3...8), limited_skip_results
221
+ end
222
+
223
+ def test_close_no_query_sent
224
+ begin
225
+ cursor = @@coll.find('a' => 1)
226
+ cursor.close
227
+ assert cursor.closed?
228
+ rescue => ex
229
+ fail ex.to_s
230
+ end
231
+ end
232
+
233
+ def test_refill_via_get_more
234
+ assert_equal 1, @@coll.count
235
+ 1000.times { |i|
236
+ assert_equal 1 + i, @@coll.count
237
+ @@coll.insert('a' => i)
238
+ }
239
+
240
+ assert_equal 1001, @@coll.count
241
+ count = 0
242
+ @@coll.find.each { |obj|
243
+ count += obj['a']
244
+ }
245
+ assert_equal 1001, @@coll.count
246
+
247
+ # do the same thing again for debugging
248
+ assert_equal 1001, @@coll.count
249
+ count2 = 0
250
+ @@coll.find.each { |obj|
251
+ count2 += obj['a']
252
+ }
253
+ assert_equal 1001, @@coll.count
254
+
255
+ assert_equal count, count2
256
+ assert_equal 499501, count
257
+ end
258
+
259
+ def test_refill_via_get_more_alt_coll
260
+ coll = @@db.collection('test-alt-coll')
261
+ coll.remove
262
+ coll.insert('a' => 1) # collection not created until it's used
263
+ assert_equal 1, coll.count
264
+
265
+ 1000.times { |i|
266
+ assert_equal 1 + i, coll.count
267
+ coll.insert('a' => i)
268
+ }
269
+
270
+ assert_equal 1001, coll.count
271
+ count = 0
272
+ coll.find.each { |obj|
273
+ count += obj['a']
274
+ }
275
+ assert_equal 1001, coll.count
276
+
277
+ # do the same thing again for debugging
278
+ assert_equal 1001, coll.count
279
+ count2 = 0
280
+ coll.find.each { |obj|
281
+ count2 += obj['a']
282
+ }
283
+ assert_equal 1001, coll.count
284
+
285
+ assert_equal count, count2
286
+ assert_equal 499501, count
287
+ end
288
+
289
+ def test_close_after_query_sent
290
+ begin
291
+ cursor = @@coll.find('a' => 1)
292
+ cursor.next_document
293
+ cursor.close
294
+ assert cursor.closed?
295
+ rescue => ex
296
+ fail ex.to_s
297
+ end
298
+ end
299
+
300
+ def test_kill_cursors
301
+ @@coll.drop
302
+
303
+ client_cursors = @@db.command("cursorInfo" => 1)["clientCursors_size"]
304
+ by_location = @@db.command("cursorInfo" => 1)["byLocation_size"]
305
+
306
+ 10000.times do |i|
307
+ @@coll.insert("i" => i)
308
+ end
309
+
310
+ assert_equal(client_cursors,
311
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
312
+ assert_equal(by_location,
313
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
314
+
315
+ 10.times do |i|
316
+ @@coll.find_one()
317
+ end
318
+
319
+ assert_equal(client_cursors,
320
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
321
+ assert_equal(by_location,
322
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
323
+
324
+ 10.times do |i|
325
+ a = @@coll.find()
326
+ a.next_document
327
+ a.close()
328
+ end
329
+
330
+ assert_equal(client_cursors,
331
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
332
+ assert_equal(by_location,
333
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
334
+
335
+ a = @@coll.find()
336
+ a.next_document
337
+
338
+ assert_not_equal(client_cursors,
339
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
340
+ assert_not_equal(by_location,
341
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
342
+
343
+ a.close()
344
+
345
+ assert_equal(client_cursors,
346
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
347
+ assert_equal(by_location,
348
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
349
+
350
+ a = @@coll.find({}, :limit => 10).next_document
351
+
352
+ assert_equal(client_cursors,
353
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
354
+ assert_equal(by_location,
355
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
356
+
357
+ @@coll.find() do |cursor|
358
+ cursor.next_document
359
+ end
360
+
361
+ assert_equal(client_cursors,
362
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
363
+ assert_equal(by_location,
364
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
365
+
366
+ @@coll.find() { |cursor|
367
+ cursor.next_document
368
+ }
369
+
370
+ assert_equal(client_cursors,
371
+ @@db.command("cursorInfo" => 1)["clientCursors_size"])
372
+ assert_equal(by_location,
373
+ @@db.command("cursorInfo" => 1)["byLocation_size"])
374
+ end
375
+
376
+ def test_count_with_fields
377
+ @@coll.remove
378
+ @@coll.save("x" => 1)
379
+
380
+ if @@version < "1.1.3"
381
+ assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
382
+ else
383
+ assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
384
+ end
385
+ end
386
+ end