Skip to content

Commit

Permalink
Merge pull request #3 from AF83/bson_null
Browse files Browse the repository at this point in the history
Bson value null
  • Loading branch information
boorad committed Oct 10, 2011
2 parents 4e1d0c5 + 9fac348 commit 3095578
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 25 deletions.
6 changes: 5 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ __nested queries__

## Tests

Ensure you have [etap](https://github.com/ngerakines/etap) and [couchdb](http://couchdb.apache.org/).
Ensure you have [etap](https://github.com/ngerakines/etap).

git clone https://github.com/ngerakines/etap.git
cd etap && make && cd ..
export ERL_LIBS="etap"

make test
15 changes: 12 additions & 3 deletions src/emongo_bson.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ encode_key_value(Key, {binary, SubType, Val}) when is_integer(SubType), is_binar
encode_key_value(Key, {oid, HexString}) when is_list(HexString) ->
encode_key_value(Key, {oid, emongo:hex2dec(HexString)});

%% UNDEFINED
encode_key_value(Key, undefined) ->
Key1 = encode_key(Key),
<<6, Key1/binary, 0>>;

encode_key_value(Key, {oid, OID}) when is_binary(OID) ->
Key1 = encode_key(Key),
<<7, Key1/binary, 0, OID/binary>>;
Expand Down Expand Up @@ -114,8 +119,8 @@ encode_key_value(Key, {datetime, Val}) ->
encode_key_value(Key, {{Year, Month, Day}, {Hour, Min, Secs}}) when is_integer(Year), is_integer(Month), is_integer(Day), is_integer(Hour), is_integer(Min), is_integer(Secs) ->
encode_key_value(Key, {datetime, {{Year, Month, Day}, {Hour, Min, Secs}}});

%% VOID
encode_key_value(Key, undefined) ->
%% NULL
encode_key_value(Key, null) ->
Key1 = encode_key(Key),
<<10, Key1/binary, 0>>;

Expand Down Expand Up @@ -206,6 +211,10 @@ decode_value(5, <<_Size:32/little-signed, 2:8/little, BinSize:32/little-signed,
decode_value(5, <<Size:32/little-signed, SubType:8/little, BinData:Size/binary-little-unit:8, Tail/binary>>) ->
{{binary, SubType, BinData}, Tail};

%% VOID
decode_value(6, Tail) ->
{undefined, Tail};

%% OID
decode_value(7, <<OID:12/binary, Tail/binary>>) ->
{{oid, OID}, Tail};
Expand All @@ -227,7 +236,7 @@ decode_value(9, <<MSecs:64/little-signed, Tail/binary>>) ->

%% VOID
decode_value(10, Tail) ->
{undefined, Tail};
{null, Tail};

%% INT
decode_value(16, <<Int:32/little-signed, Tail/binary>>) ->
Expand Down
2 changes: 1 addition & 1 deletion src/emongo_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ stop_pool(PoolId) ->

pools() ->
[{Id, Pid, Module} || {Id, Pid, _, [Module]}
<- supervisor:which_children(?MODULE)].
<- supervisor:which_children(?MODULE), Module == emongo_pool].

worker_pid(PoolId, Pools) ->
worker_pid(PoolId, Pools, 1).
Expand Down
2 changes: 1 addition & 1 deletion t/001-load.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
main(_) ->
etap:plan(unknown),
error_logger:tty(false),
etap_application:start_ok(emongo, "application 'emongo' started ok"),
etap:ok(application:start(emongo) == ok, "application 'emongo' started ok"),

etap:is(length(emongo:pools()), 1, "one pool exists in state"),

Expand Down
15 changes: 13 additions & 2 deletions t/002-bson.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
main(_) ->
etap:plan(unknown),
error_logger:tty(false),
etap_application:start_ok(emongo, "application 'emongo' started ok"),
etap:ok(application:start(emongo) == ok, "application 'emongo' started ok"),

%% 1) data_number
(fun() ->
Expand Down Expand Up @@ -63,6 +63,17 @@ main(_) ->
ok
end)(),

%% 6) data_undefined
(fun() ->
Val = undefined,
BinVal = <<>>,
Size = size(BinVal) + 8,
Encoded = emongo_bson:encode([{<<"a">>, Val}]),
etap:is(Encoded, <<Size:32/little-unsigned, 6, 97, 0, BinVal/binary, 0>>, "data_undefined encodes ok"),
etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_null decodes ok"),
ok
end)(),

%% 7) data_oid
(fun() ->
Val1 = {oid, <<255,255,255,255,255,255,255,255,255,255,255,255>>},
Expand Down Expand Up @@ -108,7 +119,7 @@ main(_) ->

%% 10) data_null
(fun() ->
Val = undefined,
Val = null,
BinVal = <<>>,
Size = size(BinVal) + 8,
Encoded = emongo_bson:encode([{<<"a">>, Val}]),
Expand Down
2 changes: 1 addition & 1 deletion t/003-find.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
main(_) ->
etap:plan(unknown),
error_logger:tty(false),
etap_application:start_ok(emongo, "application 'emongo' started ok"),
etap:ok(application:start(emongo) == ok, "application 'emongo' started ok"),

emongo:delete(test1, "sushi"),
etap:is(emongo:find_all(test1, "sushi"), [], "sushi collection is empty"),
Expand Down
2 changes: 1 addition & 1 deletion t/004-cond-exprs.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
main(_) ->
etap:plan(unknown),
error_logger:tty(false),
etap_application:start_ok(emongo, "application 'emongo' started ok"),
etap:ok(application:start(emongo) == ok, "application 'emongo' started ok"),

emongo:delete(test1, "sushi"),
etap:is(emongo:find_all(test1, "sushi"), [], "sushi collection is empty"),
Expand Down
8 changes: 4 additions & 4 deletions t/005-drop.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
main(_) ->
etap:plan(unknown),
error_logger:tty(false),
etap_application:start_ok(emongo, "application 'emongo' started ok"),
etap:ok(application:start(emongo) == ok, "application 'emongo' started ok"),

[emongo:insert(test1, "sushi", [{<<"rolls">>, I}]) || I <- lists:seq(1, 50)],
[emongo:insert(test1, "sushi2", [{<<"rolls">>, I}]) || I <- lists:seq(1, 50)],

ok = emongo:drop_database(test1),

emongo:insert(test1, "sushi", [{<<"rolls">>, 1}]),
[_] = emongo:find_all(test1, "sushi"),
[] = emongo:find_all(test1, "sushi2"),
etap:is(length(emongo:find_all(test1, "sushi")), 1, "There is 1 doc"),
etap:is(length(emongo:find_all(test1, "sushi2")), 0, "there is 0 doc"),

etap:end_tests().
etap:end_tests().
22 changes: 11 additions & 11 deletions t/006-multiup.t
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/usr/local/bin/escript
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ebin -sasl errlog_type error -boot start_sasl -noshell -config priv/example

main(_) ->
etap:plan(unknown),
error_logger:tty(false),
etap_application:start_ok(emongo, "application 'emongo' started ok"),
etap:ok(application:start(emongo) == ok, "application 'emongo' started ok"),

emongo:delete(test1, "sushi"),
etap:is(emongo:find_all(test1, "sushi"), [], "sushi collection is empty"),

[emongo:insert(test1, "sushi", [{<<"rolls">>, I}]) || I <- lists:seq(1, 50)],

(fun() ->
emongo:update(test1, "sushi", [{<<"rolls">>, [{gt, 45}]}], [{<<"$set">>, [{<<"rolls">>, 100}]}], false, true),
Docs = emongo:find_all(test1, "sushi", [{<<"rolls">>, 100}], []),
etap:is(length(Docs), 5, "correct number of results after multiupdate"),
etap:is([I || [_, {_, I}] <- Docs], [100,100,100,100,100], "correct results after multiupdate"),
ok
emongo:update(test1, "sushi", [{<<"rolls">>, [{gt, 45}]}], [{<<"$set">>, [{<<"rolls">>, 100}]}], false, true),
Docs = emongo:find_all(test1, "sushi", [{<<"rolls">>, 100}], []),
etap:is(length(Docs), 5, "correct number of results after multiupdate"),
etap:is([I || [_, {_, I}] <- Docs], [100,100,100,100,100], "correct results after multiupdate"),
ok
end)(),

etap:end_tests().

0 comments on commit 3095578

Please sign in to comment.