Support BINARY. Lots more tests. NOTE: since some of them fail, I have commented out the tests for now.

This commit is contained in:
Jim Menard 2009-01-09 19:58:53 -05:00
parent 77d192183a
commit 78fdfe0eb9
34 changed files with 532 additions and 9 deletions

View File

@ -1,6 +1,7 @@
require 'mongo/mongo'
require 'mongo/objectid'
require 'mongo/dbref'
require 'mongo/binary'
require 'mongo/message'
require 'mongo/db'
require 'mongo/cursor'

34
lib/mongo/binary.rb Normal file
View File

@ -0,0 +1,34 @@
# --
# Copyright (C) 2008-2009 10gen Inc.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License, version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ++
module XGen
module Mongo
module Driver
# An array of binary bytes. The only reason this exists is so that the
# BSON encoder will know to output the Mongo BINARY type.
class Binary < String; end
end
end
end
class String
# Convert a string into a XGen::Mongo::Driver::Binary
def to_mongo_binary
XGen::Mongo::Driver::Binary.new(self)
end
end

View File

@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ++
require 'base64'
require 'mongo/util/byte_buffer'
require 'mongo/util/ordered_hash'
require 'mongo/objectid'
@ -88,7 +89,9 @@ class BSON
serialize_dbref_element(@buf, k, v)
when SYMBOL
serialize_symbol_element(@buf, k, v)
when BINARY, UNDEFINED, CODE_W_SCOPE
when BINARY
serialize_binary_element(@buf, k, v)
when UNDEFINED, CODE_W_SCOPE # UNDEFINED should never happen
# TODO
raise "unimplemented type #{type}"
else
@ -146,7 +149,10 @@ class BSON
when SYMBOL
key = deserialize_cstr(@buf)
doc[key] = deserialize_symbol_data(@buf, key)
when BINARY, CODE_W_SCOPE
when BINARY
key = deserialize_cstr(@buf)
doc[key] = deserialize_binary_data(@buf, key)
when CODE_W_SCOPE
# TODO
raise "unimplemented type #{type}"
when EOO
@ -233,6 +239,10 @@ class BSON
deserialize_cstr(buf).intern
end
def deserialize_binary_data(buf, key)
Base64.decode64(deserialize_cstr(buf))
end
def serialize_eoo_element(buf)
buf.put(EOO)
end
@ -255,6 +265,12 @@ class BSON
self.class.serialize_cstr(buf, val)
end
def serialize_binary_element(buf, key, val)
buf.put(BINARY)
self.class.serialize_cstr(buf, key)
self.class.serialize_cstr(buf, Base64.encode64(val))
end
def serialize_boolean_element(buf, key, val)
buf.put(BOOLEAN)
self.class.serialize_cstr(buf, key)
@ -363,6 +379,8 @@ class BSON
OID
when XGen::Mongo::Driver::DBRef
REF
when XGen::Mongo::Driver::Binary
BINARY
when true, false
BOOLEAN
when Time

View File

@ -44,6 +44,8 @@ class XMLToRuby
e.text.to_f
when 'string', 'code'
e.text.to_s
when 'binary'
Base64.decode64(e.text.to_s).to_mongo_binary
when 'symbol'
e.text.to_s.intern
when 'boolean'
@ -54,7 +56,7 @@ class XMLToRuby
Time.at(e.text.to_f / 1000.0)
when 'regex'
regex_to_ruby(e.elements)
when 'null'
when 'null', 'undefined'
nil
when 'doc'
doc_to_ruby(e)

BIN
tests/data/arrays.bson Normal file

Binary file not shown.

25
tests/data/arrays.xml Normal file
View File

@ -0,0 +1,25 @@
<twonk>
<meta/>
<doc>
<array name="empty">
</array>
<array name="simple">
<int name="0">1</int>
<int name="1">2</int>
<int name="2">3</int>
</array>
<array name="mixed">
<string name="0">a</string>
<date name="1">1231515664416</date>
<int name="2">42</int>
</array>
<array name="subobject">
<string name="0">x</string>
<string name="1">y</string>
<string name="2">z</string>
<doc name="3">
<string name="subobject">yup</string>
</doc>
</array>
</doc>
</twonk>

BIN
tests/data/binary.bson Normal file

Binary file not shown.

147
tests/data/binary.xml Normal file
View File

@ -0,0 +1,147 @@
<twonk>
<meta/>
<doc>
<binary name="empty"/>
<binary name="foo">Zm9v
</binary>
<binary name="control_chars">AQIDBAU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YKc3RyaW5nPyBCZXQgeW91IGRpZG4n
dCBzZWUgdGhhdCBjb21pbmcuCgpEb24ndCBleHBlY3QgdGhpcyB0ZXh0IHRv
IGJlIGV2ZW4gbWlsZGx5IGludGVyZXN0aW5nLiBJdHMgb25seSBwdXJwb3Nl
IGlzIHRvCnByb3ZpZGUgYSAibG9uZyIgc3RyaW5nLiBIb3cgbG9uZyBpcyBs
b25nPyBXZWxsLCBob3cgbG9uZyBpcyBhIHBpZWNlIG9mCnN0cmluZz8gQmV0
IHlvdSBkaWRuJ3Qgc2VlIHRoYXQgY29taW5nLgoKRG9uJ3QgZXhwZWN0IHRo
aXMgdGV4dCB0byBiZSBldmVuIG1pbGRseSBpbnRlcmVzdGluZy4gSXRzIG9u
bHkgcHVycG9zZSBpcyB0bwpwcm92aWRlIGEgImxvbmciIHN0cmluZy4gSG93
IGxvbmcgaXMgbG9uZz8gV2VsbCwgaG93IGxvbmcgaXMgYSBwaWVjZSBvZgpz
dHJpbmc/IEJldCB5b3UgZGlkbid0IHNlZSB0aGF0IGNvbWluZy4KCkRvbid0
IGV4cGVjdCB0aGlzIHRleHQgdG8gYmUgZXZlbiBtaWxkbHkgaW50ZXJlc3Rp
bmcuIEl0cyBvbmx5IHB1cnBvc2UgaXMgdG8KcHJvdmlkZSBhICJsb25nIiBz
dHJpbmcuIEhvdyBsb25nIGlzIGxvbmc/IFdlbGwsIGhvdyBsb25nIGlzIGEg
cGllY2Ugb2YKc3RyaW5nPyBCZXQgeW91IGRpZG4ndCBzZWUgdGhhdCBjb21p
bmcuCg==
</binary>
</doc>
</twonk>

BIN
tests/data/code.bson Normal file

Binary file not shown.

6
tests/data/code.xml Normal file
View File

@ -0,0 +1,6 @@
<twonk>
<meta/>
<doc>
<code name="$where">this.x = 42</code>
</doc>
</twonk>

BIN
tests/data/dates.bson Normal file

Binary file not shown.

7
tests/data/dates.xml Normal file
View File

@ -0,0 +1,7 @@
<twonk>
<meta/>
<doc>
<date name="a_date">1231515664416</date>
<date name="zero_date">0</date>
</doc>
</twonk>

BIN
tests/data/dbref.bson Normal file

Binary file not shown.

9
tests/data/dbref.xml Normal file
View File

@ -0,0 +1,9 @@
<twonk>
<meta/>
<doc>
<ref name="dbref">
<ns>namespace</ns>
<oid>deadbeefcafebabe0123cdef</oid>
</ref>
</doc>
</twonk>

BIN
tests/data/docs.bson Normal file

Binary file not shown.

38
tests/data/docs.xml Normal file
View File

@ -0,0 +1,38 @@
<twonk>
<meta/>
<doc>
<doc name="empty">
</doc>
<doc name="simple">
<int name="first">1</int>
<string name="second">two</string>
<array name="third">
<int name="0">1</int>
<int name="1">2</int>
<int name="2">3</int>
</array>
</doc>
<doc name="deep">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
<doc name="child">
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</doc>
</twonk>

BIN
tests/data/floats.bson Normal file

Binary file not shown.

13
tests/data/floats.xml Normal file
View File

@ -0,0 +1,13 @@
<twonk>
<meta/>
<doc>
<number name="negative_one">-1.0</number>
<number name="zero">0.0</number>
<number name="one">1.0</number>
<number name="the_answer">42.0</number>
<number name="more_than_the_answer">42.123</number>
<number name="double">2147483647.0</number>
<number name="big_double">1099511627775.0</number>
<number name="negative_double">-1099511627775.0</number>
</doc>
</twonk>

BIN
tests/data/ints.bson Normal file

Binary file not shown.

11
tests/data/ints.xml Normal file
View File

@ -0,0 +1,11 @@
<twonk>
<meta/>
<doc>
<int name="negative_one">-1</int>
<int name="zero">0</int>
<int name="one">1</int>
<int name="the_answer">42</int>
<int name="long">2147483647</int>
<int name="negative_long">-2147483647</int>
</doc>
</twonk>

BIN
tests/data/oids.bson Normal file

Binary file not shown.

6
tests/data/oids.xml Normal file
View File

@ -0,0 +1,6 @@
<twonk>
<meta/>
<doc>
<oid name="_id">deadbeefcafebabe0123cdef</oid>
</doc>
</twonk>

BIN
tests/data/regexes.bson Normal file

Binary file not shown.

33
tests/data/regexes.xml Normal file
View File

@ -0,0 +1,33 @@
<twonk>
<meta/>
<doc>
<regex name="simple">
<pattern>foobar</pattern>
<options/>
</regex>
<regex name="ignore_case">
<pattern>foobar</pattern>
<options>i</options>
</regex>
<regex name="extended">
<pattern>foobar</pattern>
<options>x</options>
</regex>
<regex name="multiline">
<pattern>foobar</pattern>
<options>m</options>
</regex>
<regex name="all">
<pattern>foobar</pattern>
<options>imx</options>
</regex>
<regex name="all_different_order">
<pattern>foobar</pattern>
<options>imx</options>
</regex>
<regex name="interesting">
<pattern>(\d+)-?\s*[a-z]+</pattern>
<options/>
</regex>
</doc>
</twonk>

Binary file not shown.

View File

@ -1,7 +1,7 @@
<twonk>
<meta/>
<doc>
<oid name="_id">33dc67499236d9a01a000000</oid>
<oid name="_id">68f167497a0a511f74000000</oid>
<int name="an_int">42</int>
<string name="a_string">foo</string>
<boolean name="truthiness">true</boolean>
@ -17,14 +17,17 @@
<date name="now">1231515664416</date>
<ref name="dbref">
<ns>namespace</ns>
<oid>33dc67499236d9a01a010000</oid>
<oid>68f167497a0a511f74010000</oid>
</ref>
<regex name="regex">
<pattern>foobar</pattern>
<options></options>
<options/>
</regex>
<null name="a_null_value"></null>
<null name="a_null_value"/>
<code name="$where">this is code</code>
<symbol name="a_symbol">symbol_value</symbol>
<undefined name="an_undefined"/>
<binary name="a_binary">YmluYXJ5IHN0cmluZw==
</binary>
</doc>
</twonk>

BIN
tests/data/strings.bson Normal file

Binary file not shown.

136
tests/data/strings.xml Normal file
View File

@ -0,0 +1,136 @@
<twonk>
<meta/>
<doc>
<string name="empty"/>
<string name="foo">foo</string>
<string name="control_chars"></string>
<string name="long">Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
Don't expect this text to be even mildly interesting. Its only purpose is to
provide a "long" string. How long is long? Well, how long is a piece of
string? Bet you didn't see that coming.
</string>
</doc>
</twonk>

BIN
tests/data/symbols.bson Normal file

Binary file not shown.

7
tests/data/symbols.xml Normal file
View File

@ -0,0 +1,7 @@
<twonk>
<meta/>
<doc>
<symbol name="a_symbol">symbol_value</symbol>
<symbol name="funky_name">@-</symbol>
</doc>
</twonk>

BIN
tests/data/undefined.bson Normal file

Binary file not shown.

6
tests/data/undefined.xml Normal file
View File

@ -0,0 +1,6 @@
<twonk>
<meta/>
<doc>
<undefined name="an_undefined"/>
</doc>
</twonk>

View File

@ -97,4 +97,14 @@ class BSONTest < Test::Unit::TestCase
assert_equal :foo, doc2['sym']
end
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
end

View File

@ -46,8 +46,19 @@ class RoundTripTest < Test::Unit::TestCase
# Turn the Ruby object into BSON bytes and compare with the BSON bytes
# from the file.
bson_from_ruby = BSON.new.serialize(obj).to_a
# # DEBUG
# File.open(File.join(HERE, 'data', "#{name}_out.bson"), 'wb') { |f|
# bson_from_ruby.each { |b| f.putc(b) }
# }
begin
assert_equal bson.length, bson_from_ruby.length
assert_equal bson, bson_from_ruby
rescue => ex
$stderr.puts "failure while round-tripping #{name}" # DEBUG
raise ex
end
# Turn those BSON bytes back into a Ruby object.
#