mongo 1.3.0 → 1.12.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 (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
@@ -1,13 +1,36 @@
1
- require './test/test_helper'
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'test_helper'
16
+
17
+ class CursorUnitTest < Test::Unit::TestCase
18
+ class Mongo::Cursor
19
+ public :construct_query_spec
20
+ end
2
21
 
3
- class CursorTest < Test::Unit::TestCase
4
22
  context "Cursor options" do
5
23
  setup do
6
24
  @logger = mock()
7
25
  @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")
26
+ @connection = stub(:class => MongoClient, :logger => @logger,
27
+ :slave_ok? => false, :read => :primary, :log_duration => false,
28
+ :tag_sets => [], :acceptable_latency => 10)
29
+ @db = stub(:name => "testing", :slave_ok? => false,
30
+ :connection => @connection, :read => :primary,
31
+ :tag_sets => [], :acceptable_latency => 10)
32
+ @collection = stub(:db => @db, :name => "items", :read => :primary,
33
+ :tag_sets => [], :acceptable_latency => 10)
11
34
  @cursor = Cursor.new(@collection)
12
35
  end
13
36
 
@@ -32,6 +55,18 @@ class CursorTest < Test::Unit::TestCase
32
55
  assert_equal({:name => 1, :date => 1}, @cursor.query_options_hash[:fields])
33
56
  end
34
57
 
58
+ should "allow $meta projection operator" do
59
+ assert_nil @cursor.fields
60
+
61
+ @cursor = Cursor.new(@collection, :fields => [{ :score => { :$meta => 'textScore' } }])
62
+ assert_equal({ :score => { :$meta => 'textScore' } }, @cursor.fields)
63
+ assert_equal({ :score => { :$meta => 'textScore' } }, @cursor.query_options_hash[:fields])
64
+
65
+ @cursor = Cursor.new(@collection, :fields => [:name, { :score => { :$meta => 'textScore' } }])
66
+ assert_equal({ :name => 1, :score => { :$meta => 'textScore' } }, @cursor.fields)
67
+ assert_equal({ :name => 1, :score => { :$meta => 'textScore' } }, @cursor.query_options_hash[:fields])
68
+ end
69
+
35
70
  should "set mix fields 0 and 1" do
36
71
  assert_nil @cursor.fields
37
72
 
@@ -73,18 +108,154 @@ class CursorTest < Test::Unit::TestCase
73
108
  assert_equal "name", @cursor.query_options_hash[:hint]
74
109
  end
75
110
 
111
+ should "set comment" do
112
+ assert_nil @cursor.comment
113
+
114
+ @cursor = Cursor.new(@collection, :comment => "comment")
115
+ assert_equal "comment", @cursor.comment
116
+ assert_equal "comment", @cursor.query_options_hash[:comment]
117
+ end
118
+
76
119
  should "cache full collection name" do
77
120
  assert_equal "testing.items", @cursor.full_collection_name
78
121
  end
122
+
123
+ should "raise error when batch_size is 1" do
124
+ e = assert_raise ArgumentError do
125
+ @cursor.batch_size(1)
126
+ end
127
+ assert_equal "Invalid value for batch_size 1; must be 0 or > 1.", e.message
128
+ end
129
+
130
+ should "use the limit for batch size when it's smaller than the specified batch_size" do
131
+ @cursor.limit(99)
132
+ @cursor.batch_size(100)
133
+ assert_equal 99, @cursor.batch_size
134
+ end
135
+
136
+ should "use the specified batch_size" do
137
+ @cursor.batch_size(100)
138
+ assert_equal 100, @cursor.batch_size
139
+ end
140
+
141
+ context 'when an alternate namespace is specified' do
142
+
143
+ should 'use the alternate namespace' do
144
+ cursor = Cursor.new(@collection, :ns => 'other_db.other_collection')
145
+ assert_equal('other_db.other_collection', cursor.full_collection_name)
146
+ end
147
+ end
148
+
149
+ context "connected to mongos" do
150
+ setup do
151
+ @connection.stubs(:mongos?).returns(true)
152
+ @tag_sets = [{:dc => "ny"}]
153
+ end
154
+
155
+ should "set $readPreference" do
156
+ # secondary
157
+ cursor = Cursor.new(@collection, { :read => :secondary })
158
+
159
+ spec = cursor.construct_query_spec
160
+ assert spec.has_key?('$readPreference')
161
+ assert_equal 'secondary', spec['$readPreference'][:mode]
162
+ assert !spec['$readPreference'].has_key?(:tags)
163
+
164
+ # secondary preferred with tags
165
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => @tag_sets })
166
+
167
+ spec = cursor.construct_query_spec
168
+ assert spec.has_key?('$readPreference')
169
+ assert_equal 'secondaryPreferred', spec['$readPreference'][:mode]
170
+ assert_equal @tag_sets, spec['$readPreference'][:tags]
171
+
172
+ # primary preferred
173
+ cursor = Cursor.new(@collection, { :read => :primary_preferred })
174
+
175
+ spec = cursor.construct_query_spec
176
+ assert spec.has_key?('$readPreference')
177
+ assert_equal 'primaryPreferred', spec['$readPreference'][:mode]
178
+ assert !spec['$readPreference'].has_key?(:tags)
179
+
180
+ # primary preferred with tags
181
+ cursor = Cursor.new(@collection, { :read => :primary_preferred, :tag_sets => @tag_sets })
182
+
183
+ spec = cursor.construct_query_spec
184
+ assert spec.has_key?('$readPreference')
185
+ assert_equal 'primaryPreferred', spec['$readPreference'][:mode]
186
+ assert_equal @tag_sets, spec['$readPreference'][:tags]
187
+
188
+ # nearest
189
+ cursor = Cursor.new(@collection, { :read => :nearest })
190
+
191
+ spec = cursor.construct_query_spec
192
+ assert spec.has_key?('$readPreference')
193
+ assert_equal 'nearest', spec['$readPreference'][:mode]
194
+ assert !spec['$readPreference'].has_key?(:tags)
195
+
196
+ # nearest with tags
197
+ cursor = Cursor.new(@collection, { :read => :nearest, :tag_sets => @tag_sets })
198
+
199
+ spec = cursor.construct_query_spec
200
+ assert spec.has_key?('$readPreference')
201
+ assert_equal 'nearest', spec['$readPreference'][:mode]
202
+ assert_equal @tag_sets, spec['$readPreference'][:tags]
203
+ end
204
+
205
+ should "not set $readPreference" do
206
+ # for primary
207
+ cursor = Cursor.new(@collection, { :read => :primary, :tag_sets => @tag_sets })
208
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
209
+
210
+ # for secondary_preferred with no tags
211
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred })
212
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
213
+
214
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => [] })
215
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
216
+
217
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => nil })
218
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
219
+ end
220
+ end
221
+
222
+ context "not connected to mongos" do
223
+ setup do
224
+ @connection.stubs(:mongos?).returns(false)
225
+ end
226
+
227
+ should "not set $readPreference" do
228
+ cursor = Cursor.new(@collection, { :read => :primary })
229
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
230
+
231
+ cursor = Cursor.new(@collection, { :read => :primary_preferred })
232
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
233
+
234
+ cursor = Cursor.new(@collection, { :read => :secondary })
235
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
236
+
237
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred })
238
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
239
+
240
+ cursor = Cursor.new(@collection, { :read => :nearest })
241
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
242
+
243
+ cursor = Cursor.new(@collection, { :read => :secondary , :tag_sets => @tag_sets})
244
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
245
+ end
246
+ end
79
247
  end
80
248
 
81
249
  context "Query fields" do
82
250
  setup do
83
251
  @logger = mock()
84
252
  @logger.stubs(:debug)
85
- @connection = stub(:class => Connection, :logger => @logger)
86
- @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection)
87
- @collection = stub(:db => @db, :name => "items")
253
+ @connection = stub(:class => MongoClient, :logger => @logger, :slave_ok? => false,
254
+ :log_duration => false, :tag_sets =>{}, :acceptable_latency => 10)
255
+ @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection,
256
+ :tag_sets => {}, :acceptable_latency => 10)
257
+ @collection = stub(:db => @db, :name => "items", :read => :primary,
258
+ :tag_sets => {}, :acceptable_latency => 10)
88
259
  end
89
260
 
90
261
  should "when an array should return a hash with each key" do
@@ -106,4 +277,31 @@ class CursorTest < Test::Unit::TestCase
106
277
  assert_nil @cursor.fields
107
278
  end
108
279
  end
280
+
281
+ context "counts" do
282
+ setup do
283
+ @logger = mock()
284
+ @logger.stubs(:debug)
285
+ @connection = stub(:class => Connection, :logger => @logger,
286
+ :slave_ok? => false, :read => :primary, :log_duration => false,
287
+ :tag_sets => {}, :acceptable_latency => 10)
288
+ @db = stub(:name => "testing", :slave_ok? => false,
289
+ :connection => @connection, :read => :primary,
290
+ :tag_sets => {}, :acceptable_latency => 10)
291
+ @collection = stub(:db => @db, :name => "items", :read => :primary,
292
+ :tag_sets => {}, :acceptable_latency => 10)
293
+ @cursor = Cursor.new(@collection)
294
+ end
295
+
296
+ should "pass the comment parameter" do
297
+ query = {:field => 7}
298
+ @db.expects(:command).with({ 'count' => "items",
299
+ 'query' => query,
300
+ 'fields' => nil},
301
+ { :read => :primary,
302
+ :comment => "my comment"}).
303
+ returns({'ok' => 1, 'n' => 1})
304
+ assert_equal(1, Cursor.new(@collection, :selector => query, :comment => 'my comment').count())
305
+ end
306
+ end
109
307
  end
data/test/unit/db_test.rb CHANGED
@@ -1,4 +1,18 @@
1
- require './test/test_helper'
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'test_helper'
2
16
 
3
17
  def insert_message(db, documents)
4
18
  documents = [documents] unless documents.is_a?(Array)
@@ -9,14 +23,21 @@ def insert_message(db, documents)
9
23
  message = db.add_message_headers(Mongo::Constants::OP_INSERT, message)
10
24
  end
11
25
 
12
- class DBTest < Test::Unit::TestCase
26
+ class DBUnitTest < Test::Unit::TestCase
13
27
  context "DBTest: " do
14
28
  context "DB commands" do
15
29
  setup do
16
- @conn = stub()
17
- @conn.stubs(:safe)
18
- @db = DB.new("testing", @conn)
30
+ @client = stub()
31
+ @client.stubs(:write_concern).returns({})
32
+ @client.stubs(:read).returns(:primary)
33
+ @client.stubs(:tag_sets)
34
+ @client.stubs(:acceptable_latency)
35
+ @client.stubs(:add_auth).returns({})
36
+ @db = DB.new("testing", @client)
19
37
  @db.stubs(:safe)
38
+ @db.stubs(:read)
39
+ @db.stubs(:tag_sets)
40
+ @db.stubs(:acceptable_latency)
20
41
  @collection = mock()
21
42
  @db.stubs(:system_command_collection).returns(@collection)
22
43
  end
@@ -35,10 +56,18 @@ class DBTest < Test::Unit::TestCase
35
56
  end
36
57
  end
37
58
 
59
+ should "not include named nil opts in selector" do
60
+ @cursor = mock(:next_document => {"ok" => 1})
61
+ Cursor.expects(:new).with(@collection, :limit => -1,
62
+ :selector => {:ping => 1}, :socket => nil).returns(@cursor)
63
+ command = {:ping => 1}
64
+ @db.command(command, :socket => nil)
65
+ end
66
+
38
67
  should "create the proper cursor" do
39
68
  @cursor = mock(:next_document => {"ok" => 1})
40
69
  Cursor.expects(:new).with(@collection,
41
- :limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
70
+ :limit => -1, :selector => {:buildinfo => 1}).returns(@cursor)
42
71
  command = {:buildinfo => 1}
43
72
  @db.command(command, :check_response => true)
44
73
  end
@@ -46,23 +75,25 @@ class DBTest < Test::Unit::TestCase
46
75
  should "raise an error when the command fails" do
47
76
  @cursor = mock(:next_document => {"ok" => 0})
48
77
  Cursor.expects(:new).with(@collection,
49
- :limit => -1, :selector => {:buildinfo => 1}, :socket => nil).returns(@cursor)
78
+ :limit => -1, :selector => {:buildinfo => 1}).returns(@cursor)
50
79
  assert_raise OperationFailure do
51
80
  command = {:buildinfo => 1}
52
81
  @db.command(command, :check_response => true)
53
82
  end
54
83
  end
55
84
 
56
- should "raise an error if logging out fails" do
57
- @db.expects(:command).returns({})
58
- @conn.expects(:pool_size).returns(1)
59
- assert_raise Mongo::MongoDBError do
60
- @db.logout
85
+ should "pass on the comment" do
86
+ @cursor = mock(:next_document => {"ok" => 0})
87
+ Cursor.expects(:new).with(@collection,
88
+ :limit => -1, :selector => {:buildinfo => 1},
89
+ :comment => "my comment").returns(@cursor)
90
+ assert_raise OperationFailure do
91
+ command = {:buildinfo => 1}
92
+ @db.command(command, :check_response => true, :comment => 'my comment')
61
93
  end
62
94
  end
63
95
 
64
96
  should "raise an error if collection creation fails" do
65
- @db.expects(:collection_names).returns([])
66
97
  @db.expects(:command).returns({'ok' => 0})
67
98
  assert_raise Mongo::MongoDBError do
68
99
  @db.create_collection("foo")
@@ -89,6 +120,17 @@ class DBTest < Test::Unit::TestCase
89
120
  @db.profiling_level = :slow_only
90
121
  end
91
122
  end
123
+
124
+ should "warn when save_auth is not nil" do
125
+ assert @db.expects(:warn).with(regexp_matches(/\[DEPRECATED\] Disabling the 'save_auth' option/))
126
+ @db.authenticate('foo', 'bar', false)
127
+ end
128
+
129
+ should "allow extra authentication options" do
130
+ extra_opts = { :service_name => 'example', :canonicalize_host_name => true }
131
+ assert @client.expects(:add_auth).with(@db.name, 'emily', nil, nil, 'GSSAPI', extra_opts)
132
+ @db.authenticate('emily', nil, nil, nil, 'GSSAPI', extra_opts)
133
+ end
92
134
  end
93
135
  end
94
136
  end
@@ -1,49 +1,76 @@
1
- require './test/test_helper'
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
2
14
 
3
- class GridTest < Test::Unit::TestCase
15
+ require 'test_helper'
16
+
17
+ class GridUnitTest < Test::Unit::TestCase
4
18
 
5
19
  context "GridFS: " do
6
20
  setup do
7
- @conn = stub()
8
- @conn.stubs(:safe)
9
- @db = DB.new("testing", @conn)
21
+ @client = stub()
22
+ @client.stubs(:write_concern).returns({})
23
+ @client.stubs(:read).returns(:primary)
24
+ @client.stubs(:tag_sets)
25
+ @client.stubs(:acceptable_latency)
26
+ @db = DB.new("testing", @client)
10
27
  @files = mock()
11
28
  @chunks = mock()
12
29
 
13
- @db.expects(:[]).with('fs.files').returns(@files)
14
- @db.expects(:[]).with('fs.chunks').returns(@chunks)
30
+ @db.stubs(:[]).with('fs.files').returns(@files)
31
+ @db.stubs(:[]).with('fs.chunks').returns(@chunks)
15
32
  @db.stubs(:safe)
33
+ @db.stubs(:read).returns(:primary)
16
34
  end
17
35
 
18
- context "Grid classe with standard connections" do
36
+ context "Grid classes with standard connections" do
19
37
  setup do
20
- @conn.expects(:slave_ok?).returns(false)
38
+ @chunks.expects(:ensure_index)
21
39
  end
22
40
 
23
41
  should "create indexes for Grid" do
24
- @chunks.expects(:create_index)
25
42
  Grid.new(@db)
26
43
  end
27
44
 
28
45
  should "create indexes for GridFileSystem" do
29
- @files.expects(:create_index)
30
- @chunks.expects(:create_index)
46
+ @files.expects(:ensure_index)
31
47
  GridFileSystem.new(@db)
32
48
  end
33
49
  end
34
50
 
35
51
  context "Grid classes with slave connection" do
36
52
  setup do
37
- @conn.expects(:slave_ok?).returns(true)
53
+ @chunks.stubs(:ensure_index).raises(Mongo::ConnectionFailure)
54
+ @files.stubs(:ensure_index).raises(Mongo::ConnectionFailure)
38
55
  end
39
56
 
40
57
  should "not create indexes for Grid" do
41
- Grid.new(@db)
58
+ grid = Grid.new(@db)
59
+ data = "hello world!"
60
+ assert_raise Mongo::ConnectionFailure do
61
+ grid.put(data)
62
+ end
42
63
  end
43
64
 
44
65
  should "not create indexes for GridFileSystem" do
45
- GridFileSystem.new(@db)
66
+ gridfs = GridFileSystem.new(@db)
67
+ data = "hello world!"
68
+ assert_raise Mongo::ConnectionFailure do
69
+ gridfs.open('image.jpg', 'w') do |f|
70
+ f.write data
71
+ end
72
+ end
46
73
  end
47
74
  end
48
75
  end
49
- end
76
+ end
@@ -0,0 +1,48 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "test_helper"
16
+
17
+ class MongoShardedClientUnitTest < Test::Unit::TestCase
18
+ include Mongo
19
+
20
+ def test_initialize_with_single_mongos_uri
21
+ uri = "mongodb://localhost:27017"
22
+ with_preserved_env_uri(uri) do
23
+ client = MongoShardedClient.new(:connect => false)
24
+ assert_equal [[ "localhost", 27017 ]], client.seeds
25
+ end
26
+ end
27
+
28
+ def test_initialize_with_multiple_mongos_uris
29
+ uri = "mongodb://localhost:27017,localhost:27018"
30
+ with_preserved_env_uri(uri) do
31
+ client = MongoShardedClient.new(:connect => false)
32
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
33
+ end
34
+ end
35
+
36
+ def test_from_uri_with_string
37
+ client = MongoShardedClient.from_uri("mongodb://localhost:27017,localhost:27018", :connect => false)
38
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
39
+ end
40
+
41
+ def test_from_uri_with_env_variable
42
+ uri = "mongodb://localhost:27017,localhost:27018"
43
+ with_preserved_env_uri(uri) do
44
+ client = MongoShardedClient.from_uri(nil, :connect => false)
45
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,93 @@
1
+ # Copyright (C) 2009-2013 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'test_helper'
16
+
17
+ class NodeUnitTest < Test::Unit::TestCase
18
+
19
+ def setup
20
+ @client = stub()
21
+ manager = mock('pool_manager')
22
+ manager.stubs(:update_max_sizes)
23
+ @client.stubs(:local_manager).returns(manager)
24
+ end
25
+
26
+ should "refuse to connect to node without 'hosts' key" do
27
+ tcp = mock()
28
+ node = Node.new(@client, ['localhost', 27017])
29
+ tcp.stubs(:new).returns(new_mock_socket)
30
+ @client.stubs(:socket_class).returns(tcp)
31
+
32
+ admin_db = new_mock_db
33
+ admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1})
34
+ @client.stubs(:[]).with('admin').returns(admin_db)
35
+ @client.stubs(:op_timeout).returns(nil)
36
+ @client.stubs(:connect_timeout).returns(nil)
37
+ @client.expects(:log)
38
+ @client.expects(:mongos?).returns(false)
39
+ @client.stubs(:socket_opts)
40
+
41
+ assert node.connect
42
+ node.config
43
+ end
44
+
45
+ should "load a node from an array" do
46
+ node = Node.new(@client, ['power.level.com', 9001])
47
+ assert_equal 'power.level.com', node.host
48
+ assert_equal 9001, node.port
49
+ assert_equal 'power.level.com:9001', node.address
50
+ end
51
+
52
+ should "should default the port for an array" do
53
+ node = Node.new(@client, ['power.level.com'])
54
+ assert_equal 'power.level.com', node.host
55
+ assert_equal MongoClient::DEFAULT_PORT, node.port
56
+ assert_equal "power.level.com:#{MongoClient::DEFAULT_PORT}", node.address
57
+ end
58
+
59
+ should "load a node from a string" do
60
+ node = Node.new(@client, 'localhost:1234')
61
+ assert_equal 'localhost', node.host
62
+ assert_equal 1234, node.port
63
+ assert_equal 'localhost:1234', node.address
64
+ end
65
+
66
+ should "should default the port for a string" do
67
+ node = Node.new(@client, '192.168.0.1')
68
+ assert_equal '192.168.0.1', node.host
69
+ assert_equal MongoClient::DEFAULT_PORT, node.port
70
+ assert_equal "192.168.0.1:#{MongoClient::DEFAULT_PORT}", node.address
71
+ end
72
+
73
+ should "two nodes with the same address should be equal" do
74
+ assert_equal Node.new(@client, '192.168.0.1'),
75
+ Node.new(@client, ['192.168.0.1', MongoClient::DEFAULT_PORT])
76
+ end
77
+
78
+ should "two nodes with the same address should have the same hash" do
79
+ assert_equal Node.new(@client, '192.168.0.1').hash,
80
+ Node.new(@client, ['192.168.0.1', MongoClient::DEFAULT_PORT]).hash
81
+ end
82
+
83
+ should "two nodes with different addresses should not be equal" do
84
+ assert_not_equal Node.new(@client, '192.168.0.2'),
85
+ Node.new(@client, ['192.168.0.1', MongoClient::DEFAULT_PORT])
86
+ end
87
+
88
+ should "two nodes with the same address should have the same hash negate" do
89
+ assert_not_equal Node.new(@client, '192.168.0.1').hash,
90
+ Node.new(@client, '1239.33.4.2393:29949').hash
91
+ end
92
+
93
+ end