mongodb-mongo 0.6.7 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -86,13 +86,11 @@ class BSON
86
86
 
87
87
  # Write key/value pairs. Always write _id first if it exists.
88
88
  if obj.has_key? '_id'
89
- oid = obj['_id']
89
+ serialize_key_value('_id', obj['_id'])
90
90
  elsif obj.has_key? :_id
91
- oid = obj[:_id]
92
- else
93
- oid = false
91
+ serialize_key_value('_id', obj[:_id])
94
92
  end
95
- serialize_key_value('_id', oid) if oid
93
+
96
94
  obj.each {|k, v| serialize_key_value(k, v) unless k == '_id' || k == :_id }
97
95
 
98
96
  serialize_eoo_element(@buf)
@@ -250,7 +248,10 @@ class BSON
250
248
  end
251
249
 
252
250
  def deserialize_number_int_data(buf)
253
- buf.get_int
251
+ # sometimes ruby makes me angry... why would the same code pack as signed
252
+ # but unpack as unsigned
253
+ unsigned = buf.get_int
254
+ unsigned >= 2**32 / 2 ? unsigned - 2**32 : unsigned
254
255
  end
255
256
 
256
257
  def deserialize_object_data(buf)
@@ -389,6 +390,9 @@ class BSON
389
390
  if type == NUMBER
390
391
  buf.put_double(val)
391
392
  else
393
+ if val > 2**32 / 2 - 1 or val < -2**32 / 2
394
+ raise RangeError.new "MongoDB can only handle 4-byte ints - try converting to a double before saving"
395
+ end
392
396
  buf.put_int(val)
393
397
  end
394
398
  end
@@ -79,7 +79,7 @@ TEST_FILES = ['tests/mongo-qa/_common.rb',
79
79
 
80
80
  Gem::Specification.new do |s|
81
81
  s.name = 'mongo'
82
- s.version = '0.6.7'
82
+ s.version = '0.7'
83
83
  s.platform = Gem::Platform::RUBY
84
84
  s.summary = 'Ruby driver for the 10gen Mongo DB'
85
85
  s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
data/tests/test_bson.rb CHANGED
@@ -36,6 +36,18 @@ class BSONTest < Test::Unit::TestCase
36
36
  doc = {'doc' => 42}
37
37
  @b.serialize(doc)
38
38
  assert_equal doc, @b.deserialize
39
+
40
+ doc = {"doc" => -5600}
41
+ @b.serialize(doc)
42
+ assert_equal doc, @b.deserialize
43
+
44
+ doc = {"doc" => 2147483647}
45
+ @b.serialize(doc)
46
+ assert_equal doc, @b.deserialize
47
+
48
+ doc = {"doc" => -2147483648}
49
+ @b.serialize(doc)
50
+ assert_equal doc, @b.deserialize
39
51
  end
40
52
 
41
53
  def test_ordered_hash
@@ -181,6 +193,11 @@ class BSONTest < Test::Unit::TestCase
181
193
  assert_equal '_id', roundtrip.keys.first
182
194
  end
183
195
 
196
+ def test_nil_id
197
+ doc = {"_id" => nil}
198
+ assert_equal doc, @b.deserialize(@b.serialize(doc).to_a)
199
+ end
200
+
184
201
  def test_timestamp
185
202
  val = {"test" => [4, 20]}
186
203
  assert_equal val, @b.deserialize([0x13, 0x00, 0x00, 0x00,
@@ -190,6 +207,21 @@ class BSONTest < Test::Unit::TestCase
190
207
  0x00, 0x00, 0x00])
191
208
  end
192
209
 
210
+ def test_overflow
211
+ doc = {"x" => 2**45}
212
+ assert_raise RangeError do
213
+ @b.serialize(doc)
214
+ end
215
+
216
+ doc = {"x" => 2147483647}
217
+ assert_equal doc, @b.deserialize(@b.serialize(doc).to_a)
218
+
219
+ doc["x"] = doc["x"] + 1
220
+ assert_raise RangeError do
221
+ @b.serialize(doc)
222
+ end
223
+ end
224
+
193
225
  def test_do_not_change_original_object
194
226
  val = OrderedHash.new
195
227
  val['not_id'] = 1
data/tests/test_db_api.rb CHANGED
@@ -485,7 +485,6 @@ class DBAPITest < Test::Unit::TestCase
485
485
  assert_equal 5, @@db.eval("2 + 3;")
486
486
 
487
487
  assert_equal 5, @@db.eval(Code.new("2 + 3;"))
488
- assert_equal nil, @@db.eval(Code.new("return i;"))
489
488
  assert_equal 2, @@db.eval(Code.new("return i;", {"i" => 2}))
490
489
  assert_equal 5, @@db.eval(Code.new("i + 3;", {"i" => 2}))
491
490
 
@@ -548,14 +547,18 @@ class DBAPITest < Test::Unit::TestCase
548
547
  @@coll.clear
549
548
 
550
549
  assert_equal nil, @@db.dereference(DBRef.new("test", ObjectID.new))
551
- obj = {"x" => true}
552
- key = @@coll.insert(obj)["_id"]
553
- assert_equal true, @@db.dereference(DBRef.new("test", key))["x"]
550
+ @@coll.insert({"x" => "hello"})
551
+ key = @@coll.find_first()["_id"]
552
+ assert_equal "hello", @@db.dereference(DBRef.new("test", key))["x"]
554
553
 
555
554
  assert_equal nil, @@db.dereference(DBRef.new("test", 4))
556
555
  obj = {"_id" => 4}
557
556
  @@coll.insert(obj)
558
557
  assert_equal obj, @@db.dereference(DBRef.new("test", 4))
558
+
559
+ @@coll.clear
560
+ @@coll.insert({"x" => "hello"})
561
+ assert_equal nil, @@db.dereference(DBRef.new("test", nil))
559
562
  end
560
563
 
561
564
  # TODO this test fails with error message "Undefed Before end of object"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: "0.7"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard