mongo 1.0 → 1.1.5

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 (95) hide show
  1. data/LICENSE.txt +1 -13
  2. data/{README.rdoc → README.md} +129 -149
  3. data/Rakefile +94 -58
  4. data/bin/mongo_console +21 -0
  5. data/docs/1.0_UPGRADE.md +21 -0
  6. data/docs/CREDITS.md +123 -0
  7. data/docs/FAQ.md +112 -0
  8. data/docs/GridFS.md +158 -0
  9. data/docs/HISTORY.md +185 -0
  10. data/docs/REPLICA_SETS.md +75 -0
  11. data/docs/TUTORIAL.md +247 -0
  12. data/docs/WRITE_CONCERN.md +28 -0
  13. data/lib/mongo/collection.rb +225 -105
  14. data/lib/mongo/connection.rb +374 -315
  15. data/lib/mongo/cursor.rb +122 -77
  16. data/lib/mongo/db.rb +109 -85
  17. data/lib/mongo/exceptions.rb +6 -0
  18. data/lib/mongo/gridfs/grid.rb +19 -11
  19. data/lib/mongo/gridfs/grid_ext.rb +36 -9
  20. data/lib/mongo/gridfs/grid_file_system.rb +15 -9
  21. data/lib/mongo/gridfs/grid_io.rb +49 -16
  22. data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
  23. data/lib/mongo/repl_set_connection.rb +290 -0
  24. data/lib/mongo/util/conversions.rb +3 -1
  25. data/lib/mongo/util/core_ext.rb +17 -4
  26. data/lib/mongo/util/pool.rb +125 -0
  27. data/lib/mongo/util/server_version.rb +2 -0
  28. data/lib/mongo/util/support.rb +12 -0
  29. data/lib/mongo/util/uri_parser.rb +71 -0
  30. data/lib/mongo.rb +23 -7
  31. data/{mongo-ruby-driver.gemspec → mongo.gemspec} +9 -7
  32. data/test/auxillary/1.4_features.rb +2 -2
  33. data/test/auxillary/authentication_test.rb +1 -1
  34. data/test/auxillary/autoreconnect_test.rb +1 -1
  35. data/test/{slave_connection_test.rb → auxillary/slave_connection_test.rb} +6 -6
  36. data/test/bson/binary_test.rb +15 -0
  37. data/test/bson/bson_test.rb +537 -0
  38. data/test/bson/byte_buffer_test.rb +190 -0
  39. data/test/bson/hash_with_indifferent_access_test.rb +38 -0
  40. data/test/bson/json_test.rb +17 -0
  41. data/test/bson/object_id_test.rb +141 -0
  42. data/test/bson/ordered_hash_test.rb +197 -0
  43. data/test/collection_test.rb +195 -15
  44. data/test/connection_test.rb +93 -56
  45. data/test/conversions_test.rb +1 -1
  46. data/test/cursor_fail_test.rb +75 -0
  47. data/test/cursor_message_test.rb +43 -0
  48. data/test/cursor_test.rb +93 -32
  49. data/test/db_api_test.rb +28 -55
  50. data/test/db_connection_test.rb +2 -3
  51. data/test/db_test.rb +45 -40
  52. data/test/grid_file_system_test.rb +14 -6
  53. data/test/grid_io_test.rb +36 -7
  54. data/test/grid_test.rb +54 -10
  55. data/test/replica_sets/connect_test.rb +84 -0
  56. data/test/replica_sets/count_test.rb +35 -0
  57. data/test/{replica → replica_sets}/insert_test.rb +17 -14
  58. data/test/replica_sets/pooled_insert_test.rb +55 -0
  59. data/test/replica_sets/query_secondaries.rb +80 -0
  60. data/test/replica_sets/query_test.rb +41 -0
  61. data/test/replica_sets/replication_ack_test.rb +64 -0
  62. data/test/replica_sets/rs_test_helper.rb +29 -0
  63. data/test/safe_test.rb +68 -0
  64. data/test/support/hash_with_indifferent_access.rb +199 -0
  65. data/test/support/keys.rb +45 -0
  66. data/test/support_test.rb +19 -0
  67. data/test/test_helper.rb +53 -15
  68. data/test/threading/{test_threading_large_pool.rb → threading_with_large_pool_test.rb} +2 -2
  69. data/test/threading_test.rb +2 -2
  70. data/test/tools/repl_set_manager.rb +241 -0
  71. data/test/tools/test.rb +13 -0
  72. data/test/unit/collection_test.rb +70 -7
  73. data/test/unit/connection_test.rb +18 -39
  74. data/test/unit/cursor_test.rb +7 -8
  75. data/test/unit/db_test.rb +14 -17
  76. data/test/unit/grid_test.rb +49 -0
  77. data/test/unit/pool_test.rb +9 -0
  78. data/test/unit/repl_set_connection_test.rb +82 -0
  79. data/test/unit/safe_test.rb +125 -0
  80. metadata +132 -51
  81. data/bin/bson_benchmark.rb +0 -59
  82. data/bin/fail_if_no_c.rb +0 -11
  83. data/examples/admin.rb +0 -43
  84. data/examples/capped.rb +0 -22
  85. data/examples/cursor.rb +0 -48
  86. data/examples/gridfs.rb +0 -44
  87. data/examples/index_test.rb +0 -126
  88. data/examples/info.rb +0 -31
  89. data/examples/queries.rb +0 -70
  90. data/examples/simple.rb +0 -24
  91. data/examples/strict.rb +0 -35
  92. data/examples/types.rb +0 -36
  93. data/test/replica/count_test.rb +0 -34
  94. data/test/replica/pooled_insert_test.rb +0 -54
  95. data/test/replica/query_test.rb +0 -39
@@ -1,7 +1,7 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
 
3
3
  class TestCollection < Test::Unit::TestCase
4
- @@connection ||= Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost', ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
4
+ @@connection ||= standard_connection
5
5
  @@db = @@connection.db(MONGO_TEST_DB)
6
6
  @@test = @@db.collection("test")
7
7
  @@version = @@connection.server_version
@@ -12,9 +12,9 @@ class TestCollection < Test::Unit::TestCase
12
12
 
13
13
  def test_optional_pk_factory
14
14
  @coll_default_pk = @@db.collection('stuff')
15
- assert_equal BSON::ObjectID, @coll_default_pk.pk_factory
15
+ assert_equal BSON::ObjectId, @coll_default_pk.pk_factory
16
16
  @coll_default_pk = @@db.create_collection('more-stuff')
17
- assert_equal BSON::ObjectID, @coll_default_pk.pk_factory
17
+ assert_equal BSON::ObjectId, @coll_default_pk.pk_factory
18
18
 
19
19
  # Create a db with a pk_factory.
20
20
  @db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
@@ -26,6 +26,20 @@ class TestCollection < Test::Unit::TestCase
26
26
  assert @coll.pk_factory.is_a?(Object)
27
27
  end
28
28
 
29
+ class TestPK
30
+ def self.create_pk
31
+ end
32
+ end
33
+
34
+ def test_pk_factory_on_collection
35
+ @coll = Collection.new(@@db, 'foo', TestPK)
36
+ assert_equal TestPK, @coll.pk_factory
37
+
38
+
39
+ @coll2 = Collection.new(@@db, 'foo', :pk => TestPK)
40
+ assert_equal TestPK, @coll2.pk_factory
41
+ end
42
+
29
43
  def test_valid_names
30
44
  assert_raise Mongo::InvalidNSName do
31
45
  @@db["te$t"]
@@ -53,6 +67,17 @@ class TestCollection < Test::Unit::TestCase
53
67
  assert_equal 5, @@db.collection("test.foo").find_one()["x"]
54
68
  end
55
69
 
70
+ def test_rename_collection
71
+ @@db.drop_collection('foo1')
72
+ @@db.drop_collection('bar1')
73
+
74
+ @col = @@db.create_collection('foo1')
75
+ assert_equal 'foo1', @col.name
76
+
77
+ @col.rename('bar1')
78
+ assert_equal 'bar1', @col.name
79
+ end
80
+
56
81
  def test_nil_id
57
82
  assert_equal 5, @@test.insert({"_id" => 5, "foo" => "bar"}, {:safe => true})
58
83
  assert_equal 5, @@test.save({"_id" => 5, "foo" => "baz"}, {:safe => true})
@@ -105,16 +130,43 @@ class TestCollection < Test::Unit::TestCase
105
130
  end
106
131
 
107
132
  def test_safe_insert
133
+ @@test.create_index("hello", :unique => true)
108
134
  a = {"hello" => "world"}
109
135
  @@test.insert(a)
110
136
  @@test.insert(a)
111
- assert(@@db.error.include?("E11000"))
137
+ assert(@@db.get_last_error['err'].include?("11000"))
112
138
 
113
139
  assert_raise OperationFailure do
114
140
  @@test.insert(a, :safe => true)
115
141
  end
116
142
  end
117
143
 
144
+ def test_maximum_insert_size
145
+ docs = []
146
+ 16.times do
147
+ docs << {'foo' => 'a' * 1_000_000}
148
+ end
149
+
150
+ assert_raise InvalidOperation do
151
+ @@test.insert(docs)
152
+ end
153
+ end
154
+
155
+ if @@version >= "1.5.1"
156
+ def test_safe_mode_with_advanced_safe_with_invalid_options
157
+ assert_raise_error ArgumentError, "Unknown key(s): wtime" do
158
+ @@test.insert({:foo => 1}, :safe => {:w => 2, :wtime => 1, :fsync => true})
159
+ end
160
+ assert_raise_error ArgumentError, "Unknown key(s): wtime" do
161
+ @@test.update({:foo => 1}, {:foo => 2}, :safe => {:w => 2, :wtime => 1, :fsync => true})
162
+ end
163
+
164
+ assert_raise_error ArgumentError, "Unknown key(s): wtime" do
165
+ @@test.remove({:foo => 2}, :safe => {:w => 2, :wtime => 1, :fsync => true})
166
+ end
167
+ end
168
+ end
169
+
118
170
  def test_update
119
171
  id1 = @@test.save("x" => 5)
120
172
  @@test.update({}, {"$inc" => {"x" => 1}})
@@ -194,7 +246,7 @@ class TestCollection < Test::Unit::TestCase
194
246
  end
195
247
 
196
248
  def test_mocked_safe_remove
197
- @conn = Connection.new
249
+ @conn = standard_connection
198
250
  @db = @conn[MONGO_TEST_DB]
199
251
  @test = @db['test-safe-remove']
200
252
  @test.save({:a => 20})
@@ -207,14 +259,18 @@ class TestCollection < Test::Unit::TestCase
207
259
  end
208
260
 
209
261
  def test_safe_remove
210
- @conn = Connection.new
262
+ @conn = standard_connection
211
263
  @db = @conn[MONGO_TEST_DB]
212
264
  @test = @db['test-safe-remove']
213
265
  @test.save({:a => 50})
214
- @test.remove({}, :safe => true)
266
+ assert_equal 1, @test.remove({}, :safe => true)["n"]
215
267
  @test.drop
216
268
  end
217
269
 
270
+ def test_remove_return_value
271
+ assert_equal true, @@test.remove({})
272
+ end
273
+
218
274
  def test_count
219
275
  @@test.drop
220
276
 
@@ -253,6 +309,11 @@ class TestCollection < Test::Unit::TestCase
253
309
  end
254
310
  end
255
311
 
312
+ def test_defualt_timeout
313
+ cursor = @@test.find
314
+ assert_equal true, cursor.timeout
315
+ end
316
+
256
317
  def test_fields_as_hash
257
318
  @@test.save(:a => 1, :b => 1, :c => 1)
258
319
 
@@ -272,6 +333,16 @@ class TestCollection < Test::Unit::TestCase
272
333
  end
273
334
  end
274
335
 
336
+ if @@version >= "1.5.1"
337
+ def test_fields_with_slice
338
+ @@test.save({:foo => [1, 2, 3, 4, 5, 6], :test => 'slice'})
339
+
340
+ doc = @@test.find_one({:test => 'slice'}, :fields => {'foo' => {'$slice' => [0, 3]}})
341
+ assert_equal [1, 2, 3], doc['foo']
342
+ @@test.remove
343
+ end
344
+ end
345
+
275
346
  def test_find_one
276
347
  id = @@test.save("hello" => "world", "foo" => "bar")
277
348
 
@@ -280,15 +351,15 @@ class TestCollection < Test::Unit::TestCase
280
351
  assert_equal @@test.find_one(nil), @@test.find_one()
281
352
  assert_equal @@test.find_one({}), @@test.find_one()
282
353
  assert_equal @@test.find_one("hello" => "world"), @@test.find_one()
283
- assert_equal @@test.find_one(OrderedHash["hello", "world"]), @@test.find_one()
354
+ assert_equal @@test.find_one(BSON::OrderedHash["hello", "world"]), @@test.find_one()
284
355
 
285
356
  assert @@test.find_one(nil, :fields => ["hello"]).include?("hello")
286
357
  assert !@@test.find_one(nil, :fields => ["foo"]).include?("hello")
287
358
  assert_equal ["_id"], @@test.find_one(nil, :fields => []).keys()
288
359
 
289
360
  assert_equal nil, @@test.find_one("hello" => "foo")
290
- assert_equal nil, @@test.find_one(OrderedHash["hello", "foo"])
291
- assert_equal nil, @@test.find_one(ObjectID.new)
361
+ assert_equal nil, @@test.find_one(BSON::OrderedHash["hello", "foo"])
362
+ assert_equal nil, @@test.find_one(ObjectId.new)
292
363
 
293
364
  assert_raise TypeError do
294
365
  @@test.find_one(6)
@@ -339,7 +410,7 @@ class TestCollection < Test::Unit::TestCase
339
410
  assert c.closed?
340
411
  end
341
412
 
342
- if @@version < "1.1.1"
413
+ if @@version > "1.1.1"
343
414
  def test_map_reduce
344
415
  @@test << { "user_id" => 1 }
345
416
  @@test << { "user_id" => 2 }
@@ -375,6 +446,25 @@ class TestCollection < Test::Unit::TestCase
375
446
  assert res.find_one({"_id" => 2})
376
447
  assert res.find_one({"_id" => 3})
377
448
  end
449
+
450
+ def test_map_reduce_with_raw_response
451
+ m = Code.new("function() { emit(this.user_id, 1); }")
452
+ r = Code.new("function(k,vals) { return 1; }")
453
+ res = @@test.map_reduce(m, r, :raw => true)
454
+ assert res["result"]
455
+ assert res["counts"]
456
+ assert res["timeMillis"]
457
+ end
458
+
459
+ def test_map_reduce_with_output_collection
460
+ output_collection = "test-map-coll"
461
+ m = Code.new("function() { emit(this.user_id, 1); }")
462
+ r = Code.new("function(k,vals) { return 1; }")
463
+ res = @@test.map_reduce(m, r, :raw => true, :out => output_collection)
464
+ assert_equal output_collection, res["result"]
465
+ assert res["counts"]
466
+ assert res["timeMillis"]
467
+ end
378
468
  end
379
469
 
380
470
  if @@version > "1.3.0"
@@ -411,7 +501,7 @@ class TestCollection < Test::Unit::TestCase
411
501
  def test_saving_dates_pre_epoch
412
502
  begin
413
503
  @@test.save({'date' => Time.utc(1600)})
414
- assert_in_delta Time.utc(1600), @@test.find_one()["date"], 0.001
504
+ assert_in_delta Time.utc(1600), @@test.find_one()["date"], 2
415
505
  rescue ArgumentError
416
506
  # See note in test_date_before_epoch (BSONTest)
417
507
  end
@@ -475,8 +565,43 @@ class TestCollection < Test::Unit::TestCase
475
565
  assert_equal 1, x
476
566
  end
477
567
 
568
+
569
+ def test_ensure_index
570
+ @@test.drop_indexes
571
+ @@test.insert("x" => "hello world")
572
+ assert_equal 1, @@test.index_information.keys.count #default index
573
+
574
+ @@test.ensure_index([["x", Mongo::DESCENDING]], {})
575
+ assert_equal 2, @@test.index_information.keys.count
576
+ assert @@test.index_information.keys.include? "x_-1"
577
+
578
+ @@test.ensure_index([["x", Mongo::ASCENDING]])
579
+ assert @@test.index_information.keys.include? "x_1"
580
+
581
+ @@test.ensure_index([["type", 1], ["date", -1]])
582
+ assert @@test.index_information.keys.include? "type_1_date_-1"
583
+
584
+ @@test.drop_index("x_1")
585
+ assert_equal 3, @@test.index_information.keys.count
586
+ @@test.drop_index("x_-1")
587
+ assert_equal 2, @@test.index_information.keys.count
588
+
589
+ @@test.ensure_index([["x", Mongo::DESCENDING]], {})
590
+ assert_equal 3, @@test.index_information.keys.count
591
+ assert @@test.index_information.keys.include? "x_-1"
592
+
593
+ # Make sure that drop_index expires cache properly
594
+ @@test.ensure_index([['a', 1]])
595
+ assert @@test.index_information.keys.include?("a_1")
596
+ @@test.drop_index("a_1")
597
+ assert !@@test.index_information.keys.include?("a_1")
598
+ @@test.ensure_index([['a', 1]])
599
+ assert @@test.index_information.keys.include?("a_1")
600
+ @@test.drop_index("a_1")
601
+ end
602
+
478
603
  context "Grouping" do
479
- setup do
604
+ setup do
480
605
  @@test.remove
481
606
  @@test.save("a" => 1)
482
607
  @@test.save("b" => 1)
@@ -496,6 +621,23 @@ class TestCollection < Test::Unit::TestCase
496
621
  end
497
622
  end
498
623
 
624
+ context "Grouping with key" do
625
+ setup do
626
+ @@test.remove
627
+ @@test.save("a" => 1, "pop" => 100)
628
+ @@test.save("a" => 1, "pop" => 100)
629
+ @@test.save("a" => 2, "pop" => 100)
630
+ @@test.save("a" => 2, "pop" => 100)
631
+ @initial = {"count" => 0, "foo" => 1}
632
+ @reduce_function = "function (obj, prev) { prev.count += obj.pop; }"
633
+ end
634
+
635
+ should "group" do
636
+ result = @@test.group([:a], {}, @initial, @reduce_function, nil)
637
+ assert result.all? { |r| r['count'] == 200 }
638
+ end
639
+ end
640
+
499
641
  context "Grouping with a key function" do
500
642
  setup do
501
643
  @@test.remove
@@ -562,6 +704,22 @@ class TestCollection < Test::Unit::TestCase
562
704
  assert @collection.index_information['a_1']['unique'] == true
563
705
  end
564
706
 
707
+ should "drop duplicates" do
708
+ @collection.insert({:a => 1})
709
+ @collection.insert({:a => 1})
710
+ assert_equal 2, @collection.find({:a => 1}).count
711
+ @collection.create_index([['a', Mongo::ASCENDING]], :unique => true, :dropDups => true)
712
+ assert_equal 1, @collection.find({:a => 1}).count
713
+ end
714
+
715
+ should "drop duplicates with ruby-like drop_dups key" do
716
+ @collection.insert({:a => 1})
717
+ @collection.insert({:a => 1})
718
+ assert_equal 2, @collection.find({:a => 1}).count
719
+ @collection.create_index([['a', Mongo::ASCENDING]], :unique => true, :drop_dups => true)
720
+ assert_equal 1, @collection.find({:a => 1}).count
721
+ end
722
+
565
723
  should "create an index in the background" do
566
724
  if @@version > '1.3.1'
567
725
  @collection.create_index([['b', Mongo::ASCENDING]], :background => true)
@@ -583,6 +741,19 @@ class TestCollection < Test::Unit::TestCase
583
741
  end
584
742
  end
585
743
 
744
+ should "raise an error if index name is greater than 128" do
745
+ assert_raise Mongo::OperationFailure do
746
+ @collection.create_index([['a' * 25, 1], ['b' * 25, 1],
747
+ ['c' * 25, 1], ['d' * 25, 1], ['e' * 25, 1]])
748
+ end
749
+ end
750
+
751
+ should "allow for an alternate name to be specified" do
752
+ @collection.create_index([['a' * 25, 1], ['b' * 25, 1],
753
+ ['c' * 25, 1], ['d' * 25, 1], ['e' * 25, 1]], :name => 'foo_index')
754
+ assert @collection.index_information['foo_index']
755
+ end
756
+
586
757
  should "generate indexes in the proper order" do
587
758
  @collection.expects(:insert_documents) do |sel, coll, safe|
588
759
  assert_equal 'b_1_a_1', sel[:name]
@@ -590,6 +761,15 @@ class TestCollection < Test::Unit::TestCase
590
761
  @collection.create_index([['b', 1], ['a', 1]])
591
762
  end
592
763
 
764
+ should "allow multiple calls to create_index" do
765
+
766
+ end
767
+
768
+ should "allow creation of multiple indexes" do
769
+ assert @collection.create_index([['a', 1]])
770
+ assert @collection.create_index([['a', 1]])
771
+ end
772
+
593
773
  context "with an index created" do
594
774
  setup do
595
775
  @collection.create_index([['b', 1], ['a', 1]])
@@ -619,7 +799,7 @@ class TestCollection < Test::Unit::TestCase
619
799
  assert_nil cursor.next_document
620
800
  end
621
801
 
622
- should "" do
802
+ should "fail tailable cursor on a non-capped collection" do
623
803
  col = @@db['regular-collection']
624
804
  col.insert({:a => 1000})
625
805
  tail = Cursor.new(col, :tailable => true, :order => [['$natural', 1]])
@@ -1,88 +1,104 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
  require 'logger'
3
3
  require 'stringio'
4
4
  require 'thread'
5
5
 
6
- # NOTE: assumes Mongo is running
7
6
  class TestConnection < Test::Unit::TestCase
8
7
 
9
8
  include Mongo
10
9
  include BSON
11
10
 
12
11
  def setup
13
- @host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
14
- @port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
15
- @mongo = Connection.new(@host, @port)
12
+ @conn = standard_connection
16
13
  end
17
14
 
18
15
  def teardown
19
- @mongo.db(MONGO_TEST_DB).error
16
+ @conn[MONGO_TEST_DB].get_last_error
17
+ end
18
+
19
+ def test_connection_failure
20
+ assert_raise Mongo::ConnectionFailure do
21
+ Mongo::Connection.new('localhost', 27347)
22
+ end
20
23
  end
21
24
 
22
25
  def test_server_info
23
- server_info = @mongo.server_info
26
+ server_info = @conn.server_info
24
27
  assert server_info.keys.include?("version")
25
- assert_equal 1.0, server_info["ok"]
28
+ assert Mongo::Support.ok?(server_info)
29
+ end
30
+
31
+ def test_connection_uri
32
+ con = Connection.from_uri("mongodb://#{host_port}")
33
+ assert_equal mongo_host, con.primary_pool.host
34
+ assert_equal mongo_port, con.primary_pool.port
26
35
  end
27
36
 
28
37
  def test_server_version
29
- assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s
38
+ assert_match /\d\.\d+(\.\d+)?/, @conn.server_version.to_s
30
39
  end
31
40
 
32
41
  def test_invalid_database_names
33
- assert_raise TypeError do @mongo.db(4) end
42
+ assert_raise TypeError do @conn.db(4) end
43
+
44
+ assert_raise Mongo::InvalidNSName do @conn.db('') end
45
+ assert_raise Mongo::InvalidNSName do @conn.db('te$t') end
46
+ assert_raise Mongo::InvalidNSName do @conn.db('te.t') end
47
+ assert_raise Mongo::InvalidNSName do @conn.db('te\\t') end
48
+ assert_raise Mongo::InvalidNSName do @conn.db('te/t') end
49
+ assert_raise Mongo::InvalidNSName do @conn.db('te st') end
50
+ end
34
51
 
35
- assert_raise Mongo::InvalidNSName do @mongo.db('') end
36
- assert_raise Mongo::InvalidNSName do @mongo.db('te$t') end
37
- assert_raise Mongo::InvalidNSName do @mongo.db('te.t') end
38
- assert_raise Mongo::InvalidNSName do @mongo.db('te\\t') end
39
- assert_raise Mongo::InvalidNSName do @mongo.db('te/t') end
40
- assert_raise Mongo::InvalidNSName do @mongo.db('te st') end
52
+ def test_options_passed_to_db
53
+ @pk_mock = Object.new
54
+ db = @conn.db('test', :pk => @pk_mock, :strict => true)
55
+ assert_equal @pk_mock, db.pk_factory
56
+ assert db.strict?
41
57
  end
42
58
 
43
59
  def test_database_info
44
- @mongo.drop_database(MONGO_TEST_DB)
45
- @mongo.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
60
+ @conn.drop_database(MONGO_TEST_DB)
61
+ @conn.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
46
62
 
47
- info = @mongo.database_info
63
+ info = @conn.database_info
48
64
  assert_not_nil info
49
65
  assert_kind_of Hash, info
50
66
  assert_not_nil info[MONGO_TEST_DB]
51
67
  assert info[MONGO_TEST_DB] > 0
52
68
 
53
- @mongo.drop_database(MONGO_TEST_DB)
69
+ @conn.drop_database(MONGO_TEST_DB)
54
70
  end
55
71
 
56
72
  def test_copy_database
57
- @mongo.db('old').collection('copy-test').insert('a' => 1)
58
- @mongo.copy_database('old', 'new')
59
- old_object = @mongo.db('old').collection('copy-test').find.next_document
60
- new_object = @mongo.db('new').collection('copy-test').find.next_document
73
+ @conn.db('old').collection('copy-test').insert('a' => 1)
74
+ @conn.copy_database('old', 'new', host_port)
75
+ old_object = @conn.db('old').collection('copy-test').find.next_document
76
+ new_object = @conn.db('new').collection('copy-test').find.next_document
61
77
  assert_equal old_object, new_object
62
- @mongo.drop_database('old')
63
- @mongo.drop_database('new')
78
+ @conn.drop_database('old')
79
+ @conn.drop_database('new')
64
80
  end
65
81
 
66
82
  def test_copy_database_with_auth
67
- @mongo.db('old').collection('copy-test').insert('a' => 1)
68
- @mongo.db('old').add_user('bob', 'secret')
83
+ @conn.db('old').collection('copy-test').insert('a' => 1)
84
+ @conn.db('old').add_user('bob', 'secret')
69
85
 
70
86
  assert_raise Mongo::OperationFailure do
71
- @mongo.copy_database('old', 'new', 'localhost', 'bob', 'badpassword')
87
+ @conn.copy_database('old', 'new', host_port, 'bob', 'badpassword')
72
88
  end
73
89
 
74
- result = @mongo.copy_database('old', 'new', 'localhost', 'bob', 'secret')
75
- assert result['ok'].to_i == 1
90
+ result = @conn.copy_database('old', 'new', host_port, 'bob', 'secret')
91
+ assert Mongo::Support.ok?(result)
76
92
 
77
- @mongo.drop_database('old')
78
- @mongo.drop_database('new')
93
+ @conn.drop_database('old')
94
+ @conn.drop_database('new')
79
95
  end
80
96
 
81
97
  def test_database_names
82
- @mongo.drop_database(MONGO_TEST_DB)
83
- @mongo.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
98
+ @conn.drop_database(MONGO_TEST_DB)
99
+ @conn.db(MONGO_TEST_DB).collection('info-test').insert('a' => 1)
84
100
 
85
- names = @mongo.database_names
101
+ names = @conn.database_names
86
102
  assert_not_nil names
87
103
  assert_kind_of Array, names
88
104
  assert names.length >= 1
@@ -93,7 +109,7 @@ class TestConnection < Test::Unit::TestCase
93
109
  output = StringIO.new
94
110
  logger = Logger.new(output)
95
111
  logger.level = Logger::DEBUG
96
- db = Connection.new(@host, @port, :logger => logger).db(MONGO_TEST_DB)
112
+ connection = standard_connection(:logger => logger).db(MONGO_TEST_DB)
97
113
  assert output.string.include?("admin['$cmd'].find")
98
114
  end
99
115
 
@@ -101,36 +117,57 @@ class TestConnection < Test::Unit::TestCase
101
117
  output = StringIO.new
102
118
  logger = Logger.new(output)
103
119
  logger.level = Logger::DEBUG
104
- connection = Connection.new(@host, @port, :logger => logger)
120
+ connection = standard_connection(:logger => logger)
105
121
  assert_equal logger, connection.logger
106
-
122
+
107
123
  connection.logger.debug 'testing'
108
124
  assert output.string.include?('testing')
109
125
  end
110
126
 
111
127
  def test_drop_database
112
- db = @mongo.db('ruby-mongo-will-be-deleted')
128
+ db = @conn.db('ruby-mongo-will-be-deleted')
113
129
  coll = db.collection('temp')
114
130
  coll.remove
115
131
  coll.insert(:name => 'temp')
116
132
  assert_equal 1, coll.count()
117
- assert @mongo.database_names.include?('ruby-mongo-will-be-deleted')
133
+ assert @conn.database_names.include?('ruby-mongo-will-be-deleted')
118
134
 
119
- @mongo.drop_database('ruby-mongo-will-be-deleted')
120
- assert !@mongo.database_names.include?('ruby-mongo-will-be-deleted')
135
+ @conn.drop_database('ruby-mongo-will-be-deleted')
136
+ assert !@conn.database_names.include?('ruby-mongo-will-be-deleted')
121
137
  end
122
138
 
123
139
  def test_nodes
124
- db = Connection.paired([['foo', 27017], ['bar', 27018]], :connect => false)
140
+ db = Connection.multi([['foo', 27017], ['bar', 27018]], :connect => false)
125
141
  nodes = db.nodes
126
142
  assert_equal 2, nodes.length
127
143
  assert_equal ['foo', 27017], nodes[0]
128
144
  assert_equal ['bar', 27018], nodes[1]
129
145
  end
130
146
 
147
+ def test_fsync_lock
148
+ assert !@conn.locked?
149
+ @conn.lock!
150
+ assert @conn.locked?
151
+ assert_equal 1, @conn['admin']['$cmd.sys.inprog'].find_one['fsyncLock'], "Not fsync-locked"
152
+ assert_equal "unlock requested", @conn.unlock!['info']
153
+ unlocked = false
154
+ counter = 0
155
+ while counter < 5
156
+ if @conn['admin']['$cmd.sys.inprog'].find_one['fsyncLock'].nil?
157
+ unlocked = true
158
+ break
159
+ else
160
+ sleep(1)
161
+ counter += 1
162
+ end
163
+ end
164
+ assert !@conn.locked?
165
+ assert unlocked, "mongod failed to unlock"
166
+ end
167
+
131
168
  context "Saved authentications" do
132
169
  setup do
133
- @conn = Mongo::Connection.new
170
+ @conn = standard_connection
134
171
  @auth = {'db_name' => 'test', 'username' => 'bob', 'password' => 'secret'}
135
172
  @conn.add_auth(@auth['db_name'], @auth['username'], @auth['password'])
136
173
  end
@@ -162,35 +199,35 @@ class TestConnection < Test::Unit::TestCase
162
199
 
163
200
  context "Connection exceptions" do
164
201
  setup do
165
- @conn = Mongo::Connection.new('localhost', 27017, :pool_size => 10, :timeout => 10)
166
- @coll = @conn[MONGO_TEST_DB]['test-connection-exceptions']
202
+ @con = standard_connection(:pool_size => 10, :timeout => 10)
203
+ @coll = @con[MONGO_TEST_DB]['test-connection-exceptions']
167
204
  end
168
205
 
169
206
  should "release connection if an exception is raised on send_message" do
170
- @conn.stubs(:send_message_on_socket).raises(ConnectionFailure)
171
- assert_equal 0, @conn.checked_out.size
207
+ @con.stubs(:send_message_on_socket).raises(ConnectionFailure)
208
+ assert_equal 0, @con.primary_pool.checked_out.size
172
209
  assert_raise ConnectionFailure do
173
210
  @coll.insert({:test => "insert"})
174
211
  end
175
- assert_equal 0, @conn.checked_out.size
212
+ assert_equal 0, @con.primary_pool.checked_out.size
176
213
  end
177
214
 
178
215
  should "release connection if an exception is raised on send_with_safe_check" do
179
- @conn.stubs(:receive).raises(ConnectionFailure)
180
- assert_equal 0, @conn.checked_out.size
216
+ @con.stubs(:receive).raises(ConnectionFailure)
217
+ assert_equal 0, @con.primary_pool.checked_out.size
181
218
  assert_raise ConnectionFailure do
182
219
  @coll.insert({:test => "insert"}, :safe => true)
183
220
  end
184
- assert_equal 0, @conn.checked_out.size
221
+ assert_equal 0, @con.primary_pool.checked_out.size
185
222
  end
186
223
 
187
224
  should "release connection if an exception is raised on receive_message" do
188
- @conn.stubs(:receive).raises(ConnectionFailure)
189
- assert_equal 0, @conn.checked_out.size
225
+ @con.stubs(:receive).raises(ConnectionFailure)
226
+ assert_equal 0, @con.primary_pool.checked_out.size
190
227
  assert_raise ConnectionFailure do
191
228
  @coll.find.to_a
192
229
  end
193
- assert_equal 0, @conn.checked_out.size
230
+ assert_equal 0, @con.primary_pool.checked_out.size
194
231
  end
195
232
  end
196
233
  end
@@ -1,4 +1,4 @@
1
- require 'test/test_helper'
1
+ require './test/test_helper'
2
2
  require 'mongo/exceptions'
3
3
  require 'mongo/util/conversions'
4
4
  require 'bson/ordered_hash'