mongo-lyon 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/LICENSE.txt +190 -0
  2. data/README.md +344 -0
  3. data/Rakefile +202 -0
  4. data/bin/mongo_console +34 -0
  5. data/docs/1.0_UPGRADE.md +21 -0
  6. data/docs/CREDITS.md +123 -0
  7. data/docs/FAQ.md +116 -0
  8. data/docs/GridFS.md +158 -0
  9. data/docs/HISTORY.md +225 -0
  10. data/docs/REPLICA_SETS.md +72 -0
  11. data/docs/TUTORIAL.md +247 -0
  12. data/docs/WRITE_CONCERN.md +28 -0
  13. data/lib/mongo.rb +77 -0
  14. data/lib/mongo/collection.rb +872 -0
  15. data/lib/mongo/connection.rb +875 -0
  16. data/lib/mongo/cursor.rb +449 -0
  17. data/lib/mongo/db.rb +607 -0
  18. data/lib/mongo/exceptions.rb +68 -0
  19. data/lib/mongo/gridfs/grid.rb +106 -0
  20. data/lib/mongo/gridfs/grid_ext.rb +57 -0
  21. data/lib/mongo/gridfs/grid_file_system.rb +145 -0
  22. data/lib/mongo/gridfs/grid_io.rb +394 -0
  23. data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
  24. data/lib/mongo/repl_set_connection.rb +342 -0
  25. data/lib/mongo/util/conversions.rb +89 -0
  26. data/lib/mongo/util/core_ext.rb +60 -0
  27. data/lib/mongo/util/pool.rb +185 -0
  28. data/lib/mongo/util/server_version.rb +71 -0
  29. data/lib/mongo/util/support.rb +82 -0
  30. data/lib/mongo/util/uri_parser.rb +181 -0
  31. data/lib/mongo/version.rb +3 -0
  32. data/mongo.gemspec +34 -0
  33. data/test/auxillary/1.4_features.rb +166 -0
  34. data/test/auxillary/authentication_test.rb +68 -0
  35. data/test/auxillary/autoreconnect_test.rb +41 -0
  36. data/test/auxillary/repl_set_auth_test.rb +58 -0
  37. data/test/auxillary/slave_connection_test.rb +36 -0
  38. data/test/auxillary/threaded_authentication_test.rb +101 -0
  39. data/test/bson/binary_test.rb +15 -0
  40. data/test/bson/bson_test.rb +614 -0
  41. data/test/bson/byte_buffer_test.rb +190 -0
  42. data/test/bson/hash_with_indifferent_access_test.rb +38 -0
  43. data/test/bson/json_test.rb +17 -0
  44. data/test/bson/object_id_test.rb +154 -0
  45. data/test/bson/ordered_hash_test.rb +197 -0
  46. data/test/collection_test.rb +893 -0
  47. data/test/connection_test.rb +303 -0
  48. data/test/conversions_test.rb +120 -0
  49. data/test/cursor_fail_test.rb +75 -0
  50. data/test/cursor_message_test.rb +43 -0
  51. data/test/cursor_test.rb +457 -0
  52. data/test/db_api_test.rb +715 -0
  53. data/test/db_connection_test.rb +15 -0
  54. data/test/db_test.rb +287 -0
  55. data/test/grid_file_system_test.rb +244 -0
  56. data/test/grid_io_test.rb +120 -0
  57. data/test/grid_test.rb +200 -0
  58. data/test/load/thin/load.rb +24 -0
  59. data/test/load/unicorn/load.rb +23 -0
  60. data/test/replica_sets/connect_test.rb +86 -0
  61. data/test/replica_sets/connection_string_test.rb +32 -0
  62. data/test/replica_sets/count_test.rb +35 -0
  63. data/test/replica_sets/insert_test.rb +53 -0
  64. data/test/replica_sets/pooled_insert_test.rb +55 -0
  65. data/test/replica_sets/query_secondaries.rb +96 -0
  66. data/test/replica_sets/query_test.rb +51 -0
  67. data/test/replica_sets/replication_ack_test.rb +66 -0
  68. data/test/replica_sets/rs_test_helper.rb +27 -0
  69. data/test/safe_test.rb +68 -0
  70. data/test/support/hash_with_indifferent_access.rb +199 -0
  71. data/test/support/keys.rb +45 -0
  72. data/test/support_test.rb +19 -0
  73. data/test/test_helper.rb +83 -0
  74. data/test/threading/threading_with_large_pool_test.rb +90 -0
  75. data/test/threading_test.rb +87 -0
  76. data/test/tools/auth_repl_set_manager.rb +14 -0
  77. data/test/tools/repl_set_manager.rb +266 -0
  78. data/test/unit/collection_test.rb +130 -0
  79. data/test/unit/connection_test.rb +98 -0
  80. data/test/unit/cursor_test.rb +99 -0
  81. data/test/unit/db_test.rb +96 -0
  82. data/test/unit/grid_test.rb +49 -0
  83. data/test/unit/pool_test.rb +9 -0
  84. data/test/unit/repl_set_connection_test.rb +72 -0
  85. data/test/unit/safe_test.rb +125 -0
  86. data/test/uri_test.rb +91 -0
  87. metadata +202 -0
@@ -0,0 +1,99 @@
1
+ require './test/test_helper'
2
+
3
+ class CursorTest < Test::Unit::TestCase
4
+ context "Cursor options" do
5
+ setup do
6
+ @logger = mock()
7
+ @logger.stubs(:debug)
8
+ @connection = stub(:class => Connection, :logger => @logger)
9
+ @db = stub(:name => "testing", :slave_ok? => false, :connection => @connection)
10
+ @collection = stub(:db => @db, :name => "items")
11
+ @cursor = Cursor.new(@collection)
12
+ end
13
+
14
+ should "set timeout" do
15
+ assert_equal true, @cursor.timeout
16
+ end
17
+
18
+ should "set selector" do
19
+ assert @cursor.selector == {}
20
+
21
+ @cursor = Cursor.new(@collection, :selector => {:name => "Jones"})
22
+ assert @cursor.selector == {:name => "Jones"}
23
+ end
24
+
25
+ should "set fields" do
26
+ assert_nil @cursor.fields
27
+
28
+ @cursor = Cursor.new(@collection, :fields => [:name, :date])
29
+ assert @cursor.fields == {:name => 1, :date => 1}
30
+ end
31
+
32
+ should "set mix fields 0 and 1" do
33
+ assert_nil @cursor.fields
34
+
35
+ @cursor = Cursor.new(@collection, :fields => {:name => 1, :date => 0})
36
+ assert @cursor.fields == {:name => 1, :date => 0}
37
+ end
38
+
39
+ should "set limit" do
40
+ assert_equal 0, @cursor.limit
41
+
42
+ @cursor = Cursor.new(@collection, :limit => 10)
43
+ assert_equal 10, @cursor.limit
44
+ end
45
+
46
+
47
+ should "set skip" do
48
+ assert_equal 0, @cursor.skip
49
+
50
+ @cursor = Cursor.new(@collection, :skip => 5)
51
+ assert_equal 5, @cursor.skip
52
+ end
53
+
54
+ should "set sort order" do
55
+ assert_nil @cursor.order
56
+
57
+ @cursor = Cursor.new(@collection, :order => "last_name")
58
+ assert_equal "last_name", @cursor.order
59
+ end
60
+
61
+ should "set hint" do
62
+ assert_nil @cursor.hint
63
+
64
+ @cursor = Cursor.new(@collection, :hint => "name")
65
+ assert_equal "name", @cursor.hint
66
+ end
67
+
68
+ should "cache full collection name" do
69
+ assert_equal "testing.items", @cursor.full_collection_name
70
+ end
71
+ end
72
+
73
+ context "Query fields" do
74
+ setup do
75
+ @connection = stub(:class => Collection, :logger => @logger)
76
+ @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection)
77
+ @collection = stub(:db => @db, :name => "items")
78
+ end
79
+
80
+ should "when an array should return a hash with each key" do
81
+ @cursor = Cursor.new(@collection, :fields => [:name, :age])
82
+ result = @cursor.fields
83
+ assert_equal result.keys.sort{|a,b| a.to_s <=> b.to_s}, [:age, :name].sort{|a,b| a.to_s <=> b.to_s}
84
+ assert result.values.all? {|v| v == 1}
85
+ end
86
+
87
+ should "when a string, return a hash with just the key" do
88
+ @cursor = Cursor.new(@collection, :fields => "name")
89
+ result = @cursor.fields
90
+ assert_equal result.keys.sort, ["name"]
91
+ assert result.values.all? {|v| v == 1}
92
+ end
93
+
94
+ should "return nil when neither hash nor string nor symbol" do
95
+ @cursor = Cursor.new(@collection, :fields => 1234567)
96
+ assert_nil @cursor.fields
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,96 @@
1
+ require './test/test_helper'
2
+
3
+ class DBTest < Test::Unit::TestCase
4
+ context "DBTest: " do
5
+ setup do
6
+ def insert_message(db, documents)
7
+ documents = [documents] unless documents.is_a?(Array)
8
+ message = ByteBuffer.new
9
+ message.put_int(0)
10
+ Mongo::BSON_CODER..serialize_cstr(message, "#{db.name}.test")
11
+ documents.each { |doc| message.put_array(Mongo::BSON_CODER.new.serialize(doc, true).to_a) }
12
+ message = db.add_message_headers(Mongo::Constants::OP_INSERT, message)
13
+ end
14
+ end
15
+
16
+ context "DB commands" do
17
+ setup do
18
+ @conn = stub()
19
+ @conn.stubs(:safe)
20
+ @db = DB.new("testing", @conn)
21
+ @db.stubs(:safe)
22
+ @collection = mock()
23
+ @db.stubs(:system_command_collection).returns(@collection)
24
+ end
25
+
26
+ should "raise an error if given a hash with more than one key" do
27
+ if RUBY_VERSION < '1.9'
28
+ assert_raise MongoArgumentError do
29
+ @db.command(:buildinfo => 1, :somekey => 1)
30
+ end
31
+ end
32
+ end
33
+
34
+ should "raise an error if the selector is omitted" do
35
+ assert_raise MongoArgumentError do
36
+ @db.command({}, :check_response => true)
37
+ end
38
+ end
39
+
40
+ should "create the proper cursor" do
41
+ @cursor = mock(:next_document => {"ok" => 1})
42
+ Cursor.expects(:new).with(@collection,
43
+ :limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
44
+ command = {:buildinfo => 1}
45
+ @db.command(command, :check_response => true)
46
+ end
47
+
48
+ should "raise an error when the command fails" do
49
+ @cursor = mock(:next_document => {"ok" => 0})
50
+ Cursor.expects(:new).with(@collection,
51
+ :limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
52
+ assert_raise OperationFailure do
53
+ command = {:buildinfo => 1}
54
+ @db.command(command, :check_response => true)
55
+ end
56
+ end
57
+
58
+ should "raise an error if logging out fails" do
59
+ @db.expects(:command).returns({})
60
+ @conn.expects(:pool_size).returns(1)
61
+ assert_raise Mongo::MongoDBError do
62
+ @db.logout
63
+ end
64
+ end
65
+
66
+ should "raise an error if collection creation fails" do
67
+ @db.expects(:collection_names).returns([])
68
+ @db.expects(:command).returns({'ok' => 0})
69
+ assert_raise Mongo::MongoDBError do
70
+ @db.create_collection("foo")
71
+ end
72
+ end
73
+
74
+ should "raise an error if getlasterror fails" do
75
+ @db.expects(:command).returns({})
76
+ assert_raise Mongo::MongoDBError do
77
+ @db.get_last_error
78
+ end
79
+ end
80
+
81
+ should "raise an error if drop_index fails" do
82
+ @db.expects(:command).returns({})
83
+ assert_raise Mongo::MongoDBError do
84
+ @db.drop_index("foo", "bar")
85
+ end
86
+ end
87
+
88
+ should "raise an error if set_profiling_level fails" do
89
+ @db.expects(:command).returns({})
90
+ assert_raise Mongo::MongoDBError do
91
+ @db.profiling_level = :slow_only
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,49 @@
1
+ require './test/test_helper'
2
+
3
+ class GridTest < Test::Unit::TestCase
4
+
5
+ context "GridFS: " do
6
+ setup do
7
+ @conn = stub()
8
+ @conn.stubs(:safe)
9
+ @db = DB.new("testing", @conn)
10
+ @files = mock()
11
+ @chunks = mock()
12
+
13
+ @db.expects(:[]).with('fs.files').returns(@files)
14
+ @db.expects(:[]).with('fs.chunks').returns(@chunks)
15
+ @db.stubs(:safe)
16
+ end
17
+
18
+ context "Grid classe with standard connections" do
19
+ setup do
20
+ @conn.expects(:slave_ok?).returns(false)
21
+ end
22
+
23
+ should "create indexes for Grid" do
24
+ @chunks.expects(:create_index)
25
+ Grid.new(@db)
26
+ end
27
+
28
+ should "create indexes for GridFileSystem" do
29
+ @files.expects(:create_index)
30
+ @chunks.expects(:create_index)
31
+ GridFileSystem.new(@db)
32
+ end
33
+ end
34
+
35
+ context "Grid classes with slave connection" do
36
+ setup do
37
+ @conn.expects(:slave_ok?).returns(true)
38
+ end
39
+
40
+ should "not create indexes for Grid" do
41
+ Grid.new(@db)
42
+ end
43
+
44
+ should "not create indexes for GridFileSystem" do
45
+ GridFileSystem.new(@db)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ require './test/test_helper'
2
+ include Mongo
3
+
4
+ class PoolTest < Test::Unit::TestCase
5
+ context "Initialization: " do
6
+ should "do" do
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,72 @@
1
+ require './test/test_helper'
2
+ include Mongo
3
+
4
+ class ReplSetConnectionTest < Test::Unit::TestCase
5
+ context "Initialization: " do
6
+ setup do
7
+ def new_mock_socket(host='localhost', port=27017)
8
+ socket = Object.new
9
+ socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
10
+ socket.stubs(:close)
11
+ socket
12
+ end
13
+
14
+ def new_mock_db
15
+ db = Object.new
16
+ end
17
+ end
18
+
19
+ context "connecting to a replica set" do
20
+ setup do
21
+ TCPSocket.stubs(:new).returns(new_mock_socket('localhost', 27017))
22
+ @conn = ReplSetConnection.new(['localhost', 27017], :connect => false, :read_secondary => true)
23
+
24
+ admin_db = new_mock_db
25
+ @hosts = ['localhost:27018', 'localhost:27019', 'localhost:27020']
26
+
27
+ admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1, 'hosts' => @hosts}).
28
+ then.returns({'ok' => 1, 'ismaster' => 0, 'hosts' => @hosts, 'secondary' => 1}).
29
+ then.returns({'ok' => 1, 'ismaster' => 0, 'hosts' => @hosts, 'secondary' => 1}).
30
+ then.returns({'ok' => 1, 'ismaster' => 0, 'arbiterOnly' => 1})
31
+
32
+ @conn.stubs(:[]).with('admin').returns(admin_db)
33
+ @conn.connect
34
+ end
35
+
36
+ should "store the hosts returned from the ismaster command" do
37
+ assert_equal 'localhost', @conn.primary_pool.host
38
+ assert_equal 27017, @conn.primary_pool.port
39
+
40
+ assert_equal 'localhost', @conn.secondary_pools[0].host
41
+ assert_equal 27018, @conn.secondary_pools[0].port
42
+
43
+ assert_equal 'localhost', @conn.secondary_pools[1].host
44
+ assert_equal 27019, @conn.secondary_pools[1].port
45
+
46
+ assert_equal 2, @conn.secondary_pools.length
47
+ end
48
+ end
49
+
50
+ context "connecting to a replica set and providing seed nodes" do
51
+ setup do
52
+ TCPSocket.stubs(:new).returns(new_mock_socket)
53
+ @conn = ReplSetConnection.new(['localhost', 27017], ['localhost', 27019], :connect => false)
54
+
55
+ admin_db = new_mock_db
56
+ @hosts = ['localhost:27017', 'localhost:27018', 'localhost:27019']
57
+ admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1, 'hosts' => @hosts})
58
+ @conn.stubs(:[]).with('admin').returns(admin_db)
59
+ @conn.connect
60
+ end
61
+ end
62
+
63
+ context "initializing with a mongodb uri" do
64
+
65
+ should "parse a uri specifying multiple nodes" do
66
+ @conn = Connection.from_uri("mongodb://localhost:27017,mydb.com:27018", :connect => false)
67
+ assert_equal ['localhost', 27017], @conn.nodes[0]
68
+ assert_equal ['mydb.com', 27018], @conn.nodes[1]
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path('./test/test_helper.rb')
2
+
3
+ class SafeTest < Test::Unit::TestCase
4
+
5
+ context "Safe mode on connection: " do
6
+ setup do
7
+ @safe_value = {:w => 7}
8
+ @con = Mongo::Connection.new('localhost', 27017, :safe => @safe_value, :connect => false)
9
+ end
10
+
11
+ should "propogate to DB" do
12
+ db = @con['foo']
13
+ assert_equal @safe_value, db.safe
14
+
15
+
16
+ db = @con.db('foo')
17
+ assert_equal @safe_value, db.safe
18
+
19
+ db = DB.new('foo', @con)
20
+ assert_equal @safe_value, db.safe
21
+ end
22
+
23
+ should "allow db override" do
24
+ db = DB.new('foo', @con, :safe => false)
25
+ assert_equal false, db.safe
26
+
27
+ db = @con.db('foo', :safe => false)
28
+ assert_equal false, db.safe
29
+ end
30
+
31
+ context "on DB: " do
32
+ setup do
33
+ @db = @con['foo']
34
+ end
35
+
36
+ should "propogate to collection" do
37
+ col = @db.collection('bar')
38
+ assert_equal @safe_value, col.safe
39
+
40
+ col = @db['bar']
41
+ assert_equal @safe_value, col.safe
42
+
43
+ col = Collection.new('bar', @db)
44
+ assert_equal @safe_value, col.safe
45
+ end
46
+
47
+ should "allow override on collection" do
48
+ col = @db.collection('bar', :safe => false)
49
+ assert_equal false, col.safe
50
+
51
+ col = Collection.new('bar', @db, :safe => false)
52
+ assert_equal false, col.safe
53
+ end
54
+ end
55
+
56
+ context "on operations supporting safe mode" do
57
+ setup do
58
+ @col = @con['foo']['bar']
59
+ end
60
+
61
+ should "use default value on insert" do
62
+ @con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
63
+ safe == @safe_value
64
+ end
65
+
66
+ @col.insert({:a => 1})
67
+ end
68
+
69
+ should "allow override alternate value on insert" do
70
+ @con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
71
+ safe == {:w => 100}
72
+ end
73
+
74
+ @col.insert({:a => 1}, :safe => {:w => 100})
75
+ end
76
+
77
+ should "allow override to disable on insert" do
78
+ @con.expects(:send_message)
79
+ @col.insert({:a => 1}, :safe => false)
80
+ end
81
+
82
+ should "use default value on update" do
83
+ @con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
84
+ safe == @safe_value
85
+ end
86
+
87
+ @col.update({:a => 1}, {:a => 2})
88
+ end
89
+
90
+ should "allow override alternate value on update" do
91
+ @con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
92
+ safe == {:w => 100}
93
+ end
94
+
95
+ @col.update({:a => 1}, {:a => 2}, :safe => {:w => 100})
96
+ end
97
+
98
+ should "allow override to disable on update" do
99
+ @con.expects(:send_message)
100
+ @col.update({:a => 1}, {:a => 2}, :safe => false)
101
+ end
102
+
103
+ should "use default value on remove" do
104
+ @con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
105
+ safe == @safe_value
106
+ end
107
+
108
+ @col.remove
109
+ end
110
+
111
+ should "allow override alternate value on remove" do
112
+ @con.expects(:send_message_with_safe_check).with do |op, msg, log, n, safe|
113
+ safe == {:w => 100}
114
+ end
115
+
116
+ @col.remove({}, :safe => {:w => 100})
117
+ end
118
+
119
+ should "allow override to disable on remove" do
120
+ @con.expects(:send_message)
121
+ @col.remove({}, :safe => false)
122
+ end
123
+ end
124
+ end
125
+ end