mongo 1.0.9 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ require './test/test_helper'
2
+ require 'rubygems'
3
+ require 'json'
4
+
5
+ class JSONTest < Test::Unit::TestCase
6
+
7
+ include Mongo
8
+ include BSON
9
+
10
+ def test_object_id_as_json
11
+ id = ObjectId.new
12
+ obj = {'_id' => id}
13
+ assert_equal "{\"_id\":{\"$oid\": \"#{id.to_s}\"}}", obj.to_json
14
+ end
15
+
16
+ end
@@ -0,0 +1,132 @@
1
+ require './test/test_helper'
2
+ require 'rubygems'
3
+ require 'json'
4
+
5
+ class ObjectIdTest < Test::Unit::TestCase
6
+
7
+ include Mongo
8
+ include BSON
9
+
10
+ def setup
11
+ @o = ObjectId.new
12
+ end
13
+
14
+ def test_hashcode
15
+ assert_equal @o.instance_variable_get(:@data).hash, @o.hash
16
+ end
17
+
18
+ def test_array_uniq_for_equilavent_ids
19
+ a = ObjectId.new('123')
20
+ b = ObjectId.new('123')
21
+ assert_equal a, b
22
+ assert_equal 1, [a, b].uniq.size
23
+ end
24
+
25
+ def test_create_pk_method
26
+ doc = {:name => 'Mongo'}
27
+ doc = ObjectId.create_pk(doc)
28
+ assert doc[:_id]
29
+
30
+ doc = {:name => 'Mongo', :_id => '12345'}
31
+ doc = ObjectId.create_pk(doc)
32
+ assert_equal '12345', doc[:_id]
33
+ end
34
+
35
+ def test_different
36
+ a = ObjectId.new
37
+ b = ObjectId.new
38
+ assert_not_equal a.to_a, b.to_a
39
+ assert_not_equal a, b
40
+ end
41
+
42
+ def test_eql?
43
+ o2 = ObjectId.new(@o.to_a)
44
+ assert_equal @o, o2
45
+ end
46
+
47
+ def test_to_s
48
+ s = @o.to_s
49
+ assert_equal 24, s.length
50
+ s =~ /^([0-9a-f]+)$/
51
+ assert_equal 24, $1.length
52
+ end
53
+
54
+ def test_method
55
+ assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
56
+ end
57
+
58
+ def test_inspect
59
+ assert_equal "BSON::ObjectId('#{@o.to_s}')", @o.inspect
60
+ end
61
+
62
+ def test_save_and_restore
63
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
64
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
65
+ db = Connection.new(host, port).db(MONGO_TEST_DB)
66
+ coll = db.collection('test')
67
+
68
+ coll.remove
69
+ coll << {'a' => 1, '_id' => @o}
70
+
71
+ row = coll.find().collect.first
72
+ assert_equal 1, row['a']
73
+ assert_equal @o, row['_id']
74
+ end
75
+
76
+ def test_from_string
77
+ hex_str = @o.to_s
78
+ o2 = ObjectId.from_string(hex_str)
79
+ assert_equal hex_str, o2.to_s
80
+ assert_equal @o, o2
81
+ assert_equal @o.to_s, o2.to_s
82
+ end
83
+
84
+ def test_illegal_from_string
85
+ assert_raise InvalidObjectId do
86
+ ObjectId.from_string("")
87
+ end
88
+ end
89
+
90
+ def test_legal
91
+ assert !ObjectId.legal?(nil)
92
+ assert !ObjectId.legal?("fred")
93
+ assert !ObjectId.legal?("0000")
94
+ assert !ObjectId.legal?('000102030405060708090A0')
95
+ assert ObjectId.legal?('000102030405060708090A0B')
96
+ assert ObjectId.legal?('abcdefABCDEF123456789012')
97
+ assert !ObjectId.legal?('abcdefABCDEF12345678901x')
98
+ end
99
+
100
+ def test_from_string_leading_zeroes
101
+ hex_str = '000000000000000000000000'
102
+ o = ObjectId.from_string(hex_str)
103
+ assert_equal hex_str, o.to_s
104
+ end
105
+
106
+ def test_byte_order
107
+ hex_str = '000102030405060708090A0B'
108
+ o = ObjectId.from_string(hex_str)
109
+ assert_equal [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b], o.to_a
110
+ end
111
+
112
+ def test_generation_time
113
+ time = Time.now
114
+ id = ObjectId.new
115
+ generated_time = id.generation_time
116
+
117
+ assert_in_delta time.to_i, generated_time.to_i, 2
118
+ assert_equal "UTC", generated_time.zone
119
+ end
120
+
121
+ def test_from_time
122
+ time = Time.now.utc
123
+ id = ObjectId.from_time(time)
124
+
125
+ assert_equal time.to_i, id.generation_time.to_i
126
+ end
127
+
128
+ def test_json
129
+ id = ObjectId.new
130
+ assert_equal "{\"$oid\": \"#{id}\"}", id.to_json
131
+ end
132
+ end
@@ -0,0 +1,197 @@
1
+ require './test/test_helper'
2
+
3
+ class OrderedHashTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @oh = BSON::OrderedHash.new
7
+ @oh['c'] = 1
8
+ @oh['a'] = 2
9
+ @oh['z'] = 3
10
+ @ordered_keys = %w(c a z)
11
+ end
12
+
13
+ def test_initialize
14
+ a = BSON::OrderedHash.new
15
+ a['x'] = 1
16
+ a['y'] = 2
17
+
18
+ b = BSON::OrderedHash['x' => 1, 'y' => 2]
19
+ assert_equal a, b
20
+ end
21
+
22
+ def test_hash_code
23
+ o = BSON::OrderedHash.new
24
+ o['number'] = 50
25
+ assert o.hash
26
+ end
27
+
28
+ def test_empty
29
+ assert_equal [], BSON::OrderedHash.new.keys
30
+ end
31
+
32
+ def test_uniq
33
+ list = []
34
+ doc = BSON::OrderedHash.new
35
+ doc['_id'] = 'ab12'
36
+ doc['name'] = 'test'
37
+
38
+ same_doc = BSON::OrderedHash.new
39
+ same_doc['_id'] = 'ab12'
40
+ same_doc['name'] = 'test'
41
+ list << doc
42
+ list << same_doc
43
+
44
+ assert_equal 2, list.size
45
+ assert_equal 1, list.uniq.size
46
+ end
47
+
48
+ def test_equality
49
+ a = BSON::OrderedHash.new
50
+ a['x'] = 1
51
+ a['y'] = 2
52
+
53
+ b = BSON::OrderedHash.new
54
+ b['y'] = 2
55
+ b['x'] = 1
56
+
57
+ c = BSON::OrderedHash.new
58
+ c['x'] = 1
59
+ c['y'] = 2
60
+
61
+ d = BSON::OrderedHash.new
62
+ d['x'] = 2
63
+ d['y'] = 3
64
+
65
+ e = BSON::OrderedHash.new
66
+ e['z'] = 1
67
+ e['y'] = 2
68
+
69
+ assert_equal a, c
70
+ assert_not_equal a, b
71
+ assert_not_equal a, d
72
+ assert_not_equal a, e
73
+ end
74
+
75
+ def test_order_preserved
76
+ assert_equal @ordered_keys, @oh.keys
77
+ end
78
+
79
+ def test_to_a_order_preserved
80
+ assert_equal @ordered_keys, @oh.to_a.map {|m| m.first}
81
+ end
82
+
83
+ def test_order_preserved_after_replace
84
+ @oh['a'] = 42
85
+ assert_equal @ordered_keys, @oh.keys
86
+ @oh['c'] = 'foobar'
87
+ assert_equal @ordered_keys, @oh.keys
88
+ @oh['z'] = /huh?/
89
+ assert_equal @ordered_keys, @oh.keys
90
+ end
91
+
92
+ def test_each
93
+ keys = []
94
+ @oh.each { |k, v| keys << k }
95
+ assert_equal keys, @oh.keys
96
+
97
+ @oh['z'] = 42
98
+ assert_equal keys, @oh.keys
99
+
100
+ assert_equal @oh, @oh.each {|k,v|}
101
+ end
102
+
103
+ def test_values
104
+ assert_equal [1, 2, 3], @oh.values
105
+ end
106
+
107
+ def test_merge
108
+ other = BSON::OrderedHash.new
109
+ other['f'] = 'foo'
110
+ noob = @oh.merge(other)
111
+ assert_equal @ordered_keys + ['f'], noob.keys
112
+ assert_equal [1, 2, 3, 'foo'], noob.values
113
+ end
114
+
115
+ def test_merge_bang
116
+ other = BSON::OrderedHash.new
117
+ other['f'] = 'foo'
118
+ @oh.merge!(other)
119
+ assert_equal @ordered_keys + ['f'], @oh.keys
120
+ assert_equal [1, 2, 3, 'foo'], @oh.values
121
+ end
122
+
123
+ def test_merge_bang_with_overlap
124
+ other = BSON::OrderedHash.new
125
+ other['a'] = 'apple'
126
+ other['c'] = 'crab'
127
+ other['f'] = 'foo'
128
+ @oh.merge!(other)
129
+ assert_equal @ordered_keys + ['f'], @oh.keys
130
+ assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
131
+ end
132
+
133
+ def test_merge_bang_with_hash_with_overlap
134
+ other = Hash.new
135
+ other['a'] = 'apple'
136
+ other['c'] = 'crab'
137
+ other['f'] = 'foo'
138
+ @oh.merge!(other)
139
+ assert_equal @ordered_keys + ['f'], @oh.keys
140
+ assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
141
+ end
142
+
143
+ def test_equality_with_hash
144
+ o = BSON::OrderedHash.new
145
+ o[:a] = 1
146
+ o[:b] = 2
147
+ o[:c] = 3
148
+ r = {:a => 1, :b => 2, :c => 3}
149
+ assert r == o
150
+ assert o == r
151
+ end
152
+
153
+ def test_update
154
+ other = BSON::OrderedHash.new
155
+ other['f'] = 'foo'
156
+ noob = @oh.update(other)
157
+ assert_equal @ordered_keys + ['f'], noob.keys
158
+ assert_equal [1, 2, 3, 'foo'], noob.values
159
+ end
160
+
161
+ def test_inspect_retains_order
162
+ assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
163
+ end
164
+
165
+ def test_clear
166
+ @oh.clear
167
+ assert @oh.keys.empty?
168
+ end
169
+
170
+ def test_delete
171
+ assert @oh.keys.include?('z')
172
+ @oh.delete('z')
173
+ assert !@oh.keys.include?('z')
174
+ end
175
+
176
+ def test_delete_if
177
+ assert @oh.keys.include?('z')
178
+ @oh.delete_if { |k,v| k == 'z' }
179
+ assert !@oh.keys.include?('z')
180
+ end
181
+
182
+ def test_reject
183
+ new = @oh.reject { |k, v| k == 'foo' }
184
+ assert new.keys == @oh.keys
185
+
186
+ new = @oh.reject { |k, v| k == 'z' }
187
+ assert !new.keys.include?('z')
188
+ end
189
+
190
+ def test_clone
191
+ copy = @oh.clone
192
+ assert copy.keys == @oh.keys
193
+
194
+ copy[:foo] = 1
195
+ assert copy.keys != @oh.keys
196
+ end
197
+ end
@@ -477,7 +477,7 @@ class TestCollection < Test::Unit::TestCase
477
477
  def test_saving_dates_pre_epoch
478
478
  begin
479
479
  @@test.save({'date' => Time.utc(1600)})
480
- assert_in_delta Time.utc(1600), @@test.find_one()["date"], 0.001
480
+ assert_in_delta Time.utc(1600), @@test.find_one()["date"], 2
481
481
  rescue ArgumentError
482
482
  # See note in test_date_before_epoch (BSONTest)
483
483
  end
@@ -130,7 +130,7 @@ class TestConnection < Test::Unit::TestCase
130
130
  end
131
131
 
132
132
  def test_nodes
133
- db = Connection.paired([['foo', 27017], ['bar', 27018]], :connect => false)
133
+ db = Connection.multi([['foo', 27017], ['bar', 27018]], :connect => false)
134
134
  nodes = db.nodes
135
135
  assert_equal 2, nodes.length
136
136
  assert_equal ['foo', 27017], nodes[0]
@@ -139,10 +139,31 @@ class TestConnection < Test::Unit::TestCase
139
139
 
140
140
  def test_slave_ok_with_multiple_nodes
141
141
  assert_raise MongoArgumentError do
142
- Connection.paired([['foo', 27017], ['bar', 27018]], :connect => false, :slave_ok => true)
142
+ Connection.multi([['foo', 27017], ['bar', 27018]], :connect => false, :slave_ok => true)
143
143
  end
144
144
  end
145
145
 
146
+ def test_fsync_lock
147
+ assert !@mongo.locked?
148
+ @mongo.lock!
149
+ assert @mongo.locked?
150
+ assert_equal 1, @mongo['admin']['$cmd.sys.inprog'].find_one['fsyncLock'], "Not fsync-locked"
151
+ assert_equal "unlock requested", @mongo.unlock!['info']
152
+ unlocked = false
153
+ counter = 0
154
+ while counter < 5
155
+ if @mongo['admin']['$cmd.sys.inprog'].find_one['fsyncLock'].nil?
156
+ unlocked = true
157
+ break
158
+ else
159
+ sleep(1)
160
+ counter += 1
161
+ end
162
+ end
163
+ assert !@mongo.locked?
164
+ assert unlocked, "mongod failed to unlock"
165
+ end
166
+
146
167
  context "Saved authentications" do
147
168
  setup do
148
169
  @conn = Mongo::Connection.new
@@ -454,16 +454,6 @@ class DBAPITest < Test::Unit::TestCase
454
454
  assert_equal 2, @@coll.find('$where' => BSON::Code.new('this.a > i', {'i' => 1})).count()
455
455
  end
456
456
 
457
- def test_implicit_where
458
- @@coll.remove
459
- @@coll.insert('a' => 2)
460
- @@coll.insert('a' => 3)
461
-
462
- assert_equal 2, @@coll.count
463
- assert_equal 1, @@coll.find('this.a > 2').count()
464
- assert_equal 2, @@coll.find(BSON::Code.new('this.a > z', {'z' => 1})).to_a.length
465
- end
466
-
467
457
  def test_eval
468
458
  assert_equal 3, @@db.eval('function (x) {return x;}', 3)
469
459
 
@@ -631,6 +621,7 @@ class DBAPITest < Test::Unit::TestCase
631
621
  assert_equal("mike", @@coll.find_one()["hello"])
632
622
  end
633
623
 
624
+ if !RUBY_PLATFORM =~ /java/
634
625
  def test_invalid_key_names
635
626
  @@coll.remove
636
627
 
@@ -640,6 +631,7 @@ class DBAPITest < Test::Unit::TestCase
640
631
  assert_raise BSON::InvalidKeyName do
641
632
  @@coll.insert({"$hello" => "world"})
642
633
  end
634
+
643
635
  assert_raise BSON::InvalidKeyName do
644
636
  @@coll.insert({"hello" => {"$hello" => "world"}})
645
637
  end
@@ -666,6 +658,7 @@ class DBAPITest < Test::Unit::TestCase
666
658
  @@coll.insert({"hello" => {"hel.lo" => "world"}})
667
659
  end
668
660
  end
661
+ end
669
662
 
670
663
  def test_collection_names
671
664
  assert_raise TypeError do
@@ -75,20 +75,6 @@ class DBTest < Test::Unit::TestCase
75
75
  assert_kind_of Collection, colls[0]
76
76
  end
77
77
 
78
- def test_pair
79
- @@conn.close
80
- @@users = nil
81
- @@conn = Connection.paired([["this-should-fail", 27017], [@@host, @@port]])
82
- @@db = @@conn[MONGO_TEST_DB]
83
- assert @@conn.connected?
84
- ensure
85
- unless @@conn.connected?
86
- @@conn = Connection.new(@@host, @@port)
87
- @@db = @@conn.db(MONGO_TEST_DB)
88
- end
89
- @@users = @@db.collection('system.users')
90
- end
91
-
92
78
  def test_pk_factory
93
79
  db = Connection.new(@@host, @@port).db(MONGO_TEST_DB, :pk => TestPKFactory.new)
94
80
  coll = db.collection('test')