From e8f3696c771fa9136dbde5f3d735e678f2c56af4 Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Tue, 16 Dec 2008 07:33:39 -0500 Subject: [PATCH] Fixed boolean BSON type constant. More tests. --- lib/mongo/util/bson.rb | 2 +- tests/test_bson.rb | 60 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index e262ff0..6632d01 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -305,7 +305,7 @@ class BSON when XGen::Mongo::Driver::ObjectID OID when true, false - Boolean + BOOLEAN when Time DATE when Hash diff --git a/tests/test_bson.rb b/tests/test_bson.rb index d2d96a2..da38a3b 100644 --- a/tests/test_bson.rb +++ b/tests/test_bson.rb @@ -9,15 +9,69 @@ class BSONTest < Test::Unit::TestCase @b = BSON.new end - def test_object_encoding + def test_string + doc = {'doc' => 'hello, world'} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_code + doc = {'$where' => 'this.a.b < this.b'} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_number + doc = {'doc' => 41.99} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_int + doc = {'doc' => 42} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_object doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}} @b.serialize(doc) assert_equal doc, @b.deserialize end - def test_code_encoding - doc = {'$where' => 'this.a.b < this.b'} + def test_oid + doc = {'doc' => XGen::Mongo::Driver::ObjectID.new} @b.serialize(doc) assert_equal doc, @b.deserialize end + + def test_array + doc = {'doc' => [1, 2, 'a', 'b']} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_regex + doc = {'doc' => /foobar/i} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_boolean + doc = {'doc' => true} + @b.serialize(doc) + assert_equal doc, @b.deserialize + end + + def test_date + doc = {'date' => Time.now} + @b.serialize(doc) + doc2 = @b.deserialize + # Mongo only stores seconds, so comparing raw Time objects will fail + # because the fractional seconds will be different. + assert_equal doc['date'].to_i, doc2['date'].to_i + end + + def test_null + end end