Permalink
Browse files

Added checkkey mode to ensure no illegal keys for Mongo DB

  • Loading branch information...
1 parent 4a9210f commit 200eef7be645beabbc5565646ae20857feb4bc7e @christkv committed May 25, 2012
Showing with 684 additions and 646 deletions.
  1. +148 −121 src/bson.erl
  2. +536 −525 src/bson_tests.erl
View
269 src/bson.erl
@@ -2,7 +2,7 @@
-module(bson).
% Exported functions for the bson parser
--export ([serialize/1, deserialize/1, deserialize/2, deserialize/3]).
+-export ([serialize/1, serialize/2, deserialize/1, deserialize/2, deserialize/3]).
-export ([utf8/1, bin/1, bin/2, minkey/0, maxkey/0, javascript/1, javascript/2, regexp/2, gen_objectid/1, objectid/1, objectid/3, timestamp_to_bson_time/1, bson_time_to_timestamp/1]).
-export ([hexstr_to_bin/1]).
@@ -13,148 +13,175 @@
-include ("bson.hrl").
% Serialize document
-serialize(Doc) ->
- serialize_doc(Doc).
+serialize(Doc) -> serialize_doc(Doc, false).
+serialize(Doc, CheckKeys) ->
+ serialize_doc(Doc, CheckKeys).
-serialize_doc(Doc) ->
+serialize_doc(Doc, CheckKeys) ->
case Doc of
Doc when is_tuple(Doc), element(1, Doc) == dict ->
% Create final binary
- Bin = list_to_binary(serialize_doc_objects(dict:to_list(Doc))),
+ Bin = list_to_binary(serialize_doc_objects(dict:to_list(Doc), CheckKeys)),
% Add document header
list_to_binary([<<?put_int32u(byte_size(Bin) + 4 + 1)>>, Bin, <<0>>]);
_ ->
% Create final binary
- Bin = list_to_binary(serialize_doc_objects(Doc)),
+ Bin = list_to_binary(serialize_doc_objects(Doc, CheckKeys)),
% Add document header
list_to_binary([<<?put_int32u(byte_size(Bin) + 4 + 1)>>, Bin, <<0>>])
end.
-serialize_doc_objects([Head|Tail]) ->
+serialize_doc_objects([Head|Tail], CheckKeys) ->
% Handle the situation where the value is a dict
FinalHeadValue = case Head of
{HeadName, [HeadValue]} when is_tuple(HeadValue), element(1, HeadValue) == dict ->
{HeadName, [dict:to_list(HeadValue)]};
_ ->
Head
+ end,
+ % If we have a Check Key throw
+ Valid = case CheckKeys of
+ true ->
+ % unpack the variable
+ {NameVariable, _} = FinalHeadValue,
+ % if we have the first character $ it's an illegal key
+ case NameVariable of
+ <<"$", _/binary>> -> false;
+ _ ->
+ % match on . in the name
+ case binary:match(NameVariable, <<".">>) of
+ nomatch -> true;
+ _ -> false
+ end
+ end;
+ false -> true
end,
- % Match the case of the Head
- BinaryList = case FinalHeadValue of
- {Name, {maxkey}} when is_binary(Name) ->
- [list_to_binary([<<16#7f>>, Name, <<0>>])];
- {Name, [{maxkey}]} when is_binary(Name) ->
- [list_to_binary([<<16#7f>>, Name, <<0>>])];
- {Name, {minkey}} when is_binary(Name) ->
- [list_to_binary([<<16#ff>>, Name, <<0>>])];
- {Name, [{minkey}]} when is_binary(Name) ->
- [list_to_binary([<<16#ff>>, Name, <<0>>])];
- {Name, {regexp, RegExp, Options}} when is_binary(Name) ->
- [list_to_binary([<<16#0B>>, Name, <<0>>, RegExp, <<0>>, Options, <<0>>])];
- {Name, [{bin, SubType, Binary}]} when is_binary(Name), is_integer(SubType), is_binary(Binary) ->
- [list_to_binary([<<16#05>>, Name, <<0>>, <<?put_int32u(byte_size(Binary))>>, <<?put_int8(SubType)>>, Binary])];
- {Name, {bin, SubType, Binary}} when is_binary(Name), is_integer(SubType), is_binary(Binary) ->
- [list_to_binary([<<16#05>>, Name, <<0>>, <<?put_int32u(byte_size(Binary))>>, <<?put_int8(SubType)>>, Binary])];
- {Name, {objectid, ObjectId}} when is_binary(Name), is_binary(ObjectId) ->
- [list_to_binary([<<16#07>>, Name, <<0>>, ObjectId])];
- {Name, undefined} when is_binary(Name) ->
- [list_to_binary([<<16#0A>>, Name, <<0>>])];
- {Name, null} when is_binary(Name) ->
- [list_to_binary([<<16#0A>>, Name, <<0>>])];
- {Name, {js, Code}} when is_binary(Name), is_binary(Code) ->
- [list_to_binary([<<16#0D>>, Name, <<0>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>])];
- {Name, [{js, Code, Scope}]} when is_binary(Name), is_binary(Code), element(1, Scope) == dict ->
- [Object] = serialize_doc_objects(dict:to_list(Scope)),
- TotalLength = 4 + (byte_size(Code) + 1) + 4 + (4 + byte_size(Object) + 1),
- [list_to_binary([<<16#0F>>, Name, <<0>>, <<?put_int32u(TotalLength)>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
- {Name, [{js, Code, Scope}]} when is_binary(Name), is_binary(Code) ->
- [Object] = serialize_doc_objects(Scope),
- TotalLength = 4 + (byte_size(Code) + 1) + 4 + (4 + byte_size(Object) + 1),
- [list_to_binary([<<16#0F>>, Name, <<0>>, <<?put_int32u(TotalLength)>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
- {Name, {js, Code, Scope}} when is_binary(Name), is_binary(Code) ->
- [Object] = serialize_doc_objects(Scope),
- TotalLength = 4 + (byte_size(Code) + 1) + 4 + (4 + byte_size(Object) + 1),
- [list_to_binary([<<16#0F>>, Name, <<0>>, <<?put_int32u(TotalLength)>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
- {Name, [{js, Code}]} when is_binary(Name), is_binary(Code) ->
- [list_to_binary([<<16#0D>>, Name, <<0>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>])];
- {Name, {MegaSecs, Seconds, Micro}} ->
- [list_to_binary([<<16#09>>, Name, <<0>>, <<?put_int64(timestamp_to_bson_time({MegaSecs, Seconds, Micro}))>>])];
- {Name, Value} when is_binary(Name), is_boolean(Value) ->
- case Value of
- true->
- [list_to_binary([<<16#08>>, Name, <<0>>, <<1>>])];
- _ ->
- [list_to_binary([<<16#08>>, Name, <<0>>, <<0>>])]
- end;
- {Name, Value} when is_binary(Name), is_atom(Value) ->
- ValueBin = atom_to_binary(Value, utf8),
- [list_to_binary([<<16#0E>>, Name, <<0>>, <<?put_int32u(byte_size(ValueBin) + 1)>>, ValueBin, <<0>>])];
- {Name, [[Value]]} when is_binary(Name), is_tuple(Value) ->
- % trigger serialization of all the values
- [Object] = serialize_doc_objects([Value]),
- [list_to_binary([<<16#03>>, Name, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
- {Name, [Value]} when is_binary(Name), is_list(Value) ->
- % Serialize the array
- BinDoc = serialize_array(0, Value),
- % trigger serialization of all the values
- [list_to_binary([<<16#04>>, Name, <<0>>, <<?put_int32u(4 + byte_size(BinDoc) + 1)>>, BinDoc, <<0>>])];
- {Name, Value} when is_binary(Name), is_binary(Value) ->
- [list_to_binary([<<16#02>>, Name, <<0>>, <<?put_int32u(byte_size(Value) + 1)>>, Value, <<0>>])];
- {Name, Value} when is_binary(Name), is_float(Value) ->
- [list_to_binary([<<16#01>>, Name, <<0>>, <<?put_float(Value)>>])];
- {Name, Value} when is_binary(Name), is_integer(Value), ?fits_int32(Value) ->
- [list_to_binary([<<16#10>>, Name, <<0>>, <<?put_int32(Value)>>])];
- {Name, Value} when is_binary(Name), is_integer(Value), ?fits_int64(Value) ->
- [list_to_binary([<<16#12>>, Name, <<0>>, <<?put_int64(Value)>>])];
- {Name, Value} when is_binary(Name), is_tuple(Value) ->
- % trigger serialization of all the values
- [Object] = serialize_doc_objects([Value]),
- [list_to_binary([<<16#03>>, Name, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
- {Name, [{regexp, RegExp, Options}]} when is_binary(Name) ->
- [list_to_binary([<<16#0B>>, Name, <<0>>, RegExp, <<0>>, Options, <<0>>])];
- {Name, [undefined]} when is_binary(Name) ->
- [list_to_binary([<<16#0A>>, Name, <<0>>])];
- {Name, [null]} when is_binary(Name) ->
- [list_to_binary([<<16#0A>>, Name, <<0>>])];
- {Name, [{MegaSecs, Seconds, Micro}]} ->
- [list_to_binary([<<16#09>>, Name, <<0>>, <<?put_int64(timestamp_to_bson_time({MegaSecs, Seconds, Micro}))>>])];
- {Name, [{objectid, ObjectId}]} when is_binary(Name), is_binary(ObjectId) ->
- [list_to_binary([<<16#07>>, Name, <<0>>, ObjectId])];
- {Name, [Value]} when is_binary(Name), is_binary(Value) ->
- [list_to_binary([<<16#02>>, Name, <<0>>, <<?put_int32u(byte_size(Value) + 1)>>, Value, <<0>>])];
- {Name, [Value]} when is_binary(Name), is_boolean(Value) ->
- case Value of
- true->
- [list_to_binary([<<16#08>>, Name, <<0>>, <<1>>])];
- _ ->
- [list_to_binary([<<16#08>>, Name, <<0>>, <<0>>])]
- end;
- {Name, [Value]} when is_binary(Name), is_atom(Value) ->
- ValueBin = atom_to_binary(Value, utf8),
- [list_to_binary([<<16#0E>>, Name, <<0>>, <<?put_int32u(byte_size(ValueBin) + 1)>>, ValueBin, <<0>>])];
- {Name, [Value]} when is_binary(Name), is_float(Value) ->
- [list_to_binary([<<16#01>>, Name, <<0>>, <<?put_float(Value)>>])];
- {Name, [Value]} when is_binary(Name), is_integer(Value), ?fits_int32(Value) ->
- [list_to_binary([<<16#10>>, Name, <<0>>, <<?put_int32(Value)>>])];
- {Name, [Value]} when is_binary(Name), is_integer(Value), ?fits_int64(Value) ->
- [list_to_binary([<<16#12>>, Name, <<0>>, <<?put_int64(Value)>>])];
- {Name, [Value|_]} when is_binary(Name), is_tuple(Value) == false, is_list(Value) == false ->
- {_, ArrayValue} = Head,
- % Serialize the array
- BinDoc = serialize_array(0, ArrayValue),
- % trigger serialization of all the values
- [list_to_binary([<<16#04>>, Name, <<0>>, <<?put_int32u(4 + byte_size(BinDoc) + 1)>>, BinDoc, <<0>>])];
- _ ->
- serialize_doc_objects(Tail)
- end,
- % Process next document
- BinaryList ++ serialize_doc_objects(Tail);
-serialize_doc_objects([]) -> [].
+ % if it's a valid key process it otherwise return error
+ case Valid of
+ true ->
+ % Match the case of the Head
+ BinaryList = case FinalHeadValue of
+ {Name, {maxkey}} when is_binary(Name) ->
+ [list_to_binary([<<16#7f>>, Name, <<0>>])];
+ {Name, [{maxkey}]} when is_binary(Name) ->
+ [list_to_binary([<<16#7f>>, Name, <<0>>])];
+ {Name, {minkey}} when is_binary(Name) ->
+ [list_to_binary([<<16#ff>>, Name, <<0>>])];
+ {Name, [{minkey}]} when is_binary(Name) ->
+ [list_to_binary([<<16#ff>>, Name, <<0>>])];
+ {Name, {regexp, RegExp, Options}} when is_binary(Name) ->
+ [list_to_binary([<<16#0B>>, Name, <<0>>, RegExp, <<0>>, Options, <<0>>])];
+ {Name, [{bin, SubType, Binary}]} when is_binary(Name), is_integer(SubType), is_binary(Binary) ->
+ [list_to_binary([<<16#05>>, Name, <<0>>, <<?put_int32u(byte_size(Binary))>>, <<?put_int8(SubType)>>, Binary])];
+ {Name, {bin, SubType, Binary}} when is_binary(Name), is_integer(SubType), is_binary(Binary) ->
+ [list_to_binary([<<16#05>>, Name, <<0>>, <<?put_int32u(byte_size(Binary))>>, <<?put_int8(SubType)>>, Binary])];
+ {Name, {objectid, ObjectId}} when is_binary(Name), is_binary(ObjectId) ->
+ [list_to_binary([<<16#07>>, Name, <<0>>, ObjectId])];
+ {Name, undefined} when is_binary(Name) ->
+ [list_to_binary([<<16#0A>>, Name, <<0>>])];
+ {Name, null} when is_binary(Name) ->
+ [list_to_binary([<<16#0A>>, Name, <<0>>])];
+ {Name, {js, Code}} when is_binary(Name), is_binary(Code) ->
+ [list_to_binary([<<16#0D>>, Name, <<0>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>])];
+ {Name, [{js, Code, Scope}]} when is_binary(Name), is_binary(Code), element(1, Scope) == dict ->
+ [Object] = serialize_doc_objects(dict:to_list(Scope), CheckKeys),
+ TotalLength = 4 + (byte_size(Code) + 1) + 4 + (4 + byte_size(Object) + 1),
+ [list_to_binary([<<16#0F>>, Name, <<0>>, <<?put_int32u(TotalLength)>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
+ {Name, [{js, Code, Scope}]} when is_binary(Name), is_binary(Code) ->
+ [Object] = serialize_doc_objects(Scope, CheckKeys),
+ TotalLength = 4 + (byte_size(Code) + 1) + 4 + (4 + byte_size(Object) + 1),
+ [list_to_binary([<<16#0F>>, Name, <<0>>, <<?put_int32u(TotalLength)>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
+ {Name, {js, Code, Scope}} when is_binary(Name), is_binary(Code) ->
+ [Object] = serialize_doc_objects(Scope, CheckKeys),
+ TotalLength = 4 + (byte_size(Code) + 1) + 4 + (4 + byte_size(Object) + 1),
+ [list_to_binary([<<16#0F>>, Name, <<0>>, <<?put_int32u(TotalLength)>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
+ {Name, [{js, Code}]} when is_binary(Name), is_binary(Code) ->
+ [list_to_binary([<<16#0D>>, Name, <<0>>, <<?put_int32u(byte_size(Code) + 1)>>, Code, <<0>>])];
+ {Name, {MegaSecs, Seconds, Micro}} ->
+ [list_to_binary([<<16#09>>, Name, <<0>>, <<?put_int64(timestamp_to_bson_time({MegaSecs, Seconds, Micro}))>>])];
+ {Name, Value} when is_binary(Name), is_boolean(Value) ->
+ case Value of
+ true->
+ [list_to_binary([<<16#08>>, Name, <<0>>, <<1>>])];
+ _ ->
+ [list_to_binary([<<16#08>>, Name, <<0>>, <<0>>])]
+ end;
+ {Name, Value} when is_binary(Name), is_atom(Value) ->
+ ValueBin = atom_to_binary(Value, utf8),
+ [list_to_binary([<<16#0E>>, Name, <<0>>, <<?put_int32u(byte_size(ValueBin) + 1)>>, ValueBin, <<0>>])];
+ {Name, [[Value]]} when is_binary(Name), is_tuple(Value) ->
+ % trigger serialization of all the values
+ [Object] = serialize_doc_objects([Value], CheckKeys),
+ [list_to_binary([<<16#03>>, Name, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
+ {Name, [Value]} when is_binary(Name), is_list(Value) ->
+ % Serialize the array
+ BinDoc = serialize_array(0, Value, CheckKeys),
+ % trigger serialization of all the values
+ [list_to_binary([<<16#04>>, Name, <<0>>, <<?put_int32u(4 + byte_size(BinDoc) + 1)>>, BinDoc, <<0>>])];
+ {Name, Value} when is_binary(Name), is_binary(Value) ->
+ [list_to_binary([<<16#02>>, Name, <<0>>, <<?put_int32u(byte_size(Value) + 1)>>, Value, <<0>>])];
+ {Name, Value} when is_binary(Name), is_float(Value) ->
+ [list_to_binary([<<16#01>>, Name, <<0>>, <<?put_float(Value)>>])];
+ {Name, Value} when is_binary(Name), is_integer(Value), ?fits_int32(Value) ->
+ [list_to_binary([<<16#10>>, Name, <<0>>, <<?put_int32(Value)>>])];
+ {Name, Value} when is_binary(Name), is_integer(Value), ?fits_int64(Value) ->
+ [list_to_binary([<<16#12>>, Name, <<0>>, <<?put_int64(Value)>>])];
+ {Name, Value} when is_binary(Name), is_tuple(Value) ->
+ % trigger serialization of all the values
+ [Object] = serialize_doc_objects([Value], CheckKeys),
+ [list_to_binary([<<16#03>>, Name, <<0>>, <<?put_int32u(4 + byte_size(Object) + 1)>>, Object, <<0>>])];
+ {Name, [{regexp, RegExp, Options}]} when is_binary(Name) ->
+ [list_to_binary([<<16#0B>>, Name, <<0>>, RegExp, <<0>>, Options, <<0>>])];
+ {Name, [undefined]} when is_binary(Name) ->
+ [list_to_binary([<<16#0A>>, Name, <<0>>])];
+ {Name, [null]} when is_binary(Name) ->
+ [list_to_binary([<<16#0A>>, Name, <<0>>])];
+ {Name, [{MegaSecs, Seconds, Micro}]} ->
+ [list_to_binary([<<16#09>>, Name, <<0>>, <<?put_int64(timestamp_to_bson_time({MegaSecs, Seconds, Micro}))>>])];
+ {Name, [{objectid, ObjectId}]} when is_binary(Name), is_binary(ObjectId) ->
+ [list_to_binary([<<16#07>>, Name, <<0>>, ObjectId])];
+ {Name, [Value]} when is_binary(Name), is_binary(Value) ->
+ [list_to_binary([<<16#02>>, Name, <<0>>, <<?put_int32u(byte_size(Value) + 1)>>, Value, <<0>>])];
+ {Name, [Value]} when is_binary(Name), is_boolean(Value) ->
+ case Value of
+ true->
+ [list_to_binary([<<16#08>>, Name, <<0>>, <<1>>])];
+ _ ->
+ [list_to_binary([<<16#08>>, Name, <<0>>, <<0>>])]
+ end;
+ {Name, [Value]} when is_binary(Name), is_atom(Value) ->
+ ValueBin = atom_to_binary(Value, utf8),
+ [list_to_binary([<<16#0E>>, Name, <<0>>, <<?put_int32u(byte_size(ValueBin) + 1)>>, ValueBin, <<0>>])];
+ {Name, [Value]} when is_binary(Name), is_float(Value) ->
+ [list_to_binary([<<16#01>>, Name, <<0>>, <<?put_float(Value)>>])];
+ {Name, [Value]} when is_binary(Name), is_integer(Value), ?fits_int32(Value) ->
+ [list_to_binary([<<16#10>>, Name, <<0>>, <<?put_int32(Value)>>])];
+ {Name, [Value]} when is_binary(Name), is_integer(Value), ?fits_int64(Value) ->
+ [list_to_binary([<<16#12>>, Name, <<0>>, <<?put_int64(Value)>>])];
+ {Name, [Value|_]} when is_binary(Name), is_tuple(Value) == false, is_list(Value) == false ->
+ {_, ArrayValue} = Head,
+ % Serialize the array
+ BinDoc = serialize_array(0, ArrayValue, CheckKeys),
+ % trigger serialization of all the values
+ [list_to_binary([<<16#04>>, Name, <<0>>, <<?put_int32u(4 + byte_size(BinDoc) + 1)>>, BinDoc, <<0>>])];
+ _ ->
+ serialize_doc_objects(Tail, CheckKeys)
+ end,
+ % Process next document
+ BinaryList ++ serialize_doc_objects(Tail, CheckKeys);
+ false ->
+ % Unpack the head value
+ {KeyName, _} = FinalHeadValue,
+ % throw with the error
+ throw({key_check_error, KeyName})
+ end;
+serialize_doc_objects([], _) -> [].
% Serialize an array of values using the code for all the other types
-serialize_array(Number, [Head|Tail]) ->
- list_to_binary([serialize_doc_objects([{utf8(integer_to_list(Number)), [Head]}]), serialize_array(Number + 1, Tail)]);
-serialize_array(_, []) -> [].
+serialize_array(Number, [Head|Tail], CheckKeys) ->
+ list_to_binary([serialize_doc_objects([{utf8(integer_to_list(Number)), [Head]}], CheckKeys), serialize_array(Number + 1, Tail, CheckKeys)]);
+serialize_array(_, [], _) -> [].
% Deserialize document
deserialize(BinDoc) when is_binary(BinDoc) ->
View
1,061 src/bson_tests.erl
@@ -3,604 +3,615 @@
-include_lib("eunit/include/eunit.hrl").
%%
-%% Serialization tests
+%% Serialization tests
%%
simple_single_level_two_attribute_serialization_test() ->
- % Build a simple string key-value dict and serialize the doc
- Doc = orddict:append(bson:utf8("b"), 2, orddict:append(bson:utf8("name"), bson:utf8("value"), orddict:new())),
- BinDoc = bson:serialize(Doc),
- <<28,0,0,0,16,98,0,2,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,0>> = BinDoc,
- % Build a simple string key-value proplist and serialize the doc
- DocProp = [{bson:utf8("name"), bson:utf8("value")}, {bson:utf8("b"), 2}],
- BinDocProp = bson:serialize(DocProp),
- <<28,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,16,98,0,2,0,0,0,0>> = BinDocProp,
- % Build a simple string key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("b"), 2, dict:append(bson:utf8("name"), bson:utf8("value"), dict:new())),
- BinDocDict = bson:serialize(DocDict),
- <<28,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,16,98,0,2,0,0,0,0>> = BinDocDict.
+ % Build a simple string key-value dict and serialize the doc
+ Doc = orddict:append(bson:utf8("b"), 2, orddict:append(bson:utf8("name"), bson:utf8("value"), orddict:new())),
+ BinDoc = bson:serialize(Doc),
+ <<28,0,0,0,16,98,0,2,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,0>> = BinDoc,
+ % Build a simple string key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("name"), bson:utf8("value")}, {bson:utf8("b"), 2}],
+ BinDocProp = bson:serialize(DocProp),
+ <<28,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,16,98,0,2,0,0,0,0>> = BinDocProp,
+ % Build a simple string key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("b"), 2, dict:append(bson:utf8("name"), bson:utf8("value"), dict:new())),
+ BinDocDict = bson:serialize(DocDict),
+ <<28,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,16,98,0,2,0,0,0,0>> = BinDocDict.
simple_single_level_float_doc_serialization_test() ->
- % Build a simple float key-value dict and serialize the doc
- Doc = orddict:append(bson:utf8("name"), 1.34, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>> = BinDoc,
- % Build a simple float key-value proplist and serialize the doc
- DocProp = [{bson:utf8("name"), 1.34}],
- BinDocProp = bson:serialize(DocProp),
- <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>> = BinDocProp,
- % Build a simple float key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("name"), 1.34, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>> = BinDocDict.
+ % Build a simple float key-value dict and serialize the doc
+ Doc = orddict:append(bson:utf8("name"), 1.34, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>> = BinDoc,
+ % Build a simple float key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("name"), 1.34}],
+ BinDocProp = bson:serialize(DocProp),
+ <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>> = BinDocProp,
+ % Build a simple float key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("name"), 1.34, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>> = BinDocDict.
simple_single_level_32_bit_integer_doc_serialization_test() ->
- % Build a simple integer key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("name"), 1000, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>> = BinDoc,
- % Build a simple integer key-value proplist and serialize the doc
- DocProp = [{bson:utf8("name"), 1000}],
- BinDocProp = bson:serialize(DocProp),
- <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>> = BinDocProp,
- % Build a simple integer key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("name"), 1000, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>> = BinDocDict.
+ % Build a simple integer key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("name"), 1000, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>> = BinDoc,
+ % Build a simple integer key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("name"), 1000}],
+ BinDocProp = bson:serialize(DocProp),
+ <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>> = BinDocProp,
+ % Build a simple integer key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("name"), 1000, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>> = BinDocDict.
simple_single_level_64_bit_integer_doc_serialization_test() ->
- % Build a simple integer key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("name"), 16#7000000000000000, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>> = BinDoc,
- % Build a simple integer key-value proplist and serialize the doc
- DocProp = [{bson:utf8("name"), 16#7000000000000000}],
- BinDocProp = bson:serialize(DocProp),
- <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>> = BinDocProp,
- % Build a simple integer key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("name"), 16#7000000000000000, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>> = BinDocDict.
-
+ % Build a simple integer key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("name"), 16#7000000000000000, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>> = BinDoc,
+ % Build a simple integer key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("name"), 16#7000000000000000}],
+ BinDocProp = bson:serialize(DocProp),
+ <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>> = BinDocProp,
+ % Build a simple integer key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("name"), 16#7000000000000000, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>> = BinDocDict.
+
simple_two_level_document_with_values_test() ->
- % Build a simple integer key-value doc and serialize the doc
- InnerDoc = orddict:append(bson:utf8("integer"), 16#7000000000000000, orddict:new()),
- Doc = orddict:append(bson:utf8("name"), InnerDoc, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>> = BinDoc,
- % Build a simple integer key-value proplist and serialize the doc
- DocProp = [{bson:utf8("name"), {bson:utf8("integer"), 16#7000000000000000}}],
- BinDocProp = bson:serialize(DocProp),
- <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>> = BinDocProp,
- % Build a simple integer key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("name"), dict:append(bson:utf8("integer"), 16#7000000000000000, dict:new()), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>> = BinDocDict.
+ % Build a simple integer key-value doc and serialize the doc
+ InnerDoc = orddict:append(bson:utf8("integer"), 16#7000000000000000, orddict:new()),
+ Doc = orddict:append(bson:utf8("name"), InnerDoc, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>> = BinDoc,
+ % Build a simple integer key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("name"), {bson:utf8("integer"), 16#7000000000000000}}],
+ BinDocProp = bson:serialize(DocProp),
+ <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>> = BinDocProp,
+ % Build a simple integer key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("name"), dict:append(bson:utf8("integer"), 16#7000000000000000, dict:new()), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>> = BinDocDict.
simple_document_with_array_of_integers_test() ->
- % Build a simple integer key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("integers"), [1,2,3,4], orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>> = BinDoc,
- % Build a simple integer key-value proplist and serialize the doc
- DocProp = [{bson:utf8("integers"), [1,2,3,4]}],
- BinDocProp = bson:serialize(DocProp),
- <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>> = BinDocProp,
- % Build a simple integer key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("integers"), [1,2,3,4], dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>> = BinDocDict.
-
+ % Build a simple integer key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("integers"), [1,2,3,4], orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>> = BinDoc,
+ % Build a simple integer key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("integers"), [1,2,3,4]}],
+ BinDocProp = bson:serialize(DocProp),
+ <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>> = BinDocProp,
+ % Build a simple integer key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("integers"), [1,2,3,4], dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>> = BinDocDict.
+
simple_document_with_regexp_test() ->
- % Build a simple regexp key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("regexp"), bson:regexp("test", "s"), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>> = BinDoc,
- % Build a simple regexp key-value proplist and serialize the doc
- DocProp = [{bson:utf8("regexp"), bson:regexp("test", "s")}],
- BinDocProp = bson:serialize(DocProp),
- <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>> = BinDocProp,
- % Build a simple regexp key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("regexp"), bson:regexp("test", "s"), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>> = BinDocDict.
+ % Build a simple regexp key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("regexp"), bson:regexp("test", "s"), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>> = BinDoc,
+ % Build a simple regexp key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("regexp"), bson:regexp("test", "s")}],
+ BinDocProp = bson:serialize(DocProp),
+ <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>> = BinDocProp,
+ % Build a simple regexp key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("regexp"), bson:regexp("test", "s"), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>> = BinDocDict.
simple_document_with_objectid_test() ->
- % Generate ObjectId from hex string
- ObjectId = bson:objectid("4f8d7e1f95f13b4370000000"),
- % Build a simple integer key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("objectid"), ObjectId, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>> = BinDoc,
- % Build a simple objectid key-value proplist and serialize the doc
- DocProp = [{bson:utf8("objectid"), ObjectId}],
- BinDocProp = bson:serialize(DocProp),
- <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>> = BinDocProp,
- % Build a simple objectid key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("objectid"), ObjectId, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>> = BinDocDict.
+ % Generate ObjectId from hex string
+ ObjectId = bson:objectid("4f8d7e1f95f13b4370000000"),
+ % Build a simple integer key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("objectid"), ObjectId, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>> = BinDoc,
+ % Build a simple objectid key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("objectid"), ObjectId}],
+ BinDocProp = bson:serialize(DocProp),
+ <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>> = BinDocProp,
+ % Build a simple objectid key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("objectid"), ObjectId, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>> = BinDocDict.
simple_document_with_boolean_test() ->
- % Build a simple integer key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("bool"), true, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<12,0,0,0,8,98,111,111,108,0,1,0>> = BinDoc,
- Doc2 = orddict:append(bson:utf8("bool"), false, orddict:new()),
- BinDoc2 = bson:serialize(Doc2),
- <<12,0,0,0,8,98,111,111,108,0,0,0>> = BinDoc2,
- % Build a simple boolean key-value proplist and serialize the doc
- DocProp = [{bson:utf8("bool"), true}],
- BinDocProp = bson:serialize(DocProp),
- <<12,0,0,0,8,98,111,111,108,0,1,0>> = BinDocProp,
- DocProp2 = [{bson:utf8("bool"), false}],
- BinDocProp2 = bson:serialize(DocProp2),
- <<12,0,0,0,8,98,111,111,108,0,0,0>> = BinDocProp2,
- % Build a simple boolean key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("bool"), true, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<12,0,0,0,8,98,111,111,108,0,1,0>> = BinDocDict,
- DocDict2 = dict:append(bson:utf8("bool"), false, dict:new()),
- BinDocDict2 = bson:serialize(DocDict2),
- <<12,0,0,0,8,98,111,111,108,0,0,0>> = BinDocDict2.
+ % Build a simple integer key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("bool"), true, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<12,0,0,0,8,98,111,111,108,0,1,0>> = BinDoc,
+ Doc2 = orddict:append(bson:utf8("bool"), false, orddict:new()),
+ BinDoc2 = bson:serialize(Doc2),
+ <<12,0,0,0,8,98,111,111,108,0,0,0>> = BinDoc2,
+ % Build a simple boolean key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("bool"), true}],
+ BinDocProp = bson:serialize(DocProp),
+ <<12,0,0,0,8,98,111,111,108,0,1,0>> = BinDocProp,
+ DocProp2 = [{bson:utf8("bool"), false}],
+ BinDocProp2 = bson:serialize(DocProp2),
+ <<12,0,0,0,8,98,111,111,108,0,0,0>> = BinDocProp2,
+ % Build a simple boolean key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("bool"), true, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<12,0,0,0,8,98,111,111,108,0,1,0>> = BinDocDict,
+ DocDict2 = dict:append(bson:utf8("bool"), false, dict:new()),
+ BinDocDict2 = bson:serialize(DocDict2),
+ <<12,0,0,0,8,98,111,111,108,0,0,0>> = BinDocDict2.
simple_document_with_null_or_undefined_test() ->
- % Build a simple null/undefined key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("bool"), undefined, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<11,0,0,0,10,98,111,111,108,0,0>> = BinDoc,
- Doc2 = orddict:append(bson:utf8("bool"), null, orddict:new()),
- BinDoc2 = bson:serialize(Doc2),
- <<11,0,0,0,10,98,111,111,108,0,0>> = BinDoc2,
- % Build a simple null/undefined key-value proplist and serialize the doc
- DocProp = [{bson:utf8("bool"), undefined}],
- BinDocProp = bson:serialize(DocProp),
- <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocProp,
- DocProp2 = [{bson:utf8("bool"), null}],
- BinDocProp2 = bson:serialize(DocProp2),
- <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocProp2,
- % Build a simple null/undefined key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("bool"), null, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocDict,
- DocDict2 = dict:append(bson:utf8("bool"), undefined, dict:new()),
- BinDocDict2 = bson:serialize(DocDict2),
- <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocDict2.
+ % Build a simple null/undefined key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("bool"), undefined, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<11,0,0,0,10,98,111,111,108,0,0>> = BinDoc,
+ Doc2 = orddict:append(bson:utf8("bool"), null, orddict:new()),
+ BinDoc2 = bson:serialize(Doc2),
+ <<11,0,0,0,10,98,111,111,108,0,0>> = BinDoc2,
+ % Build a simple null/undefined key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("bool"), undefined}],
+ BinDocProp = bson:serialize(DocProp),
+ <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocProp,
+ DocProp2 = [{bson:utf8("bool"), null}],
+ BinDocProp2 = bson:serialize(DocProp2),
+ <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocProp2,
+ % Build a simple null/undefined key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("bool"), null, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocDict,
+ DocDict2 = dict:append(bson:utf8("bool"), undefined, dict:new()),
+ BinDocDict2 = bson:serialize(DocDict2),
+ <<11,0,0,0,10,98,111,111,108,0,0>> = BinDocDict2.
simple_document_with_datetime_test() ->
- % Build a simple datetime key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("date"), bson:bson_time_to_timestamp(1334686404073), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>> = BinDoc,
- % Build a simple datetime key-value proplist and serialize the doc
- DocProp = [{bson:utf8("date"), bson:bson_time_to_timestamp(1334686404073)}],
- BinDocProp = bson:serialize(DocProp),
- <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>> = BinDocProp,
- % Build a simple datetime key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("date"), bson:bson_time_to_timestamp(1334686404073), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>> = BinDocDict.
+ % Build a simple datetime key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("date"), bson:bson_time_to_timestamp(1334686404073), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>> = BinDoc,
+ % Build a simple datetime key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("date"), bson:bson_time_to_timestamp(1334686404073)}],
+ BinDocProp = bson:serialize(DocProp),
+ <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>> = BinDocProp,
+ % Build a simple datetime key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("date"), bson:bson_time_to_timestamp(1334686404073), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>> = BinDocDict.
simple_document_with_symbol_test() ->
- % Build a simple symbol key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("symbol"), atom, orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>> = BinDoc,
- % Build a simple symbol key-value proplist and serialize the doc
- DocProp = [{bson:utf8("symbol"), atom}],
- BinDocProp = bson:serialize(DocProp),
- <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>> = BinDocProp,
- % Build a simple symbol key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("symbol"), atom, dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>> = BinDocDict.
+ % Build a simple symbol key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("symbol"), atom, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>> = BinDoc,
+ % Build a simple symbol key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("symbol"), atom}],
+ BinDocProp = bson:serialize(DocProp),
+ <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>> = BinDocProp,
+ % Build a simple symbol key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("symbol"), atom, dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>> = BinDocDict.
simple_document_with_minkey_test() ->
- % Build a simple symbol key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("key"), bson:minkey(), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<10,0,0,0,255,107,101,121,0,0>> = BinDoc,
- % Build a simple symbol key-value proplist and serialize the doc
- DocProp = [{bson:utf8("key"), bson:minkey()}],
- BinDocProp = bson:serialize(DocProp),
- <<10,0,0,0,255,107,101,121,0,0>> = BinDocProp,
- % Build a simple symbol key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("key"), bson:minkey(), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<10,0,0,0,255,107,101,121,0,0>> = BinDocDict.
+ % Build a simple symbol key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("key"), bson:minkey(), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<10,0,0,0,255,107,101,121,0,0>> = BinDoc,
+ % Build a simple symbol key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("key"), bson:minkey()}],
+ BinDocProp = bson:serialize(DocProp),
+ <<10,0,0,0,255,107,101,121,0,0>> = BinDocProp,
+ % Build a simple symbol key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("key"), bson:minkey(), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<10,0,0,0,255,107,101,121,0,0>> = BinDocDict.
simple_document_with_maxkey_test() ->
- % Build a simple symbol key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("key"), bson:maxkey(), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<10,0,0,0,127,107,101,121,0,0>> = BinDoc,
- % Build a simple symbol key-value proplist and serialize the doc
- DocProp = [{bson:utf8("key"), bson:maxkey()}],
- BinDocProp = bson:serialize(DocProp),
- <<10,0,0,0,127,107,101,121,0,0>> = BinDocProp,
- % Build a simple symbol key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("key"), bson:maxkey(), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<10,0,0,0,127,107,101,121,0,0>> = BinDocDict.
+ % Build a simple symbol key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("key"), bson:maxkey(), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<10,0,0,0,127,107,101,121,0,0>> = BinDoc,
+ % Build a simple symbol key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("key"), bson:maxkey()}],
+ BinDocProp = bson:serialize(DocProp),
+ <<10,0,0,0,127,107,101,121,0,0>> = BinDocProp,
+ % Build a simple symbol key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("key"), bson:maxkey(), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<10,0,0,0,127,107,101,121,0,0>> = BinDocDict.
simple_document_with_javascript_test() ->
- % Build a simple symbol key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}")), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>> = BinDoc,
- % Build a simple symbol key-value proplist and serialize the doc
- DocProp = [{bson:utf8("code"), bson:javascript(bson:utf8("function(){}"))}],
- BinDocProp = bson:serialize(DocProp),
- <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>> = BinDocProp,
- % Build a simple symbol key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}")), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>> = BinDocDict.
+ % Build a simple symbol key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}")), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>> = BinDoc,
+ % Build a simple symbol key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("code"), bson:javascript(bson:utf8("function(){}"))}],
+ BinDocProp = bson:serialize(DocProp),
+ <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>> = BinDocProp,
+ % Build a simple symbol key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}")), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>> = BinDocDict.
simple_document_with_javascript_code_test() ->
- % Build a simple symbol key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}"), orddict:append(bson:utf8("v"), 1, orddict:new())), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>> = BinDoc,
- % Build a simple symbol key-value proplist and serialize the doc
- DocProp = [{bson:utf8("code"), bson:javascript(bson:utf8("function(){}"), [{bson:utf8("v"), 1}])}],
- BinDocProp = bson:serialize(DocProp),
- <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>> = BinDocProp,
- % Build a simple symbol key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}"), dict:append(bson:utf8("v"), 1, dict:new())), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>> = BinDocDict.
-
+ % Build a simple symbol key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}"), orddict:append(bson:utf8("v"), 1, orddict:new())), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>> = BinDoc,
+ % Build a simple symbol key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("code"), bson:javascript(bson:utf8("function(){}"), [{bson:utf8("v"), 1}])}],
+ BinDocProp = bson:serialize(DocProp),
+ <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>> = BinDocProp,
+ % Build a simple symbol key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("code"), bson:javascript(bson:utf8("function(){}"), dict:append(bson:utf8("v"), 1, dict:new())), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>> = BinDocDict.
+
simple_document_with_binary_test() ->
- % Build a simple symbol key-value doc and serialize the doc
- Doc = orddict:append(bson:utf8("bin"), bson:bin(list_to_binary("hello")), orddict:new()),
- BinDoc = bson:serialize(Doc),
- <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>> = BinDoc,
- Doc2 = orddict:append(bson:utf8("bin"), bson:bin(1, list_to_binary("hello")), orddict:new()),
- BinDoc2 = bson:serialize(Doc2),
- <<20,0,0,0,5,98,105,110,0,5,0,0,0,1,104,101,108,108,111,0>> = BinDoc2,
- % Build a simple symbol key-value proplist and serialize the doc
- DocProp = [{bson:utf8("bin"), bson:bin(list_to_binary("hello"))}],
- BinDocProp = bson:serialize(DocProp),
- <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>> = BinDocProp,
- % Build a simple symbol key-value dict and serialize the doc
- DocDict = dict:append(bson:utf8("bin"), bson:bin(list_to_binary("hello")), dict:new()),
- BinDocDict = bson:serialize(DocDict),
- <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>> = BinDocDict.
+ % Build a simple symbol key-value doc and serialize the doc
+ Doc = orddict:append(bson:utf8("bin"), bson:bin(list_to_binary("hello")), orddict:new()),
+ BinDoc = bson:serialize(Doc),
+ <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>> = BinDoc,
+ Doc2 = orddict:append(bson:utf8("bin"), bson:bin(1, list_to_binary("hello")), orddict:new()),
+ BinDoc2 = bson:serialize(Doc2),
+ <<20,0,0,0,5,98,105,110,0,5,0,0,0,1,104,101,108,108,111,0>> = BinDoc2,
+ % Build a simple symbol key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("bin"), bson:bin(list_to_binary("hello"))}],
+ BinDocProp = bson:serialize(DocProp),
+ <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>> = BinDocProp,
+ % Build a simple symbol key-value dict and serialize the doc
+ DocDict = dict:append(bson:utf8("bin"), bson:bin(list_to_binary("hello")), dict:new()),
+ BinDocDict = bson:serialize(DocDict),
+ <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>> = BinDocDict.
%%
-%% Deserialization tests
+%% Deserialization tests
%%
deserialize_simple_string_key_value_test() ->
- Bin = <<21,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,0>>,
- % Unpack the binary as propertylist
- [{Key, Value}] = bson:deserialize(Bin, pl),
- % Verify the correctness of the values
- "name" = binary_to_list(Key),
- "value" = binary_to_list(Value),
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- "value" = binary_to_list(dict:fetch(bson:utf8("name"), Dict)),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- "value" = binary_to_list(orddict:fetch(bson:utf8("name"), OrdDict)).
+ Bin = <<21,0,0,0,2,110,97,109,101,0,6,0,0,0,118,97,108,117,101,0,0>>,
+ % Unpack the binary as propertylist
+ [{Key, Value}] = bson:deserialize(Bin, pl),
+ % Verify the correctness of the values
+ "name" = binary_to_list(Key),
+ "value" = binary_to_list(Value),
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ "value" = binary_to_list(dict:fetch(bson:utf8("name"), Dict)),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ "value" = binary_to_list(orddict:fetch(bson:utf8("name"), OrdDict)).
deserialize_simple_two_attribute_object_test() ->
- Bin = <<19,0,0,0,16,97,0,1,0,0,0,16,98,0,2,0,0,0,0>>,
- [{Key1, Value1}, {Key2, Value2}] = bson:deserialize(Bin, pl),
- % Verify the correctness of the values
- "a" = binary_to_list(Key1),
- 1 = Value1,
- "b" = binary_to_list(Key2),
- 2 = Value2,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- 1 = dict:fetch(bson:utf8("a"), Dict),
- 2 = dict:fetch(bson:utf8("b"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- 1 = orddict:fetch(bson:utf8("a"), OrdDict),
- 2 = orddict:fetch(bson:utf8("b"), OrdDict).
+ Bin = <<19,0,0,0,16,97,0,1,0,0,0,16,98,0,2,0,0,0,0>>,
+ [{Key1, Value1}, {Key2, Value2}] = bson:deserialize(Bin, pl),
+ % Verify the correctness of the values
+ "a" = binary_to_list(Key1),
+ 1 = Value1,
+ "b" = binary_to_list(Key2),
+ 2 = Value2,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ 1 = dict:fetch(bson:utf8("a"), Dict),
+ 2 = dict:fetch(bson:utf8("b"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ 1 = orddict:fetch(bson:utf8("a"), OrdDict),
+ 2 = orddict:fetch(bson:utf8("b"), OrdDict).
deserialize_simple_float_key_value_test() ->
- Bin = <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>>,
- % Unpack the binary
- [{Key, Value}] = bson:deserialize(Bin, pl),
- % Verify the correctness of the values
- "name" = binary_to_list(Key),
- 1.34 = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- 1.34 = dict:fetch(bson:utf8("name"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- 1.34 = orddict:fetch(bson:utf8("name"), OrdDict).
+ Bin = <<19,0,0,0,1,110,97,109,101,0,113,61,10,215,163,112,245,63,0>>,
+ % Unpack the binary
+ [{Key, Value}] = bson:deserialize(Bin, pl),
+ % Verify the correctness of the values
+ "name" = binary_to_list(Key),
+ 1.34 = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ 1.34 = dict:fetch(bson:utf8("name"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ 1.34 = orddict:fetch(bson:utf8("name"), OrdDict).
deserialize_simple_32_bit_integer_key_value_test() ->
- Bin = <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>>,
- % Unpack the binary
- [{Key, Value}] = bson:deserialize(Bin, pl),
- % Verify the correctness of the values
- "name" = binary_to_list(Key),
- 1000 = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- 1000 = dict:fetch(bson:utf8("name"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- 1000 = orddict:fetch(bson:utf8("name"), OrdDict).
+ Bin = <<15,0,0,0,16,110,97,109,101,0,232,3,0,0,0>>,
+ % Unpack the binary
+ [{Key, Value}] = bson:deserialize(Bin, pl),
+ % Verify the correctness of the values
+ "name" = binary_to_list(Key),
+ 1000 = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ 1000 = dict:fetch(bson:utf8("name"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ 1000 = orddict:fetch(bson:utf8("name"), OrdDict).
deserialize_simple_64_bit_integer_key_value_test() ->
- Bin = <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>>,
- % Unpack the binary
- [{Key, Value}] = bson:deserialize(Bin, pl),
- % Verify the correctness of the values
- "name" = binary_to_list(Key),
- 16#7000000000000000 = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- 16#7000000000000000 = dict:fetch(bson:utf8("name"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- 16#7000000000000000 = orddict:fetch(bson:utf8("name"), OrdDict).
+ Bin = <<19,0,0,0,18,110,97,109,101,0,0,0,0,0,0,0,0,112,0>>,
+ % Unpack the binary
+ [{Key, Value}] = bson:deserialize(Bin, pl),
+ % Verify the correctness of the values
+ "name" = binary_to_list(Key),
+ 16#7000000000000000 = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ 16#7000000000000000 = dict:fetch(bson:utf8("name"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ 16#7000000000000000 = orddict:fetch(bson:utf8("name"), OrdDict).
deserialize_simple_two_layer_document_test() ->
- Bin = <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>>,
- % Unpack the binary
- [{Key, [{Key2, Integer}]}] = bson:deserialize(Bin, pl),
- % Verify the correctness of the values
- "name" = binary_to_list(Key),
- "integer" = binary_to_list(Key2),
- 16#7000000000000000 = Integer,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- Dict2 = dict:fetch(bson:utf8("name"), Dict),
- 16#7000000000000000 = dict:fetch(bson:utf8("integer"), Dict2),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- OrdDict2 = orddict:fetch(bson:utf8("name"), OrdDict),
- 16#7000000000000000 = orddict:fetch(bson:utf8("integer"), OrdDict2).
+ Bin = <<33,0,0,0,3,110,97,109,101,0,22,0,0,0,18,105,110,116,101,103,101,114,0,0,0,0,0,0,0,0,112,0,0>>,
+ % Unpack the binary
+ [{Key, [{Key2, Integer}]}] = bson:deserialize(Bin, pl),
+ % Verify the correctness of the values
+ "name" = binary_to_list(Key),
+ "integer" = binary_to_list(Key2),
+ 16#7000000000000000 = Integer,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ Dict2 = dict:fetch(bson:utf8("name"), Dict),
+ 16#7000000000000000 = dict:fetch(bson:utf8("integer"), Dict2),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ OrdDict2 = orddict:fetch(bson:utf8("name"), OrdDict),
+ 16#7000000000000000 = orddict:fetch(bson:utf8("integer"), OrdDict2).
deserialize_simple_array_of_integers_document_test() ->
- Bin = <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "integers" = binary_to_list(Key),
- [1,2,3,4] = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- [1,2,3,4] = dict:fetch(bson:utf8("integers"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- [1,2,3,4] = orddict:fetch(bson:utf8("integers"), OrdDict).
-
+ Bin = <<48,0,0,0,4,105,110,116,101,103,101,114,115,0,33,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,16,51,0,4,0,0,0,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "integers" = binary_to_list(Key),
+ [1,2,3,4] = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ [1,2,3,4] = dict:fetch(bson:utf8("integers"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ [1,2,3,4] = orddict:fetch(bson:utf8("integers"), OrdDict).
+
deserialize_simple_regexp_document_test() ->
- Bin = <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "regexp" = binary_to_list(Key),
- regexp = element(1, Value),
- "test" = binary_to_list(element(2, Value)),
- "s" = binary_to_list(element(3, Value)),
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- Regexp = dict:fetch(bson:utf8("regexp"), Dict),
- regexp = element(1, Regexp),
- "test" = binary_to_list(element(2, Regexp)),
- "s" = binary_to_list(element(3, Regexp)),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- Regexp2 = orddict:fetch(bson:utf8("regexp"), OrdDict),
- regexp = element(1, Regexp2),
- "test" = binary_to_list(element(2, Regexp2)),
- "s" = binary_to_list(element(3, Regexp2)).
+ Bin = <<20,0,0,0,11,114,101,103,101,120,112,0,116,101,115,116,0,115,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "regexp" = binary_to_list(Key),
+ regexp = element(1, Value),
+ "test" = binary_to_list(element(2, Value)),
+ "s" = binary_to_list(element(3, Value)),
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ Regexp = dict:fetch(bson:utf8("regexp"), Dict),
+ regexp = element(1, Regexp),
+ "test" = binary_to_list(element(2, Regexp)),
+ "s" = binary_to_list(element(3, Regexp)),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ Regexp2 = orddict:fetch(bson:utf8("regexp"), OrdDict),
+ regexp = element(1, Regexp2),
+ "test" = binary_to_list(element(2, Regexp2)),
+ "s" = binary_to_list(element(3, Regexp2)).
deserialize_simple_objectid_document_test() ->
- Bin = <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "objectid" = binary_to_list(Key),
- objectid = element(1, Value),
- <<79,141,126,31,149,241,59,67,112,0,0,0>> = element(2, Value),
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- ObjectId = dict:fetch(bson:utf8("objectid"), Dict),
- objectid = element(1, ObjectId),
- <<79,141,126,31,149,241,59,67,112,0,0,0>> = element(2, ObjectId),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- ObjectId2 = orddict:fetch(bson:utf8("objectid"), OrdDict),
- objectid = element(1, ObjectId2),
- <<79,141,126,31,149,241,59,67,112,0,0,0>> = element(2, ObjectId2).
+ Bin = <<27,0,0,0,7,111,98,106,101,99,116,105,100,0,79,141,126,31,149,241,59,67,112,0,0,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "objectid" = binary_to_list(Key),
+ objectid = element(1, Value),
+ <<79,141,126,31,149,241,59,67,112,0,0,0>> = element(2, Value),
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ ObjectId = dict:fetch(bson:utf8("objectid"), Dict),
+ objectid = element(1, ObjectId),
+ <<79,141,126,31,149,241,59,67,112,0,0,0>> = element(2, ObjectId),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ ObjectId2 = orddict:fetch(bson:utf8("objectid"), OrdDict),
+ objectid = element(1, ObjectId2),
+ <<79,141,126,31,149,241,59,67,112,0,0,0>> = element(2, ObjectId2).
deserialize_simple_boolean_document_test() ->
- Bin = <<12,0,0,0,8,98,111,111,108,0,1,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "bool" = binary_to_list(Key),
- true = Value,
- % Verify a false value
- Bin2 = <<12,0,0,0,8,98,111,111,108,0,0,0>>,
- Doc2 = bson:deserialize(Bin2, pl),
- [{Key2, Value2}] = Doc2,
- "bool" = binary_to_list(Key2),
- false = Value2,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- true = dict:fetch(bson:utf8("bool"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- true = orddict:fetch(bson:utf8("bool"), OrdDict).
+ Bin = <<12,0,0,0,8,98,111,111,108,0,1,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "bool" = binary_to_list(Key),
+ true = Value,
+ % Verify a false value
+ Bin2 = <<12,0,0,0,8,98,111,111,108,0,0,0>>,
+ Doc2 = bson:deserialize(Bin2, pl),
+ [{Key2, Value2}] = Doc2,
+ "bool" = binary_to_list(Key2),
+ false = Value2,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ true = dict:fetch(bson:utf8("bool"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ true = orddict:fetch(bson:utf8("bool"), OrdDict).
deserialize_simple_null_document_test() ->
- Bin = <<11,0,0,0,10,98,111,111,108,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "bool" = binary_to_list(Key),
- null = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- null = dict:fetch(bson:utf8("bool"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- null = orddict:fetch(bson:utf8("bool"), OrdDict).
+ Bin = <<11,0,0,0,10,98,111,111,108,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "bool" = binary_to_list(Key),
+ null = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ null = dict:fetch(bson:utf8("bool"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ null = orddict:fetch(bson:utf8("bool"), OrdDict).
deserialize_simple_datetime_document_test() ->
- Bin = <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "date" = binary_to_list(Key),
- {1,334686,404073} = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- {1,334686,404073} = dict:fetch(bson:utf8("date"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- {1,334686,404073} = orddict:fetch(bson:utf8("date"), OrdDict).
+ Bin = <<19,0,0,0,9,100,97,116,101,0,233,77,130,193,54,1,0,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "date" = binary_to_list(Key),
+ {1,334686,404073} = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ {1,334686,404073} = dict:fetch(bson:utf8("date"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ {1,334686,404073} = orddict:fetch(bson:utf8("date"), OrdDict).
deserialize_simple_symbol_document_test() ->
- Bin = <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "symbol" = binary_to_list(Key),
- atom = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- atom = dict:fetch(bson:utf8("symbol"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- atom = orddict:fetch(bson:utf8("symbol"), OrdDict).
-
+ Bin = <<22,0,0,0,14,115,121,109,98,111,108,0,5,0,0,0,97,116,111,109,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "symbol" = binary_to_list(Key),
+ atom = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ atom = dict:fetch(bson:utf8("symbol"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ atom = orddict:fetch(bson:utf8("symbol"), OrdDict).
+
deserialize_simple_minkey_document_test() ->
- Bin = <<10,0,0,0,255,107,101,121,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "key" = binary_to_list(Key),
- {minkey} = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- {minkey} = dict:fetch(bson:utf8("key"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- {minkey} = orddict:fetch(bson:utf8("key"), OrdDict).
+ Bin = <<10,0,0,0,255,107,101,121,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "key" = binary_to_list(Key),
+ {minkey} = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ {minkey} = dict:fetch(bson:utf8("key"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ {minkey} = orddict:fetch(bson:utf8("key"), OrdDict).
deserialize_simple_maxkey_document_test() ->
- Bin = <<10,0,0,0,127,107,101,121,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "key" = binary_to_list(Key),
- {maxkey} = Value,
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- {maxkey} = dict:fetch(bson:utf8("key"), Dict),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- {maxkey} = orddict:fetch(bson:utf8("key"), OrdDict).
+ Bin = <<10,0,0,0,127,107,101,121,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "key" = binary_to_list(Key),
+ {maxkey} = Value,
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ {maxkey} = dict:fetch(bson:utf8("key"), Dict),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ {maxkey} = orddict:fetch(bson:utf8("key"), OrdDict).
deserialize_simple_javascript_document_test() ->
- Bin = <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "code" = binary_to_list(Key),
- js = element(1, Value),
- <<"function(){}">> = element(2, Value),
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- Code = dict:fetch(bson:utf8("code"), Dict),
- js = element(1, Code),
- <<"function(){}">> = element(2, Code),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- Code2 = orddict:fetch(bson:utf8("code"), OrdDict),
- js = element(1, Code2),
- <<"function(){}">> = element(2, Code2).
+ Bin = <<28,0,0,0,13,99,111,100,101,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "code" = binary_to_list(Key),
+ js = element(1, Value),
+ <<"function(){}">> = element(2, Value),
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ Code = dict:fetch(bson:utf8("code"), Dict),
+ js = element(1, Code),
+ <<"function(){}">> = element(2, Code),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ Code2 = orddict:fetch(bson:utf8("code"), OrdDict),
+ js = element(1, Code2),
+ <<"function(){}">> = element(2, Code2).
deserialize_simple_javascript_scope_document_test() ->
- Bin = <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "code" = binary_to_list(Key),
- js = element(1, Value),
- <<"function(){}">> = element(2, Value),
- [{<<"v">>,1}] = element(3, Value),
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- Code = dict:fetch(bson:utf8("code"), Dict),
- js = element(1, Code),
- <<"function(){}">> = element(2, Code),
- 1 = dict:fetch(bson:utf8("v"), element(3, Code)),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- Code2 = orddict:fetch(bson:utf8("code"), OrdDict),
- js = element(1, Code2),
- <<"function(){}">> = element(2, Code2),
- 1 = orddict:fetch(bson:utf8("v"), element(3, Code2)).
+ Bin = <<44,0,0,0,15,99,111,100,101,0,33,0,0,0,13,0,0,0,102,117,110,99,116,105,111,110,40,41,123,125,0,12,0,0,0,16,118,0,1,0,0,0,0,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "code" = binary_to_list(Key),
+ js = element(1, Value),
+ <<"function(){}">> = element(2, Value),
+ [{<<"v">>,1}] = element(3, Value),
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ Code = dict:fetch(bson:utf8("code"), Dict),
+ js = element(1, Code),
+ <<"function(){}">> = element(2, Code),
+ 1 = dict:fetch(bson:utf8("v"), element(3, Code)),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ Code2 = orddict:fetch(bson:utf8("code"), OrdDict),
+ js = element(1, Code2),
+ <<"function(){}">> = element(2, Code2),
+ 1 = orddict:fetch(bson:utf8("v"), element(3, Code2)).
deserialize_simple_binary_document_test() ->
- Bin = <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>>,
- % Upack the binary
- Doc = bson:deserialize(Bin, pl),
- [{Key, Value}] = Doc,
- % Verify the correctness of the values
- "bin" = binary_to_list(Key),
- bin = element(1, Value),
- 0 = element(2, Value),
- <<"hello">> = element(3, Value),
- % Unpack as binary dictionary
- Dict = bson:deserialize(Bin, dict),
- Binary = dict:fetch(bson:utf8("bin"), Dict),
- bin = element(1, Binary),
- 0 = element(2, Binary),
- <<"hello">> = element(3, Binary),
- % Unpack as ordered dictionary
- OrdDict = bson:deserialize(Bin, orddict),
- Binary2 = orddict:fetch(bson:utf8("bin"), OrdDict),
- bin = element(1, Binary2),
- 0 = element(2, Binary2),
- <<"hello">> = element(3, Binary2).
+ Bin = <<20,0,0,0,5,98,105,110,0,5,0,0,0,0,104,101,108,108,111,0>>,
+ % Upack the binary
+ Doc = bson:deserialize(Bin, pl),
+ [{Key, Value}] = Doc,
+ % Verify the correctness of the values
+ "bin" = binary_to_list(Key),
+ bin = element(1, Value),
+ 0 = element(2, Value),
+ <<"hello">> = element(3, Value),
+ % Unpack as binary dictionary
+ Dict = bson:deserialize(Bin, dict),
+ Binary = dict:fetch(bson:utf8("bin"), Dict),
+ bin = element(1, Binary),
+ 0 = element(2, Binary),
+ <<"hello">> = element(3, Binary),
+ % Unpack as ordered dictionary
+ OrdDict = bson:deserialize(Bin, orddict),
+ Binary2 = orddict:fetch(bson:utf8("bin"), OrdDict),
+ bin = element(1, Binary2),
+ 0 = element(2, Binary2),
+ <<"hello">> = element(3, Binary2).
%%
-%% Scan for key and deserialize tests
+%% Scan for key and deserialize tests
%%
deserialize_first_level_key_test() ->
- % Build a simple integer key-value doc and serialize the doc
- InnerDoc = orddict:append(bson:utf8("integer"), 16#7000000000000000, orddict:new()),
- Doc = orddict:append(bson:utf8("name"), InnerDoc, orddict:new()),
- BinDoc = bson:serialize(Doc),
-
- % Fetch the doc based on the top level name tag
- SelectedDoc = bson:deserialize(BinDoc, [<<"name">>], pl),
- [{<<"name">>,[{<<"integer">>,8070450532247928832}]}] = SelectedDoc,
-
- % Fetch the doc based on the name.integer level tag
- SelectedDoc2 = bson:deserialize(BinDoc, [<<"name">>, <<"integer">>], pl),
- [{<<"integer">>,8070450532247928832}] = SelectedDoc2,
-
- % No match found
- {err, nomatch} = bson:deserialize(BinDoc, [<<"name2">>], pl).
+ % Build a simple integer key-value doc and serialize the doc
+ InnerDoc = orddict:append(bson:utf8("integer"), 16#7000000000000000, orddict:new()),
+ Doc = orddict:append(bson:utf8("name"), InnerDoc, orddict:new()),
+ BinDoc = bson:serialize(Doc),
+
+ % Fetch the doc based on the top level name tag
+ SelectedDoc = bson:deserialize(BinDoc, [<<"name">>], pl),
+ [{<<"name">>,[{<<"integer">>,8070450532247928832}]}] = SelectedDoc,
+
+ % Fetch the doc based on the name.integer level tag
+ SelectedDoc2 = bson:deserialize(BinDoc, [<<"name">>, <<"integer">>], pl),
+ [{<<"integer">>,8070450532247928832}] = SelectedDoc2,
+
+ % No match found
+ {err, nomatch} = bson:deserialize(BinDoc, [<<"name2">>], pl).
+
+%%
+%% Throw due to illegal keys
+%%
+
+throws_error_during_serialization_due_to_illegal_key_test() ->
+ % Build a simple string key-value proplist and serialize the doc
+ DocProp = [{bson:utf8("$name"), bson:utf8("value")}, {bson:utf8("b"), 2}],
+ ?assertThrow({key_check_error, <<"$name">>}, bson:serialize(DocProp, true)),
+ DocProp2 = [{bson:utf8("name.test"), bson:utf8("value")}, {bson:utf8("b"), 2}],
+ ?assertThrow({key_check_error, <<"name.test">>}, bson:serialize(DocProp2, true)).
% %
% % Simple serialize benchmark

0 comments on commit 200eef7

Please sign in to comment.