epgsql_pool_SUITE.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Connection = proplists:get_value(connection, Config),
  60. epgsql_pool_settings:set_connection_params(my_pool, Connection#epgsql_connection.params),
  61. {ok, _} = epgsql_pool:start(my_pool, 5, 10),
  62. {FirstCatId, CatIds2, ItemIds2} =
  63. epgsql_pool:transaction(my_pool,
  64. fun(Worker) ->
  65. ct:pal("worker:~p", [Worker]),
  66. {ok, 3, _, CatIds0} =
  67. epgsql_pool:equery(Worker,
  68. "INSERT INTO category (title) "
  69. "VALUES ('cat 4'), ('cat 5'), ('cat 6') "
  70. "RETURNING id"),
  71. CatIds1 = lists:map(fun({Cid}) -> Cid end, CatIds0),
  72. CatId = hd(CatIds1),
  73. {ok, 2, _, ItemIds0} =
  74. epgsql_pool:equery(Worker,
  75. "INSERT INTO item (category_id, title, num) "
  76. "VALUES ($1, 'item 1', 5), ($1, 'item 2', 7) "
  77. "RETURNING id", [CatId]),
  78. ItemIds1 = lists:map(fun({Iid}) -> Iid end, ItemIds0),
  79. {CatId, CatIds1, ItemIds1}
  80. end),
  81. WaitForCats = lists:zip(CatIds2, [<<"cat 4">>, <<"cat 5">>, <<"cat 6">>]),
  82. {ok, _, CatRows} = epgsql_pool:equery(my_pool, "SELECT id, title FROM category ORDER by id ASC"),
  83. ct:pal("CatRows ~p", [CatRows]),
  84. ?assertEqual(WaitForCats, CatRows),
  85. WaitForItems = lists:map(fun({ItemId, {Title, Num}}) -> {ItemId, FirstCatId, Title, Num} end,
  86. lists:zip(ItemIds2, [{<<"item 1">>, 5}, {<<"item 2">>, 7}])),
  87. {ok, _, ItemRows} = epgsql_pool:equery(my_pool, "SELECT id, category_id, title, num FROM item ORDER by id ASC"),
  88. ct:pal("ItemRows ~p", [ItemRows]),
  89. ?assertEqual(WaitForItems, ItemRows),
  90. %% TODO invalid transation (with exception)
  91. ok = epgsql_pool:stop(my_pool),
  92. ok.
  93. reconnect_test(Config) ->
  94. throw(not_implemented),
  95. ok.