Browse Source

unit tests for herd_db

Yuriy Zhloba 10 years ago
parent
commit
9a28a2146e
3 changed files with 114 additions and 47 deletions
  1. 9 4
      include/herd.hrl
  2. 24 43
      src/herd_db.erl
  3. 81 0
      test/herd_db_test.erl

+ 9 - 4
include/herd.hrl

@@ -3,9 +3,14 @@
 
 
 %% jiffy json format
 %% jiffy json format
 -type(json_key() :: atom() | binary()).
 -type(json_key() :: atom() | binary()).
--type(json_value() :: integer() | float() | binary() | json_obj() | [json_value()]).
+-type(json_value() :: atom() | integer() | float() | binary() | json_obj() | [json_value()]).
 -type(json_obj() :: {[{json_key(), json_value()}]}).
 -type(json_obj() :: {[{json_key(), json_value()}]}).
 
 
-
-%% PostgreSQL returns datetime like this: 2014-12-20 17:38:56.475565+03
--type(db_datetime() :: {calendar:date(), {0..23, 0..59, float()}}).
+%% db types (PostgreSQL with epgsql driver)
+-type(db_name() :: binary()).
+-type(db_type() :: atom()).
+-type(db_value() :: term()).
+-type(db_row() :: tuple()).
+-type(db_column() :: {column, db_name(), db_type(), term(), term(), term()}).
+-type(db_select() :: {ok, [db_column()], [db_row()]}).
+-type(db_datetime() :: {calendar:date(), {0..23, 0..59, float()}}). % PostgreSQL datetime format: 2014-12-20 17:38:56.475565+03

+ 24 - 43
src/herd_db.erl

@@ -3,57 +3,38 @@
 -export([epgsql_res_to_json/1]).
 -export([epgsql_res_to_json/1]).
 
 
 -include("herd.hrl").
 -include("herd.hrl").
--include_lib("eunit/include/eunit.hrl").
+
 
 
 %%% module API
 %%% module API
 
 
--spec epgsql_res_to_json({ok, [tuple()], [tuple()]}) -> [json_obj()].
+-spec epgsql_res_to_json(db_select()) -> [json_obj()].
 epgsql_res_to_json({ok, Columns, Rows}) ->
 epgsql_res_to_json({ok, Columns, Rows}) ->
-    NameType = lists:map(fun({column, Name, Type, _, _, _}) -> {Name, Type} end, Columns),
-    lists:map(fun(Row) -> epgsql_row_to_json(NameType, Row) end, Rows).
+    lists:map(fun(Row) -> epgsql_row_to_json(Columns, Row) end, Rows).
+
 
 
+%%% inner functions
 
 
--spec epgsql_row_to_json([{binary(), atom()}], tuple()) -> json_obj().
-epgsql_row_to_json(NameType, Row) ->
-    KV = lists:map(fun(Index) ->
-                           {Name, Type} = lists:nth(Index, NameType),
-                           Value = epgsql_value_to_json(Type, element(Index, Row)),
-                           {Name, Value}
-                   end, lists:seq(1, length(NameType))),
+-spec epgsql_row_to_json([db_column()], db_row()) -> json_obj().
+epgsql_row_to_json(Columns, Row) ->
+    KV = lists:map(
+           fun(Index) ->
+                   {column, Name, Type, _, _, _} = lists:nth(Index, Columns),
+                   Value = epgsql_value_to_json(Type, element(Index, Row)),
+                   {Name, Value}
+           end, lists:seq(1, length(Columns))),
     {KV}.
     {KV}.
 
 
 
 
--spec epgsql_value_to_json(atom(), term()) -> json_value().
-epgsql_value_to_json(timestamp, null) -> null;
-epgsql_value_to_json(timestamp, Timestamp) ->
+-spec epgsql_value_to_json(db_type(), db_value()) -> json_value().
+epgsql_value_to_json(_Type, Value) when is_atom(Value);
+                                        is_integer(Value);
+                                        is_float(Value);
+                                        is_binary(Value)
+                                        -> Value;
+epgsql_value_to_json(time, {H, M, S}) -> H * 3600 + M * 60 + trunc(S);
+epgsql_value_to_json(date, Date) -> epgsql_value_to_json(timestamp, {Date, {0, 0, 0}});
+epgsql_value_to_json(timestamp, Timestamp) -> epgsql_value_to_json(timestamptz, Timestamp);
+epgsql_value_to_json(timestamptz, Timestamp) ->
     herd_datetime:datetime_to_timestamp(
     herd_datetime:datetime_to_timestamp(
       herd_datetime:datetime_from_db(Timestamp));
       herd_datetime:datetime_from_db(Timestamp));
-epgsql_value_to_json(_Type, Value) -> Value.
-
-
-epgsql_res_to_json_test() ->
-    Data = {ok,
-            [{column,<<"id">>,int8,8,-1,1},
-             {column,<<"name">>,varchar,-1,36,1},
-             {column,<<"registration_date">>,timestamp,8,-1,1},
-             {column,<<"account_type">>,{unknown_oid,33521},4,-1,0},
-             {column,<<"address">>,text,-1,-1,1},
-             {column,<<"country">>,text,-1,-1,1}
-            ],
-            [{12,<<"Bob">>,{{2015,2,17},{16,10,31.277398}},<<"base">>,<<"Minsk, bla-bla-bla">>,<<"BY">>},
-             {13,<<"Bill">>,{{2015,2,17},{16,11,1.211181}},<<"premium">>,<<"Amsterdam, bla-bla-bla">>,<<"NL">>}]},
-    JSON = epgsql_res_to_json(Data),
-    ?assertEqual([{[{<<"id">>,12},
-                    {<<"name">>,<<"Bob">>},
-                    {<<"registration_date">>,1424189431},
-                    {<<"account_type">>,<<"base">>},
-                    {<<"address">>,<<"Minsk, bla-bla-bla">>},
-                    {<<"country">>,<<"BY">>}]},
-                  {[{<<"id">>,13},
-                    {<<"name">>,<<"Bill">>},
-                    {<<"registration_date">>,1424189461},
-                    {<<"account_type">>,<<"premium">>},
-                    {<<"address">>,<<"Amsterdam, bla-bla-bla">>},
-                    {<<"country">>,<<"NL">>}]}],
-                 JSON),
-    ok.
+epgsql_value_to_json(Type, Value) -> throw({type_not_supported, Type, Value}).

+ 81 - 0
test/herd_db_test.erl

@@ -0,0 +1,81 @@
+-module(herd_db_test).
+
+-include_lib("eunit/include/eunit.hrl").
+
+
+-spec epgsql_res_to_json_test() -> ok.
+epgsql_res_to_json_test() ->
+    Data1 = {ok,
+             [{column,<<"id">>,int8,8,-1,1},
+              {column,<<"name">>,varchar,-1,36,1},
+              {column,<<"registration_date">>,timestamp,8,-1,1},
+              {column,<<"account_type">>,{unknown_oid,33521},4,-1,0},
+              {column,<<"address">>,text,-1,-1,1},
+              {column,<<"country">>,text,-1,-1,1}
+             ],
+             [{12,<<"Bob">>,{{2015,2,17},{16,10,31.277398}},<<"base">>,<<"Minsk, bla-bla-bla">>,<<"BY">>},
+              {13,<<"Bill">>,{{2015,2,17},{16,11,1.211181}},<<"premium">>,<<"Amsterdam, bla-bla-bla">>,<<"NL">>}]},
+    ?assertEqual([{[{<<"id">>,12},
+                    {<<"name">>,<<"Bob">>},
+                    {<<"registration_date">>,1424189431},
+                    {<<"account_type">>,<<"base">>},
+                    {<<"address">>,<<"Minsk, bla-bla-bla">>},
+                    {<<"country">>,<<"BY">>}]},
+                  {[{<<"id">>,13},
+                    {<<"name">>,<<"Bill">>},
+                    {<<"registration_date">>,1424189461},
+                    {<<"account_type">>,<<"premium">>},
+                    {<<"address">>,<<"Amsterdam, bla-bla-bla">>},
+                    {<<"country">>,<<"NL">>}]}],
+                 herd_db:epgsql_res_to_json(Data1)),
+
+    Data2 = {ok,
+             [{column,<<"id">>,int4,4,-1,1},
+              {column,<<"token">>,uuid,16,-1,1},
+              {column,<<"name">>,varchar,-1,68,1},
+              {column,<<"reg_date">>,date,4,-1,1},
+              {column,<<"reg_time">>,time,8,-1,1},
+              {column,<<"ts_1">>,timestamp,8,-1,1},
+              {column,<<"ts_2">>,timestamptz,8,-1,1}
+             ],
+             [{1,<<"1f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>,<<"Bob">>,
+               {2015,2,18}, null, {{2015,2,18},{13,5,23.140576}}, null},
+              {2,<<"2f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>,<<"Bill">>,
+               null, {13,5,23.140576}, null, {{2015,2,18},{10,5,23.140576}}},
+              {3,<<"3f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>,<<"Helen">>,
+               {2015,2,18}, {13,5,23.140576}, null,null},
+              {4,<<"4f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>,<<"Kate">>,
+               null,null, {{2015,2,18},{13,5,23.140576}}, {{2015,2,18},{10,5,23.140576}}}]},
+
+    ?assertEqual([{[{<<"id">>,1},
+                    {<<"token">>,<<"1f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>},
+                    {<<"name">>,<<"Bob">>},
+                    {<<"reg_date">>,1424217600},
+                    {<<"reg_time">>,null},
+                    {<<"ts_1">>,1424264723},
+                    {<<"ts_2">>,null}]},
+                  {[{<<"id">>,2},
+                    {<<"token">>,<<"2f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>},
+                    {<<"name">>,<<"Bill">>},
+                    {<<"reg_date">>,null},
+                    {<<"reg_time">>,47123},
+                    {<<"ts_1">>,null},
+                    {<<"ts_2">>,1424253923}]},
+                  {[{<<"id">>,3},
+                    {<<"token">>,<<"3f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>},
+                    {<<"name">>,<<"Helen">>},
+                    {<<"reg_date">>,1424217600},
+                    {<<"reg_time">>,47123},
+                    {<<"ts_1">>,null},
+                    {<<"ts_2">>,null}]},
+                  {[{<<"id">>,4},
+                    {<<"token">>,<<"4f5cb2d8-14b4-4ec4-a45e-a3fa06f07650">>},
+                    {<<"name">>,<<"Kate">>},
+                    {<<"reg_date">>,null},
+                    {<<"reg_time">>,null},
+                    {<<"ts_1">>,1424264723},
+                    {<<"ts_2">>,1424253923}]}],
+
+                 herd_db:epgsql_res_to_json(Data2)),
+
+    ok.