2008-12-16 12:20:20 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'mongo'
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
# NOTE: assumes Mongo is running
|
|
|
|
class BSONTest < Test::Unit::TestCase
|
|
|
|
|
2008-12-19 02:57:20 +00:00
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
2008-12-16 12:20:20 +00:00
|
|
|
def setup
|
2009-01-09 20:22:10 +00:00
|
|
|
# We don't pass a DB to the constructor, even though we are about to test
|
|
|
|
# deserialization. This means that when we deserialize, any DBRefs will
|
|
|
|
# have nil @db ivars. That's fine for now.
|
2008-12-16 12:20:20 +00:00
|
|
|
@b = BSON.new
|
|
|
|
end
|
|
|
|
|
2008-12-16 12:33:39 +00:00
|
|
|
def test_string
|
|
|
|
doc = {'doc' => 'hello, world'}
|
2008-12-16 12:23:22 +00:00
|
|
|
@b.serialize(doc)
|
|
|
|
assert_equal doc, @b.deserialize
|
|
|
|
end
|
|
|
|
|
2008-12-16 12:33:39 +00:00
|
|
|
def test_code
|
2008-12-16 12:23:22 +00:00
|
|
|
doc = {'$where' => 'this.a.b < this.b'}
|
2008-12-16 12:20:20 +00:00
|
|
|
@b.serialize(doc)
|
|
|
|
assert_equal doc, @b.deserialize
|
|
|
|
end
|
2008-12-16 12:33:39 +00:00
|
|
|
|
|
|
|
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_oid
|
2008-12-19 02:57:20 +00:00
|
|
|
doc = {'doc' => ObjectID.new}
|
2008-12-16 12:33:39 +00:00
|
|
|
@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
|
2009-01-08 19:43:30 +00:00
|
|
|
|
|
|
|
def test_dbref
|
|
|
|
oid = ObjectID.new
|
2009-01-09 20:22:10 +00:00
|
|
|
doc = {}
|
|
|
|
doc['dbref'] = DBRef.new(doc, 'dbref', nil, 'namespace', oid)
|
2009-01-08 19:43:30 +00:00
|
|
|
@b.serialize(doc)
|
|
|
|
doc2 = @b.deserialize
|
|
|
|
assert_equal 'namespace', doc2['dbref'].namespace
|
|
|
|
assert_equal oid, doc2['dbref'].object_id
|
|
|
|
end
|
|
|
|
|
2009-01-09 23:20:53 +00:00
|
|
|
def test_symbol
|
|
|
|
doc = {'sym' => :foo}
|
|
|
|
@b.serialize(doc)
|
|
|
|
doc2 = @b.deserialize
|
|
|
|
assert_equal :foo, doc2['sym']
|
|
|
|
end
|
|
|
|
|
2009-01-10 00:58:53 +00:00
|
|
|
def test_binary
|
|
|
|
bin = 'binstring'.to_mongo_binary
|
|
|
|
assert_kind_of Binary, bin
|
|
|
|
|
|
|
|
doc = {'bin' => bin}
|
|
|
|
@b.serialize(doc)
|
|
|
|
doc2 = @b.deserialize
|
|
|
|
assert_equal 'binstring', doc2['bin']
|
|
|
|
end
|
|
|
|
|
2009-01-12 17:08:02 +00:00
|
|
|
def test_undefined
|
|
|
|
doc = {'undef' => Undefined.new}
|
|
|
|
@b.serialize(doc)
|
|
|
|
doc2 = @b.deserialize
|
|
|
|
assert_kind_of Undefined, doc2['undef']
|
|
|
|
end
|
|
|
|
|
2008-12-16 12:20:20 +00:00
|
|
|
end
|