mongo 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/{LICENSE.txt → LICENSE} +0 -0
  2. data/README.md +124 -111
  3. data/Rakefile +9 -325
  4. data/VERSION +1 -0
  5. data/bin/mongo_console +4 -4
  6. data/examples/admin.rb +43 -0
  7. data/examples/capped.rb +22 -0
  8. data/examples/cursor.rb +48 -0
  9. data/examples/gridfs.rb +44 -0
  10. data/examples/index_test.rb +126 -0
  11. data/examples/info.rb +31 -0
  12. data/examples/queries.rb +74 -0
  13. data/examples/replica_set.rb +26 -0
  14. data/examples/simple.rb +25 -0
  15. data/examples/strict.rb +35 -0
  16. data/examples/types.rb +36 -0
  17. data/{test/load → examples/web}/thin/load.rb +3 -1
  18. data/{test/load → examples/web}/unicorn/load.rb +5 -3
  19. data/lib/mongo.rb +8 -10
  20. data/lib/mongo/collection.rb +134 -114
  21. data/lib/mongo/cursor.rb +21 -14
  22. data/lib/mongo/db.rb +30 -28
  23. data/lib/mongo/exceptions.rb +1 -1
  24. data/lib/mongo/gridfs/grid.rb +8 -7
  25. data/lib/mongo/gridfs/grid_ext.rb +1 -1
  26. data/lib/mongo/gridfs/grid_file_system.rb +6 -5
  27. data/lib/mongo/gridfs/grid_io.rb +22 -19
  28. data/lib/mongo/legacy.rb +82 -0
  29. data/lib/mongo/{connection.rb → mongo_client.rb} +82 -61
  30. data/lib/mongo/{repl_set_connection.rb → mongo_replica_set_client.rb} +54 -39
  31. data/lib/mongo/{sharded_connection.rb → mongo_sharded_client.rb} +9 -9
  32. data/lib/mongo/networking.rb +25 -20
  33. data/lib/mongo/util/conversions.rb +1 -1
  34. data/lib/mongo/util/core_ext.rb +1 -1
  35. data/lib/mongo/util/logging.rb +20 -4
  36. data/lib/mongo/util/node.rb +16 -16
  37. data/lib/mongo/util/pool.rb +56 -27
  38. data/lib/mongo/util/pool_manager.rb +28 -27
  39. data/lib/mongo/util/server_version.rb +1 -1
  40. data/lib/mongo/util/sharding_pool_manager.rb +8 -8
  41. data/lib/mongo/util/ssl_socket.rb +1 -5
  42. data/lib/mongo/util/support.rb +24 -8
  43. data/lib/mongo/util/tcp_socket.rb +0 -4
  44. data/lib/mongo/util/uri_parser.rb +54 -38
  45. data/lib/mongo/util/write_concern.rb +67 -0
  46. data/mongo.gemspec +21 -32
  47. data/test/auxillary/{1.4_features.rb → 1.4_feature_test.rb} +4 -5
  48. data/test/auxillary/authentication_test.rb +18 -20
  49. data/test/auxillary/autoreconnect_test.rb +3 -5
  50. data/test/auxillary/fork_test.rb +5 -7
  51. data/test/auxillary/repl_set_auth_test.rb +13 -15
  52. data/test/auxillary/slave_connection_test.rb +8 -7
  53. data/test/auxillary/threaded_authentication_test.rb +15 -17
  54. data/test/bson/binary_test.rb +1 -1
  55. data/test/bson/bson_test.rb +60 -36
  56. data/test/bson/byte_buffer_test.rb +1 -1
  57. data/test/bson/hash_with_indifferent_access_test.rb +2 -2
  58. data/test/bson/json_test.rb +1 -2
  59. data/test/bson/object_id_test.rb +1 -2
  60. data/test/bson/ordered_hash_test.rb +1 -1
  61. data/test/bson/timestamp_test.rb +1 -1
  62. data/test/{collection_test.rb → functional/collection_test.rb} +57 -57
  63. data/test/{connection_test.rb → functional/connection_test.rb} +75 -89
  64. data/test/{conversions_test.rb → functional/conversions_test.rb} +1 -1
  65. data/test/{cursor_fail_test.rb → functional/cursor_fail_test.rb} +3 -29
  66. data/test/{cursor_message_test.rb → functional/cursor_message_test.rb} +1 -1
  67. data/test/{cursor_test.rb → functional/cursor_test.rb} +5 -1
  68. data/test/{db_api_test.rb → functional/db_api_test.rb} +8 -9
  69. data/test/{db_connection_test.rb → functional/db_connection_test.rb} +3 -5
  70. data/test/{db_test.rb → functional/db_test.rb} +13 -13
  71. data/test/{grid_file_system_test.rb → functional/grid_file_system_test.rb} +2 -2
  72. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +6 -6
  73. data/test/{grid_test.rb → functional/grid_test.rb} +4 -10
  74. data/test/{pool_test.rb → functional/pool_test.rb} +1 -1
  75. data/test/functional/safe_test.rb +84 -0
  76. data/test/{support_test.rb → functional/support_test.rb} +1 -1
  77. data/test/{threading_test.rb → functional/threading_test.rb} +9 -9
  78. data/test/{timeout_test.rb → functional/timeout_test.rb} +1 -1
  79. data/test/{uri_test.rb → functional/uri_test.rb} +1 -1
  80. data/test/functional/write_concern_test.rb +104 -0
  81. data/test/replica_set/basic_test.rb +139 -0
  82. data/test/replica_set/client_test.rb +255 -0
  83. data/test/replica_set/complex_connect_test.rb +62 -0
  84. data/test/replica_set/connection_test.rb +255 -0
  85. data/test/{replica_sets → replica_set}/count_test.rb +17 -14
  86. data/test/replica_set/cursor_test.rb +75 -0
  87. data/test/{replica_sets → replica_set}/insert_test.rb +19 -16
  88. data/test/replica_set/query_test.rb +64 -0
  89. data/test/replica_set/refresh_test.rb +153 -0
  90. data/test/{replica_sets → replica_set}/replication_ack_test.rb +21 -17
  91. data/test/sharded_cluster/basic_test.rb +31 -50
  92. data/test/support/hash_with_indifferent_access.rb +1 -1
  93. data/test/test_helper.rb +56 -9
  94. data/test/threading/threading_with_large_pool_test.rb +8 -8
  95. data/test/tools/mongo_config.rb +270 -58
  96. data/test/tools/mongo_config_test.rb +146 -0
  97. data/test/unit/client_test.rb +230 -0
  98. data/test/unit/collection_test.rb +45 -32
  99. data/test/unit/connection_test.rb +82 -74
  100. data/test/unit/cursor_test.rb +14 -6
  101. data/test/unit/db_test.rb +8 -8
  102. data/test/unit/grid_test.rb +11 -11
  103. data/test/unit/node_test.rb +24 -24
  104. data/test/unit/pool_manager_test.rb +13 -13
  105. data/test/unit/pool_test.rb +1 -1
  106. data/test/unit/read_test.rb +21 -26
  107. data/test/unit/safe_test.rb +52 -33
  108. data/test/unit/util_test.rb +55 -0
  109. data/test/unit/write_concern_test.rb +161 -0
  110. metadata +158 -171
  111. data/docs/CREDITS.md +0 -123
  112. data/docs/FAQ.md +0 -116
  113. data/docs/GRID_FS.md +0 -158
  114. data/docs/HISTORY.md +0 -392
  115. data/docs/READ_PREFERENCE.md +0 -99
  116. data/docs/RELEASES.md +0 -54
  117. data/docs/REPLICA_SETS.md +0 -113
  118. data/docs/TAILABLE_CURSORS.md +0 -51
  119. data/docs/TUTORIAL.md +0 -356
  120. data/docs/WRITE_CONCERN.md +0 -31
  121. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  122. data/lib/mongo/version.rb +0 -3
  123. data/test/bson/test_helper.rb +0 -30
  124. data/test/replica_sets/basic_test.rb +0 -119
  125. data/test/replica_sets/complex_connect_test.rb +0 -57
  126. data/test/replica_sets/complex_read_preference_test.rb +0 -237
  127. data/test/replica_sets/connect_test.rb +0 -156
  128. data/test/replica_sets/cursor_test.rb +0 -70
  129. data/test/replica_sets/pooled_insert_test.rb +0 -57
  130. data/test/replica_sets/query_test.rb +0 -50
  131. data/test/replica_sets/read_preference_test.rb +0 -234
  132. data/test/replica_sets/refresh_test.rb +0 -156
  133. data/test/replica_sets/refresh_with_threads_test.rb +0 -60
  134. data/test/replica_sets/rs_test_helper.rb +0 -39
  135. data/test/safe_test.rb +0 -68
  136. data/test/sharded_cluster/mongo_config_test.rb +0 -126
  137. data/test/sharded_cluster/sc_test_helper.rb +0 -39
  138. data/test/tools/repl_set_manager.rb +0 -418
@@ -0,0 +1,146 @@
1
+ require 'test_helper'
2
+
3
+ class MongoConfig < Test::Unit::TestCase
4
+
5
+ def startup
6
+ @sys_proc = nil
7
+ end
8
+
9
+ def shutdown
10
+ @sys_proc.stop if @sys_proc && @sys_proc.running?
11
+ end
12
+
13
+ test "config defaults" do
14
+ [ Mongo::Config::DEFAULT_BASE_OPTS,
15
+ Mongo::Config::DEFAULT_REPLICA_SET,
16
+ Mongo::Config::DEFAULT_SHARDED_SIMPLE,
17
+ Mongo::Config::DEFAULT_SHARDED_REPLICA
18
+ ].each do |params|
19
+ config = Mongo::Config.cluster(params)
20
+ assert(config.size > 0)
21
+ end
22
+ end
23
+
24
+ test "get available port" do
25
+ assert_not_nil(Mongo::Config.get_available_port)
26
+ end
27
+
28
+ test "SysProc start" do
29
+ cmd = "true"
30
+ @sys_proc = Mongo::Config::SysProc.new(cmd)
31
+ assert_equal(cmd, @sys_proc.cmd)
32
+ assert_nil(@sys_proc.pid)
33
+ start_and_assert_running?(@sys_proc)
34
+ end
35
+
36
+ test "SysProc wait" do
37
+ @sys_proc = Mongo::Config::SysProc.new("true")
38
+ start_and_assert_running?(@sys_proc)
39
+ assert(@sys_proc.running?)
40
+ @sys_proc.wait
41
+ assert(!@sys_proc.running?)
42
+ end
43
+
44
+ test "SysProc kill" do
45
+ @sys_proc = Mongo::Config::SysProc.new("true")
46
+ start_and_assert_running?(@sys_proc)
47
+ @sys_proc.kill
48
+ @sys_proc.wait
49
+ assert(!@sys_proc.running?)
50
+ end
51
+
52
+ test "SysProc stop" do
53
+ @sys_proc = Mongo::Config::SysProc.new("true")
54
+ start_and_assert_running?(@sys_proc)
55
+ @sys_proc.stop
56
+ assert(!@sys_proc.running?)
57
+ end
58
+
59
+ test "SysProc zombie respawn" do
60
+ @sys_proc = Mongo::Config::SysProc.new("true")
61
+ start_and_assert_running?(@sys_proc)
62
+ prev_pid = @sys_proc.pid
63
+ @sys_proc.kill
64
+ # don't wait, leaving a zombie
65
+ assert(@sys_proc.running?)
66
+ start_and_assert_running?(@sys_proc)
67
+ assert(prev_pid && @sys_proc.pid && prev_pid != @sys_proc.pid, 'SysProc#start should spawn a new process after a zombie')
68
+ @sys_proc.stop
69
+ assert(!@sys_proc.running?)
70
+ end
71
+
72
+ test "Server" do
73
+ server = Mongo::Config::Server.new('a cmd', 'host', 1234)
74
+ assert_equal('a cmd', server.cmd)
75
+ assert_equal('host', server.host)
76
+ assert_equal(1234, server.port)
77
+ end
78
+
79
+ test "DbServer" do
80
+ config = Mongo::Config::DEFAULT_BASE_OPTS
81
+ server = Mongo::Config::DbServer.new(config)
82
+ assert_equal(config, server.config)
83
+ assert_equal("mongod --dbpath data --logpath data/log", server.cmd)
84
+ assert_equal(config[:host], server.host)
85
+ assert_equal(config[:port], server.port)
86
+ end
87
+
88
+ def cluster_test(opts)
89
+ #debug 1, opts.inspect
90
+ config = Mongo::Config.cluster(opts)
91
+ #debug 1, config.inspect
92
+ manager = Mongo::Config::ClusterManager.new(config)
93
+ assert_equal(config, manager.config)
94
+ manager.start
95
+ yield manager
96
+ manager.stop
97
+ manager.servers.each{|s| assert(!s.running?)}
98
+ manager.clobber
99
+ end
100
+
101
+ test "cluster manager base" do
102
+ cluster_test(Mongo::Config::DEFAULT_BASE_OPTS) do |manager|
103
+
104
+ end
105
+ end
106
+
107
+ test "cluster manager replica set" do
108
+ cluster_test(Mongo::Config::DEFAULT_REPLICA_SET) do |manager|
109
+ servers = manager.servers
110
+ servers.each do |server|
111
+ assert_not_nil(Mongo::MongoClient.new(server.host, server.port))
112
+ assert_match(/oplogSize/, server.cmd, '--oplogSize option should be specified')
113
+ assert_match(/smallfiles/, server.cmd, '--smallfiles option should be specified')
114
+ assert_no_match(/nojournal/, server.cmd, '--nojournal option should not be specified')
115
+ assert_match(/noprealloc/, server.cmd, '--noprealloc option should be specified')
116
+ end
117
+ end
118
+ end
119
+
120
+ test "cluster manager sharded simple" do
121
+ cluster_test(Mongo::Config::DEFAULT_SHARDED_SIMPLE) do |manager|
122
+ servers = manager.shards + manager.configs
123
+ servers.each do |server|
124
+ assert_not_nil(Mongo::MongoClient.new(server.host, server.port))
125
+ assert_match(/oplogSize/, server.cmd, '--oplogSize option should be specified')
126
+ assert_match(/smallfiles/, server.cmd, '--smallfiles option should be specified')
127
+ assert_no_match(/nojournal/, server.cmd, '--nojournal option should not be specified')
128
+ assert_match(/noprealloc/, server.cmd, '--noprealloc option should be specified')
129
+ end
130
+ end
131
+ end
132
+
133
+ test "cluster manager sharded replica" do
134
+ #cluster_test(Mongo::Config::DEFAULT_SHARDED_REPLICA) # not yet supported by ClusterManager
135
+ end
136
+
137
+ private
138
+
139
+ def start_and_assert_running?(sys_proc)
140
+ assert_not_nil(sys_proc.start(0))
141
+ assert_not_nil(sys_proc.pid)
142
+ assert(sys_proc.running?)
143
+ end
144
+
145
+ end
146
+
@@ -0,0 +1,230 @@
1
+ require 'test_helper'
2
+ include Mongo
3
+
4
+ class ClientTest < Test::Unit::TestCase
5
+ context "Mongo::MongoClient intialization " do
6
+ context "given a single node" do
7
+ setup do
8
+ @client = MongoClient.new('localhost', 27017, :connect => false)
9
+ TCPSocket.stubs(:new).returns(new_mock_socket)
10
+
11
+ admin_db = new_mock_db
12
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
13
+ @client.expects(:[]).with('admin').returns(admin_db)
14
+ @client.connect
15
+ end
16
+
17
+ should "gle writes by default" do
18
+ assert_equal 1, @client.write_concern[:w]
19
+ end
20
+
21
+ should "set localhost and port to master" do
22
+ assert_equal 'localhost', @client.primary_pool.host
23
+ assert_equal 27017, @client.primary_pool.port
24
+ end
25
+
26
+ should "set connection pool to 1" do
27
+ assert_equal 1, @client.primary_pool.size
28
+ end
29
+
30
+ should "default slave_ok to false" do
31
+ assert !@client.slave_ok?
32
+ end
33
+
34
+ should "raise exception for invalid host or port" do
35
+ assert_raise MongoArgumentError do
36
+ MongoClient.new(:w => 1)
37
+ end
38
+ assert_raise MongoArgumentError do
39
+ MongoClient.new('localhost', :w => 1)
40
+ end
41
+ end
42
+
43
+ should "warn if invalid options are specified" do
44
+ client = MongoClient.allocate
45
+ opts = {:connect => false}
46
+
47
+ MongoReplicaSetClient::REPL_SET_OPTS.each do |opt|
48
+ client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
49
+ opts[opt] = true
50
+ end
51
+
52
+ args = ['localhost', 27017, opts]
53
+ client.send(:initialize, *args)
54
+ end
55
+
56
+ context "given a replica set" do
57
+
58
+ should "warn if invalid options are specified" do
59
+ client = MongoReplicaSetClient.allocate
60
+ opts = {:connect => false}
61
+
62
+ MongoClient::CLIENT_ONLY_OPTS.each do |opt|
63
+ client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
64
+ opts[opt] = true
65
+ end
66
+
67
+ args = [['localhost:27017'], opts]
68
+ client.send(:initialize, *args)
69
+ end
70
+ end
71
+ end
72
+
73
+ context "initializing with a mongodb uri" do
74
+ should "parse a simple uri" do
75
+ @client = MongoClient.from_uri("mongodb://localhost", :connect => false)
76
+ assert_equal ['localhost', 27017], @client.host_to_try
77
+ end
78
+
79
+ should "allow a complex host names" do
80
+ host_name = "foo.bar-12345.org"
81
+ @client = MongoClient.from_uri("mongodb://#{host_name}", :connect => false)
82
+ assert_equal [host_name, 27017], @client.host_to_try
83
+ end
84
+
85
+ should "allow db without username and password" do
86
+ host_name = "foo.bar-12345.org"
87
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo", :connect => false)
88
+ assert_equal [host_name, 27017], @client.host_to_try
89
+ end
90
+
91
+ should "set write concern options on connection" do
92
+ host_name = "localhost"
93
+ opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true"
94
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
95
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
96
+ end
97
+
98
+ should "set timeout options on connection" do
99
+ host_name = "localhost"
100
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
101
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
102
+ assert_equal 1, @client.connect_timeout
103
+ assert_equal 5, @client.op_timeout
104
+ end
105
+
106
+ should "parse a uri with a hyphen & underscore in the username or password" do
107
+ @client = MongoClient.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
108
+ assert_equal ['localhost', 27017], @client.host_to_try
109
+ auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
110
+ assert_equal auth_hash, @client.auths[0]
111
+ end
112
+
113
+ should "attempt to connect" do
114
+ TCPSocket.stubs(:new).returns(new_mock_socket)
115
+ @client = MongoClient.from_uri("mongodb://localhost", :connect => false)
116
+
117
+ admin_db = new_mock_db
118
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
119
+ @client.expects(:[]).with('admin').returns(admin_db)
120
+ @client.connect
121
+ end
122
+
123
+ should "raise an error on invalid uris" do
124
+ assert_raise MongoArgumentError do
125
+ MongoClient.from_uri("mongo://localhost", :connect => false)
126
+ end
127
+
128
+ assert_raise MongoArgumentError do
129
+ MongoClient.from_uri("mongodb://localhost:abc", :connect => false)
130
+ end
131
+ end
132
+
133
+ should "require all of username, if password and db are specified" do
134
+ assert MongoClient.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
135
+
136
+ assert_raise MongoArgumentError do
137
+ MongoClient.from_uri("mongodb://kyle:password@localhost", :connect => false)
138
+ end
139
+ end
140
+ end
141
+
142
+ context "initializing with ENV['MONGODB_URI']" do
143
+ setup do
144
+ @old_mongodb_uri = ENV['MONGODB_URI']
145
+ end
146
+
147
+ teardown do
148
+ ENV['MONGODB_URI'] = @old_mongodb_uri
149
+ end
150
+
151
+ should "parse a simple uri" do
152
+ ENV['MONGODB_URI'] = "mongodb://localhost?connect=false"
153
+ @client = MongoClient.new
154
+ assert_equal ['localhost', 27017], @client.host_to_try
155
+ end
156
+
157
+ should "allow a complex host names" do
158
+ host_name = "foo.bar-12345.org"
159
+ ENV['MONGODB_URI'] = "mongodb://#{host_name}?connect=false"
160
+ @client = MongoClient.new
161
+ assert_equal [host_name, 27017], @client.host_to_try
162
+ end
163
+
164
+ should "allow db without username and password" do
165
+ host_name = "foo.bar-12345.org"
166
+ ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?connect=false"
167
+ @client = MongoClient.new
168
+ assert_equal [host_name, 27017], @client.host_to_try
169
+ end
170
+
171
+ should "set write concern options on connection" do
172
+ host_name = "localhost"
173
+ opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
174
+ ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
175
+ @client = MongoClient.new
176
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
177
+ end
178
+
179
+ should "set timeout options on connection" do
180
+ host_name = "localhost"
181
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
182
+ ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?#{opts}"
183
+ @client = MongoClient.new
184
+ assert_equal 1, @client.connect_timeout
185
+ assert_equal 5, @client.op_timeout
186
+ end
187
+
188
+ should "parse a uri with a hyphen & underscore in the username or password" do
189
+ ENV['MONGODB_URI'] = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
190
+ @client = MongoClient.new
191
+ assert_equal ['localhost', 27017], @client.host_to_try
192
+ auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
193
+ assert_equal auth_hash, @client.auths[0]
194
+ end
195
+
196
+ should "attempt to connect" do
197
+ TCPSocket.stubs(:new).returns(new_mock_socket)
198
+ ENV['MONGODB_URI'] = "mongodb://localhost?connect=false" # connect=false ??
199
+ @client = MongoClient.new
200
+
201
+ admin_db = new_mock_db
202
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
203
+ @client.expects(:[]).with('admin').returns(admin_db)
204
+ @client.connect
205
+ end
206
+
207
+ should "raise an error on invalid uris" do
208
+ ENV['MONGODB_URI'] = "mongo://localhost"
209
+ assert_raise MongoArgumentError do
210
+ MongoClient.new
211
+ end
212
+
213
+ ENV['MONGODB_URI'] = "mongodb://localhost:abc"
214
+ assert_raise MongoArgumentError do
215
+ MongoClient.new
216
+ end
217
+ end
218
+
219
+ should "require all of username, if password and db are specified" do
220
+ ENV['MONGODB_URI'] = "mongodb://kyle:jones@localhost/db?connect=false"
221
+ assert MongoClient.new
222
+
223
+ ENV['MONGODB_URI'] = "mongodb://kyle:password@localhost"
224
+ assert_raise MongoArgumentError do
225
+ MongoClient.new
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path("../../test_helper", __FILE__)
1
+ require 'test_helper'
2
2
 
3
3
  class CollectionTest < Test::Unit::TestCase
4
4
 
@@ -10,10 +10,10 @@ class CollectionTest < Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  should "send update message" do
13
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
14
- @db = @conn['testing']
13
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
14
+ @db = @client['testing']
15
15
  @coll = @db.collection('books')
16
- @conn.expects(:send_message).with do |op, msg, log|
16
+ @client.expects(:send_message_with_gle).with do |op, msg, log|
17
17
  op == 2001
18
18
  end
19
19
  @coll.stubs(:log_operation)
@@ -21,10 +21,10 @@ class CollectionTest < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  should "send insert message" do
24
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
25
- @db = @conn['testing']
24
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
25
+ @db = @client['testing']
26
26
  @coll = @db.collection('books')
27
- @conn.expects(:send_message).with do |op, msg, log|
27
+ @client.expects(:send_message_with_gle).with do |op, msg, log|
28
28
  op == 2002
29
29
  end
30
30
  @coll.expects(:log_operation).with do |name, payload|
@@ -34,11 +34,11 @@ class CollectionTest < Test::Unit::TestCase
34
34
  end
35
35
 
36
36
  should "send sort data" do
37
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
38
- @db = @conn['testing']
37
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
38
+ @db = @client['testing']
39
39
  @coll = @db.collection('books')
40
- @conn.expects(:checkout_reader).returns(mock(:pool))
41
- @conn.expects(:receive_message).with do |op, msg, log, sock|
40
+ @client.expects(:checkout_reader).returns(mock(:pool))
41
+ @client.expects(:receive_message).with do |op, msg, log, sock|
42
42
  op == 2004
43
43
  end.returns([[], 0, 0])
44
44
  @logger.expects(:debug)
@@ -46,11 +46,11 @@ class CollectionTest < Test::Unit::TestCase
46
46
  end
47
47
 
48
48
  should "not log binary data" do
49
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
50
- @db = @conn['testing']
49
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
50
+ @db = @client['testing']
51
51
  @coll = @db.collection('books')
52
52
  data = BSON::Binary.new(("BINARY " * 1000).unpack("c*"))
53
- @conn.expects(:send_message).with do |op, msg, log|
53
+ @client.expects(:send_message_with_gle).with do |op, msg, log|
54
54
  op == 2002
55
55
  end
56
56
  @coll.expects(:log_operation).with do |name, payload|
@@ -60,32 +60,45 @@ class CollectionTest < Test::Unit::TestCase
60
60
  end
61
61
 
62
62
  should "send safe update message" do
63
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
64
- @db = @conn['testing']
63
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
64
+ @db = @client['testing']
65
65
  @coll = @db.collection('books')
66
- @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
66
+ @client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
67
67
  op == 2001
68
68
  end
69
69
  @coll.expects(:log_operation).with do |name, payload|
70
70
  (name == :update) && payload[:document][:title].include?('Moby')
71
71
  end
72
- @coll.update({}, {:title => 'Moby Dick'}, :safe => true)
72
+ @coll.update({}, {:title => 'Moby Dick'})
73
+ end
74
+
75
+ should "send safe update message with legacy" do
76
+ @connection = Connection.new('localhost', 27017, :logger => @logger, :safe => true, :connect => false)
77
+ @db = @connection['testing']
78
+ @coll = @db.collection('books')
79
+ @connection.expects(:send_message_with_gle).with do |op, msg, db_name, log|
80
+ op == 2001
81
+ end
82
+ @coll.expects(:log_operation).with do |name, payload|
83
+ (name == :update) && payload[:document][:title].include?('Moby')
84
+ end
85
+ @coll.update({}, {:title => 'Moby Dick'})
73
86
  end
74
87
 
75
88
  should "send safe insert message" do
76
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
77
- @db = @conn['testing']
89
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
90
+ @db = @client['testing']
78
91
  @coll = @db.collection('books')
79
- @conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
92
+ @client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
80
93
  op == 2001
81
94
  end
82
95
  @coll.stubs(:log_operation)
83
- @coll.update({}, {:title => 'Moby Dick'}, :safe => true)
96
+ @coll.update({}, {:title => 'Moby Dick'})
84
97
  end
85
98
 
86
99
  should "not call insert for each ensure_index call" do
87
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
88
- @db = @conn['testing']
100
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
101
+ @db = @client['testing']
89
102
  @coll = @db.collection('books')
90
103
  @coll.expects(:generate_indexes).once
91
104
 
@@ -94,8 +107,8 @@ class CollectionTest < Test::Unit::TestCase
94
107
  end
95
108
 
96
109
  should "call generate_indexes for a new direction on the same field for ensure_index" do
97
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
98
- @db = @conn['testing']
110
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
111
+ @db = @client['testing']
99
112
  @coll = @db.collection('books')
100
113
  @coll.expects(:generate_indexes).twice
101
114
 
@@ -105,8 +118,8 @@ class CollectionTest < Test::Unit::TestCase
105
118
  end
106
119
 
107
120
  should "call generate_indexes twice because the cache time is 0 seconds" do
108
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
109
- @db = @conn['testing']
121
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
122
+ @db = @client['testing']
110
123
  @db.cache_time = 0
111
124
  @coll = @db.collection('books')
112
125
  @coll.expects(:generate_indexes).twice
@@ -116,8 +129,8 @@ class CollectionTest < Test::Unit::TestCase
116
129
  end
117
130
 
118
131
  should "call generate_indexes for each key when calling ensure_indexes" do
119
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
120
- @db = @conn['testing']
132
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
133
+ @db = @client['testing']
121
134
  @db.cache_time = 300
122
135
  @coll = @db.collection('books')
123
136
  @coll.expects(:generate_indexes).once.with do |a, b, c|
@@ -128,8 +141,8 @@ class CollectionTest < Test::Unit::TestCase
128
141
  end
129
142
 
130
143
  should "use the connection's logger" do
131
- @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
132
- @db = @conn['testing']
144
+ @client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
145
+ @db = @client['testing']
133
146
  @coll = @db.collection('books')
134
147
  @logger.expects(:warn).with do |msg|
135
148
  msg == "MONGODB [WARNING] test warning"