mongo 1.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE +190 -0
  5. data/README.md +149 -0
  6. data/Rakefile +31 -0
  7. data/VERSION +1 -0
  8. data/bin/mongo_console +43 -0
  9. data/ext/jsasl/target/jsasl.jar +0 -0
  10. data/lib/mongo.rb +90 -0
  11. data/lib/mongo/bulk_write_collection_view.rb +380 -0
  12. data/lib/mongo/collection.rb +1164 -0
  13. data/lib/mongo/collection_writer.rb +364 -0
  14. data/lib/mongo/connection.rb +19 -0
  15. data/lib/mongo/connection/node.rb +239 -0
  16. data/lib/mongo/connection/pool.rb +347 -0
  17. data/lib/mongo/connection/pool_manager.rb +325 -0
  18. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  21. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  22. data/lib/mongo/connection/socket/tcp_socket.rb +86 -0
  23. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  24. data/lib/mongo/cursor.rb +719 -0
  25. data/lib/mongo/db.rb +735 -0
  26. data/lib/mongo/exception.rb +88 -0
  27. data/lib/mongo/functional.rb +21 -0
  28. data/lib/mongo/functional/authentication.rb +318 -0
  29. data/lib/mongo/functional/logging.rb +85 -0
  30. data/lib/mongo/functional/read_preference.rb +174 -0
  31. data/lib/mongo/functional/sasl_java.rb +48 -0
  32. data/lib/mongo/functional/uri_parser.rb +374 -0
  33. data/lib/mongo/functional/write_concern.rb +66 -0
  34. data/lib/mongo/gridfs.rb +18 -0
  35. data/lib/mongo/gridfs/grid.rb +112 -0
  36. data/lib/mongo/gridfs/grid_ext.rb +53 -0
  37. data/lib/mongo/gridfs/grid_file_system.rb +163 -0
  38. data/lib/mongo/gridfs/grid_io.rb +484 -0
  39. data/lib/mongo/legacy.rb +140 -0
  40. data/lib/mongo/mongo_client.rb +702 -0
  41. data/lib/mongo/mongo_replica_set_client.rb +523 -0
  42. data/lib/mongo/mongo_sharded_client.rb +159 -0
  43. data/lib/mongo/networking.rb +370 -0
  44. data/lib/mongo/utils.rb +19 -0
  45. data/lib/mongo/utils/conversions.rb +110 -0
  46. data/lib/mongo/utils/core_ext.rb +70 -0
  47. data/lib/mongo/utils/server_version.rb +69 -0
  48. data/lib/mongo/utils/support.rb +80 -0
  49. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  50. data/mongo.gemspec +36 -0
  51. data/test/functional/authentication_test.rb +35 -0
  52. data/test/functional/bulk_api_stress_test.rb +133 -0
  53. data/test/functional/bulk_write_collection_view_test.rb +1129 -0
  54. data/test/functional/client_test.rb +565 -0
  55. data/test/functional/collection_test.rb +2073 -0
  56. data/test/functional/collection_writer_test.rb +83 -0
  57. data/test/functional/conversions_test.rb +163 -0
  58. data/test/functional/cursor_fail_test.rb +63 -0
  59. data/test/functional/cursor_message_test.rb +57 -0
  60. data/test/functional/cursor_test.rb +625 -0
  61. data/test/functional/db_api_test.rb +819 -0
  62. data/test/functional/db_connection_test.rb +27 -0
  63. data/test/functional/db_test.rb +344 -0
  64. data/test/functional/grid_file_system_test.rb +285 -0
  65. data/test/functional/grid_io_test.rb +252 -0
  66. data/test/functional/grid_test.rb +273 -0
  67. data/test/functional/pool_test.rb +62 -0
  68. data/test/functional/safe_test.rb +98 -0
  69. data/test/functional/ssl_test.rb +29 -0
  70. data/test/functional/support_test.rb +62 -0
  71. data/test/functional/timeout_test.rb +58 -0
  72. data/test/functional/uri_test.rb +330 -0
  73. data/test/functional/write_concern_test.rb +118 -0
  74. data/test/helpers/general.rb +50 -0
  75. data/test/helpers/test_unit.rb +317 -0
  76. data/test/replica_set/authentication_test.rb +35 -0
  77. data/test/replica_set/basic_test.rb +174 -0
  78. data/test/replica_set/client_test.rb +341 -0
  79. data/test/replica_set/complex_connect_test.rb +77 -0
  80. data/test/replica_set/connection_test.rb +138 -0
  81. data/test/replica_set/count_test.rb +64 -0
  82. data/test/replica_set/cursor_test.rb +212 -0
  83. data/test/replica_set/insert_test.rb +140 -0
  84. data/test/replica_set/max_values_test.rb +145 -0
  85. data/test/replica_set/pinning_test.rb +55 -0
  86. data/test/replica_set/query_test.rb +73 -0
  87. data/test/replica_set/read_preference_test.rb +214 -0
  88. data/test/replica_set/refresh_test.rb +175 -0
  89. data/test/replica_set/replication_ack_test.rb +94 -0
  90. data/test/replica_set/ssl_test.rb +32 -0
  91. data/test/sharded_cluster/basic_test.rb +197 -0
  92. data/test/shared/authentication/basic_auth_shared.rb +286 -0
  93. data/test/shared/authentication/bulk_api_auth_shared.rb +259 -0
  94. data/test/shared/authentication/gssapi_shared.rb +164 -0
  95. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  96. data/test/shared/ssl_shared.rb +235 -0
  97. data/test/test_helper.rb +56 -0
  98. data/test/threading/basic_test.rb +120 -0
  99. data/test/tools/mongo_config.rb +608 -0
  100. data/test/tools/mongo_config_test.rb +160 -0
  101. data/test/unit/client_test.rb +347 -0
  102. data/test/unit/collection_test.rb +166 -0
  103. data/test/unit/connection_test.rb +325 -0
  104. data/test/unit/cursor_test.rb +299 -0
  105. data/test/unit/db_test.rb +136 -0
  106. data/test/unit/grid_test.rb +76 -0
  107. data/test/unit/mongo_sharded_client_test.rb +48 -0
  108. data/test/unit/node_test.rb +93 -0
  109. data/test/unit/pool_manager_test.rb +142 -0
  110. data/test/unit/read_pref_test.rb +115 -0
  111. data/test/unit/read_test.rb +159 -0
  112. data/test/unit/safe_test.rb +158 -0
  113. data/test/unit/sharding_pool_manager_test.rb +84 -0
  114. data/test/unit/write_concern_test.rb +175 -0
  115. metadata +260 -0
  116. metadata.gz.sig +0 -0
@@ -0,0 +1,299 @@
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
21
+
22
+ context "Cursor options" do
23
+ setup do
24
+ @logger = mock()
25
+ @logger.stubs(:debug)
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)
34
+ @cursor = Cursor.new(@collection)
35
+ end
36
+
37
+ should "set timeout" do
38
+ assert @cursor.timeout
39
+ assert @cursor.query_options_hash[:timeout]
40
+ end
41
+
42
+ should "set selector" do
43
+ assert_equal({}, @cursor.selector)
44
+
45
+ @cursor = Cursor.new(@collection, :selector => {:name => "Jones"})
46
+ assert_equal({:name => "Jones"}, @cursor.selector)
47
+ assert_equal({:name => "Jones"}, @cursor.query_options_hash[:selector])
48
+ end
49
+
50
+ should "set fields" do
51
+ assert_nil @cursor.fields
52
+
53
+ @cursor = Cursor.new(@collection, :fields => [:name, :date])
54
+ assert_equal({:name => 1, :date => 1}, @cursor.fields)
55
+ assert_equal({:name => 1, :date => 1}, @cursor.query_options_hash[:fields])
56
+ end
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
+
70
+ should "set mix fields 0 and 1" do
71
+ assert_nil @cursor.fields
72
+
73
+ @cursor = Cursor.new(@collection, :fields => {:name => 1, :date => 0})
74
+ assert_equal({:name => 1, :date => 0}, @cursor.fields)
75
+ assert_equal({:name => 1, :date => 0}, @cursor.query_options_hash[:fields])
76
+ end
77
+
78
+ should "set limit" do
79
+ assert_equal 0, @cursor.limit
80
+
81
+ @cursor = Cursor.new(@collection, :limit => 10)
82
+ assert_equal 10, @cursor.limit
83
+ assert_equal 10, @cursor.query_options_hash[:limit]
84
+ end
85
+
86
+
87
+ should "set skip" do
88
+ assert_equal 0, @cursor.skip
89
+
90
+ @cursor = Cursor.new(@collection, :skip => 5)
91
+ assert_equal 5, @cursor.skip
92
+ assert_equal 5, @cursor.query_options_hash[:skip]
93
+ end
94
+
95
+ should "set sort order" do
96
+ assert_nil @cursor.order
97
+
98
+ @cursor = Cursor.new(@collection, :order => "last_name")
99
+ assert_equal "last_name", @cursor.order
100
+ assert_equal "last_name", @cursor.query_options_hash[:order]
101
+ end
102
+
103
+ should "set hint" do
104
+ assert_nil @cursor.hint
105
+
106
+ @cursor = Cursor.new(@collection, :hint => "name")
107
+ assert_equal "name", @cursor.hint
108
+ assert_equal "name", @cursor.query_options_hash[:hint]
109
+ end
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
+
119
+ should "cache full collection name" do
120
+ assert_equal "testing.items", @cursor.full_collection_name
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 "conected to mongos" do
142
+ setup do
143
+ @connection.stubs(:mongos?).returns(true)
144
+ @tag_sets = [{:dc => "ny"}]
145
+ end
146
+
147
+ should "set $readPreference" do
148
+ # secondary
149
+ cursor = Cursor.new(@collection, { :read => :secondary })
150
+
151
+ spec = cursor.construct_query_spec
152
+ assert spec.has_key?('$readPreference')
153
+ assert_equal 'secondary', spec['$readPreference'][:mode]
154
+ assert !spec['$readPreference'].has_key?(:tags)
155
+
156
+ # secondary preferred with tags
157
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => @tag_sets })
158
+
159
+ spec = cursor.construct_query_spec
160
+ assert spec.has_key?('$readPreference')
161
+ assert_equal 'secondaryPreferred', spec['$readPreference'][:mode]
162
+ assert_equal @tag_sets, spec['$readPreference'][:tags]
163
+
164
+ # primary preferred
165
+ cursor = Cursor.new(@collection, { :read => :primary_preferred })
166
+
167
+ spec = cursor.construct_query_spec
168
+ assert spec.has_key?('$readPreference')
169
+ assert_equal 'primaryPreferred', spec['$readPreference'][:mode]
170
+ assert !spec['$readPreference'].has_key?(:tags)
171
+
172
+ # primary preferred with tags
173
+ cursor = Cursor.new(@collection, { :read => :primary_preferred, :tag_sets => @tag_sets })
174
+
175
+ spec = cursor.construct_query_spec
176
+ assert spec.has_key?('$readPreference')
177
+ assert_equal 'primaryPreferred', spec['$readPreference'][:mode]
178
+ assert_equal @tag_sets, spec['$readPreference'][:tags]
179
+
180
+ # nearest
181
+ cursor = Cursor.new(@collection, { :read => :nearest })
182
+
183
+ spec = cursor.construct_query_spec
184
+ assert spec.has_key?('$readPreference')
185
+ assert_equal 'nearest', spec['$readPreference'][:mode]
186
+ assert !spec['$readPreference'].has_key?(:tags)
187
+
188
+ # nearest with tags
189
+ cursor = Cursor.new(@collection, { :read => :nearest, :tag_sets => @tag_sets })
190
+
191
+ spec = cursor.construct_query_spec
192
+ assert spec.has_key?('$readPreference')
193
+ assert_equal 'nearest', spec['$readPreference'][:mode]
194
+ assert_equal @tag_sets, spec['$readPreference'][:tags]
195
+ end
196
+
197
+ should "not set $readPreference" do
198
+ # for primary
199
+ cursor = Cursor.new(@collection, { :read => :primary, :tag_sets => @tag_sets })
200
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
201
+
202
+ # for secondary_preferred with no tags
203
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred })
204
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
205
+
206
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => [] })
207
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
208
+
209
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred, :tag_sets => nil })
210
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
211
+ end
212
+ end
213
+
214
+ context "not conected to mongos" do
215
+ setup do
216
+ @connection.stubs(:mongos?).returns(false)
217
+ end
218
+
219
+ should "not set $readPreference" do
220
+ cursor = Cursor.new(@collection, { :read => :primary })
221
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
222
+
223
+ cursor = Cursor.new(@collection, { :read => :primary_preferred })
224
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
225
+
226
+ cursor = Cursor.new(@collection, { :read => :secondary })
227
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
228
+
229
+ cursor = Cursor.new(@collection, { :read => :secondary_preferred })
230
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
231
+
232
+ cursor = Cursor.new(@collection, { :read => :nearest })
233
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
234
+
235
+ cursor = Cursor.new(@collection, { :read => :secondary , :tag_sets => @tag_sets})
236
+ assert !cursor.construct_query_spec.has_key?('$readPreference')
237
+ end
238
+ end
239
+ end
240
+
241
+ context "Query fields" do
242
+ setup do
243
+ @logger = mock()
244
+ @logger.stubs(:debug)
245
+ @connection = stub(:class => MongoClient, :logger => @logger, :slave_ok? => false,
246
+ :log_duration => false, :tag_sets =>{}, :acceptable_latency => 10)
247
+ @db = stub(:slave_ok? => true, :name => "testing", :connection => @connection,
248
+ :tag_sets => {}, :acceptable_latency => 10)
249
+ @collection = stub(:db => @db, :name => "items", :read => :primary,
250
+ :tag_sets => {}, :acceptable_latency => 10)
251
+ end
252
+
253
+ should "when an array should return a hash with each key" do
254
+ @cursor = Cursor.new(@collection, :fields => [:name, :age])
255
+ result = @cursor.fields
256
+ assert_equal result.keys.sort{|a,b| a.to_s <=> b.to_s}, [:age, :name].sort{|a,b| a.to_s <=> b.to_s}
257
+ assert result.values.all? {|v| v == 1}
258
+ end
259
+
260
+ should "when a string, return a hash with just the key" do
261
+ @cursor = Cursor.new(@collection, :fields => "name")
262
+ result = @cursor.fields
263
+ assert_equal result.keys.sort, ["name"]
264
+ assert result.values.all? {|v| v == 1}
265
+ end
266
+
267
+ should "return nil when neither hash nor string nor symbol" do
268
+ @cursor = Cursor.new(@collection, :fields => 1234567)
269
+ assert_nil @cursor.fields
270
+ end
271
+ end
272
+
273
+ context "counts" do
274
+ setup do
275
+ @logger = mock()
276
+ @logger.stubs(:debug)
277
+ @connection = stub(:class => Connection, :logger => @logger,
278
+ :slave_ok? => false, :read => :primary, :log_duration => false,
279
+ :tag_sets => {}, :acceptable_latency => 10)
280
+ @db = stub(:name => "testing", :slave_ok? => false,
281
+ :connection => @connection, :read => :primary,
282
+ :tag_sets => {}, :acceptable_latency => 10)
283
+ @collection = stub(:db => @db, :name => "items", :read => :primary,
284
+ :tag_sets => {}, :acceptable_latency => 10)
285
+ @cursor = Cursor.new(@collection)
286
+ end
287
+
288
+ should "pass the comment parameter" do
289
+ query = {:field => 7}
290
+ @db.expects(:command).with({ 'count' => "items",
291
+ 'query' => query,
292
+ 'fields' => nil},
293
+ { :read => :primary,
294
+ :comment => "my comment"}).
295
+ returns({'ok' => 1, 'n' => 1})
296
+ assert_equal(1, Cursor.new(@collection, :selector => query, :comment => 'my comment').count())
297
+ end
298
+ end
299
+ end
@@ -0,0 +1,136 @@
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
+ def insert_message(db, documents)
18
+ documents = [documents] unless documents.is_a?(Array)
19
+ message = ByteBuffer.new
20
+ message.put_int(0)
21
+ Mongo::BSON_CODER.serialize_cstr(message, "#{db.name}.test")
22
+ documents.each { |doc| message.put_array(Mongo::BSON_CODER.new.serialize(doc, true).to_a) }
23
+ message = db.add_message_headers(Mongo::Constants::OP_INSERT, message)
24
+ end
25
+
26
+ class DBUnitTest < Test::Unit::TestCase
27
+ context "DBTest: " do
28
+ context "DB commands" do
29
+ setup do
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)
37
+ @db.stubs(:safe)
38
+ @db.stubs(:read)
39
+ @db.stubs(:tag_sets)
40
+ @db.stubs(:acceptable_latency)
41
+ @collection = mock()
42
+ @db.stubs(:system_command_collection).returns(@collection)
43
+ end
44
+
45
+ should "raise an error if given a hash with more than one key" do
46
+ if RUBY_VERSION < '1.9'
47
+ assert_raise MongoArgumentError do
48
+ @db.command(:buildinfo => 1, :somekey => 1)
49
+ end
50
+ end
51
+ end
52
+
53
+ should "raise an error if the selector is omitted" do
54
+ assert_raise MongoArgumentError do
55
+ @db.command({}, :check_response => true)
56
+ end
57
+ end
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
+
67
+ should "create the proper cursor" do
68
+ @cursor = mock(:next_document => {"ok" => 1})
69
+ Cursor.expects(:new).with(@collection,
70
+ :limit => -1, :selector => {:buildinfo => 1}).returns(@cursor)
71
+ command = {:buildinfo => 1}
72
+ @db.command(command, :check_response => true)
73
+ end
74
+
75
+ should "raise an error when the command fails" do
76
+ @cursor = mock(:next_document => {"ok" => 0})
77
+ Cursor.expects(:new).with(@collection,
78
+ :limit => -1, :selector => {:buildinfo => 1}).returns(@cursor)
79
+ assert_raise OperationFailure do
80
+ command = {:buildinfo => 1}
81
+ @db.command(command, :check_response => true)
82
+ end
83
+ end
84
+
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')
93
+ end
94
+ end
95
+
96
+ should "raise an error if collection creation fails" do
97
+ @db.expects(:command).returns({'ok' => 0})
98
+ assert_raise Mongo::MongoDBError do
99
+ @db.create_collection("foo")
100
+ end
101
+ end
102
+
103
+ should "raise an error if getlasterror fails" do
104
+ @db.expects(:command).returns({})
105
+ assert_raise Mongo::MongoDBError do
106
+ @db.get_last_error
107
+ end
108
+ end
109
+
110
+ should "raise an error if drop_index fails" do
111
+ @db.expects(:command).returns({})
112
+ assert_raise Mongo::MongoDBError do
113
+ @db.drop_index("foo", "bar")
114
+ end
115
+ end
116
+
117
+ should "raise an error if set_profiling_level fails" do
118
+ @db.expects(:command).returns({})
119
+ assert_raise Mongo::MongoDBError do
120
+ @db.profiling_level = :slow_only
121
+ end
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 = { :gssapiservicename => 'example', :canonicalizehostname => 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
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,76 @@
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 GridUnitTest < Test::Unit::TestCase
18
+
19
+ context "GridFS: " do
20
+ setup do
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)
27
+ @files = mock()
28
+ @chunks = mock()
29
+
30
+ @db.stubs(:[]).with('fs.files').returns(@files)
31
+ @db.stubs(:[]).with('fs.chunks').returns(@chunks)
32
+ @db.stubs(:safe)
33
+ @db.stubs(:read).returns(:primary)
34
+ end
35
+
36
+ context "Grid classes with standard connections" do
37
+ setup do
38
+ @chunks.expects(:ensure_index)
39
+ end
40
+
41
+ should "create indexes for Grid" do
42
+ Grid.new(@db)
43
+ end
44
+
45
+ should "create indexes for GridFileSystem" do
46
+ @files.expects(:ensure_index)
47
+ GridFileSystem.new(@db)
48
+ end
49
+ end
50
+
51
+ context "Grid classes with slave connection" do
52
+ setup do
53
+ @chunks.stubs(:ensure_index).raises(Mongo::ConnectionFailure)
54
+ @files.stubs(:ensure_index).raises(Mongo::ConnectionFailure)
55
+ end
56
+
57
+ should "not create indexes for Grid" do
58
+ grid = Grid.new(@db)
59
+ data = "hello world!"
60
+ assert_raise Mongo::ConnectionFailure do
61
+ grid.put(data)
62
+ end
63
+ end
64
+
65
+ should "not create indexes for GridFileSystem" do
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
73
+ end
74
+ end
75
+ end
76
+ end