mongo 1.8.0 → 1.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -29
- data/VERSION +1 -1
- data/lib/mongo.rb +3 -0
- data/lib/mongo/collection.rb +99 -49
- data/lib/mongo/cursor.rb +17 -17
- data/lib/mongo/db.rb +30 -14
- data/lib/mongo/gridfs/grid.rb +5 -3
- data/lib/mongo/gridfs/grid_file_system.rb +5 -3
- data/lib/mongo/gridfs/grid_io.rb +5 -3
- data/lib/mongo/legacy.rb +9 -2
- data/lib/mongo/mongo_client.rb +100 -72
- data/lib/mongo/mongo_replica_set_client.rb +46 -60
- data/lib/mongo/mongo_sharded_client.rb +5 -66
- data/lib/mongo/networking.rb +2 -1
- data/lib/mongo/util/node.rb +41 -42
- data/lib/mongo/util/pool.rb +15 -43
- data/lib/mongo/util/pool_manager.rb +16 -65
- data/lib/mongo/util/read_preference.rb +82 -0
- data/lib/mongo/util/sharding_pool_manager.rb +0 -86
- data/lib/mongo/util/ssl_socket.rb +2 -1
- data/lib/mongo/util/support.rb +8 -18
- data/lib/mongo/util/tcp_socket.rb +5 -4
- data/lib/mongo/util/thread_local_variable_manager.rb +29 -0
- data/lib/mongo/util/unix_socket.rb +23 -0
- data/lib/mongo/util/uri_parser.rb +31 -18
- data/lib/mongo/util/write_concern.rb +7 -2
- data/mongo.gemspec +1 -1
- data/test/auxillary/repl_set_auth_test.rb +2 -2
- data/test/bson/bson_test.rb +1 -1
- data/test/bson/byte_buffer_test.rb +24 -26
- data/test/bson/hash_with_indifferent_access_test.rb +11 -1
- data/test/functional/collection_test.rb +16 -16
- data/test/functional/connection_test.rb +1 -4
- data/test/functional/db_api_test.rb +14 -10
- data/test/functional/pool_test.rb +23 -31
- data/test/functional/timeout_test.rb +3 -5
- data/test/functional/uri_test.rb +10 -5
- data/test/replica_set/basic_test.rb +3 -8
- data/test/replica_set/client_test.rb +47 -31
- data/test/replica_set/complex_connect_test.rb +12 -10
- data/test/replica_set/connection_test.rb +8 -151
- data/test/replica_set/count_test.rb +9 -5
- data/test/replica_set/cursor_test.rb +17 -27
- data/test/replica_set/insert_test.rb +5 -10
- data/test/replica_set/query_test.rb +4 -9
- data/test/replica_set/read_preference_test.rb +200 -0
- data/test/replica_set/refresh_test.rb +54 -65
- data/test/replica_set/replication_ack_test.rb +16 -14
- data/test/sharded_cluster/basic_test.rb +30 -0
- data/test/test_helper.rb +33 -15
- data/test/threading/basic_test.rb +79 -0
- data/test/tools/mongo_config.rb +62 -22
- data/test/unit/client_test.rb +36 -14
- data/test/unit/collection_test.rb +23 -0
- data/test/unit/connection_test.rb +30 -14
- data/test/unit/cursor_test.rb +137 -7
- data/test/unit/db_test.rb +17 -4
- data/test/unit/grid_test.rb +2 -2
- data/test/unit/node_test.rb +2 -1
- data/test/unit/pool_manager_test.rb +29 -1
- data/test/unit/read_test.rb +15 -15
- data/test/unit/safe_test.rb +4 -4
- data/test/unit/write_concern_test.rb +4 -4
- metadata +134 -143
- data/examples/admin.rb +0 -43
- data/examples/capped.rb +0 -22
- data/examples/cursor.rb +0 -48
- data/examples/gridfs.rb +0 -44
- data/examples/index_test.rb +0 -126
- data/examples/info.rb +0 -31
- data/examples/queries.rb +0 -74
- data/examples/replica_set.rb +0 -26
- data/examples/simple.rb +0 -25
- data/examples/strict.rb +0 -35
- data/examples/types.rb +0 -36
- data/examples/web/thin/load.rb +0 -23
- data/examples/web/unicorn/load.rb +0 -25
- data/test/support/hash_with_indifferent_access.rb +0 -186
- data/test/support/keys.rb +0 -45
- data/test/threading/threading_with_large_pool_test.rb +0 -90
data/test/unit/client_test.rb
CHANGED
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
include Mongo
|
3
3
|
|
4
4
|
class ClientTest < Test::Unit::TestCase
|
5
|
-
context "Mongo::MongoClient
|
5
|
+
context "Mongo::MongoClient initialization " do
|
6
6
|
context "given a single node" do
|
7
7
|
setup do
|
8
8
|
@client = MongoClient.new('localhost', 27017, :connect => false)
|
@@ -31,12 +31,12 @@ class ClientTest < Test::Unit::TestCase
|
|
31
31
|
assert !@client.slave_ok?
|
32
32
|
end
|
33
33
|
|
34
|
-
should "raise
|
35
|
-
|
36
|
-
MongoClient.new(:w => 1)
|
34
|
+
should "not raise error if no host or port is supplied" do
|
35
|
+
assert_nothing_raised do
|
36
|
+
MongoClient.new(:w => 1, :connect => false)
|
37
37
|
end
|
38
|
-
|
39
|
-
MongoClient.new('localhost', :w => 1)
|
38
|
+
assert_nothing_raised do
|
39
|
+
MongoClient.new('localhost', :w => 1, :connect=> false)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -67,25 +67,47 @@ class ClientTest < Test::Unit::TestCase
|
|
67
67
|
args = [['localhost:27017'], opts]
|
68
68
|
client.send(:initialize, *args)
|
69
69
|
end
|
70
|
+
|
71
|
+
should "throw error if superflous arguments are specified" do
|
72
|
+
assert_raise MongoArgumentError do
|
73
|
+
MongoReplicaSetClient.new(['localhost:27017'], ['localhost:27018'], {:connect => false})
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "initializing with a unix socket" do
|
80
|
+
setup do
|
81
|
+
@connection = Mongo::Connection.new('/tmp/mongod.sock', :safe => true, :connect => false)
|
82
|
+
UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
|
83
|
+
end
|
84
|
+
should "parse a unix socket" do
|
85
|
+
assert_equal "/tmp/mongod.sock", @connection.host_port.first
|
70
86
|
end
|
71
87
|
end
|
72
88
|
|
73
89
|
context "initializing with a mongodb uri" do
|
74
90
|
should "parse a simple uri" do
|
75
91
|
@client = MongoClient.from_uri("mongodb://localhost", :connect => false)
|
76
|
-
assert_equal ['localhost', 27017], @client.
|
92
|
+
assert_equal ['localhost', 27017], @client.host_port
|
77
93
|
end
|
78
94
|
|
95
|
+
#should "parse a unix socket" do
|
96
|
+
# socket_address = "/tmp/mongodb-27017.sock"
|
97
|
+
# @client = MongoClient.from_uri("mongodb://#{socket_address}")
|
98
|
+
# assert_equal socket_address, @client.host_port.first
|
99
|
+
#end
|
100
|
+
|
79
101
|
should "allow a complex host names" do
|
80
102
|
host_name = "foo.bar-12345.org"
|
81
103
|
@client = MongoClient.from_uri("mongodb://#{host_name}", :connect => false)
|
82
|
-
assert_equal [host_name, 27017], @client.
|
104
|
+
assert_equal [host_name, 27017], @client.host_port
|
83
105
|
end
|
84
106
|
|
85
107
|
should "allow db without username and password" do
|
86
108
|
host_name = "foo.bar-12345.org"
|
87
109
|
@client = MongoClient.from_uri("mongodb://#{host_name}/foo", :connect => false)
|
88
|
-
assert_equal [host_name, 27017], @client.
|
110
|
+
assert_equal [host_name, 27017], @client.host_port
|
89
111
|
end
|
90
112
|
|
91
113
|
should "set write concern options on connection" do
|
@@ -105,7 +127,7 @@ class ClientTest < Test::Unit::TestCase
|
|
105
127
|
|
106
128
|
should "parse a uri with a hyphen & underscore in the username or password" do
|
107
129
|
@client = MongoClient.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
|
108
|
-
assert_equal ['localhost', 27017], @client.
|
130
|
+
assert_equal ['localhost', 27017], @client.host_port
|
109
131
|
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
|
110
132
|
assert_equal auth_hash, @client.auths[0]
|
111
133
|
end
|
@@ -151,21 +173,21 @@ class ClientTest < Test::Unit::TestCase
|
|
151
173
|
should "parse a simple uri" do
|
152
174
|
ENV['MONGODB_URI'] = "mongodb://localhost?connect=false"
|
153
175
|
@client = MongoClient.new
|
154
|
-
assert_equal ['localhost', 27017], @client.
|
176
|
+
assert_equal ['localhost', 27017], @client.host_port
|
155
177
|
end
|
156
178
|
|
157
179
|
should "allow a complex host names" do
|
158
180
|
host_name = "foo.bar-12345.org"
|
159
181
|
ENV['MONGODB_URI'] = "mongodb://#{host_name}?connect=false"
|
160
182
|
@client = MongoClient.new
|
161
|
-
assert_equal [host_name, 27017], @client.
|
183
|
+
assert_equal [host_name, 27017], @client.host_port
|
162
184
|
end
|
163
185
|
|
164
186
|
should "allow db without username and password" do
|
165
187
|
host_name = "foo.bar-12345.org"
|
166
188
|
ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?connect=false"
|
167
189
|
@client = MongoClient.new
|
168
|
-
assert_equal [host_name, 27017], @client.
|
190
|
+
assert_equal [host_name, 27017], @client.host_port
|
169
191
|
end
|
170
192
|
|
171
193
|
should "set write concern options on connection" do
|
@@ -188,7 +210,7 @@ class ClientTest < Test::Unit::TestCase
|
|
188
210
|
should "parse a uri with a hyphen & underscore in the username or password" do
|
189
211
|
ENV['MONGODB_URI'] = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
|
190
212
|
@client = MongoClient.new
|
191
|
-
assert_equal ['localhost', 27017], @client.
|
213
|
+
assert_equal ['localhost', 27017], @client.host_port
|
192
214
|
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
|
193
215
|
assert_equal auth_hash, @client.auths[0]
|
194
216
|
end
|
@@ -140,6 +140,29 @@ class CollectionTest < Test::Unit::TestCase
|
|
140
140
|
@coll.ensure_index [["x", Mongo::DESCENDING], ["y", Mongo::DESCENDING]]
|
141
141
|
end
|
142
142
|
|
143
|
+
should "call generate_indexes for each key when calling ensure_indexes with a hash" do
|
144
|
+
@client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
|
145
|
+
@db = @client['testing']
|
146
|
+
@db.cache_time = 300
|
147
|
+
@coll = @db.collection('books')
|
148
|
+
oh = BSON::OrderedHash.new
|
149
|
+
oh['x'] = Mongo::DESCENDING
|
150
|
+
oh['y'] = Mongo::DESCENDING
|
151
|
+
@coll.expects(:generate_indexes).once.with do |a, b, c|
|
152
|
+
a == oh
|
153
|
+
end
|
154
|
+
|
155
|
+
if RUBY_VERSION > '1.9'
|
156
|
+
@coll.ensure_index({"x" => Mongo::DESCENDING, "y" => Mongo::DESCENDING})
|
157
|
+
else
|
158
|
+
ordered_hash = BSON::OrderedHash.new
|
159
|
+
ordered_hash['x'] = Mongo::DESCENDING
|
160
|
+
ordered_hash['y'] = Mongo::DESCENDING
|
161
|
+
@coll.ensure_index(ordered_hash)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
|
143
166
|
should "use the connection's logger" do
|
144
167
|
@client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
|
145
168
|
@db = @client['testing']
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class ConnectionTest < Test::Unit::TestCase
|
4
|
-
context "Mongo::MongoClient
|
4
|
+
context "Mongo::MongoClient initialization " do
|
5
5
|
context "given a single node" do
|
6
6
|
setup do
|
7
7
|
@connection = Mongo::Connection.new('localhost', 27017, :safe => true, :connect => false)
|
@@ -30,12 +30,12 @@ class ConnectionTest < Test::Unit::TestCase
|
|
30
30
|
assert !@connection.slave_ok?
|
31
31
|
end
|
32
32
|
|
33
|
-
should "raise
|
34
|
-
|
35
|
-
Mongo::Connection.new(:safe => true)
|
33
|
+
should "not raise error if no host or port is supplied" do
|
34
|
+
assert_nothing_raised do
|
35
|
+
Mongo::Connection.new(:safe => true, :connect => false)
|
36
36
|
end
|
37
|
-
|
38
|
-
Mongo::Connection.new('localhost', :safe => true)
|
37
|
+
assert_nothing_raised do
|
38
|
+
Mongo::Connection.new('localhost', :safe => true, :connect => false)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -70,22 +70,38 @@ class ConnectionTest < Test::Unit::TestCase
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
+
context "initializing with a unix socket" do
|
74
|
+
setup do
|
75
|
+
@connection = Mongo::Connection.new('/tmp/mongod.sock', :safe => true, :connect => false)
|
76
|
+
UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
|
77
|
+
end
|
78
|
+
should "parse a unix socket" do
|
79
|
+
assert_equal "/tmp/mongod.sock", @connection.host_port.first
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
73
83
|
context "initializing with a mongodb uri" do
|
74
84
|
should "parse a simple uri" do
|
75
85
|
@connection = Mongo::Connection.from_uri("mongodb://localhost", :connect => false)
|
76
|
-
assert_equal ['localhost', 27017], @connection.
|
86
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
77
87
|
end
|
78
88
|
|
89
|
+
#should "parse a unix socket" do
|
90
|
+
# socket_address = "/tmp/mongodb-27017.sock"
|
91
|
+
# @client = MongoClient.from_uri("mongodb://#{socket_address}")
|
92
|
+
# assert_equal socket_address, @client.host_port.first
|
93
|
+
#end
|
94
|
+
|
79
95
|
should "allow a complex host names" do
|
80
96
|
host_name = "foo.bar-12345.org"
|
81
97
|
@connection = Mongo::Connection.from_uri("mongodb://#{host_name}", :connect => false)
|
82
|
-
assert_equal [host_name, 27017], @connection.
|
98
|
+
assert_equal [host_name, 27017], @connection.host_port
|
83
99
|
end
|
84
100
|
|
85
101
|
should "allow db without username and password" do
|
86
102
|
host_name = "foo.bar-12345.org"
|
87
103
|
@connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo", :connect => false)
|
88
|
-
assert_equal [host_name, 27017], @connection.
|
104
|
+
assert_equal [host_name, 27017], @connection.host_port
|
89
105
|
end
|
90
106
|
|
91
107
|
should "set safe options on connection" do
|
@@ -105,7 +121,7 @@ class ConnectionTest < Test::Unit::TestCase
|
|
105
121
|
|
106
122
|
should "parse a uri with a hyphen & underscore in the username or password" do
|
107
123
|
@connection = Mongo::Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
|
108
|
-
assert_equal ['localhost', 27017], @connection.
|
124
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
109
125
|
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
|
110
126
|
assert_equal auth_hash, @connection.auths[0]
|
111
127
|
end
|
@@ -151,21 +167,21 @@ class ConnectionTest < Test::Unit::TestCase
|
|
151
167
|
should "parse a simple uri" do
|
152
168
|
ENV['MONGODB_URI'] = "mongodb://localhost?connect=false"
|
153
169
|
@connection = Mongo::Connection.new
|
154
|
-
assert_equal ['localhost', 27017], @connection.
|
170
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
155
171
|
end
|
156
172
|
|
157
173
|
should "allow a complex host names" do
|
158
174
|
host_name = "foo.bar-12345.org"
|
159
175
|
ENV['MONGODB_URI'] = "mongodb://#{host_name}?connect=false"
|
160
176
|
@connection = Mongo::Connection.new
|
161
|
-
assert_equal [host_name, 27017], @connection.
|
177
|
+
assert_equal [host_name, 27017], @connection.host_port
|
162
178
|
end
|
163
179
|
|
164
180
|
should "allow db without username and password" do
|
165
181
|
host_name = "foo.bar-12345.org"
|
166
182
|
ENV['MONGODB_URI'] = "mongodb://#{host_name}/foo?connect=false"
|
167
183
|
@connection = Mongo::Connection.new
|
168
|
-
assert_equal [host_name, 27017], @connection.
|
184
|
+
assert_equal [host_name, 27017], @connection.host_port
|
169
185
|
end
|
170
186
|
|
171
187
|
should "set safe options on connection" do
|
@@ -188,7 +204,7 @@ class ConnectionTest < Test::Unit::TestCase
|
|
188
204
|
should "parse a uri with a hyphen & underscore in the username or password" do
|
189
205
|
ENV['MONGODB_URI'] = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
|
190
206
|
@connection = Mongo::Connection.new
|
191
|
-
assert_equal ['localhost', 27017], @connection.
|
207
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
192
208
|
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
|
193
209
|
assert_equal auth_hash, @connection.auths[0]
|
194
210
|
end
|
data/test/unit/cursor_test.rb
CHANGED
@@ -1,18 +1,22 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class CursorTest < Test::Unit::TestCase
|
4
|
+
class Mongo::Cursor
|
5
|
+
public :construct_query_spec
|
6
|
+
end
|
7
|
+
|
4
8
|
context "Cursor options" do
|
5
9
|
setup do
|
6
10
|
@logger = mock()
|
7
11
|
@logger.stubs(:debug)
|
8
12
|
@connection = stub(:class => MongoClient, :logger => @logger,
|
9
|
-
:slave_ok? => false, :
|
10
|
-
:tag_sets =>
|
13
|
+
:slave_ok? => false, :read => :primary, :log_duration => false,
|
14
|
+
:tag_sets => [], :acceptable_latency => 10)
|
11
15
|
@db = stub(:name => "testing", :slave_ok? => false,
|
12
|
-
:connection => @connection, :
|
13
|
-
:tag_sets =>
|
14
|
-
@collection = stub(:db => @db, :name => "items", :
|
15
|
-
:tag_sets =>
|
16
|
+
:connection => @connection, :read => :primary,
|
17
|
+
:tag_sets => [], :acceptable_latency => 10)
|
18
|
+
@collection = stub(:db => @db, :name => "items", :read => :primary,
|
19
|
+
:tag_sets => [], :acceptable_latency => 10)
|
16
20
|
@cursor = Cursor.new(@collection)
|
17
21
|
end
|
18
22
|
|
@@ -107,6 +111,105 @@ class CursorTest < Test::Unit::TestCase
|
|
107
111
|
@cursor.batch_size(100)
|
108
112
|
assert_equal 100, @cursor.batch_size
|
109
113
|
end
|
114
|
+
|
115
|
+
context "conected to mongos" do
|
116
|
+
setup do
|
117
|
+
@connection.stubs(:mongos?).returns(true)
|
118
|
+
@tag_sets = [{:dc => "ny"}]
|
119
|
+
end
|
120
|
+
|
121
|
+
should "set $readPreference" do
|
122
|
+
# secondary
|
123
|
+
cursor = Cursor.new(@collection, { :read => :secondary })
|
124
|
+
|
125
|
+
spec = cursor.construct_query_spec
|
126
|
+
assert spec.has_key?('$readPreference')
|
127
|
+
assert_equal :secondary, spec['$readPreference'][:mode]
|
128
|
+
assert !spec['$readPreference'].has_key?(:tags)
|
129
|
+
|
130
|
+
# secondary preferred with tags
|
131
|
+
cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => @tag_sets })
|
132
|
+
|
133
|
+
spec = cursor.construct_query_spec
|
134
|
+
assert spec.has_key?('$readPreference')
|
135
|
+
assert_equal :secondaryPreferred, spec['$readPreference'][:mode]
|
136
|
+
assert_equal @tag_sets, spec['$readPreference'][:tags]
|
137
|
+
|
138
|
+
# primary preferred
|
139
|
+
cursor = Cursor.new(@collection, { :read => :primary_preferred })
|
140
|
+
|
141
|
+
spec = cursor.construct_query_spec
|
142
|
+
assert spec.has_key?('$readPreference')
|
143
|
+
assert_equal :primaryPreferred, spec['$readPreference'][:mode]
|
144
|
+
assert !spec['$readPreference'].has_key?(:tags)
|
145
|
+
|
146
|
+
# primary preferred with tags
|
147
|
+
cursor = Cursor.new(@collection, { :read => :primary_preferred, :tag_sets => @tag_sets })
|
148
|
+
|
149
|
+
spec = cursor.construct_query_spec
|
150
|
+
assert spec.has_key?('$readPreference')
|
151
|
+
assert_equal :primaryPreferred, spec['$readPreference'][:mode]
|
152
|
+
assert_equal @tag_sets, spec['$readPreference'][:tags]
|
153
|
+
|
154
|
+
# nearest
|
155
|
+
cursor = Cursor.new(@collection, { :read => :nearest })
|
156
|
+
|
157
|
+
spec = cursor.construct_query_spec
|
158
|
+
assert spec.has_key?('$readPreference')
|
159
|
+
assert_equal :nearest, spec['$readPreference'][:mode]
|
160
|
+
assert !spec['$readPreference'].has_key?(:tags)
|
161
|
+
|
162
|
+
# nearest with tags
|
163
|
+
cursor = Cursor.new(@collection, { :read => :nearest, :tag_sets => @tag_sets })
|
164
|
+
|
165
|
+
spec = cursor.construct_query_spec
|
166
|
+
assert spec.has_key?('$readPreference')
|
167
|
+
assert_equal :nearest, spec['$readPreference'][:mode]
|
168
|
+
assert_equal @tag_sets, spec['$readPreference'][:tags]
|
169
|
+
end
|
170
|
+
|
171
|
+
should "not set $readPreference" do
|
172
|
+
# for primary
|
173
|
+
cursor = Cursor.new(@collection, { :read => :primary, :tag_sets => @tag_sets })
|
174
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
175
|
+
|
176
|
+
# for secondary_preferred with no tags
|
177
|
+
cursor = Cursor.new(@collection, { :read => :secondary_preferred })
|
178
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
179
|
+
|
180
|
+
cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => [] })
|
181
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
182
|
+
|
183
|
+
cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => nil })
|
184
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context "not conected to mongos" do
|
189
|
+
setup do
|
190
|
+
@connection.stubs(:mongos?).returns(false)
|
191
|
+
end
|
192
|
+
|
193
|
+
should "not set $readPreference" do
|
194
|
+
cursor = Cursor.new(@collection, { :read => :primary })
|
195
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
196
|
+
|
197
|
+
cursor = Cursor.new(@collection, { :read => :primary_preferred })
|
198
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
199
|
+
|
200
|
+
cursor = Cursor.new(@collection, { :read => :secondary })
|
201
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
202
|
+
|
203
|
+
cursor = Cursor.new(@collection, { :read => :secondary_preferred })
|
204
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
205
|
+
|
206
|
+
cursor = Cursor.new(@collection, { :read => :nearest })
|
207
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
208
|
+
|
209
|
+
cursor = Cursor.new(@collection, { :read => :secondary , :tag_sets => @tag_sets})
|
210
|
+
assert !cursor.construct_query_spec.has_key?('$readPreference')
|
211
|
+
end
|
212
|
+
end
|
110
213
|
end
|
111
214
|
|
112
215
|
context "Query fields" do
|
@@ -117,7 +220,7 @@ class CursorTest < Test::Unit::TestCase
|
|
117
220
|
:log_duration => false, :tag_sets =>{}, :acceptable_latency => 10)
|
118
221
|
@db = stub(:slave_ok? => true, :name => "testing", :connection => @connection,
|
119
222
|
:tag_sets => {}, :acceptable_latency => 10)
|
120
|
-
@collection = stub(:db => @db, :name => "items", :
|
223
|
+
@collection = stub(:db => @db, :name => "items", :read => :primary,
|
121
224
|
:tag_sets => {}, :acceptable_latency => 10)
|
122
225
|
end
|
123
226
|
|
@@ -140,4 +243,31 @@ class CursorTest < Test::Unit::TestCase
|
|
140
243
|
assert_nil @cursor.fields
|
141
244
|
end
|
142
245
|
end
|
246
|
+
|
247
|
+
context "counts" do
|
248
|
+
setup do
|
249
|
+
@logger = mock()
|
250
|
+
@logger.stubs(:debug)
|
251
|
+
@connection = stub(:class => Connection, :logger => @logger,
|
252
|
+
:slave_ok? => false, :read => :primary, :log_duration => false,
|
253
|
+
:tag_sets => {}, :acceptable_latency => 10)
|
254
|
+
@db = stub(:name => "testing", :slave_ok? => false,
|
255
|
+
:connection => @connection, :read => :primary,
|
256
|
+
:tag_sets => {}, :acceptable_latency => 10)
|
257
|
+
@collection = stub(:db => @db, :name => "items", :read => :primary,
|
258
|
+
:tag_sets => {}, :acceptable_latency => 10)
|
259
|
+
@cursor = Cursor.new(@collection)
|
260
|
+
end
|
261
|
+
|
262
|
+
should "pass the comment parameter" do
|
263
|
+
query = {:field => 7}
|
264
|
+
@db.expects(:command).with({ 'count' => "items",
|
265
|
+
'query' => query,
|
266
|
+
'fields' => nil},
|
267
|
+
{ :read => :primary,
|
268
|
+
:comment => "my comment"}).
|
269
|
+
returns({'ok' => 1, 'n' => 1})
|
270
|
+
assert_equal(1, Cursor.new(@collection, :selector => query, :comment => 'my comment').count())
|
271
|
+
end
|
272
|
+
end
|
143
273
|
end
|
data/test/unit/db_test.rb
CHANGED
@@ -15,12 +15,12 @@ class DBTest < Test::Unit::TestCase
|
|
15
15
|
setup do
|
16
16
|
@client = stub()
|
17
17
|
@client.stubs(:write_concern).returns({})
|
18
|
-
@client.stubs(:
|
18
|
+
@client.stubs(:read).returns(:primary)
|
19
19
|
@client.stubs(:tag_sets)
|
20
20
|
@client.stubs(:acceptable_latency)
|
21
21
|
@db = DB.new("testing", @client)
|
22
22
|
@db.stubs(:safe)
|
23
|
-
@db.stubs(:
|
23
|
+
@db.stubs(:read)
|
24
24
|
@db.stubs(:tag_sets)
|
25
25
|
@db.stubs(:acceptable_latency)
|
26
26
|
@collection = mock()
|
@@ -44,7 +44,8 @@ class DBTest < Test::Unit::TestCase
|
|
44
44
|
should "create the proper cursor" do
|
45
45
|
@cursor = mock(:next_document => {"ok" => 1})
|
46
46
|
Cursor.expects(:new).with(@collection,
|
47
|
-
:limit => -1, :selector => {:buildinfo => 1},
|
47
|
+
:limit => -1, :selector => {:buildinfo => 1},
|
48
|
+
:socket => nil, :read => nil, :comment => nil).returns(@cursor)
|
48
49
|
command = {:buildinfo => 1}
|
49
50
|
@db.command(command, :check_response => true)
|
50
51
|
end
|
@@ -52,13 +53,25 @@ class DBTest < Test::Unit::TestCase
|
|
52
53
|
should "raise an error when the command fails" do
|
53
54
|
@cursor = mock(:next_document => {"ok" => 0})
|
54
55
|
Cursor.expects(:new).with(@collection,
|
55
|
-
:limit => -1, :selector => {:buildinfo => 1},
|
56
|
+
:limit => -1, :selector => {:buildinfo => 1},
|
57
|
+
:socket => nil, :read => nil, :comment => nil).returns(@cursor)
|
56
58
|
assert_raise OperationFailure do
|
57
59
|
command = {:buildinfo => 1}
|
58
60
|
@db.command(command, :check_response => true)
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
64
|
+
should "pass on the comment" do
|
65
|
+
@cursor = mock(:next_document => {"ok" => 0})
|
66
|
+
Cursor.expects(:new).with(@collection,
|
67
|
+
:limit => -1, :selector => {:buildinfo => 1},
|
68
|
+
:socket => nil, :read => nil, :comment => "my comment").returns(@cursor)
|
69
|
+
assert_raise OperationFailure do
|
70
|
+
command = {:buildinfo => 1}
|
71
|
+
@db.command(command, :check_response => true, :comment => 'my comment')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
62
75
|
should "raise an error if logging out fails" do
|
63
76
|
@db.expects(:command).returns({})
|
64
77
|
@client.expects(:pool_size).returns(1)
|