epgsql_ct.erl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. -module(epgsql_ct).
  2. -include_lib("common_test/include/ct.hrl").
  3. -include_lib("eunit/include/eunit.hrl").
  4. -export([
  5. connection_data/1,
  6. connect_only/2,
  7. with_connection/2,
  8. with_connection/3,
  9. with_connection/4,
  10. with_rollback/2,
  11. with_min_version/4,
  12. flush/0
  13. ]).
  14. connection_data(Config) ->
  15. PgConfig = ?config(pg_config, Config),
  16. Host = ?config(host, PgConfig),
  17. Port = ?config(port, PgConfig),
  18. {Host, Port}.
  19. connect_only(Config, Args) ->
  20. {Host, Port} = connection_data(Config),
  21. Module = ?config(module, Config),
  22. TestOpts = [{port, Port}],
  23. case Args of
  24. [User, Opts] -> Args2 = [User, TestOpts ++ Opts];
  25. [User, Pass, Opts] -> Args2 = [User, Pass, TestOpts ++ Opts];
  26. Opts -> Args2 = [TestOpts ++ Opts]
  27. end,
  28. {ok, C} = apply(Module, connect, [Host | Args2]),
  29. Module:close(C),
  30. flush().
  31. with_connection(Config, F) ->
  32. with_connection(Config, F, "epgsql_test", []).
  33. with_connection(Config, F, Args) ->
  34. with_connection(Config, F, "epgsql_test", Args).
  35. with_connection(Config, F, Username, Args) ->
  36. {Host, Port} = connection_data(Config),
  37. Module = ?config(module, Config),
  38. Args2 = [{port, Port}, {database, "epgsql_test_db1"} | Args],
  39. {ok, C} = Module:connect(Host, Username, Args2),
  40. try
  41. F(C)
  42. after
  43. Module:close(C)
  44. end,
  45. flush().
  46. with_rollback(Config, F) ->
  47. Module = ?config(module, Config),
  48. with_connection(
  49. Config,
  50. fun(C) ->
  51. try
  52. Module:squery(C, "begin"),
  53. F(C)
  54. after
  55. Module:squery(C, "rollback")
  56. end
  57. end).
  58. with_min_version(Config, Min, F, Args) ->
  59. PgConf = ?config(pg_config, Config),
  60. Ver = ?config(version, PgConf),
  61. case Ver >= Min of
  62. true ->
  63. epgsql_ct:with_connection(Config, F, Args);
  64. false ->
  65. ?debugFmt("skipping test requiring PostgreSQL >= ~p, but we have ~p ~p",
  66. [Min, Ver, Config])
  67. end.
  68. %% flush mailbox
  69. flush() ->
  70. ?assertEqual([], flush([])).
  71. flush(Acc) ->
  72. receive
  73. {'EXIT', _Pid, normal} -> flush(Acc);
  74. M -> flush([M | Acc])
  75. after
  76. 0 -> lists:reverse(Acc)
  77. end.