Browse Source

create test suite

Yuriy Zhloba 9 years ago
parent
commit
a2bc636943

+ 2 - 1
.gitignore

@@ -2,4 +2,5 @@
 *.beam
 *.beam
 ebin
 ebin
 deps/
 deps/
-.rebar
+.rebar
+logs

+ 8 - 1
src/epgsql_pool.erl

@@ -75,4 +75,11 @@ transaction(PoolName0, Fun) ->
 -spec equery_with_worker(pid(), epgsql:sql_query(), [epgsql:bind_param()]) -> epgsql:reply().
 -spec equery_with_worker(pid(), epgsql:sql_query(), [epgsql:bind_param()]) -> epgsql:reply().
 equery_with_worker(Worker, Stmt, Params) ->
 equery_with_worker(Worker, Stmt, Params) ->
     Timeout = epgsql_pool_settings:get(query_timeout),
     Timeout = epgsql_pool_settings:get(query_timeout),
-    gen_server:call(Worker, {equery, Stmt, Params}, Timeout). % TODO need other way to implement it
+    % TODO process timeout,
+    % try-catch
+    % send cancel
+    % log error
+    % reply to client with error
+    % reconnect
+    % return to pool
+    gen_server:call(Worker, {equery, Stmt, Params}, Timeout).

+ 0 - 1
src/epgsql_pool_worker.erl

@@ -36,7 +36,6 @@ handle_call({equery, _, _}, _From, #state{connection = undefined} = State) ->
 
 
 handle_call({equery, Stmt, Params}, _From, #state{connection = Connection} = State) ->
 handle_call({equery, Stmt, Params}, _From, #state{connection = Connection} = State) ->
     %% TStart = os:timestamp(),
     %% TStart = os:timestamp(),
-    %% TODO: query_timeout
     Sock = Connection#epgsql_connection.connection_sock,
     Sock = Connection#epgsql_connection.connection_sock,
     Result = epgsql:equery(Sock, Stmt, Params),
     Result = epgsql:equery(Sock, Stmt, Params),
     %% Time = timer:now_diff(os:timestamp(), TStart),
     %% Time = timer:now_diff(os:timestamp(), TStart),

+ 51 - 0
test/epgsql_pool_SUITE.erl

@@ -0,0 +1,51 @@
+-module(epgsql_pool_SUITE).
+
+%% test needs connection to database
+%% and database should be inited with ./testdb_schema.sql
+
+-include("epgsql_pool.hrl").
+-include_lib("common_test/include/ct.hrl").
+
+-export([all/0,
+         init_per_suite/1, end_per_suite/1,
+         init_per_testcase/2, end_per_testcase/2,
+         test_1/1
+        ]).
+
+
+all() -> [
+    test_1
+].
+
+
+init_per_suite(Config) ->
+    application:ensure_all_started(epgsql_pool),
+    Config.
+
+
+end_per_suite(Config) ->
+    application:stop(epgsql_pool),
+    Config.
+
+
+init_per_testcase(_, Config) ->
+    Params = #epgsql_connection_params{host = "localhost", port = 5432, username = "test", password = "test", database = "testdb"},
+    {ok, Connection} = epgsql_pool_utils:open_connection(Params),
+    #epgsql_connection{connection_sock = Sock} = Connection,
+    epgsql:equery(Sock, "TRUNCATE TABLE item"),
+    epgsql:equery(Sock, "TRUNCATE TABLE category CASCADE"),
+    [{connection, Connection}].
+
+
+end_per_testcase(_, Config) ->
+    Connection = proplists:get_value(connection, Config),
+    epgsql_pool_utils:close_connection(Connection),
+    Config.
+
+
+test_1(Config) ->
+    Connection = proplists:get_value(connection, Config),
+    #epgsql_connection{connection_sock = Sock} = Connection,
+    Res = epgsql:equery(Sock, "SELECT * FROM item"),
+    ct:pal("Res:~p", [Res]),
+    ok.

+ 11 - 0
test/epgsql_pool_utils_tests.erl

@@ -0,0 +1,11 @@
+-module(epgsql_pool_utils_tests).
+
+-include("epgsql_pool.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+
+pool_name_to_atom_test() ->
+    ?assertEqual(my_pool, epgsql_pool_utils:pool_name_to_atom(my_pool)),
+    ?assertEqual(my_pool, epgsql_pool_utils:pool_name_to_atom("my_pool")),
+    ?assertEqual(my_pool, epgsql_pool_utils:pool_name_to_atom(<<"my_pool">>)),
+    ok.

+ 18 - 0
test/testdb_schema.sql

@@ -0,0 +1,18 @@
+--
+-- testdb schema
+--
+
+CREATE TABLE category (
+    id bigserial,
+    title text,
+    PRIMARY KEY (id)
+);
+
+CREATE TABLE item (
+    id bigserial,
+    category_id bigint,
+    title text,
+    num int,
+    PRIMARY KEY (id),
+    FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE SET NULL
+);