epgsql_pool_SUITE.erl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. -module(epgsql_pool_SUITE).
  2. %% test needs connection to database
  3. %% and database should be inited with ./testdb_schema.sql
  4. -include("epgsql_pool.hrl").
  5. -include_lib("common_test/include/ct.hrl").
  6. -include_lib("eunit/include/eunit.hrl").
  7. -export([all/0,
  8. init_per_suite/1, end_per_suite/1, init_per_testcase/2, end_per_testcase/2,
  9. start_stop_test/1, equery_test/1, transaction_test/1, reconnect_test/1
  10. ]).
  11. all() ->
  12. [start_stop_test,
  13. equery_test,
  14. transaction_test,
  15. reconnect_test
  16. ].
  17. init_per_suite(Config) ->
  18. application:ensure_all_started(epgsql_pool),
  19. Config.
  20. end_per_suite(Config) ->
  21. application:stop(epgsql_pool),
  22. Config.
  23. init_per_testcase(_, Config) ->
  24. Params = #epgsql_connection_params{host = "localhost", port = 5432, username = "test", password = "test", database = "testdb"},
  25. {ok, Connection} = epgsql_pool_utils:open_connection(Params),
  26. #epgsql_connection{connection_sock = Sock} = Connection,
  27. epgsql:equery(Sock, "TRUNCATE TABLE item"),
  28. epgsql:equery(Sock, "TRUNCATE TABLE category CASCADE"),
  29. [{connection, Connection} | proplists:delete(connection, Config)].
  30. end_per_testcase(_, Config) ->
  31. Connection = proplists:get_value(connection, Config),
  32. Connection2 = epgsql_pool_utils:close_connection(Connection),
  33. #epgsql_connection{connection_sock = undefined} = Connection2,
  34. [{connection, Connection2} | proplists:delete(connection, Config)].
  35. start_stop_test(Config) ->
  36. Params = #epgsql_connection_params{host = "localhost", port = 5432,
  37. username="test", password="test",
  38. database="testdb"},
  39. epgsql_pool_settings:set_connection_params(my_pool, Params),
  40. {ok, _} = epgsql_pool:start(my_pool, 5, 10),
  41. ok = epgsql_pool:stop(my_pool),
  42. ok.
  43. equery_test(Config) ->
  44. Connection = proplists:get_value(connection, Config),
  45. epgsql_pool_settings:set_connection_params(my_pool, Connection#epgsql_connection.params),
  46. {ok, _} = epgsql_pool:start(my_pool, 5, 10),
  47. {ok, 3, _, Ids} = epgsql_pool:equery(my_pool,
  48. "INSERT INTO category (title) "
  49. "VALUES ('cat 1'), ('cat 2'), ('cat 3') "
  50. "RETURNING id"),
  51. WaitForRows = lists:map(fun({{Id}, Title}) -> {Id, Title} end,
  52. lists:zip(Ids, [<<"cat 1">>, <<"cat 2">>, <<"cat 3">>])),
  53. {ok, _, Rows} = epgsql_pool:equery(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
  54. ct:pal("Rows ~p", [Rows]),
  55. ?assertEqual(WaitForRows, Rows),
  56. ok = epgsql_pool:stop(my_pool),
  57. ok.
  58. transaction_test(Config) ->
  59. throw(not_implemented),
  60. ok.
  61. reconnect_test(Config) ->
  62. throw(not_implemented),
  63. ok.