Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #3 from AF83/bson_null

Bson value null
  • Loading branch information...
commit 30955783a9f9a000d68c6dba3e910a877bf5cabf 2 parents 4e1d0c5 + 9fac348
@boorad authored
View
6 README.markdown
@@ -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
View
15 src/emongo_bson.erl
@@ -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>>;
@@ -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>>;
@@ -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};
@@ -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>>) ->
View
2  src/emongo_sup.erl
@@ -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).
View
2  t/001-load.t
@@ -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"),
View
15 t/002-bson.t
@@ -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() ->
@@ -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>>},
@@ -108,7 +119,7 @@ main(_) ->
%% 10) data_null
(fun() ->
- Val = undefined,
+ Val = null,
BinVal = <<>>,
Size = size(BinVal) + 8,
Encoded = emongo_bson:encode([{<<"a">>, Val}]),
View
2  t/003-find.t
@@ -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"),
View
2  t/004-cond-exprs.t
@@ -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"),
View
8 t/005-drop.t
@@ -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:insert(test1, "sushi", [{<<"rolls">>, I}]) || I <- lists:seq(1, 50)],
[emongo:insert(test1, "sushi2", [{<<"rolls">>, I}]) || I <- lists:seq(1, 50)],
@@ -13,7 +13,7 @@ main(_) ->
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().
View
22 t/006-multiup.t
@@ -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().
Please sign in to comment.
Something went wrong with that request. Please try again.