mongo 1.10.2 → 1.11.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -17,9 +17,7 @@ require 'test_helper'
17
17
  class DBConnectionTest < Test::Unit::TestCase
18
18
 
19
19
  def test_no_exceptions
20
- host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
21
- port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT
22
- db = MongoClient.new(host, port).db(TEST_DB)
20
+ db = standard_connection.db(TEST_DB)
23
21
  coll = db.collection('test')
24
22
  coll.remove
25
23
  db.get_last_error
@@ -28,35 +28,37 @@ class DBTest < Test::Unit::TestCase
28
28
 
29
29
  include Mongo
30
30
 
31
- @@client = standard_connection
32
- @@db = @@client.db(TEST_DB)
33
- @@version = @@client.server_version
31
+ def setup
32
+ @client = standard_connection
33
+ @db = @client.db(TEST_DB)
34
+ @version = @client.server_version
35
+ end
34
36
 
35
37
  def test_close
36
- @@client.close
37
- assert !@@client.connected?
38
+ @client.close
39
+ assert !@client.connected?
38
40
  begin
39
- @@db.collection('test').insert('a' => 1)
41
+ @db.collection('test').insert('a' => 1)
40
42
  fail "expected 'NilClass' exception"
41
43
  rescue => ex
42
44
  assert_match(/NilClass/, ex.to_s)
43
45
  ensure
44
- @@db = standard_connection.db(TEST_DB)
46
+ @db = standard_connection.db(TEST_DB)
45
47
  end
46
48
  end
47
49
 
48
50
  def test_create_collection
49
- col = @@db.create_collection('foo')
50
- assert_equal @@db['foo'].name, col.name
51
+ col = @db.create_collection('foo')
52
+ assert_equal @db['foo'].name, col.name
51
53
 
52
- col = @@db.create_collection(:foo)
53
- assert_equal @@db['foo'].name, col.name
54
+ col = @db.create_collection(:foo)
55
+ assert_equal @db['foo'].name, col.name
54
56
 
55
- @@db.drop_collection('foo')
57
+ @db.drop_collection('foo')
56
58
  end
57
59
 
58
60
  def test_get_and_drop_collection
59
- db = @@client.db(TEST_DB, :strict => true)
61
+ db = @client.db(TEST_DB, :strict => true)
60
62
  db.create_collection('foo')
61
63
  assert db.collection('foo')
62
64
  assert db.drop_collection('foo')
@@ -78,15 +80,15 @@ class DBTest < Test::Unit::TestCase
78
80
  end
79
81
 
80
82
  def test_full_coll_name
81
- coll = @@db.collection('test')
82
- assert_equal "#{TEST_DB}.test", @@db.full_collection_name(coll.name)
83
+ coll = @db.collection('test')
84
+ assert_equal "#{TEST_DB}.test", @db.full_collection_name(coll.name)
83
85
  end
84
86
 
85
87
  def test_collection_names
86
- @@db.collection("test").insert("foo" => 5)
87
- @@db.collection("test.mike").insert("bar" => 0)
88
+ @db.collection("test").insert("foo" => 5)
89
+ @db.collection("test.mike").insert("bar" => 0)
88
90
 
89
- colls = @@db.collection_names()
91
+ colls = @db.collection_names()
90
92
  assert colls.include?("test")
91
93
  assert colls.include?("test.mike")
92
94
  colls.each { |name|
@@ -95,10 +97,10 @@ class DBTest < Test::Unit::TestCase
95
97
  end
96
98
 
97
99
  def test_collections
98
- @@db.collection("test.durran").insert("foo" => 5)
99
- @@db.collection("test.les").insert("bar" => 0)
100
+ @db.collection("test.durran").insert("foo" => 5)
101
+ @db.collection("test.les").insert("bar" => 0)
100
102
 
101
- colls = @@db.collections()
103
+ colls = @db.collections()
102
104
  assert_not_nil colls.select { |coll| coll.name == "test.durran" }
103
105
  assert_not_nil colls.select { |coll| coll.name == "test.les" }
104
106
  assert_equal [], colls.select { |coll| coll.name == "does_not_exist" }
@@ -145,91 +147,92 @@ class DBTest < Test::Unit::TestCase
145
147
 
146
148
  def test_command
147
149
  assert_raise OperationFailure do
148
- @@db.command({:non_command => 1}, :check_response => true)
150
+ @db.command({:non_command => 1}, :check_response => true)
149
151
  end
150
152
 
151
- result = @@db.command({:non_command => 1}, :check_response => false)
153
+ result = @db.command({:non_command => 1}, :check_response => false)
152
154
  assert !Mongo::Support.ok?(result)
153
155
  end
154
156
 
155
157
  def test_error
156
- @@db.reset_error_history
157
- assert_nil @@db.get_last_error['err']
158
- assert !@@db.error?
159
- assert_nil @@db.previous_error
160
-
161
- @@db.command({:forceerror => 1}, :check_response => false)
162
- assert @@db.error?
163
- assert_not_nil @@db.get_last_error['err']
164
- assert_not_nil @@db.previous_error
165
-
166
- @@db.command({:forceerror => 1}, :check_response => false)
167
- assert @@db.error?
168
- assert @@db.get_last_error['err']
169
- prev_error = @@db.previous_error
158
+ @db.reset_error_history
159
+ assert_nil @db.get_last_error['err']
160
+ assert !@db.error?
161
+ assert_nil @db.previous_error
162
+
163
+ @db.command({:forceerror => 1}, :check_response => false)
164
+ assert @db.error?
165
+ assert_not_nil @db.get_last_error['err']
166
+ assert_not_nil @db.previous_error
167
+
168
+ @db.command({:forceerror => 1}, :check_response => false)
169
+ assert @db.error?
170
+ assert @db.get_last_error['err']
171
+ prev_error = @db.previous_error
170
172
  assert_equal 1, prev_error['nPrev']
171
- assert_equal prev_error["err"], @@db.get_last_error['err']
172
-
173
- @@db.collection('test').find_one
174
- assert_nil @@db.get_last_error['err']
175
- assert !@@db.error?
176
- assert @@db.previous_error
177
- assert_equal 2, @@db.previous_error['nPrev']
178
-
179
- @@db.reset_error_history
180
- assert_nil @@db.get_last_error['err']
181
- assert !@@db.error?
182
- assert_nil @@db.previous_error
173
+ assert_equal prev_error["err"], @db.get_last_error['err']
174
+
175
+ @db.collection('test').find_one
176
+ assert_nil @db.get_last_error['err']
177
+ assert !@db.error?
178
+ assert @db.previous_error
179
+ assert_equal 2, @db.previous_error['nPrev']
180
+
181
+ @db.reset_error_history
182
+ assert_nil @db.get_last_error['err']
183
+ assert !@db.error?
184
+ assert_nil @db.previous_error
183
185
  end
184
186
 
185
187
  def test_check_command_response
186
- command = {:forceerror => 1}
187
- raised = false
188
- begin
189
- @@db.command(command)
190
- rescue => ex
191
- raised = true
192
- assert ex.message.include?("forced error") || ex.result.has_key?("assertion") && ex.result["assertion"].include?("forced error"),
193
- "error message does not contain 'forced error'"
194
- assert_equal 10038, ex.error_code
195
-
196
- if @@version >= "2.1.0"
197
- assert_equal 10038, ex.result['code']
198
- else
199
- assert_equal 10038, ex.result['assertionCode']
188
+ if @version >= "2.1.0"
189
+ command = {:create => "$$$$"}
190
+ expected_codes = [10356, 2]
191
+ expected_msg = "invalid"
192
+ raised = false
193
+ begin
194
+ @db.command(command)
195
+ rescue => ex
196
+ raised = true
197
+ assert ex.message.include?(expected_msg) ||
198
+ (ex.result.has_key?("assertion") &&
199
+ ex.result["assertion"].include?(expected_msg)),
200
+ "error message does not contain '#{expected_msg}'"
201
+ assert expected_codes.include?(ex.error_code)
202
+ assert expected_codes.include?(ex.result['code'])
203
+ ensure
204
+ assert raised, "No assertion raised!"
200
205
  end
201
- ensure
202
- assert raised, "No assertion raised!"
203
206
  end
204
207
  end
205
208
 
206
209
  def test_arbitrary_command_opts
207
- with_forced_timeout(@@client) do
210
+ with_forced_timeout(@client) do
208
211
  assert_raise ExecutionTimeout do
209
212
  cmd = OrderedHash.new
210
213
  cmd[:ping] = 1
211
214
  cmd[:maxTimeMS] = 100
212
- @@db.command(cmd)
215
+ @db.command(cmd)
213
216
  end
214
217
  end
215
218
  end
216
219
 
217
220
  def test_command_with_bson
218
- normal_response = @@db.command({:buildInfo => 1})
221
+ normal_response = @db.command({:buildInfo => 1})
219
222
  bson = BSON::BSON_CODER.serialize({:buildInfo => 1}, false, false)
220
- bson_response = @@db.command({:bson => bson})
223
+ bson_response = @db.command({:bson => bson})
221
224
  assert_equal normal_response, bson_response
222
225
  end
223
226
 
224
227
  def test_last_status
225
- @@db['test'].remove
226
- @@db['test'].save("i" => 1)
228
+ @db['test'].remove
229
+ @db['test'].save("i" => 1)
227
230
 
228
- @@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
229
- assert @@db.get_last_error()["updatedExisting"]
231
+ @db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
232
+ assert @db.get_last_error()["updatedExisting"]
230
233
 
231
- @@db['test'].update({"i" => 1}, {"$set" => {"i" => 500}})
232
- assert !@@db.get_last_error()["updatedExisting"]
234
+ @db['test'].update({"i" => 1}, {"$set" => {"i" => 500}})
235
+ assert !@db.get_last_error()["updatedExisting"]
233
236
  end
234
237
 
235
238
  def test_text_port_number_raises_no_errors
@@ -239,62 +242,60 @@ class DBTest < Test::Unit::TestCase
239
242
  end
240
243
 
241
244
  def test_stored_function_management
242
- @@db.add_stored_function("sum", "function (x, y) { return x + y; }")
243
- assert_equal @@db.eval("return sum(2,3);"), 5
244
- assert @@db.remove_stored_function("sum")
245
+ grant_admin_user_eval_role(@client)
246
+ @db.add_stored_function("sum", "function (x, y) { return x + y; }")
247
+ assert_equal @db.eval("return sum(2,3);"), 5
248
+ assert @db.remove_stored_function("sum")
245
249
  assert_raise OperationFailure do
246
- @@db.eval("return sum(2,3);")
250
+ @db.eval("return sum(2,3);")
247
251
  end
248
252
  end
249
253
 
250
254
  def test_eval
251
- @@db.eval("db.system.save({_id:'hello', value: function() { print('hello'); } })")
252
- assert_equal 'hello', @@db['system'].find_one['_id']
255
+ grant_admin_user_eval_role(@client)
256
+ @db.eval("db.system.save({_id:'hello', value: function() { print('hello'); } })")
257
+ assert_equal 'hello', @db['system'].find_one['_id']
253
258
  end
254
259
 
255
- def test_eval_nolock
260
+ def test_eval_nook
261
+ grant_admin_user_eval_role(@client)
256
262
  function = "db.system.save({_id:'hello', value: function(string) { print(string); } })"
257
- @@db.expects(:command).with do |selector, opts|
258
- selector[:nolock] == true
263
+ @db.expects(:command).with do |selector, opts|
264
+ selector[:nolock] == true
259
265
  end.returns({ 'ok' => 1, 'retval' => 1 })
260
- @@db.eval(function, 'hello', :nolock => true)
266
+ @db.eval(function, 'hello', :nolock => true)
261
267
  end
262
268
 
263
- if @@version >= '2.5.3'
264
- def test_default_admin_roles
265
- # admin user
266
- db = Mongo::MongoClient.new()['admin']
267
- db.logout
268
- silently { db.add_user('admin', 'pass') }
269
- db.authenticate('admin', 'pass')
270
- info = db.command(:usersInfo => 'admin')['users'].first
271
- assert_equal 'root', info['roles'].first['role']
272
-
273
- # read-only admin user
274
- silently { db.add_user('ro-admin', 'pass', true) }
275
- db.logout
276
- db.authenticate('ro-admin', 'pass')
277
- info = db.command(:usersInfo => 'ro-admin')['users'].first
278
- assert_equal 'readAnyDatabase', info['roles'].first['role']
279
- db.logout
280
-
281
- db.authenticate('admin', 'pass')
282
- db.command(:dropAllUsersFromDatabase => 1)
283
- db.logout
269
+ def test_default_admin_roles
270
+ return unless @version >= '2.5.3'
271
+ # admin user
272
+ @db.stubs(:command).returns({}, true)
273
+ @db.expects(:command).with do |command, cmd_opts|
274
+ command[:createUser] == TEST_USER
275
+ cmd_opts[:roles] == ['root'] if cmd_opts
284
276
  end
285
- end
286
277
 
287
- if @@version >= "1.3.5"
288
- def test_db_stats
289
- stats = @@db.stats
290
- assert stats.has_key?('collections')
291
- assert stats.has_key?('dataSize')
278
+ silently { @db.add_user(TEST_USER, TEST_USER_PWD) }
279
+
280
+ @db.stubs(:command).returns({}, true)
281
+ @db.expects(:command).with do |command, cmd_opts|
282
+ command[:createUser] == TEST_USER
283
+ cmd_opts[:roles] == ['readAnyDatabase'] if cmd_opts
292
284
  end
285
+
286
+ silently { @db.add_user(TEST_USER, TEST_USER_PWD, true) }
287
+ end
288
+
289
+ def test_db_stats
290
+ return unless @version >= "1.3.5"
291
+ stats = @db.stats
292
+ assert stats.has_key?('collections')
293
+ assert stats.has_key?('dataSize')
293
294
  end
294
295
 
295
296
  context "database profiling" do
296
297
  setup do
297
- @db = @@client[TEST_DB]
298
+ @db = @client[TEST_DB]
298
299
  @coll = @db['test']
299
300
  @coll.remove
300
301
  @r1 = @coll.insert('a' => 1) # collection not created until it's used
@@ -319,7 +320,7 @@ class DBTest < Test::Unit::TestCase
319
320
  end
320
321
 
321
322
  should "return profiling info" do
322
- if @@version >= "2.2"
323
+ if @version >= "2.2"
323
324
  @db.profiling_level = :all
324
325
  @coll.find()
325
326
  @db.profiling_level = :off
@@ -335,7 +336,7 @@ class DBTest < Test::Unit::TestCase
335
336
 
336
337
  should "validate collection" do
337
338
  doc = @db.validate_collection(@coll.name)
338
- if @@version >= "1.9.1"
339
+ if @version >= "1.9.1"
339
340
  assert doc['valid']
340
341
  else
341
342
  assert doc['result']
@@ -97,114 +97,114 @@ class GridFileSystemTest < Test::Unit::TestCase
97
97
  end
98
98
 
99
99
  context "When writing:" do
100
- setup do
101
- @data = "BYTES" * 50
102
- @grid = GridFileSystem.new(@db)
103
- @grid.open('sample', 'w') do |f|
104
- f.write @data
105
- end
106
- end
107
-
108
- should "read sample data" do
109
- data = @grid.open('sample', 'r') { |f| f.read }
110
- assert_equal data.length, @data.length
111
- end
112
-
113
- should "return the total number of bytes written" do
114
- data = 'a' * 300000
115
- assert_equal 300000, @grid.open('sample', 'w') {|f| f.write(data) }
116
- end
117
-
118
- should "more read sample data" do
119
- data = @grid.open('sample', 'r') { |f| f.read }
120
- assert_equal data.length, @data.length
121
- end
122
-
123
- should "raise exception if file not found" do
124
- assert_raise GridFileNotFound do
125
- @grid.open('io', 'r') { |f| f.write('hello') }
126
- end
127
- end
128
-
129
- should "raise exception if not opened for write" do
130
- assert_raise GridError do
131
- @grid.open('sample', 'r') { |f| f.write('hello') }
132
- end
133
- end
134
-
135
- context "and when overwriting the file" do
136
- setup do
137
- @old = @grid.open('sample', 'r')
138
-
139
- @new_data = "DATA" * 10
140
- @grid.open('sample', 'w') do |f|
141
- f.write @new_data
142
- end
143
-
144
- @new = @grid.open('sample', 'r')
145
- end
146
-
147
- should "have a newer upload date" do
148
- assert @new.upload_date > @old.upload_date, "New data is not greater than old date."
149
- end
150
-
151
- should "have a different files_id" do
152
- assert_not_equal @new.files_id, @old.files_id
153
- end
154
-
155
- should "contain the new data" do
156
- assert_equal @new_data, @new.read, "Expected DATA"
157
- end
158
-
159
- context "and on a second overwrite" do
160
- setup do
161
- @new_data = "NEW" * 1000
162
- @grid.open('sample', 'w') do |f|
163
- f.write @new_data
164
- end
165
-
166
- @ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
167
- end
168
-
169
- should "write a third version of the file" do
170
- assert_equal 3, @db['fs.files'].find({'filename' => 'sample'}).count
171
- assert_equal 3, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
172
- end
173
-
174
- should "remove all versions and their data on delete" do
175
- @grid.delete('sample')
176
- assert_equal 0, @db['fs.files'].find({'filename' => 'sample'}).count
177
- assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
178
- end
179
-
180
- should "delete all versions which exceed the number of versions to keep specified by the option :versions" do
181
- @versions = 1 + rand(4-1)
182
- @grid.open('sample', 'w', :versions => @versions) do |f|
183
- f.write @new_data
184
- end
185
- @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
186
- assert_equal @versions, @new_ids.length
187
- id = @new_ids.first
188
- assert !@ids.include?(id)
189
- assert_equal @versions, @db['fs.files'].find({'filename' => 'sample'}).count
190
- end
191
-
192
- should "delete old versions on write with :delete_old is passed in" do
193
- @grid.open('sample', 'w', :delete_old => true) do |f|
194
- f.write @new_data
195
- end
196
- @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
197
- assert_equal 1, @new_ids.length
198
- id = @new_ids.first
199
- assert !@ids.include?(id)
200
- assert_equal 1, @db['fs.files'].find({'filename' => 'sample'}).count
201
- assert_equal 1, @db['fs.chunks'].find({'files_id' => id}).count
202
- end
203
- end
204
- end
205
- end
206
-
207
- context "When writing chunks:" do
100
+ setup do
101
+ @data = "BYTES" * 50
102
+ @grid = GridFileSystem.new(@db)
103
+ @grid.open('sample', 'w') do |f|
104
+ f.write @data
105
+ end
106
+ end
107
+
108
+ should "read sample data" do
109
+ data = @grid.open('sample', 'r') { |f| f.read }
110
+ assert_equal data.length, @data.length
111
+ end
112
+
113
+ should "return the total number of bytes written" do
114
+ data = 'a' * 300000
115
+ assert_equal 300000, @grid.open('sample', 'w') {|f| f.write(data) }
116
+ end
117
+
118
+ should "more read sample data" do
119
+ data = @grid.open('sample', 'r') { |f| f.read }
120
+ assert_equal data.length, @data.length
121
+ end
122
+
123
+ should "raise exception if file not found" do
124
+ assert_raise GridFileNotFound do
125
+ @grid.open('io', 'r') { |f| f.write('hello') }
126
+ end
127
+ end
128
+
129
+ should "raise exception if not opened for write" do
130
+ assert_raise GridError do
131
+ @grid.open('sample', 'r') { |f| f.write('hello') }
132
+ end
133
+ end
134
+
135
+ context "and when overwriting the file" do
136
+ setup do
137
+ @old = @grid.open('sample', 'r')
138
+
139
+ @new_data = "DATA" * 10
140
+ @grid.open('sample', 'w') do |f|
141
+ f.write @new_data
142
+ end
143
+
144
+ @new = @grid.open('sample', 'r')
145
+ end
146
+
147
+ should "have a newer upload date" do
148
+ assert @new.upload_date > @old.upload_date, "New data is not greater than old date."
149
+ end
150
+
151
+ should "have a different files_id" do
152
+ assert_not_equal @new.files_id, @old.files_id
153
+ end
154
+
155
+ should "contain the new data" do
156
+ assert_equal @new_data, @new.read, "Expected DATA"
157
+ end
158
+
159
+ context "and on a second overwrite" do
160
+ setup do
161
+ @new_data = "NEW" * 1000
162
+ @grid.open('sample', 'w') do |f|
163
+ f.write @new_data
164
+ end
165
+
166
+ @ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
167
+ end
168
+
169
+ should "write a third version of the file" do
170
+ assert_equal 3, @db['fs.files'].find({'filename' => 'sample'}).count
171
+ assert_equal 3, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
172
+ end
173
+
174
+ should "remove all versions and their data on delete" do
175
+ @grid.delete('sample')
176
+ assert_equal 0, @db['fs.files'].find({'filename' => 'sample'}).count
177
+ assert_equal 0, @db['fs.chunks'].find({'files_id' => {'$in' => @ids}}).count
178
+ end
179
+
180
+ should "delete all versions which exceed the number of versions to keep specified by the option :versions" do
181
+ @versions = 1 + rand(4-1)
182
+ @grid.open('sample', 'w', :versions => @versions) do |f|
183
+ f.write @new_data
184
+ end
185
+ @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
186
+ assert_equal @versions, @new_ids.length
187
+ id = @new_ids.first
188
+ assert !@ids.include?(id)
189
+ assert_equal @versions, @db['fs.files'].find({'filename' => 'sample'}).count
190
+ end
191
+
192
+ should "delete old versions on write with :delete_old is passed in" do
193
+ @grid.open('sample', 'w', :delete_old => true) do |f|
194
+ f.write @new_data
195
+ end
196
+ @new_ids = @db['fs.files'].find({'filename' => 'sample'}).map {|file| file['_id']}
197
+ assert_equal 1, @new_ids.length
198
+ id = @new_ids.first
199
+ assert !@ids.include?(id)
200
+ assert_equal 1, @db['fs.files'].find({'filename' => 'sample'}).count
201
+ assert_equal 1, @db['fs.chunks'].find({'files_id' => id}).count
202
+ end
203
+ end
204
+ end
205
+ end
206
+
207
+ context "When writing chunks:" do
208
208
  setup do
209
209
  data = "B" * 50000
210
210
  @grid = GridFileSystem.new(@db)