From 78fdfe0eb92f696ab9dfa27d2754f8f6d54ae2ed Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Fri, 9 Jan 2009 19:58:53 -0500 Subject: [PATCH] Support BINARY. Lots more tests. NOTE: since some of them fail, I have commented out the tests for now. --- lib/mongo.rb | 1 + lib/mongo/binary.rb | 34 ++++++++ lib/mongo/util/bson.rb | 22 ++++- lib/mongo/util/xml_to_ruby.rb | 4 +- tests/data/arrays.bson | Bin 0 -> 160 bytes tests/data/arrays.xml | 25 ++++++ tests/data/binary.bson | Bin 0 -> 6145 bytes tests/data/binary.xml | 147 ++++++++++++++++++++++++++++++++++ tests/data/code.bson | Bin 0 -> 29 bytes tests/data/code.xml | 6 ++ tests/data/dates.bson | Bin 0 -> 40 bytes tests/data/dates.xml | 7 ++ tests/data/dbref.bson | Bin 0 -> 34 bytes tests/data/dbref.xml | 9 +++ tests/data/docs.bson | Bin 0 -> 221 bytes tests/data/docs.xml | 38 +++++++++ tests/data/floats.bson | Bin 0 -> 165 bytes tests/data/floats.xml | 13 +++ tests/data/ints.bson | Bin 0 -> 87 bytes tests/data/ints.xml | 11 +++ tests/data/oids.bson | Bin 0 -> 22 bytes tests/data/oids.xml | 6 ++ tests/data/regexes.bson | Bin 0 -> 161 bytes tests/data/regexes.xml | 33 ++++++++ tests/data/smorgasbord.bson | Bin 270 -> 312 bytes tests/data/smorgasbord.xml | 11 ++- tests/data/strings.bson | Bin 0 -> 6145 bytes tests/data/strings.xml | 136 +++++++++++++++++++++++++++++++ tests/data/symbols.bson | Bin 0 -> 43 bytes tests/data/symbols.xml | 7 ++ tests/data/undefined.bson | Bin 0 -> 19 bytes tests/data/undefined.xml | 6 ++ tests/test_bson.rb | 10 +++ tests/test_round_trip.rb | 15 +++- 34 files changed, 532 insertions(+), 9 deletions(-) create mode 100644 lib/mongo/binary.rb create mode 100644 tests/data/arrays.bson create mode 100644 tests/data/arrays.xml create mode 100644 tests/data/binary.bson create mode 100644 tests/data/binary.xml create mode 100644 tests/data/code.bson create mode 100644 tests/data/code.xml create mode 100644 tests/data/dates.bson create mode 100644 tests/data/dates.xml create mode 100644 tests/data/dbref.bson create mode 100644 tests/data/dbref.xml create mode 100644 tests/data/docs.bson create mode 100644 tests/data/docs.xml create mode 100644 tests/data/floats.bson create mode 100644 tests/data/floats.xml create mode 100644 tests/data/ints.bson create mode 100644 tests/data/ints.xml create mode 100644 tests/data/oids.bson create mode 100644 tests/data/oids.xml create mode 100644 tests/data/regexes.bson create mode 100644 tests/data/regexes.xml create mode 100644 tests/data/strings.bson create mode 100644 tests/data/strings.xml create mode 100644 tests/data/symbols.bson create mode 100644 tests/data/symbols.xml create mode 100644 tests/data/undefined.bson create mode 100644 tests/data/undefined.xml diff --git a/lib/mongo.rb b/lib/mongo.rb index 5c60ec4..9fa970d 100644 --- a/lib/mongo.rb +++ b/lib/mongo.rb @@ -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' diff --git a/lib/mongo/binary.rb b/lib/mongo/binary.rb new file mode 100644 index 0000000..a53a96a --- /dev/null +++ b/lib/mongo/binary.rb @@ -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 . +# ++ + +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 diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index b9c9873..14769ad 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -14,6 +14,7 @@ # along with this program. If not, see . # ++ +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 diff --git a/lib/mongo/util/xml_to_ruby.rb b/lib/mongo/util/xml_to_ruby.rb index b053c93..7b2b593 100644 --- a/lib/mongo/util/xml_to_ruby.rb +++ b/lib/mongo/util/xml_to_ruby.rb @@ -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) diff --git a/tests/data/arrays.bson b/tests/data/arrays.bson new file mode 100644 index 0000000000000000000000000000000000000000..19f1270f7467fa5e70ee94a83d2caf0df8cbd004 GIT binary patch literal 160 zcmZ3$z`($gnp;p($-oL^GO!e9<`(3nGDrd00tO6>Aj*(|2}Bt&Fas$DmfXyW)D#8< zAd|@eER)E{(fOn4^P&rVU7FbzvX;OYtR%&tygEc~F1p^brv`PjhBe3Qw24-Uh R36Khe1{NT + + + + + + 1 + 2 + 3 + + + a + 1231515664416 + 42 + + + x + y + z + + yup + + + + diff --git a/tests/data/binary.bson b/tests/data/binary.bson new file mode 100644 index 0000000000000000000000000000000000000000..7ca93da00c634ad64a69a492b2d2e52c259abd5a GIT binary patch literal 6145 zcmeHLK~4iP4D@abhy6e~j(XvO_(G+M1H6DET6W!Tq--1|Zt4Dxr;&gV{Q{D6ab($v z6*=~JnmeMF=co%cR91Tsp>^3**$xskiGLfWK4rQs>ZV<-Z>aSWj`X=9dXjLj;7jB| zp_9u{zLcHdoxx`gn4KT}0vD7MXLaHDh!@R}P_*%sqGU$(lP)GXyOGgjTQArmYx=f- zmJd`-wX1T6SN8q^)0MtQbUZNR(EYgWuxG_0CyZ{aQD){6Vy_rvuE%zrMQ?APOS)O+ z*srykQ_8AUOzT!xJ=J=xAq7Kef7sKIg5i6cPRi_LNWtb>*i>$ug7G=VMj4l8JmxPu LYY6#YA!MR20^C%2 literal 0 HcmV?d00001 diff --git a/tests/data/binary.xml b/tests/data/binary.xml new file mode 100644 index 0000000..b22798f --- /dev/null +++ b/tests/data/binary.xml @@ -0,0 +1,147 @@ + + + + + Zm9v + + AQIDBAU= + + RG9uJ3QgZXhwZWN0IHRoaXMgdGV4dCB0byBiZSBldmVuIG1pbGRseSBpbnRl +cmVzdGluZy4gSXRzIG9ubHkg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pcyB0ZXh0IHRv +IGJlIGV2ZW4gbWlsZGx5IGludGVyZXN0aW5nLiBJdHMgb25seSBwdXJwb3Nl +IGlzIHRvCnByb3ZpZGUgYSAibG9uZyIgc3RyaW5nLiBIb3cgbG9uZyBpcyBs +b25nPyBXZWxsLCBob3cgbG9uZyBpcyBhIHBpZWNlIG9mCnN0cmluZz8gQmV0 +IHlvdSBkaWRuJ3Qgc2VlIHRoYXQgY29taW5nLgoKRG9uJ3QgZXhwZWN0IHRo +aXMgdGV4dCB0byBiZSBldmVuIG1pbGRseSBpbnRlcmVzdGluZy4gSXRzIG9u +bHkgcHVycG9zZSBpcyB0bwpwcm92aWRlIGEgImxvbmciIHN0cmluZy4gSG93 +IGxvbmcgaXMgbG9uZz8gV2VsbCwgaG93IGxvbmcgaXMgYSBwaWVjZSBvZgpz +dHJpbmc/IEJldCB5b3UgZGlkbid0IHNlZSB0aGF0IGNvbWluZy4KCkRvbid0 +IGV4cGVjdCB0aGlzIHRleHQgdG8gYmUgZXZlbiBtaWxkbHkgaW50ZXJlc3Rp +bmcuIEl0cyBvbmx5IHB1cnBvc2UgaXMgdG8KcHJvdmlkZSBhICJsb25nIiBz +dHJpbmcuIEhvdyBsb25nIGlzIGxvbmc/IFdlbGwsIGhvdyBsb25nIGlzIGEg +cGllY2Ugb2YKc3RyaW5nPyBCZXQgeW91IGRpZG4ndCBzZWUgdGhhdCBjb21p +bmcuCg== + + + diff --git a/tests/data/code.bson b/tests/data/code.bson new file mode 100644 index 0000000000000000000000000000000000000000..45665ce22baf7addf7370b895e673f0c38397418 GIT binary patch literal 29 icmb1TU|`@?DbGkPN@d^yvP&{Di}flLY!yt57#ILjp9Qx7 literal 0 HcmV?d00001 diff --git a/tests/data/code.xml b/tests/data/code.xml new file mode 100644 index 0000000..f9e7f5b --- /dev/null +++ b/tests/data/code.xml @@ -0,0 +1,6 @@ + + + + this.x = 42 + + diff --git a/tests/data/dates.bson b/tests/data/dates.bson new file mode 100644 index 0000000000000000000000000000000000000000..b6b88508d9a00fc74ece46577aa99ab79df1f2c1 GIT binary patch literal 40 kcmdO3U|`@(j891{No7ztz`I9|5y-DfEy{-o0c9Zo0K4M|9smFU literal 0 HcmV?d00001 diff --git a/tests/data/dates.xml b/tests/data/dates.xml new file mode 100644 index 0000000..e26c14b --- /dev/null +++ b/tests/data/dates.xml @@ -0,0 +1,7 @@ + + + + 1231515664416 + 0 + + diff --git a/tests/data/dbref.bson b/tests/data/dbref.bson new file mode 100644 index 0000000000000000000000000000000000000000..9d3237f866cdd1c92f8293af7d121ed2f8b77ec9 GIT binary patch literal 34 qcmY#lU|`@$Nh(TBW5`R)O)V}+OipFEw|3wAQ~!4DV^lu-o&f;FrVcj% literal 0 HcmV?d00001 diff --git a/tests/data/dbref.xml b/tests/data/dbref.xml new file mode 100644 index 0000000..789be0c --- /dev/null +++ b/tests/data/dbref.xml @@ -0,0 +1,9 @@ + + + + + namespace + deadbeefcafebabe0123cdef + + + diff --git a/tests/data/docs.bson b/tests/data/docs.bson new file mode 100644 index 0000000000000000000000000000000000000000..0d998c656041fcd867a7bf8e3074066e84189341 GIT binary patch literal 221 zcmcc1z`($qnp;p($-oL^GB6is<`(3nGB^U+0%@5=#U%`kKqgahYI1&F3Ihv}T~eOU zz*3TtS(L&c1yW@IRv}=>zy#tMF)#xupg}3AsRazRAXailW=;x2A%dBTU`8UCz6d7B YcBnQ}1XB~iltnNF5lpZTfLs^=07;G~tN;K2 literal 0 HcmV?d00001 diff --git a/tests/data/docs.xml b/tests/data/docs.xml new file mode 100644 index 0000000..2e17b47 --- /dev/null +++ b/tests/data/docs.xml @@ -0,0 +1,38 @@ + + + + + + + 1 + two + + 1 + 2 + 3 + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/floats.bson b/tests/data/floats.bson new file mode 100644 index 0000000000000000000000000000000000000000..89e6bab7274b934b570aa5853b652c148b9c1e5e GIT binary patch literal 165 zcmZ3=z`($mmzthfl3A7-pP!cs + + + -1.0 + 0.0 + 1.0 + 42.0 + 42.123 + 2147483647.0 + 1099511627775.0 + -1099511627775.0 + + diff --git a/tests/data/ints.bson b/tests/data/ints.bson new file mode 100644 index 0000000000000000000000000000000000000000..7ab5fc116065261703ec1670b35958fbcb87da3c GIT binary patch literal 87 zcmWG%U|k-<)LO{h03;^73925Wm literal 0 HcmV?d00001 diff --git a/tests/data/ints.xml b/tests/data/ints.xml new file mode 100644 index 0000000..7e1e482 --- /dev/null +++ b/tests/data/ints.xml @@ -0,0 +1,11 @@ + + + + -1 + 0 + 1 + 42 + 2147483647 + -2147483647 + + diff --git a/tests/data/oids.bson b/tests/data/oids.bson new file mode 100644 index 0000000000000000000000000000000000000000..813e41b859e0cb0f243167702345a00f51a734e0 GIT binary patch literal 22 ecmWe*U|?X6&rD&sw|3wAQ~!4DV^lu-o&f+?e+i-h literal 0 HcmV?d00001 diff --git a/tests/data/oids.xml b/tests/data/oids.xml new file mode 100644 index 0000000..f5ccc05 --- /dev/null +++ b/tests/data/oids.xml @@ -0,0 +1,6 @@ + + + + deadbeefcafebabe0123cdef + + diff --git a/tests/data/regexes.bson b/tests/data/regexes.bson new file mode 100644 index 0000000000000000000000000000000000000000..c8377781afed4fafeac7e62e70a8e5fa490e77d7 GIT binary patch literal 161 zcmY+8K@P$&3 literal 0 HcmV?d00001 diff --git a/tests/data/regexes.xml b/tests/data/regexes.xml new file mode 100644 index 0000000..cde4136 --- /dev/null +++ b/tests/data/regexes.xml @@ -0,0 +1,33 @@ + + + + + foobar + + + + foobar + i + + + foobar + x + + + foobar + m + + + foobar + imx + + + foobar + imx + + + (\d+)-?\s*[a-z]+ + + + + diff --git a/tests/data/smorgasbord.bson b/tests/data/smorgasbord.bson index 571013a110e2336a1c734c267c7372cb6198865b..5a8d01fb1ae5184158e290469eb95dc7af0170fd 100644 GIT binary patch delta 83 zcmeBU+QB4b!N|bC9-o=Qknu6yvx+NFzGR}%B2ENnZ-6#iVqSb{UP@|OW?pIv15;vr aQf6LaQ6&Q(0|NtusZd-}l$n>#zyJV1=^8lz delta 41 ocmdnN)W;;m$H>6I9-o=QV0 - 33dc67499236d9a01a000000 + 68f167497a0a511f74000000 42 foo true @@ -17,14 +17,17 @@ 1231515664416 namespace - 33dc67499236d9a01a010000 + 68f167497a0a511f74010000 foobar - + - + this is code symbol_value + + YmluYXJ5IHN0cmluZw== + diff --git a/tests/data/strings.bson b/tests/data/strings.bson new file mode 100644 index 0000000000000000000000000000000000000000..7ca93da00c634ad64a69a492b2d2e52c259abd5a GIT binary patch literal 6145 zcmeHLK~4iP4D@abhy6e~j(XvO_(G+M1H6DET6W!Tq--1|Zt4Dxr;&gV{Q{D6ab($v z6*=~JnmeMF=co%cR91Tsp>^3**$xskiGLfWK4rQs>ZV<-Z>aSWj`X=9dXjLj;7jB| zp_9u{zLcHdoxx`gn4KT}0vD7MXLaHDh!@R}P_*%sqGU$(lP)GXyOGgjTQArmYx=f- zmJd`-wX1T6SN8q^)0MtQbUZNR(EYgWuxG_0CyZ{aQD){6Vy_rvuE%zrMQ?APOS)O+ z*srykQ_8AUOzT!xJ=J=xAq7Kef7sKIg5i6cPRi_LNWtb>*i>$ug7G=VMj4l8JmxPu LYY6#YA!MR20^C%2 literal 0 HcmV?d00001 diff --git a/tests/data/strings.xml b/tests/data/strings.xml new file mode 100644 index 0000000..c1b0408 --- /dev/null +++ b/tests/data/strings.xml @@ -0,0 +1,136 @@ + + + + + foo +  + 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. + + + diff --git a/tests/data/symbols.bson b/tests/data/symbols.bson new file mode 100644 index 0000000000000000000000000000000000000000..d0d98a4ce6d0f378a00ea80989ad9831d127a4ab GIT binary patch literal 43 tcmdO9U|`@&j4!UtP0G( + + + symbol_value + @- + + diff --git a/tests/data/undefined.bson b/tests/data/undefined.bson new file mode 100644 index 0000000000000000000000000000000000000000..6e69e313a99754ed1a277cd64460a4c617e9369b GIT binary patch literal 19 acmWe;U|?WN%!@D0OG!=3%u7vSU;qFsQv~_| literal 0 HcmV?d00001 diff --git a/tests/data/undefined.xml b/tests/data/undefined.xml new file mode 100644 index 0000000..d96615e --- /dev/null +++ b/tests/data/undefined.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/tests/test_bson.rb b/tests/test_bson.rb index 54cf623..83881e8 100644 --- a/tests/test_bson.rb +++ b/tests/test_bson.rb @@ -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 diff --git a/tests/test_round_trip.rb b/tests/test_round_trip.rb index 03ab613..2351990 100644 --- a/tests/test_round_trip.rb +++ b/tests/test_round_trip.rb @@ -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 - assert_equal bson.length, bson_from_ruby.length - assert_equal bson, bson_from_ruby + +# # 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. #