mongo 0.18.1 → 0.18.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,8 +54,8 @@ class TestConnection < Test::Unit::TestCase
54
54
  def test_copy_database
55
55
  @mongo.db('old').collection('copy-test').insert('a' => 1)
56
56
  @mongo.copy_database('old', 'new')
57
- old_object = @mongo.db('old').collection('copy-test').find.next_object
58
- new_object = @mongo.db('new').collection('copy-test').find.next_object
57
+ old_object = @mongo.db('old').collection('copy-test').find.next_document
58
+ new_object = @mongo.db('new').collection('copy-test').find.next_document
59
59
  assert_equal old_object, new_object
60
60
  end
61
61
 
@@ -6,9 +6,9 @@ require 'mongo/util/ordered_hash'
6
6
  class ConversionsTest < Test::Unit::TestCase
7
7
  include Mongo::Conversions
8
8
 
9
- def test_array_as_sort_parameters_with_array_of_strings
10
- params = array_as_sort_parameters(["field1", "field2"])
11
- assert_equal({ "field1" => 1, "field2" => 1 }, params)
9
+ def test_array_as_sort_parameters_with_array_of_key_and_value
10
+ params = array_as_sort_parameters(["field1", "asc"])
11
+ assert_equal({"field1" => 1}, params)
12
12
  end
13
13
 
14
14
  def test_array_as_sort_parameters_with_array_of_string_and_values
data/test/test_cursor.rb CHANGED
@@ -58,36 +58,60 @@ class CursorTest < Test::Unit::TestCase
58
58
  assert_equal 0, @@db['acollectionthatdoesn'].count()
59
59
  end
60
60
 
61
+ def test_next_object_deprecation
62
+ @@coll.remove
63
+ @@coll.insert({"a" => 1})
64
+
65
+ assert_equal 1, @@coll.find().next_object["a"]
66
+ end
67
+
61
68
  def test_sort
62
69
  @@coll.remove
63
- 5.times{|x| @@coll.insert({"a" => x}) }
70
+ 5.times{|x| @@coll.insert({"age" => x}) }
71
+
72
+ assert_kind_of Cursor, @@coll.find().sort(:age, 1)
64
73
 
65
- assert_kind_of Cursor, @@coll.find().sort(:a, 1)
74
+ assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"]
75
+ assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"]
76
+ assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"]
66
77
 
67
- assert_equal 0, @@coll.find().sort(:a, 1).next_object["a"]
68
- assert_equal 4, @@coll.find().sort(:a, -1).next_object["a"]
69
- assert_equal 0, @@coll.find().sort([["a", :asc]]).next_object["a"]
78
+ assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]])
70
79
 
71
- assert_kind_of Cursor, @@coll.find().sort([[:a, -1], [:b, 1]])
80
+ assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
81
+ assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
72
82
 
73
- assert_equal 4, @@coll.find().sort(:a, 1).sort(:a, -1).next_object["a"]
74
- assert_equal 0, @@coll.find().sort(:a, -1).sort(:a, 1).next_object["a"]
83
+ assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
84
+ assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
75
85
 
76
86
  cursor = @@coll.find()
77
- cursor.next_object()
87
+ cursor.next_document
78
88
  assert_raise InvalidOperation do
79
- cursor.sort(["a"])
89
+ cursor.sort(["age"])
80
90
  end
81
91
 
82
92
  assert_raise InvalidSortValueError do
83
- @@coll.find().sort(:a, 25).next_object
93
+ @@coll.find().sort(:age, 25).next_document
84
94
  end
85
95
 
86
96
  assert_raise InvalidSortValueError do
87
- @@coll.find().sort(25).next_object
97
+ @@coll.find().sort(25).next_document
88
98
  end
89
99
  end
90
100
 
101
+ def test_sort_date
102
+ @@coll.remove
103
+ 5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) }
104
+
105
+ assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year
106
+ assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year
107
+
108
+ assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year
109
+ assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year
110
+
111
+ assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
112
+ assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
113
+ end
114
+
91
115
  def test_limit
92
116
  @@coll.remove
93
117
 
@@ -106,7 +130,7 @@ class CursorTest < Test::Unit::TestCase
106
130
  end
107
131
 
108
132
  cursor = @@coll.find()
109
- firstResult = cursor.next_object()
133
+ firstResult = cursor.next_document
110
134
  assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
111
135
  cursor.limit(1)
112
136
  end
@@ -140,7 +164,7 @@ class CursorTest < Test::Unit::TestCase
140
164
  end
141
165
 
142
166
  cursor = @@coll.find()
143
- firstResult = cursor.next_object()
167
+ firstResult = cursor.next_document
144
168
  assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
145
169
  cursor.skip(1)
146
170
  end
@@ -233,7 +257,7 @@ class CursorTest < Test::Unit::TestCase
233
257
  def test_close_after_query_sent
234
258
  begin
235
259
  cursor = @@coll.find('a' => 1)
236
- cursor.next_object
260
+ cursor.next_document
237
261
  cursor.close
238
262
  assert cursor.closed?
239
263
  rescue => ex
@@ -267,7 +291,7 @@ class CursorTest < Test::Unit::TestCase
267
291
 
268
292
  10.times do |i|
269
293
  a = @@coll.find()
270
- a.next_object()
294
+ a.next_document
271
295
  a.close()
272
296
  end
273
297
 
@@ -277,7 +301,7 @@ class CursorTest < Test::Unit::TestCase
277
301
  @@db.command("cursorInfo" => 1)["byLocation_size"])
278
302
 
279
303
  a = @@coll.find()
280
- a.next_object()
304
+ a.next_document
281
305
 
282
306
  assert_not_equal(client_cursors,
283
307
  @@db.command("cursorInfo" => 1)["clientCursors_size"])
@@ -291,7 +315,7 @@ class CursorTest < Test::Unit::TestCase
291
315
  assert_equal(by_location,
292
316
  @@db.command("cursorInfo" => 1)["byLocation_size"])
293
317
 
294
- a = @@coll.find({}, :limit => 10).next_object()
318
+ a = @@coll.find({}, :limit => 10).next_document
295
319
 
296
320
  assert_equal(client_cursors,
297
321
  @@db.command("cursorInfo" => 1)["clientCursors_size"])
@@ -299,7 +323,7 @@ class CursorTest < Test::Unit::TestCase
299
323
  @@db.command("cursorInfo" => 1)["byLocation_size"])
300
324
 
301
325
  @@coll.find() do |cursor|
302
- cursor.next_object()
326
+ cursor.next_document
303
327
  end
304
328
 
305
329
  assert_equal(client_cursors,
@@ -308,7 +332,7 @@ class CursorTest < Test::Unit::TestCase
308
332
  @@db.command("cursorInfo" => 1)["byLocation_size"])
309
333
 
310
334
  @@coll.find() { |cursor|
311
- cursor.next_object()
335
+ cursor.next_document
312
336
  }
313
337
 
314
338
  assert_equal(client_cursors,
data/test/test_db_api.rb CHANGED
@@ -180,7 +180,7 @@ class DBAPITest < Test::Unit::TestCase
180
180
  assert_equal 3, docs[2]['a']
181
181
  assert_equal 4, docs[3]['a']
182
182
 
183
- docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => ['b', 'a']).to_a
183
+ docs = @@coll.find({'a' => { '$lt' => 10 }}, :sort => [['b', 'asc'], ['a', 'asc']]).to_a
184
184
  assert_equal 4, docs.size
185
185
  assert_equal 2, docs[0]['a']
186
186
  assert_equal 4, docs[1]['a']
@@ -553,6 +553,12 @@ class DBAPITest < Test::Unit::TestCase
553
553
  assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }")[0]["count"]
554
554
  assert_equal 1, test.group([], {"a" => {"$gt" => 1}}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true)[0]["count"]
555
555
 
556
+ finalize = "function (obj) { obj.f = obj.count - 1; }"
557
+ assert_equal 2, test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", true, finalize)[0]["f"]
558
+ assert_raise OperationFailure do
559
+ test.group([], {}, {"count" => 0}, "function (obj, prev) { prev.count++; }", false, finalize)[0]["f"]
560
+ end
561
+
556
562
  test.insert("a" => 2, "b" => 3)
557
563
  expected = [{"a" => 2, "count" => 2},
558
564
  {"a" => nil, "count" => 1},
@@ -71,9 +71,23 @@ class GridStoreTest < Test::Unit::TestCase
71
71
  assert_equal "hello", GridStore.read(@@db, 'foobar', 5)
72
72
  end
73
73
 
74
+ def test_read_with_and_without_length
75
+ GridStore.open(@@db, 'read-types', 'w') do |f|
76
+ f.write('hello, there')
77
+ end
78
+
79
+ GridStore.open(@@db, 'read-types', 'r') do |f|
80
+ assert_equal 'hello, ', f.read(7)
81
+ assert_equal 'there', f.read
82
+ end
83
+ end
84
+
85
+ def test_access_length
86
+ assert_equal 13, GridStore.new(@@db, 'foobar').length
87
+ end
88
+
74
89
  # Also tests seek
75
90
  def test_read_with_offset
76
- assert_equal "world", GridStore.read(@@db, 'foobar', 5, 7)
77
91
  assert_equal "world!", GridStore.read(@@db, 'foobar', nil, 7)
78
92
  end
79
93
 
@@ -116,7 +130,7 @@ class GridStoreTest < Test::Unit::TestCase
116
130
  }
117
131
 
118
132
  assert_equal 3, @@chunks.count
119
- assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
133
+ #assert_equal ('x' * size) + ('y' * size) + ('z' * size), GridStore.read(@@db, 'biggie')
120
134
  end
121
135
 
122
136
  def test_puts_and_readlines
@@ -79,6 +79,12 @@ class ObjectIDTest < Test::Unit::TestCase
79
79
  assert_equal @o.to_s, o2.to_s
80
80
  end
81
81
 
82
+ def test_illegal_from_string
83
+ assert_raise InvalidObjectID do
84
+ ObjectID.from_string("")
85
+ end
86
+ end
87
+
82
88
  def test_from_string_legacy
83
89
  hex_str = @o.to_s_legacy
84
90
  o2 = ObjectID.from_string_legacy(hex_str)
@@ -87,6 +93,12 @@ class ObjectIDTest < Test::Unit::TestCase
87
93
  assert_equal @o.to_s, o2.to_s
88
94
  end
89
95
 
96
+ def test_illegal_from_string_legacy
97
+ assert_raise InvalidObjectID do
98
+ ObjectID.from_string_legacy("")
99
+ end
100
+ end
101
+
90
102
  def test_legal
91
103
  assert !ObjectID.legal?(nil)
92
104
  assert !ObjectID.legal?("fred")
@@ -23,6 +23,22 @@ class OrderedHashTest < Test::Unit::TestCase
23
23
  assert_equal [], OrderedHash.new.keys
24
24
  end
25
25
 
26
+ def test_uniq
27
+ list = []
28
+ doc = OrderedHash.new
29
+ doc['_id'] = 'ab12'
30
+ doc['name'] = 'test'
31
+
32
+ same_doc = OrderedHash.new
33
+ same_doc['_id'] = 'ab12'
34
+ same_doc['name'] = 'test'
35
+ list << doc
36
+ list << same_doc
37
+
38
+ assert_equal 2, list.size
39
+ assert_equal 1, list.uniq.size
40
+ end
41
+
26
42
  def test_equality
27
43
  a = OrderedHash.new
28
44
  a['x'] = 1
@@ -4,7 +4,7 @@ class TestThreading < Test::Unit::TestCase
4
4
 
5
5
  include Mongo
6
6
 
7
- @@db = Connection.new('localhost', 27017, :pool_size => 1, :timeout => 3).db('ruby-mongo-test')
7
+ @@db = Connection.new('localhost', 27017, :pool_size => 1, :timeout => 30).db('ruby-mongo-test')
8
8
  @@coll = @@db.collection('thread-test-collection')
9
9
 
10
10
  def set_up_safe_data
@@ -34,7 +34,7 @@ class TestThreading < Test::Unit::TestCase
34
34
  end
35
35
  end
36
36
  end
37
-
37
+
38
38
  100.times do |i|
39
39
  threads[i].join
40
40
  end
@@ -54,7 +54,7 @@ class TestThreading < Test::Unit::TestCase
54
54
  end
55
55
  end
56
56
  end
57
-
57
+
58
58
  100.times do |i|
59
59
  threads[i].join
60
60
  end
@@ -6,7 +6,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
6
6
 
7
7
  include Mongo
8
8
 
9
- @@db = Connection.new('localhost', 27017, :pool_size => 50, :timeout => 15).db('ruby-mongo-test')
9
+ @@db = Connection.new('localhost', 27017, :pool_size => 50, :timeout => 60).db('ruby-mongo-test')
10
10
  @@coll = @@db.collection('thread-test-collection')
11
11
 
12
12
  def set_up_safe_data
@@ -25,7 +25,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
25
25
  def test_safe_update
26
26
  set_up_safe_data
27
27
  threads = []
28
- 100.times do |i|
28
+ 1000.times do |i|
29
29
  threads[i] = Thread.new do
30
30
  if i % 2 == 0
31
31
  assert_raise Mongo::OperationFailure do
@@ -36,8 +36,8 @@ class TestThreadingLargePool < Test::Unit::TestCase
36
36
  end
37
37
  end
38
38
  end
39
-
40
- 100.times do |i|
39
+
40
+ 1000.times do |i|
41
41
  threads[i].join
42
42
  end
43
43
  end
@@ -45,7 +45,7 @@ class TestThreadingLargePool < Test::Unit::TestCase
45
45
  def test_safe_insert
46
46
  set_up_safe_data
47
47
  threads = []
48
- 100.times do |i|
48
+ 1000.times do |i|
49
49
  threads[i] = Thread.new do
50
50
  if i % 2 == 0
51
51
  assert_raise Mongo::OperationFailure do
@@ -56,8 +56,8 @@ class TestThreadingLargePool < Test::Unit::TestCase
56
56
  end
57
57
  end
58
58
  end
59
-
60
- 100.times do |i|
59
+
60
+ 1000.times do |i|
61
61
  threads[i].join
62
62
  end
63
63
  end
@@ -2,26 +2,26 @@ require 'test/test_helper'
2
2
 
3
3
  class ConnectionTest < Test::Unit::TestCase
4
4
 
5
- context "Basic operations: " do
6
- setup do
5
+ context "Basic operations: " do
6
+ setup do
7
7
  @logger = mock()
8
8
  end
9
9
 
10
- should "send update message" do
10
+ should "send update message" do
11
11
  @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
12
12
  @db = @conn['testing']
13
13
  @coll = @db.collection('books')
14
- @conn.expects(:send_message).with do |op, msg, log|
14
+ @conn.expects(:send_message).with do |op, msg, log|
15
15
  op == 2001 && log.include?("db.books.update")
16
16
  end
17
17
  @coll.update({}, {:title => 'Moby Dick'})
18
18
  end
19
19
 
20
- should "send insert message" do
20
+ should "send insert message" do
21
21
  @conn = Connection.new('localhost', 27017, :logger => @logger, :connect => false)
22
22
  @db = @conn['testing']
23
23
  @coll = @db.collection('books')
24
- @conn.expects(:send_message).with do |op, msg, log|
24
+ @conn.expects(:send_message).with do |op, msg, log|
25
25
  op == 2002 && log.include?("db.books.insert")
26
26
  end
27
27
  @coll.insert({:title => 'Moby Dick'})
@@ -49,4 +49,4 @@ class ConnectionTest < Test::Unit::TestCase
49
49
  end
50
50
  end
51
51
 
52
-
52
+
@@ -12,11 +12,6 @@ class ConnectionTest < Test::Unit::TestCase
12
12
  db = Object.new
13
13
  end
14
14
 
15
- # Make a few methods public for these tests.
16
- class Mongo::Connection
17
- public :checkin, :checkout, :clear_stale_cached_connections!
18
- end
19
-
20
15
  context "Initialization: " do
21
16
 
22
17
  context "given a single node" do
@@ -60,79 +55,5 @@ class ConnectionTest < Test::Unit::TestCase
60
55
  assert_equal 2500, @conn.port
61
56
  end
62
57
  end
63
-
64
- context "Connection pooling: " do
65
- setup do
66
- TCPSocket.stubs(:new).returns(new_mock_socket)
67
- @conn = Connection.new('localhost', 27107, :connect => false,
68
- :pool_size => 3)
69
-
70
- admin_db = new_mock_db
71
- admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
72
- @conn.expects(:[]).with('admin').returns(admin_db)
73
- @conn.connect_to_master
74
- end
75
-
76
- should "check out a new connection" do
77
- socket = @conn.checkout
78
- assert @conn.reserved_connections.keys.include?(Thread.current.object_id)
79
- end
80
-
81
- context "with multiple threads" do
82
- setup do
83
- @thread1 = Object.new
84
- @thread2 = Object.new
85
- @thread3 = Object.new
86
- @thread4 = Object.new
87
-
88
- Thread.stubs(:current).returns(@thread1)
89
- @socket1 = @conn.checkout
90
- Thread.stubs(:current).returns(@thread2)
91
- @socket2 = @conn.checkout
92
- Thread.stubs(:current).returns(@thread3)
93
- @socket3 = @conn.checkout
94
- end
95
-
96
- should "add each thread to the reserved pool" do
97
- assert @conn.reserved_connections.keys.include?(@thread1.object_id)
98
- assert @conn.reserved_connections.keys.include?(@thread2.object_id)
99
- assert @conn.reserved_connections.keys.include?(@thread3.object_id)
100
- end
101
-
102
- should "only add one socket per thread" do
103
- @conn.reserved_connections.values do |socket|
104
- assert [@socket1, @socket2, @socket3].include?(socket)
105
- end
106
- end
107
-
108
- should "check out all sockets" do
109
- assert_equal @conn.sockets.size, @conn.checked_out.size
110
- @conn.sockets.each do |sock|
111
- assert @conn.checked_out.include?(sock)
112
- end
113
- end
114
-
115
- should "raise an error if no more sockets can be checked out" do
116
- # This can't be tested with mock threads.
117
- # Will test in integration tests.
118
- end
119
-
120
- context "when releasing dead threads" do
121
- setup do
122
- @thread1.expects(:alive?).returns(false)
123
- @thread2.expects(:alive?).returns(true)
124
- @thread3.expects(:alive?).returns(true)
125
- Thread.expects(:list).returns([@thread1, @thread2, @thread3])
126
- @conn.clear_stale_cached_connections!
127
- end
128
-
129
- should "return connections for dead threads" do
130
- assert !@conn.checked_out.include?(@socket1)
131
- assert_nil @conn.reserved_connections[@thread1.object_id]
132
- end
133
- end
134
-
135
- end
136
- end
137
58
  end
138
59