mongo 1.0 → 1.1.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.
- data/LICENSE.txt +1 -13
- data/{README.rdoc → README.md} +129 -149
- data/Rakefile +94 -58
- data/bin/mongo_console +21 -0
- data/docs/1.0_UPGRADE.md +21 -0
- data/docs/CREDITS.md +123 -0
- data/docs/FAQ.md +112 -0
- data/docs/GridFS.md +158 -0
- data/docs/HISTORY.md +185 -0
- data/docs/REPLICA_SETS.md +75 -0
- data/docs/TUTORIAL.md +247 -0
- data/docs/WRITE_CONCERN.md +28 -0
- data/lib/mongo/collection.rb +225 -105
- data/lib/mongo/connection.rb +374 -315
- data/lib/mongo/cursor.rb +122 -77
- data/lib/mongo/db.rb +109 -85
- data/lib/mongo/exceptions.rb +6 -0
- data/lib/mongo/gridfs/grid.rb +19 -11
- data/lib/mongo/gridfs/grid_ext.rb +36 -9
- data/lib/mongo/gridfs/grid_file_system.rb +15 -9
- data/lib/mongo/gridfs/grid_io.rb +49 -16
- data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
- data/lib/mongo/repl_set_connection.rb +290 -0
- data/lib/mongo/util/conversions.rb +3 -1
- data/lib/mongo/util/core_ext.rb +17 -4
- data/lib/mongo/util/pool.rb +125 -0
- data/lib/mongo/util/server_version.rb +2 -0
- data/lib/mongo/util/support.rb +12 -0
- data/lib/mongo/util/uri_parser.rb +71 -0
- data/lib/mongo.rb +23 -7
- data/{mongo-ruby-driver.gemspec → mongo.gemspec} +9 -7
- data/test/auxillary/1.4_features.rb +2 -2
- data/test/auxillary/authentication_test.rb +1 -1
- data/test/auxillary/autoreconnect_test.rb +1 -1
- data/test/{slave_connection_test.rb → auxillary/slave_connection_test.rb} +6 -6
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +537 -0
- data/test/bson/byte_buffer_test.rb +190 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +141 -0
- data/test/bson/ordered_hash_test.rb +197 -0
- data/test/collection_test.rb +195 -15
- data/test/connection_test.rb +93 -56
- data/test/conversions_test.rb +1 -1
- data/test/cursor_fail_test.rb +75 -0
- data/test/cursor_message_test.rb +43 -0
- data/test/cursor_test.rb +93 -32
- data/test/db_api_test.rb +28 -55
- data/test/db_connection_test.rb +2 -3
- data/test/db_test.rb +45 -40
- data/test/grid_file_system_test.rb +14 -6
- data/test/grid_io_test.rb +36 -7
- data/test/grid_test.rb +54 -10
- data/test/replica_sets/connect_test.rb +84 -0
- data/test/replica_sets/count_test.rb +35 -0
- data/test/{replica → replica_sets}/insert_test.rb +17 -14
- data/test/replica_sets/pooled_insert_test.rb +55 -0
- data/test/replica_sets/query_secondaries.rb +80 -0
- data/test/replica_sets/query_test.rb +41 -0
- data/test/replica_sets/replication_ack_test.rb +64 -0
- data/test/replica_sets/rs_test_helper.rb +29 -0
- data/test/safe_test.rb +68 -0
- data/test/support/hash_with_indifferent_access.rb +199 -0
- data/test/support/keys.rb +45 -0
- data/test/support_test.rb +19 -0
- data/test/test_helper.rb +53 -15
- data/test/threading/{test_threading_large_pool.rb → threading_with_large_pool_test.rb} +2 -2
- data/test/threading_test.rb +2 -2
- data/test/tools/repl_set_manager.rb +241 -0
- data/test/tools/test.rb +13 -0
- data/test/unit/collection_test.rb +70 -7
- data/test/unit/connection_test.rb +18 -39
- data/test/unit/cursor_test.rb +7 -8
- data/test/unit/db_test.rb +14 -17
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/pool_test.rb +9 -0
- data/test/unit/repl_set_connection_test.rb +82 -0
- data/test/unit/safe_test.rb +125 -0
- metadata +132 -51
- data/bin/bson_benchmark.rb +0 -59
- data/bin/fail_if_no_c.rb +0 -11
- 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 -70
- data/examples/simple.rb +0 -24
- data/examples/strict.rb +0 -35
- data/examples/types.rb +0 -36
- data/test/replica/count_test.rb +0 -34
- data/test/replica/pooled_insert_test.rb +0 -54
- data/test/replica/query_test.rb +0 -39
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
require 'logger'
|
|
3
|
+
|
|
4
|
+
class CursorTest < Test::Unit::TestCase
|
|
5
|
+
|
|
6
|
+
include Mongo
|
|
7
|
+
|
|
8
|
+
@@connection = standard_connection
|
|
9
|
+
@@db = @@connection.db(MONGO_TEST_DB)
|
|
10
|
+
@@coll = @@db.collection('test')
|
|
11
|
+
@@version = @@connection.server_version
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
@@coll.remove
|
|
15
|
+
@@coll.insert('a' => 1) # collection not created until it's used
|
|
16
|
+
@@coll_full_name = "#{MONGO_TEST_DB}.test"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_refill_via_get_more
|
|
20
|
+
assert_equal 1, @@coll.count
|
|
21
|
+
1000.times { |i|
|
|
22
|
+
assert_equal 1 + i, @@coll.count
|
|
23
|
+
@@coll.insert('a' => i)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
assert_equal 1001, @@coll.count
|
|
27
|
+
count = 0
|
|
28
|
+
@@coll.find.each { |obj|
|
|
29
|
+
count += obj['a']
|
|
30
|
+
}
|
|
31
|
+
assert_equal 1001, @@coll.count
|
|
32
|
+
|
|
33
|
+
# do the same thing again for debugging
|
|
34
|
+
assert_equal 1001, @@coll.count
|
|
35
|
+
count2 = 0
|
|
36
|
+
@@coll.find.each { |obj|
|
|
37
|
+
count2 += obj['a']
|
|
38
|
+
}
|
|
39
|
+
assert_equal 1001, @@coll.count
|
|
40
|
+
|
|
41
|
+
assert_equal count, count2
|
|
42
|
+
assert_equal 499501, count
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_refill_via_get_more_alt_coll
|
|
46
|
+
coll = @@db.collection('test-alt-coll')
|
|
47
|
+
coll.remove
|
|
48
|
+
coll.insert('a' => 1) # collection not created until it's used
|
|
49
|
+
assert_equal 1, coll.count
|
|
50
|
+
|
|
51
|
+
1000.times { |i|
|
|
52
|
+
assert_equal 1 + i, coll.count
|
|
53
|
+
coll.insert('a' => i)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
assert_equal 1001, coll.count
|
|
57
|
+
count = 0
|
|
58
|
+
coll.find.each { |obj|
|
|
59
|
+
count += obj['a']
|
|
60
|
+
}
|
|
61
|
+
assert_equal 1001, coll.count
|
|
62
|
+
|
|
63
|
+
# do the same thing again for debugging
|
|
64
|
+
assert_equal 1001, coll.count
|
|
65
|
+
count2 = 0
|
|
66
|
+
coll.find.each { |obj|
|
|
67
|
+
count2 += obj['a']
|
|
68
|
+
}
|
|
69
|
+
assert_equal 1001, coll.count
|
|
70
|
+
|
|
71
|
+
assert_equal count, count2
|
|
72
|
+
assert_equal 499501, count
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
require 'logger'
|
|
3
|
+
|
|
4
|
+
class CursorTest < Test::Unit::TestCase
|
|
5
|
+
|
|
6
|
+
include Mongo
|
|
7
|
+
|
|
8
|
+
@@connection = standard_connection
|
|
9
|
+
@@db = @@connection.db(MONGO_TEST_DB)
|
|
10
|
+
@@coll = @@db.collection('test')
|
|
11
|
+
@@version = @@connection.server_version
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
@@coll.remove
|
|
15
|
+
@@coll.insert('a' => 1) # collection not created until it's used
|
|
16
|
+
@@coll_full_name = "#{MONGO_TEST_DB}.test"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_valid_batch_sizes
|
|
20
|
+
assert_raise ArgumentError do
|
|
21
|
+
@@coll.find({}, :batch_size => 1, :limit => 5)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
assert_raise ArgumentError do
|
|
25
|
+
@@coll.find({}, :batch_size => -1, :limit => 5)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
assert @@coll.find({}, :batch_size => 0, :limit => 5)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_batch_size
|
|
32
|
+
@@coll.remove
|
|
33
|
+
200.times do |n|
|
|
34
|
+
@@coll.insert({:a => n})
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
list = @@coll.find({}, :batch_size => 2, :limit => 6).to_a
|
|
38
|
+
assert_equal 6, list.length
|
|
39
|
+
|
|
40
|
+
list = @@coll.find({}, :batch_size => 100, :limit => 101).to_a
|
|
41
|
+
assert_equal 101, list.length
|
|
42
|
+
end
|
|
43
|
+
end
|
data/test/cursor_test.rb
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
require 'test/test_helper'
|
|
1
|
+
require './test/test_helper'
|
|
2
2
|
require 'logger'
|
|
3
3
|
|
|
4
|
-
# NOTE: assumes Mongo is running
|
|
5
4
|
class CursorTest < Test::Unit::TestCase
|
|
6
5
|
|
|
7
6
|
include Mongo
|
|
8
7
|
|
|
9
|
-
@@connection =
|
|
10
|
-
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
|
8
|
+
@@connection = standard_connection
|
|
11
9
|
@@db = @@connection.db(MONGO_TEST_DB)
|
|
12
10
|
@@coll = @@db.collection('test')
|
|
13
11
|
@@version = @@connection.server_version
|
|
@@ -18,6 +16,13 @@ class CursorTest < Test::Unit::TestCase
|
|
|
18
16
|
@@coll_full_name = "#{MONGO_TEST_DB}.test"
|
|
19
17
|
end
|
|
20
18
|
|
|
19
|
+
def test_inspect
|
|
20
|
+
selector = {:a => 1}
|
|
21
|
+
cursor = @@coll.find(selector)
|
|
22
|
+
assert_equal "<Mongo::Cursor:0x#{cursor.object_id.to_s(16)} namespace='#{@@db.name}.#{@@coll.name}' " +
|
|
23
|
+
"@selector=#{selector.inspect}>", cursor.inspect
|
|
24
|
+
end
|
|
25
|
+
|
|
21
26
|
def test_explain
|
|
22
27
|
cursor = @@coll.find('a' => 1)
|
|
23
28
|
explaination = cursor.explain
|
|
@@ -41,6 +46,10 @@ class CursorTest < Test::Unit::TestCase
|
|
|
41
46
|
assert_equal 10, @@coll.find({}, :limit => 5).count()
|
|
42
47
|
assert_equal 10, @@coll.find({}, :skip => 5).count()
|
|
43
48
|
|
|
49
|
+
assert_equal 5, @@coll.find({}, :limit => 5).count(true)
|
|
50
|
+
assert_equal 5, @@coll.find({}, :skip => 5).count(true)
|
|
51
|
+
assert_equal 2, @@coll.find({}, :skip => 5, :limit => 2).count(true)
|
|
52
|
+
|
|
44
53
|
assert_equal 1, @@coll.find({"x" => 1}).count()
|
|
45
54
|
assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
|
|
46
55
|
|
|
@@ -120,18 +129,18 @@ class CursorTest < Test::Unit::TestCase
|
|
|
120
129
|
@@coll.remove
|
|
121
130
|
|
|
122
131
|
t1 = Time.now
|
|
123
|
-
t1_id =
|
|
132
|
+
t1_id = ObjectId.from_time(t1)
|
|
124
133
|
@@coll.save({:t => 't1'})
|
|
125
134
|
@@coll.save({:t => 't1'})
|
|
126
135
|
@@coll.save({:t => 't1'})
|
|
127
136
|
sleep(2)
|
|
128
137
|
t2 = Time.now
|
|
129
|
-
t2_id =
|
|
138
|
+
t2_id = ObjectId.from_time(t2)
|
|
130
139
|
@@coll.save({:t => 't2'})
|
|
131
140
|
@@coll.save({:t => 't2'})
|
|
132
141
|
@@coll.save({:t => 't2'})
|
|
133
142
|
|
|
134
|
-
assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id
|
|
143
|
+
assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id, '$lt' => t2_id}}).count
|
|
135
144
|
@@coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
|
|
136
145
|
assert_equal 't2', doc['t']
|
|
137
146
|
end
|
|
@@ -149,11 +158,34 @@ class CursorTest < Test::Unit::TestCase
|
|
|
149
158
|
assert_equal 5, results.length
|
|
150
159
|
end
|
|
151
160
|
|
|
152
|
-
def
|
|
153
|
-
|
|
154
|
-
|
|
161
|
+
def test_timeout_options
|
|
162
|
+
cursor = Cursor.new(@@coll)
|
|
163
|
+
assert_equal true, cursor.timeout
|
|
164
|
+
|
|
165
|
+
cursor = @@coll.find
|
|
166
|
+
assert_equal true, cursor.timeout
|
|
167
|
+
|
|
168
|
+
cursor = @@coll.find({}, :timeout => nil)
|
|
169
|
+
assert_equal true, cursor.timeout
|
|
170
|
+
|
|
171
|
+
cursor = Cursor.new(@@coll, :timeout => false)
|
|
172
|
+
assert_equal false, cursor.timeout
|
|
173
|
+
|
|
174
|
+
@@coll.find({}, :timeout => false) do |cursor|
|
|
175
|
+
assert_equal false, cursor.timeout
|
|
155
176
|
end
|
|
177
|
+
end
|
|
156
178
|
|
|
179
|
+
def test_timeout
|
|
180
|
+
opts = Cursor.new(@@coll).query_opts
|
|
181
|
+
assert_equal 0, opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
|
|
182
|
+
|
|
183
|
+
opts = Cursor.new(@@coll, :timeout => false).query_opts
|
|
184
|
+
assert_equal Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT,
|
|
185
|
+
opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def test_limit_exceptions
|
|
157
189
|
cursor = @@coll.find()
|
|
158
190
|
firstResult = cursor.next_document
|
|
159
191
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
|
@@ -184,10 +216,6 @@ class CursorTest < Test::Unit::TestCase
|
|
|
184
216
|
end
|
|
185
217
|
|
|
186
218
|
def test_skip_exceptions
|
|
187
|
-
assert_raise ArgumentError do
|
|
188
|
-
cursor = @@coll.find().skip('not-an-integer')
|
|
189
|
-
end
|
|
190
|
-
|
|
191
219
|
cursor = @@coll.find()
|
|
192
220
|
firstResult = cursor.next_document
|
|
193
221
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
|
@@ -294,7 +322,6 @@ class CursorTest < Test::Unit::TestCase
|
|
|
294
322
|
@@coll.drop
|
|
295
323
|
|
|
296
324
|
client_cursors = @@db.command("cursorInfo" => 1)["clientCursors_size"]
|
|
297
|
-
by_location = @@db.command("cursorInfo" => 1)["byLocation_size"]
|
|
298
325
|
|
|
299
326
|
10000.times do |i|
|
|
300
327
|
@@coll.insert("i" => i)
|
|
@@ -302,8 +329,6 @@ class CursorTest < Test::Unit::TestCase
|
|
|
302
329
|
|
|
303
330
|
assert_equal(client_cursors,
|
|
304
331
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
305
|
-
assert_equal(by_location,
|
|
306
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
307
332
|
|
|
308
333
|
10.times do |i|
|
|
309
334
|
@@coll.find_one()
|
|
@@ -311,8 +336,6 @@ class CursorTest < Test::Unit::TestCase
|
|
|
311
336
|
|
|
312
337
|
assert_equal(client_cursors,
|
|
313
338
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
314
|
-
assert_equal(by_location,
|
|
315
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
316
339
|
|
|
317
340
|
10.times do |i|
|
|
318
341
|
a = @@coll.find()
|
|
@@ -322,30 +345,22 @@ class CursorTest < Test::Unit::TestCase
|
|
|
322
345
|
|
|
323
346
|
assert_equal(client_cursors,
|
|
324
347
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
325
|
-
assert_equal(by_location,
|
|
326
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
327
348
|
|
|
328
349
|
a = @@coll.find()
|
|
329
350
|
a.next_document
|
|
330
351
|
|
|
331
352
|
assert_not_equal(client_cursors,
|
|
332
353
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
333
|
-
assert_not_equal(by_location,
|
|
334
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
335
354
|
|
|
336
355
|
a.close()
|
|
337
356
|
|
|
338
357
|
assert_equal(client_cursors,
|
|
339
358
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
340
|
-
assert_equal(by_location,
|
|
341
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
342
359
|
|
|
343
360
|
a = @@coll.find({}, :limit => 10).next_document
|
|
344
361
|
|
|
345
362
|
assert_equal(client_cursors,
|
|
346
363
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
347
|
-
assert_equal(by_location,
|
|
348
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
349
364
|
|
|
350
365
|
@@coll.find() do |cursor|
|
|
351
366
|
cursor.next_document
|
|
@@ -353,8 +368,6 @@ class CursorTest < Test::Unit::TestCase
|
|
|
353
368
|
|
|
354
369
|
assert_equal(client_cursors,
|
|
355
370
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
356
|
-
assert_equal(by_location,
|
|
357
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
358
371
|
|
|
359
372
|
@@coll.find() { |cursor|
|
|
360
373
|
cursor.next_document
|
|
@@ -362,8 +375,6 @@ class CursorTest < Test::Unit::TestCase
|
|
|
362
375
|
|
|
363
376
|
assert_equal(client_cursors,
|
|
364
377
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
|
365
|
-
assert_equal(by_location,
|
|
366
|
-
@@db.command("cursorInfo" => 1)["byLocation_size"])
|
|
367
378
|
end
|
|
368
379
|
|
|
369
380
|
def test_count_with_fields
|
|
@@ -390,4 +401,54 @@ class CursorTest < Test::Unit::TestCase
|
|
|
390
401
|
|
|
391
402
|
assert_equal false, cursor.has_next?
|
|
392
403
|
end
|
|
393
|
-
|
|
404
|
+
|
|
405
|
+
def test_cursor_invalid
|
|
406
|
+
@@coll.remove
|
|
407
|
+
10000.times do |n|
|
|
408
|
+
@@coll.insert({:a => n})
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
cursor = @@coll.find({})
|
|
412
|
+
|
|
413
|
+
assert_raise_error Mongo::OperationFailure, "CURSOR_NOT_FOUND" do
|
|
414
|
+
9999.times do
|
|
415
|
+
cursor.next_document
|
|
416
|
+
cursor.instance_variable_set(:@cursor_id, 1234567890)
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
def test_enumberables
|
|
422
|
+
@@coll.remove
|
|
423
|
+
100.times do |n|
|
|
424
|
+
@@coll.insert({:a => n})
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
assert_equal 100, @@coll.find.to_a.length
|
|
428
|
+
assert_equal 100, @@coll.find.to_set.length
|
|
429
|
+
|
|
430
|
+
cursor = @@coll.find
|
|
431
|
+
50.times { |n| cursor.next_document }
|
|
432
|
+
assert_equal 50, cursor.to_a.length
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def test_rewind
|
|
436
|
+
@@coll.remove
|
|
437
|
+
100.times do |n|
|
|
438
|
+
@@coll.insert({:a => n})
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
cursor = @@coll.find
|
|
442
|
+
cursor.to_a
|
|
443
|
+
assert_equal [], cursor.map {|doc| doc }
|
|
444
|
+
|
|
445
|
+
cursor.rewind!
|
|
446
|
+
assert_equal 100, cursor.map {|doc| doc }.length
|
|
447
|
+
|
|
448
|
+
cursor.rewind!
|
|
449
|
+
5.times { cursor.next_document }
|
|
450
|
+
cursor.rewind!
|
|
451
|
+
assert_equal 100, cursor.map {|doc| doc }.length
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
end
|
data/test/db_api_test.rb
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
require 'test/test_helper'
|
|
1
|
+
require './test/test_helper'
|
|
2
2
|
|
|
3
|
-
# NOTE: assumes Mongo is running
|
|
4
3
|
class DBAPITest < Test::Unit::TestCase
|
|
5
4
|
include Mongo
|
|
6
5
|
include BSON
|
|
7
6
|
|
|
8
|
-
@@conn =
|
|
9
|
-
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT)
|
|
7
|
+
@@conn = standard_connection
|
|
10
8
|
@@db = @@conn.db(MONGO_TEST_DB)
|
|
11
9
|
@@coll = @@db.collection('test')
|
|
12
10
|
@@version = @@conn.server_version
|
|
@@ -20,7 +18,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
20
18
|
|
|
21
19
|
def teardown
|
|
22
20
|
@@coll.remove
|
|
23
|
-
@@db.
|
|
21
|
+
@@db.get_last_error
|
|
24
22
|
end
|
|
25
23
|
|
|
26
24
|
def test_clear
|
|
@@ -30,8 +28,8 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
30
28
|
end
|
|
31
29
|
|
|
32
30
|
def test_insert
|
|
33
|
-
assert_kind_of BSON::
|
|
34
|
-
assert_kind_of BSON::
|
|
31
|
+
assert_kind_of BSON::ObjectId, @@coll.insert('a' => 2)
|
|
32
|
+
assert_kind_of BSON::ObjectId, @@coll.insert('b' => 3)
|
|
35
33
|
|
|
36
34
|
assert_equal 3, @@coll.count
|
|
37
35
|
docs = @@coll.find().to_a
|
|
@@ -47,14 +45,14 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
47
45
|
end
|
|
48
46
|
|
|
49
47
|
def test_save_ordered_hash
|
|
50
|
-
oh = OrderedHash.new
|
|
48
|
+
oh = BSON::OrderedHash.new
|
|
51
49
|
oh['a'] = -1
|
|
52
50
|
oh['b'] = 'foo'
|
|
53
51
|
|
|
54
52
|
oid = @@coll.save(oh)
|
|
55
53
|
assert_equal 'foo', @@coll.find_one(oid)['b']
|
|
56
54
|
|
|
57
|
-
oh = OrderedHash['a' => 1, 'b' => 'foo']
|
|
55
|
+
oh = BSON::OrderedHash['a' => 1, 'b' => 'foo']
|
|
58
56
|
oid = @@coll.save(oh)
|
|
59
57
|
assert_equal 'foo', @@coll.find_one(oid)['b']
|
|
60
58
|
end
|
|
@@ -63,7 +61,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
63
61
|
ids = @@coll.insert([{'a' => 2}, {'b' => 3}])
|
|
64
62
|
|
|
65
63
|
ids.each do |i|
|
|
66
|
-
assert_kind_of BSON::
|
|
64
|
+
assert_kind_of BSON::ObjectId, i
|
|
67
65
|
end
|
|
68
66
|
|
|
69
67
|
assert_equal 3, @@coll.count
|
|
@@ -188,7 +186,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
188
186
|
|
|
189
187
|
# Sorting using ordered hash. You can use an unordered one, but then the
|
|
190
188
|
# order of the keys won't be guaranteed thus your sort won't make sense.
|
|
191
|
-
oh = OrderedHash.new
|
|
189
|
+
oh = BSON::OrderedHash.new
|
|
192
190
|
oh['a'] = -1
|
|
193
191
|
assert_raise InvalidSortValueError do
|
|
194
192
|
docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => oh).to_a
|
|
@@ -290,7 +288,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
290
288
|
def test_index_create_with_symbol
|
|
291
289
|
assert_equal @@coll.index_information.length, 1
|
|
292
290
|
|
|
293
|
-
name = @@
|
|
291
|
+
name = @@coll.create_index([['a', 1]])
|
|
294
292
|
info = @@db.index_information(@@coll.name)
|
|
295
293
|
assert_equal name, "a_1"
|
|
296
294
|
assert_equal @@coll.index_information, info
|
|
@@ -366,15 +364,12 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
366
364
|
end
|
|
367
365
|
|
|
368
366
|
def test_array
|
|
369
|
-
@@coll
|
|
367
|
+
@@coll.remove
|
|
368
|
+
@@coll.insert({'b' => [1, 2, 3]})
|
|
369
|
+
@@coll.insert({'b' => [1, 2, 3]})
|
|
370
370
|
rows = @@coll.find({}, {:fields => ['b']}).to_a
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
assert_equal [1, 2, 3], rows[0]['b']
|
|
374
|
-
else
|
|
375
|
-
assert_equal 2, rows.length
|
|
376
|
-
assert_equal [1, 2, 3], rows[1]['b']
|
|
377
|
-
end
|
|
371
|
+
assert_equal 2, rows.length
|
|
372
|
+
assert_equal [1, 2, 3], rows[1]['b']
|
|
378
373
|
end
|
|
379
374
|
|
|
380
375
|
def test_regex
|
|
@@ -446,32 +441,13 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
446
441
|
@@db.drop_collection('foobar')
|
|
447
442
|
end
|
|
448
443
|
|
|
449
|
-
def test_to_a
|
|
450
|
-
cursor = @@coll.find()
|
|
451
|
-
rows = cursor.to_a
|
|
452
|
-
|
|
453
|
-
assert_raise InvalidOperation do
|
|
454
|
-
cursor.to_a
|
|
455
|
-
end
|
|
456
|
-
|
|
457
|
-
cursor.each { |doc| fail "should be no docs in each now" }
|
|
458
|
-
end
|
|
459
|
-
|
|
460
|
-
def test_to_a_after_each
|
|
461
|
-
cursor = @@coll.find
|
|
462
|
-
cursor.each { |row| row }
|
|
463
|
-
assert_raise InvalidOperation do
|
|
464
|
-
cursor.to_a
|
|
465
|
-
end
|
|
466
|
-
end
|
|
467
|
-
|
|
468
444
|
def test_where
|
|
469
445
|
@@coll.insert('a' => 2)
|
|
470
446
|
@@coll.insert('a' => 3)
|
|
471
447
|
|
|
472
448
|
assert_equal 3, @@coll.count
|
|
473
|
-
assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count()
|
|
474
|
-
assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count()
|
|
449
|
+
assert_equal 1, @@coll.find('$where' => BSON::Code.new('this.a > 2')).count()
|
|
450
|
+
assert_equal 2, @@coll.find('$where' => BSON::Code.new('this.a > i', {'i' => 1})).count()
|
|
475
451
|
end
|
|
476
452
|
|
|
477
453
|
def test_eval
|
|
@@ -566,7 +542,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
566
542
|
def test_deref
|
|
567
543
|
@@coll.remove
|
|
568
544
|
|
|
569
|
-
assert_equal nil, @@db.dereference(DBRef.new("test",
|
|
545
|
+
assert_equal nil, @@db.dereference(DBRef.new("test", ObjectId.new))
|
|
570
546
|
@@coll.insert({"x" => "hello"})
|
|
571
547
|
key = @@coll.find_one()["_id"]
|
|
572
548
|
assert_equal "hello", @@db.dereference(DBRef.new("test", key))["x"]
|
|
@@ -587,7 +563,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
587
563
|
a = {"hello" => "world"}
|
|
588
564
|
|
|
589
565
|
id = @@coll.save(a)
|
|
590
|
-
assert_kind_of
|
|
566
|
+
assert_kind_of ObjectId, id
|
|
591
567
|
assert_equal 1, @@coll.count
|
|
592
568
|
|
|
593
569
|
assert_equal id, @@coll.save(a)
|
|
@@ -616,14 +592,14 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
616
592
|
|
|
617
593
|
@@coll.save("hello" => "mike")
|
|
618
594
|
id = @@coll.save("hello" => "world")
|
|
619
|
-
assert_kind_of
|
|
595
|
+
assert_kind_of ObjectId, id
|
|
620
596
|
|
|
621
597
|
assert_equal "world", @@coll.find_one(:_id => id)["hello"]
|
|
622
598
|
@@coll.find(:_id => id).to_a.each do |doc|
|
|
623
599
|
assert_equal "world", doc["hello"]
|
|
624
600
|
end
|
|
625
601
|
|
|
626
|
-
id =
|
|
602
|
+
id = ObjectId.from_string(id.to_s)
|
|
627
603
|
assert_equal "world", @@coll.find_one(:_id => id)["hello"]
|
|
628
604
|
end
|
|
629
605
|
|
|
@@ -650,6 +626,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
650
626
|
assert_raise BSON::InvalidKeyName do
|
|
651
627
|
@@coll.insert({"$hello" => "world"})
|
|
652
628
|
end
|
|
629
|
+
|
|
653
630
|
assert_raise BSON::InvalidKeyName do
|
|
654
631
|
@@coll.insert({"hello" => {"$hello" => "world"}})
|
|
655
632
|
end
|
|
@@ -733,16 +710,7 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
733
710
|
|
|
734
711
|
a.rename("bar")
|
|
735
712
|
|
|
736
|
-
assert_equal 0, a.count()
|
|
737
|
-
assert_equal 2, b.count()
|
|
738
|
-
|
|
739
|
-
assert_equal 1, b.find().to_a()[0]["x"]
|
|
740
|
-
assert_equal 2, b.find().to_a()[1]["x"]
|
|
741
|
-
|
|
742
|
-
b.rename(:foo)
|
|
743
|
-
|
|
744
713
|
assert_equal 2, a.count()
|
|
745
|
-
assert_equal 0, b.count()
|
|
746
714
|
end
|
|
747
715
|
|
|
748
716
|
# doesn't really test functionality, just that the option is set correctly
|
|
@@ -759,7 +727,12 @@ class DBAPITest < Test::Unit::TestCase
|
|
|
759
727
|
utf8 = "hello world".encode("UTF-8")
|
|
760
728
|
iso8859 = "hello world".encode("ISO-8859-1")
|
|
761
729
|
|
|
762
|
-
|
|
730
|
+
if RUBY_PLATFORM =~ /jruby/
|
|
731
|
+
assert_equal "ASCII-8BIT", ascii.encoding.name
|
|
732
|
+
else
|
|
733
|
+
assert_equal "US-ASCII", ascii.encoding.name
|
|
734
|
+
end
|
|
735
|
+
|
|
763
736
|
assert_equal "UTF-8", utf8.encoding.name
|
|
764
737
|
assert_equal "ISO-8859-1", iso8859.encoding.name
|
|
765
738
|
|
data/test/db_connection_test.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
require 'test/test_helper'
|
|
1
|
+
require './test/test_helper'
|
|
2
2
|
|
|
3
|
-
# NOTE: assumes Mongo is running
|
|
4
3
|
class DBConnectionTest < Test::Unit::TestCase
|
|
5
4
|
|
|
6
5
|
include Mongo
|
|
@@ -11,6 +10,6 @@ class DBConnectionTest < Test::Unit::TestCase
|
|
|
11
10
|
db = Connection.new(host, port).db(MONGO_TEST_DB)
|
|
12
11
|
coll = db.collection('test')
|
|
13
12
|
coll.remove
|
|
14
|
-
db.
|
|
13
|
+
db.get_last_error
|
|
15
14
|
end
|
|
16
15
|
end
|