mongo 0.15.1 → 0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/LICENSE.txt +202 -0
  2. data/README.rdoc +15 -2
  3. data/Rakefile +17 -3
  4. data/bin/autoreconnect.rb +26 -0
  5. data/bin/fail_if_no_c.rb +11 -0
  6. data/lib/mongo.rb +9 -2
  7. data/lib/mongo/admin.rb +1 -1
  8. data/lib/mongo/collection.rb +86 -25
  9. data/lib/mongo/connection.rb +11 -0
  10. data/lib/mongo/constants.rb +15 -0
  11. data/lib/mongo/cursor.rb +176 -36
  12. data/lib/mongo/db.rb +84 -97
  13. data/lib/mongo/errors.rb +7 -1
  14. data/lib/mongo/types/objectid.rb +5 -0
  15. data/lib/mongo/util/byte_buffer.rb +12 -0
  16. data/lib/mongo/util/server_version.rb +69 -0
  17. data/lib/mongo/{message.rb → util/support.rb} +7 -4
  18. data/mongo-ruby-driver.gemspec +9 -86
  19. data/test/test_admin.rb +2 -2
  20. data/test/test_bson.rb +14 -0
  21. data/test/test_byte_buffer.rb +14 -0
  22. data/test/test_chunk.rb +4 -4
  23. data/test/test_collection.rb +107 -17
  24. data/test/test_connection.rb +13 -4
  25. data/test/test_cursor.rb +17 -14
  26. data/test/test_db.rb +7 -7
  27. data/test/test_db_api.rb +31 -19
  28. data/test/test_db_connection.rb +1 -1
  29. data/test/test_grid_store.rb +8 -8
  30. data/test/test_helper.rb +25 -0
  31. data/test/test_objectid.rb +11 -1
  32. data/test/test_slave_connection.rb +37 -0
  33. data/test/test_threading.rb +1 -1
  34. data/test/unit/cursor_test.rb +122 -0
  35. metadata +26 -46
  36. data/bin/mongo_console +0 -21
  37. data/bin/run_test_script +0 -19
  38. data/bin/standard_benchmark +0 -109
  39. data/lib/mongo/message/get_more_message.rb +0 -32
  40. data/lib/mongo/message/insert_message.rb +0 -37
  41. data/lib/mongo/message/kill_cursors_message.rb +0 -31
  42. data/lib/mongo/message/message.rb +0 -80
  43. data/lib/mongo/message/message_header.rb +0 -45
  44. data/lib/mongo/message/msg_message.rb +0 -29
  45. data/lib/mongo/message/opcodes.rb +0 -27
  46. data/lib/mongo/message/query_message.rb +0 -69
  47. data/lib/mongo/message/remove_message.rb +0 -37
  48. data/lib/mongo/message/update_message.rb +0 -38
  49. data/lib/mongo/query.rb +0 -118
  50. data/test/mongo-qa/admin +0 -26
  51. data/test/mongo-qa/capped +0 -22
  52. data/test/mongo-qa/count1 +0 -18
  53. data/test/mongo-qa/dbs +0 -22
  54. data/test/mongo-qa/find +0 -10
  55. data/test/mongo-qa/find1 +0 -15
  56. data/test/mongo-qa/gridfs_in +0 -16
  57. data/test/mongo-qa/gridfs_out +0 -17
  58. data/test/mongo-qa/indices +0 -49
  59. data/test/mongo-qa/remove +0 -25
  60. data/test/mongo-qa/stress1 +0 -35
  61. data/test/mongo-qa/test1 +0 -11
  62. data/test/mongo-qa/update +0 -18
  63. data/test/test_message.rb +0 -35
@@ -19,6 +19,17 @@ class TestConnection < Test::Unit::TestCase
19
19
  @mongo.db('ruby-mongo-test').error
20
20
  end
21
21
 
22
+ def test_server_info
23
+ server_info = @mongo.server_info
24
+ assert server_info.keys.include? "version"
25
+ assert server_info.keys.include? "bits"
26
+ assert_equal 1.0, server_info["ok"]
27
+ end
28
+
29
+ def test_server_version
30
+ assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s
31
+ end
32
+
22
33
  def test_invalid_database_names
23
34
  assert_raise TypeError do @mongo.db(4) end
24
35
 
@@ -61,10 +72,8 @@ class TestConnection < Test::Unit::TestCase
61
72
  logger = Logger.new(output)
62
73
  logger.level = Logger::DEBUG
63
74
  db = Connection.new(@host, @port, :logger => logger).db('ruby-mongo-test')
64
- db['test'].find().to_a
65
75
 
66
- assert output.string.include?("db.test.find")
67
- assert !output.string.include?("db.test.remove")
76
+ assert output.string.include?("2004")
68
77
  end
69
78
 
70
79
  def test_connection_logger
@@ -81,7 +90,7 @@ class TestConnection < Test::Unit::TestCase
81
90
  def test_drop_database
82
91
  db = @mongo.db('ruby-mongo-will-be-deleted')
83
92
  coll = db.collection('temp')
84
- coll.clear
93
+ coll.remove
85
94
  coll.insert(:name => 'temp')
86
95
  assert_equal 1, coll.count()
87
96
  assert @mongo.database_names.include?('ruby-mongo-will-be-deleted')
@@ -7,18 +7,20 @@ class CursorTest < Test::Unit::TestCase
7
7
 
8
8
  include Mongo
9
9
 
10
- @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
11
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
10
+ @@connection = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
11
+ ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
12
+ @@db = @@connection.db('ruby-mongo-test')
12
13
  @@coll = @@db.collection('test')
14
+ @@version = @@connection.server_version
13
15
 
14
16
  def setup
15
- @@coll.clear
17
+ @@coll.remove
16
18
  @@coll.insert('a' => 1) # collection not created until it's used
17
19
  @@coll_full_name = 'ruby-mongo-test.test'
18
20
  end
19
21
 
20
22
  def teardown
21
- @@coll.clear
23
+ @@coll.remove
22
24
  @@db.error
23
25
  end
24
26
 
@@ -32,7 +34,7 @@ class CursorTest < Test::Unit::TestCase
32
34
  end
33
35
 
34
36
  def test_count
35
- @@coll.clear
37
+ @@coll.remove
36
38
 
37
39
  assert_equal 0, @@coll.find().count()
38
40
 
@@ -59,7 +61,7 @@ class CursorTest < Test::Unit::TestCase
59
61
  end
60
62
 
61
63
  def test_sort
62
- @@coll.clear
64
+ @@coll.remove
63
65
  5.times{|x| @@coll.insert({"a" => x}) }
64
66
 
65
67
  assert_kind_of Cursor, @@coll.find().sort(:a, 1)
@@ -89,7 +91,7 @@ class CursorTest < Test::Unit::TestCase
89
91
  end
90
92
 
91
93
  def test_limit
92
- @@coll.clear
94
+ @@coll.remove
93
95
 
94
96
  10.times do |i|
95
97
  @@coll.save("x" => i)
@@ -119,7 +121,7 @@ class CursorTest < Test::Unit::TestCase
119
121
  end
120
122
 
121
123
  def test_skip
122
- @@coll.clear
124
+ @@coll.remove
123
125
 
124
126
  10.times do |i|
125
127
  @@coll.save("x" => i)
@@ -153,7 +155,7 @@ class CursorTest < Test::Unit::TestCase
153
155
  end
154
156
 
155
157
  def test_limit_skip_chaining
156
- @@coll.clear
158
+ @@coll.remove
157
159
  10.times do |i|
158
160
  @@coll.save("x" => i)
159
161
  end
@@ -202,7 +204,7 @@ class CursorTest < Test::Unit::TestCase
202
204
 
203
205
  def test_refill_via_get_more_alt_coll
204
206
  coll = @@db.collection('test-alt-coll')
205
- coll.clear
207
+ coll.remove
206
208
  coll.insert('a' => 1) # collection not created until it's used
207
209
  assert_equal 1, coll.count
208
210
 
@@ -318,12 +320,13 @@ class CursorTest < Test::Unit::TestCase
318
320
  end
319
321
 
320
322
  def test_count_with_fields
321
- @@coll.clear
323
+ @@coll.remove
322
324
  @@coll.save("x" => 1)
323
325
 
324
- @@coll.find({}, :fields => ["a"]).each do |doc|
325
- fail "shouldn't have any results here"
326
+ if @@version < "1.1.3"
327
+ assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
328
+ else
329
+ assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
326
330
  end
327
- assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
328
331
  end
329
332
  end
@@ -25,12 +25,12 @@ class DBTest < Test::Unit::TestCase
25
25
  def setup
26
26
  @spongebob = 'spongebob'
27
27
  @spongebob_password = 'squarepants'
28
- @@users.clear
28
+ @@users.remove
29
29
  @@users.insert(:user => @spongebob, :pwd => @@db.send(:hash_password, @spongebob, @spongebob_password))
30
30
  end
31
31
 
32
32
  def teardown
33
- @@users.clear if @@users
33
+ @@users.remove if @@users
34
34
  @@db.error
35
35
  end
36
36
 
@@ -61,7 +61,7 @@ class DBTest < Test::Unit::TestCase
61
61
 
62
62
  def test_full_coll_name
63
63
  coll = @@db.collection('test')
64
- assert_equal 'ruby-mongo-test.test', @@db.full_coll_name(coll.name)
64
+ assert_equal 'ruby-mongo-test.test', @@db.full_collection_name(coll.name)
65
65
  end
66
66
 
67
67
  def test_collection_names
@@ -101,7 +101,7 @@ class DBTest < Test::Unit::TestCase
101
101
  def test_pk_factory
102
102
  db = Connection.new(@@host, @@port).db('ruby-mongo-test', :pk => TestPKFactory.new)
103
103
  coll = db.collection('test')
104
- coll.clear
104
+ coll.remove
105
105
 
106
106
  insert_id = coll.insert('name' => 'Fred', 'age' => 42)
107
107
  # new id gets added to returned object
@@ -118,7 +118,7 @@ class DBTest < Test::Unit::TestCase
118
118
  assert_equal oid, db_oid
119
119
  assert_equal data, row
120
120
 
121
- coll.clear
121
+ coll.remove
122
122
  end
123
123
 
124
124
  def test_pk_factory_reset
@@ -190,7 +190,7 @@ class DBTest < Test::Unit::TestCase
190
190
  end
191
191
 
192
192
  def test_last_status
193
- @@db['test'].clear
193
+ @@db['test'].remove
194
194
  @@db['test'].save("i" => 1)
195
195
 
196
196
  @@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
@@ -203,7 +203,7 @@ class DBTest < Test::Unit::TestCase
203
203
  def test_text_port_number
204
204
  db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]])
205
205
  # If there is no error, all is well
206
- db.collection('users').clear
206
+ db.collection('users').remove
207
207
  end
208
208
 
209
209
  end
@@ -6,25 +6,27 @@ require 'test/unit'
6
6
  class DBAPITest < Test::Unit::TestCase
7
7
  include Mongo
8
8
 
9
- @@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
10
- ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
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")
11
12
  @@coll = @@db.collection('test')
13
+ @@version = @@connection.server_version
12
14
 
13
15
  def setup
14
- @@coll.clear
16
+ @@coll.remove
15
17
  @r1 = {'a' => 1}
16
18
  @@coll.insert(@r1) # collection not created until it's used
17
19
  @@coll_full_name = 'ruby-mongo-test.test'
18
20
  end
19
21
 
20
22
  def teardown
21
- @@coll.clear
23
+ @@coll.remove
22
24
  @@db.error
23
25
  end
24
26
 
25
27
  def test_clear
26
28
  assert_equal 1, @@coll.count
27
- @@coll.clear
29
+ @@coll.remove
28
30
  assert_equal 0, @@coll.count
29
31
  end
30
32
 
@@ -142,7 +144,7 @@ class DBAPITest < Test::Unit::TestCase
142
144
  end
143
145
 
144
146
  def test_find_sorting
145
- @@coll.clear
147
+ @@coll.remove
146
148
  @@coll.insert('a' => 1, 'b' => 2)
147
149
  @@coll.insert('a' => 2, 'b' => 1)
148
150
  @@coll.insert('a' => 3, 'b' => 2)
@@ -233,7 +235,7 @@ class DBAPITest < Test::Unit::TestCase
233
235
  end
234
236
 
235
237
  def test_find_one_no_records
236
- @@coll.clear
238
+ @@coll.remove
237
239
  x = @@coll.find_one('a' => 1)
238
240
  assert_nil x
239
241
  end
@@ -386,16 +388,26 @@ class DBAPITest < Test::Unit::TestCase
386
388
  def test_array
387
389
  @@coll << {'b' => [1, 2, 3]}
388
390
  rows = @@coll.find({}, {:fields => ['b']}).to_a
389
- assert_equal 1, rows.length
390
- assert_equal [1, 2, 3], rows[0]['b']
391
+ if @@version < "1.1.3"
392
+ assert_equal 1, rows.length
393
+ assert_equal [1, 2, 3], rows[0]['b']
394
+ else
395
+ assert_equal 2, rows.length
396
+ assert_equal [1, 2, 3], rows[1]['b']
397
+ end
391
398
  end
392
399
 
393
400
  def test_regex
394
401
  regex = /foobar/i
395
402
  @@coll << {'b' => regex}
396
403
  rows = @@coll.find({}, {:fields => ['b']}).to_a
397
- assert_equal 1, rows.length
398
- assert_equal regex, rows[0]['b']
404
+ if @@version < "1.1.3"
405
+ assert_equal 1, rows.length
406
+ assert_equal regex, rows[0]['b']
407
+ else
408
+ assert_equal 2, rows.length
409
+ assert_equal regex, rows[1]['b']
410
+ end
399
411
  end
400
412
 
401
413
  def test_non_oid_id
@@ -580,7 +592,7 @@ class DBAPITest < Test::Unit::TestCase
580
592
  end
581
593
 
582
594
  def test_deref
583
- @@coll.clear
595
+ @@coll.remove
584
596
 
585
597
  assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
586
598
  @@coll.insert({"x" => "hello"})
@@ -592,13 +604,13 @@ class DBAPITest < Test::Unit::TestCase
592
604
  @@coll.insert(obj)
593
605
  assert_equal obj, @@db.dereference(DBRef.new("test", 4))
594
606
 
595
- @@coll.clear
607
+ @@coll.remove
596
608
  @@coll.insert({"x" => "hello"})
597
609
  assert_equal nil, @@db.dereference(DBRef.new("test", nil))
598
610
  end
599
611
 
600
612
  def test_save
601
- @@coll.clear
613
+ @@coll.remove
602
614
 
603
615
  a = {"hello" => "world"}
604
616
 
@@ -622,13 +634,13 @@ class DBAPITest < Test::Unit::TestCase
622
634
  end
623
635
 
624
636
  def test_save_long
625
- @@coll.clear
637
+ @@coll.remove
626
638
  @@coll.insert("x" => 9223372036854775807)
627
639
  assert_equal 9223372036854775807, @@coll.find_one()["x"]
628
640
  end
629
641
 
630
642
  def test_find_by_oid
631
- @@coll.clear
643
+ @@coll.remove
632
644
 
633
645
  @@coll.save("hello" => "mike")
634
646
  id = @@coll.save("hello" => "world")
@@ -644,7 +656,7 @@ class DBAPITest < Test::Unit::TestCase
644
656
  end
645
657
 
646
658
  def test_save_with_object_that_has_id_but_does_not_actually_exist_in_collection
647
- @@coll.clear
659
+ @@coll.remove
648
660
 
649
661
  a = {'_id' => '1', 'hello' => 'world'}
650
662
  @@coll.save(a)
@@ -658,7 +670,7 @@ class DBAPITest < Test::Unit::TestCase
658
670
  end
659
671
 
660
672
  def test_invalid_key_names
661
- @@coll.clear
673
+ @@coll.remove
662
674
 
663
675
  @@coll.insert({"hello" => "world"})
664
676
  @@coll.insert({"hello" => {"hello" => "world"}})
@@ -779,7 +791,7 @@ class DBAPITest < Test::Unit::TestCase
779
791
  assert_equal "UTF-8", utf8.encoding.name
780
792
  assert_equal "ISO-8859-1", iso8859.encoding.name
781
793
 
782
- @@coll.clear
794
+ @@coll.remove
783
795
  @@coll.save("ascii" => ascii, "utf8" => utf8, "iso8859" => iso8859)
784
796
  doc = @@coll.find_one()
785
797
 
@@ -12,7 +12,7 @@ class DBConnectionTest < Test::Unit::TestCase
12
12
  port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
13
13
  db = Connection.new(host, port).db('ruby-mongo-demo')
14
14
  coll = db.collection('test')
15
- coll.clear
15
+ coll.remove
16
16
  db.error
17
17
  end
18
18
  end
@@ -14,14 +14,14 @@ class GridStoreTest < Test::Unit::TestCase
14
14
  @@chunks = @@db.collection('fs.chunks')
15
15
 
16
16
  def setup
17
- @@chunks.clear
18
- @@files.clear
17
+ @@chunks.remove
18
+ @@files.remove
19
19
  GridStore.open(@@db, 'foobar', 'w') { |f| f.write("hello, world!") }
20
20
  end
21
21
 
22
22
  def teardown
23
- @@chunks.clear
24
- @@files.clear
23
+ @@chunks.remove
24
+ @@files.remove
25
25
  @@db.error
26
26
  end
27
27
 
@@ -106,8 +106,8 @@ class GridStoreTest < Test::Unit::TestCase
106
106
  end
107
107
 
108
108
  def test_multi_chunk
109
- @@chunks.clear
110
- @@files.clear
109
+ @@chunks.remove
110
+ @@files.remove
111
111
 
112
112
  size = 512
113
113
  GridStore.open(@@db, 'biggie', 'w') { |f|
@@ -167,8 +167,8 @@ class GridStoreTest < Test::Unit::TestCase
167
167
  end
168
168
 
169
169
  def test_save_empty_file
170
- @@chunks.clear
171
- @@files.clear
170
+ @@chunks.remove
171
+ @@files.remove
172
172
  GridStore.open(@@db, 'empty', 'w') {} # re-write with zero bytes
173
173
  assert_equal 1, @@files.count
174
174
  assert_equal 0, @@chunks.count
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+
5
+ begin
6
+ require 'shoulda'
7
+ require 'mocha'
8
+ rescue LoadError
9
+ puts <<MSG
10
+
11
+ This test suite now requires shoulda and mocha.
12
+ You can install these gems as follows:
13
+ gem install shoulda
14
+ gem install mocha
15
+
16
+ MSG
17
+ exit
18
+ end
19
+
20
+ require 'mongo'
21
+
22
+ # NOTE: most tests assume that MongoDB is running.
23
+ class Test::Unit::TestCase
24
+ include Mongo
25
+ end
@@ -10,6 +10,16 @@ class ObjectIDTest < Test::Unit::TestCase
10
10
  @o = ObjectID.new()
11
11
  end
12
12
 
13
+ def test_create_pk_method
14
+ doc = {:name => 'Mongo'}
15
+ doc = ObjectID.create_pk(doc)
16
+ assert doc[:_id]
17
+
18
+ doc = {:name => 'Mongo', :_id => '12345'}
19
+ doc = ObjectID.create_pk(doc)
20
+ assert_equal '12345', doc[:_id]
21
+ end
22
+
13
23
  def test_different
14
24
  a = ObjectID.new
15
25
  b = ObjectID.new
@@ -44,7 +54,7 @@ class ObjectIDTest < Test::Unit::TestCase
44
54
  db = Connection.new(host, port).db('ruby-mongo-test')
45
55
  coll = db.collection('test')
46
56
 
47
- coll.clear
57
+ coll.remove
48
58
  coll << {'a' => 1, '_id' => @o}
49
59
 
50
60
  row = coll.find().collect.first
@@ -0,0 +1,37 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'test/unit'
4
+
5
+ # NOTE: these tests are run only if we can connect to a single MongoDB in slave mode.
6
+ class SlaveConnectionTest < Test::Unit::TestCase
7
+ include Mongo
8
+
9
+ def self.connect_to_slave
10
+ @@host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
11
+ @@port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
12
+ db = Connection.new(@@host, @@port, :slave_ok => true).db('ruby-mongo-demo')
13
+ !db.master?
14
+ end
15
+
16
+ if self.connect_to_slave
17
+ puts "Connected to slave; running slave tests."
18
+
19
+ def test_connect_to_slave
20
+ assert_raise Mongo::ConfigurationError do
21
+ @db = Connection.new(@@host, @@port, :slave_ok => false).db('ruby-mongo-demo')
22
+ end
23
+ end
24
+
25
+ def test_slave_ok_sent_to_queries
26
+ @db = Connection.new(@@host, @@port, :slave_ok => true).db('ruby-mongo-demo')
27
+ assert_equal true, @db.slave_ok?
28
+ end
29
+ else
30
+ puts "Not connected to slave; skipping slave connection tests."
31
+
32
+ def test_slave_ok_false_on_queries
33
+ @db = Connection.new(@@host, @@port).db('ruby-mongo-demo')
34
+ assert !@db.slave_ok?
35
+ end
36
+ end
37
+ end