pgsql_tests.erl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. -module(pgsql_tests).
  2. -export([run_tests/0]).
  3. -include_lib("eunit/include/eunit.hrl").
  4. -include_lib("public_key/include/public_key.hrl").
  5. -include("pgsql.hrl").
  6. -define(host, "localhost").
  7. -define(port, 5432).
  8. -define(ssl_apps, [crypto, public_key, ssl]).
  9. connect_test() ->
  10. connect_only([]).
  11. connect_to_db_test() ->
  12. connect_only([{database, "epgsql_test_db1"}]).
  13. connect_as_test() ->
  14. connect_only(["epgsql_test", [{database, "epgsql_test_db1"}]]).
  15. connect_with_cleartext_test() ->
  16. connect_only(["epgsql_test_cleartext",
  17. "epgsql_test_cleartext",
  18. [{database, "epgsql_test_db1"}]]).
  19. connect_with_md5_test() ->
  20. connect_only(["epgsql_test_md5",
  21. "epgsql_test_md5",
  22. [{database, "epgsql_test_db1"}]]).
  23. connect_with_invalid_user_test() ->
  24. {error, invalid_authorization_specification} =
  25. pgsql:connect(?host,
  26. "epgsql_test_invalid",
  27. "epgsql_test_invalid",
  28. [{port, ?port}, {database, "epgsql_test_db1"}]).
  29. connect_with_invalid_password_test() ->
  30. {error, Why} =
  31. pgsql:connect(?host,
  32. "epgsql_test_md5",
  33. "epgsql_test_invalid",
  34. [{port, ?port}, {database, "epgsql_test_db1"}]),
  35. case Why of
  36. invalid_authorization_specification -> ok; % =< 8.4
  37. invalid_password -> ok % >= 9.0
  38. end.
  39. connect_with_ssl_test() ->
  40. lists:foreach(fun application:start/1, ?ssl_apps),
  41. with_connection(
  42. fun(C) ->
  43. {ok, _Cols, [{true}]} = pgsql:equery(C, "select ssl_is_used()")
  44. end,
  45. "epgsql_test",
  46. [{ssl, true}]).
  47. connect_with_client_cert_test() ->
  48. lists:foreach(fun application:start/1, ?ssl_apps),
  49. Dir = filename:join(filename:dirname(code:which(pgsql_tests)), "../test_data"),
  50. File = fun(Name) -> filename:join(Dir, Name) end,
  51. {ok, Pem} = file:read_file(File("epgsql.crt")),
  52. [{'Certificate', Der, not_encrypted}] = public_key:pem_decode(Pem),
  53. Cert = public_key:pkix_decode_cert(Der, plain),
  54. #'TBSCertificate'{serialNumber = Serial} = Cert#'Certificate'.tbsCertificate,
  55. Serial2 = list_to_binary(integer_to_list(Serial)),
  56. with_connection(
  57. fun(C) ->
  58. {ok, _, [{true}]} = pgsql:equery(C, "select ssl_is_used()"),
  59. {ok, _, [{Serial2}]} = pgsql:equery(C, "select ssl_client_serial()")
  60. end,
  61. "epgsql_test_cert",
  62. [{ssl, true}, {keyfile, File("epgsql.key")}, {certfile, File("epgsql.crt")}]).
  63. select_test() ->
  64. with_connection(
  65. fun(C) ->
  66. {ok, Cols, Rows} = pgsql:squery(C, "select * from test_table1"),
  67. [#column{name = <<"id">>, type = int4, size = 4},
  68. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  69. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  70. end).
  71. insert_test() ->
  72. with_rollback(
  73. fun(C) ->
  74. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  75. end).
  76. update_test() ->
  77. with_rollback(
  78. fun(C) ->
  79. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  80. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  81. {ok, 2} = pgsql:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  82. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1 where value = 'foo'")
  83. end).
  84. delete_test() ->
  85. with_rollback(
  86. fun(C) ->
  87. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  88. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  89. {ok, 2} = pgsql:squery(C, "delete from test_table1 where id > 2"),
  90. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1")
  91. end).
  92. create_and_drop_table_test() ->
  93. with_rollback(
  94. fun(C) ->
  95. {ok, [], []} = pgsql:squery(C, "create table test_table3 (id int4)"),
  96. {ok, [#column{type = int4}], []} = pgsql:squery(C, "select * from test_table3"),
  97. {ok, [], []} = pgsql:squery(C, "drop table test_table3")
  98. end).
  99. cursor_test() ->
  100. with_connection(
  101. fun(C) ->
  102. {ok, [], []} = pgsql:squery(C, "begin"),
  103. {ok, [], []} = pgsql:squery(C, "declare c cursor for select id from test_table1"),
  104. {ok, 2} = pgsql:squery(C, "move forward 2 from c"),
  105. {ok, 1} = pgsql:squery(C, "move backward 1 from c"),
  106. {ok, 1, _Cols, [{<<"2">>}]} = pgsql:squery(C, "fetch next from c"),
  107. {ok, [], []} = pgsql:squery(C, "close c")
  108. end).
  109. multiple_result_test() ->
  110. with_connection(
  111. fun(C) ->
  112. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = pgsql:squery(C, "select 1; select 2"),
  113. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = pgsql:squery(C, "select 1; select foo;")
  114. end).
  115. extended_select_test() ->
  116. with_connection(
  117. fun(C) ->
  118. {ok, Cols, Rows} = pgsql:equery(C, "select * from test_table1", []),
  119. [#column{name = <<"id">>, type = int4, size = 4},
  120. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  121. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  122. end).
  123. extended_sync_ok_test() ->
  124. with_connection(
  125. fun(C) ->
  126. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1]),
  127. {ok, _Cols, [{<<"two">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [2])
  128. end).
  129. extended_sync_error_test() ->
  130. with_connection(
  131. fun(C) ->
  132. {error, #error{}} = pgsql:equery(C, "select _alue from test_table1 where id = $1", [1]),
  133. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1])
  134. end).
  135. returning_from_insert_test() ->
  136. with_rollback(
  137. fun(C) ->
  138. {ok, 1, _Cols, [{3}]} = pgsql:equery(C, "insert into test_table1 (id) values (3) returning id")
  139. end).
  140. returning_from_update_test() ->
  141. with_rollback(
  142. fun(C) ->
  143. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "update test_table1 set value = 'hi' returning id")
  144. end).
  145. returning_from_delete_test() ->
  146. with_rollback(
  147. fun(C) ->
  148. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "delete from test_table1 returning id")
  149. end).
  150. parse_test() ->
  151. with_connection(
  152. fun(C) ->
  153. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  154. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  155. ok = pgsql:close(C, S),
  156. ok = pgsql:sync(C)
  157. end).
  158. parse_column_format_test() ->
  159. with_connection(
  160. fun(C) ->
  161. {ok, S} = pgsql:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  162. [#column{type = int4},
  163. #column{type = bool},
  164. #column{type = float4}] = S#statement.columns,
  165. ok = pgsql:bind(C, S, []),
  166. {ok, [{1, false, 2.0}]} = pgsql:execute(C, S, 0),
  167. ok = pgsql:close(C, S),
  168. ok = pgsql:sync(C)
  169. end).
  170. parse_error_test() ->
  171. with_connection(
  172. fun(C) ->
  173. {error, #error{}} = pgsql:parse(C, "select _ from test_table1"),
  174. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  175. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  176. ok = pgsql:close(C, S),
  177. ok = pgsql:sync(C)
  178. end).
  179. parse_and_close_test() ->
  180. with_connection(
  181. fun(C) ->
  182. Parse = fun() -> pgsql:parse(C, "test", "select * from test_table1", []) end,
  183. {ok, S} = Parse(),
  184. {error, #error{code = <<"42P05">>}} = Parse(),
  185. pgsql:close(C, S),
  186. {ok, S} = Parse(),
  187. ok = pgsql:sync(C)
  188. end).
  189. bind_test() ->
  190. with_connection(
  191. fun(C) ->
  192. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  193. ok = pgsql:bind(C, S, [1]),
  194. ok = pgsql:close(C, S),
  195. ok = pgsql:sync(C)
  196. end).
  197. bind_parameter_format_test() ->
  198. with_connection(
  199. fun(C) ->
  200. {ok, S} = pgsql:parse(C, "select $1, $2, $3", [int2, text, bool]),
  201. [int2, text, bool] = S#statement.types,
  202. ok = pgsql:bind(C, S, [1, "hi", true]),
  203. {ok, [{1, <<"hi">>, true}]} = pgsql:execute(C, S, 0),
  204. ok = pgsql:close(C, S),
  205. ok = pgsql:sync(C)
  206. end).
  207. bind_error_test() ->
  208. with_connection(
  209. fun(C) ->
  210. {ok, S} = pgsql:parse(C, "select $1::char"),
  211. {error, #error{}} = pgsql:bind(C, S, [0]),
  212. ok = pgsql:bind(C, S, [$A]),
  213. ok = pgsql:close(C, S),
  214. ok = pgsql:sync(C)
  215. end).
  216. bind_and_close_test() ->
  217. with_connection(
  218. fun(C) ->
  219. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  220. ok = pgsql:bind(C, S, "one", []),
  221. {error, #error{code = <<"42P03">>}} = pgsql:bind(C, S, "one", []),
  222. ok = pgsql:close(C, portal, "one"),
  223. ok = pgsql:bind(C, S, "one", []),
  224. ok = pgsql:sync(C)
  225. end).
  226. execute_error_test() ->
  227. with_connection(
  228. fun(C) ->
  229. {ok, S} = pgsql:parse(C, "insert into test_table1 (id, value) values ($1, $2)"),
  230. ok = pgsql:bind(C, S, [1, <<"foo">>]),
  231. {error, #error{code = <<"23505">>}} = pgsql:execute(C, S, 0),
  232. {error, sync_required} = pgsql:bind(C, S, [3, <<"quux">>]),
  233. ok = pgsql:sync(C),
  234. ok = pgsql:bind(C, S, [3, <<"quux">>]),
  235. {ok, _} = pgsql:execute(C, S, 0),
  236. {ok, 1} = pgsql:squery(C, "delete from test_table1 where id = 3")
  237. end).
  238. describe_test() ->
  239. with_connection(
  240. fun(C) ->
  241. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  242. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  243. {ok, S} = pgsql:describe(C, S),
  244. ok = pgsql:close(C, S),
  245. ok = pgsql:sync(C)
  246. end).
  247. describe_with_param_test() ->
  248. with_connection(
  249. fun(C) ->
  250. {ok, S} = pgsql:parse(C, "select id from test_table1 where id = $1"),
  251. [int4] = S#statement.types,
  252. [#column{name = <<"id">>}] = S#statement.columns,
  253. {ok, S} = pgsql:describe(C, S),
  254. ok = pgsql:close(C, S),
  255. ok = pgsql:sync(C)
  256. end).
  257. describe_named_test() ->
  258. with_connection(
  259. fun(C) ->
  260. {ok, S} = pgsql:parse(C, "name", "select * from test_table1", []),
  261. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  262. {ok, S} = pgsql:describe(C, S),
  263. ok = pgsql:close(C, S),
  264. ok = pgsql:sync(C)
  265. end).
  266. describe_error_test() ->
  267. with_connection(
  268. fun(C) ->
  269. {error, #error{}} = pgsql:describe(C, statement, ""),
  270. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  271. {ok, S} = pgsql:describe(C, statement, ""),
  272. ok = pgsql:sync(C)
  273. end).
  274. portal_test() ->
  275. with_connection(
  276. fun(C) ->
  277. {ok, S} = pgsql:parse(C, "select value from test_table1"),
  278. ok = pgsql:bind(C, S, []),
  279. {partial, [{<<"one">>}]} = pgsql:execute(C, S, 1),
  280. {partial, [{<<"two">>}]} = pgsql:execute(C, S, 1),
  281. {ok, []} = pgsql:execute(C, S,1),
  282. ok = pgsql:close(C, S),
  283. ok = pgsql:sync(C)
  284. end).
  285. returning_test() ->
  286. with_rollback(
  287. fun(C) ->
  288. {ok, S} = pgsql:parse(C, "update test_table1 set value = $1 returning id"),
  289. ok = pgsql:bind(C, S, ["foo"]),
  290. {ok, 2, [{1}, {2}]} = pgsql:execute(C, S),
  291. ok = pgsql:sync(C)
  292. end).
  293. multiple_statement_test() ->
  294. with_connection(
  295. fun(C) ->
  296. {ok, S1} = pgsql:parse(C, "one", "select value from test_table1 where id = 1", []),
  297. ok = pgsql:bind(C, S1, []),
  298. {partial, [{<<"one">>}]} = pgsql:execute(C, S1, 1),
  299. {ok, S2} = pgsql:parse(C, "two", "select value from test_table1 where id = 2", []),
  300. ok = pgsql:bind(C, S2, []),
  301. {partial, [{<<"two">>}]} = pgsql:execute(C, S2, 1),
  302. {ok, []} = pgsql:execute(C, S1, 1),
  303. {ok, []} = pgsql:execute(C, S2, 1),
  304. ok = pgsql:close(C, S1),
  305. ok = pgsql:close(C, S2),
  306. ok = pgsql:sync(C)
  307. end).
  308. multiple_portal_test() ->
  309. with_connection(
  310. fun(C) ->
  311. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  312. ok = pgsql:bind(C, S, "one", [1]),
  313. ok = pgsql:bind(C, S, "two", [2]),
  314. {ok, [{<<"one">>}]} = pgsql:execute(C, S, "one", 0),
  315. {ok, [{<<"two">>}]} = pgsql:execute(C, S, "two", 0),
  316. ok = pgsql:close(C, S),
  317. ok = pgsql:sync(C)
  318. end).
  319. execute_function_test() ->
  320. with_rollback(
  321. fun(C) ->
  322. {ok, _Cols1, [{3}]} = pgsql:equery(C, "select insert_test1(3, 'three')"),
  323. {ok, _Cols2, [{<<>>}]} = pgsql:equery(C, "select do_nothing()")
  324. end).
  325. parameter_get_test() ->
  326. with_connection(
  327. fun(C) ->
  328. {ok, <<"off">>} = pgsql:get_parameter(C, "is_superuser")
  329. end).
  330. parameter_set_test() ->
  331. with_connection(
  332. fun(C) ->
  333. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'ISO, MDY'"),
  334. {ok, <<"ISO, MDY">>} = pgsql:get_parameter(C, "DateStyle"),
  335. {ok, _Cols, [{<<"2000-01-02">>}]} = pgsql:squery(C, "select '2000-01-02'::date"),
  336. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'German'"),
  337. {ok, <<"German, DMY">>} = pgsql:get_parameter(C, "DateStyle"),
  338. {ok, _Cols, [{<<"02.01.2000">>}]} = pgsql:squery(C, "select '2000-01-02'::date")
  339. end).
  340. numeric_type_test() ->
  341. check_type(int2, "1", 1, [0, 256, -32768, +32767]),
  342. check_type(int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  343. check_type(int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  344. check_type(float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  345. check_type(float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]).
  346. character_type_test() ->
  347. Alpha = unicode:characters_to_binary([16#03B1]),
  348. Ka = unicode:characters_to_binary([16#304B]),
  349. One = unicode:characters_to_binary([16#10D360]),
  350. check_type(bpchar, "'A'", $A, [1, $1, 16#7F, Alpha, Ka, One], "c_char"),
  351. check_type(text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  352. check_type(varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]).
  353. date_time_type_test() ->
  354. with_connection(
  355. fun(C) ->
  356. case pgsql:get_parameter(C, "integer_datetimes") of
  357. {ok, <<"on">>} -> MaxTsDate = 294276;
  358. {ok, <<"off">>} -> MaxTsDate = 5874897
  359. end,
  360. check_type(date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  361. check_type(time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  362. check_type(timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  363. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  364. check_type(timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  365. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}]),
  366. check_type(interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  367. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  368. end).
  369. misc_type_test() ->
  370. check_type(bool, "true", true, [true, false]),
  371. check_type(bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  372. array_type_test() ->
  373. with_connection(
  374. fun(C) ->
  375. {ok, _, [{[1, 2]}]} = pgsql:equery(C, "select ($1::int[])[1:2]", [[1, 2, 3]]),
  376. Select = fun(Type, A) ->
  377. Query = "select $1::" ++ atom_to_list(Type) ++ "[]",
  378. {ok, _Cols, [{A2}]} = pgsql:equery(C, Query, [A]),
  379. case lists:all(fun({V, V2}) -> compare(Type, V, V2) end, lists:zip(A, A2)) of
  380. true -> ok;
  381. false -> ?assertMatch(A, A2)
  382. end
  383. end,
  384. Select(int2, []),
  385. Select(int2, [1, 2, 3, 4]),
  386. Select(int2, [[1], [2], [3], [4]]),
  387. Select(int2, [[[[[[1, 2]]]]]]),
  388. Select(bool, [true]),
  389. Select(char, [$a, $b, $c]),
  390. Select(int4, [[1, 2]]),
  391. Select(int8, [[[[1, 2]], [[3, 4]]]]),
  392. Select(text, [<<"one">>, <<"two>">>]),
  393. Select(float4, [0.0, 1.0, 0.123]),
  394. Select(float8, [0.0, 1.0, 0.123])
  395. end).
  396. text_format_test() ->
  397. with_connection(
  398. fun(C) ->
  399. Select = fun(Type, V) ->
  400. V2 = list_to_binary(V),
  401. Query = "select $1::" ++ Type,
  402. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
  403. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
  404. end,
  405. Select("inet", "127.0.0.1"),
  406. Select("numeric", "123456")
  407. end).
  408. connect_timeout_test() ->
  409. {error, timeout} = pgsql:connect(?host, [{port, ?port}, {timeout, 0}]).
  410. query_timeout_test() ->
  411. with_connection(
  412. fun(C) ->
  413. {error, timeout} = pgsql:squery(C, "select pg_sleep(1)"),
  414. {error, timeout} = pgsql:equery(C, "select pg_sleep(2)"),
  415. {ok, _Cols, [{1}]} = pgsql:equery(C, "select 1")
  416. end,
  417. [{timeout, 10}]).
  418. execute_timeout_test() ->
  419. with_connection(
  420. fun(C) ->
  421. {ok, S} = pgsql:parse(C, "select pg_sleep($1)"),
  422. ok = pgsql:bind(C, S, [2]),
  423. {error, timeout} = pgsql:execute(C, S, 0),
  424. ok = pgsql:bind(C, S, [0]),
  425. {ok, [{<<>>}]} = pgsql:execute(C, S, 0),
  426. ok = pgsql:close(C, S),
  427. ok = pgsql:sync(C)
  428. end,
  429. [{timeout, 10}]).
  430. connection_closed_test() ->
  431. P = self(),
  432. F = fun() ->
  433. process_flag(trap_exit, true),
  434. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  435. P ! {connected, C},
  436. receive
  437. Any -> P ! Any
  438. end
  439. end,
  440. spawn_link(F),
  441. receive
  442. {connected, C} ->
  443. timer:sleep(100),
  444. pgsql:close(C),
  445. {'EXIT', C, _} = receive R -> R end
  446. end,
  447. flush().
  448. active_connection_closed_test() ->
  449. P = self(),
  450. F = fun() ->
  451. process_flag(trap_exit, true),
  452. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  453. P ! {connected, C},
  454. R = pgsql:squery(C, "select pg_sleep(10)"),
  455. P ! R
  456. end,
  457. spawn_link(F),
  458. receive
  459. {connected, C} ->
  460. timer:sleep(100),
  461. pgsql:close(C),
  462. {error, closed} = receive R -> R end
  463. end,
  464. flush().
  465. warning_notice_test() ->
  466. with_connection(
  467. fun(C) ->
  468. Q = "create function pg_temp.raise() returns void as $$
  469. begin
  470. raise warning 'oops';
  471. end;
  472. $$ language plpgsql;
  473. select pg_temp.raise()",
  474. [{ok, _, _}, _] = pgsql:squery(C, Q),
  475. receive
  476. {pgsql, C, {notice, #error{message = <<"oops">>}}} -> ok
  477. after
  478. 100 -> erlang:error(didnt_receive_notice)
  479. end
  480. end,
  481. [{async, self()}]).
  482. listen_notify_test() ->
  483. with_connection(
  484. fun(C) ->
  485. {ok, [], []} = pgsql:squery(C, "listen epgsql_test"),
  486. {ok, _, [{Pid}]} = pgsql:equery(C, "select pg_backend_pid()"),
  487. {ok, [], []} = pgsql:squery(C, "notify epgsql_test"),
  488. receive
  489. {pgsql, C, {notification, <<"epgsql_test">>, Pid, <<>>}} -> ok
  490. after
  491. 100 -> erlang:error(didnt_receive_notification)
  492. end
  493. end,
  494. [{async, self()}]).
  495. listen_notify_payload_test() ->
  496. with_min_version(
  497. 9.0,
  498. fun(C) ->
  499. {ok, [], []} = pgsql:squery(C, "listen epgsql_test"),
  500. {ok, _, [{Pid}]} = pgsql:equery(C, "select pg_backend_pid()"),
  501. {ok, [], []} = pgsql:squery(C, "notify epgsql_test, 'test!'"),
  502. receive
  503. {pgsql, C, {notification, <<"epgsql_test">>, Pid, <<"test!">>}} -> ok
  504. after
  505. 100 -> erlang:error(didnt_receive_notification)
  506. end
  507. end,
  508. [{async, self()}]).
  509. application_test() ->
  510. lists:foreach(fun application:start/1, ?ssl_apps),
  511. ok = application:start(epgsql).
  512. %% -- run all tests --
  513. run_tests() ->
  514. Files = filelib:wildcard("test_ebin/*tests.beam"),
  515. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  516. eunit:test(Mods, []).
  517. %% -- internal functions --
  518. connect_only(Args) ->
  519. TestOpts = [{port, ?port}],
  520. case Args of
  521. [User, Opts] -> Args2 = [User, TestOpts ++ Opts];
  522. [User, Pass, Opts] -> Args2 = [User, Pass, TestOpts ++ Opts];
  523. Opts -> Args2 = [TestOpts ++ Opts]
  524. end,
  525. {ok, C} = apply(pgsql, connect, [?host | Args2]),
  526. pgsql:close(C),
  527. flush().
  528. with_connection(F) ->
  529. with_connection(F, "epgsql_test", []).
  530. with_connection(F, Args) ->
  531. with_connection(F, "epgsql_test", Args).
  532. with_connection(F, Username, Args) ->
  533. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  534. {ok, C} = pgsql:connect(?host, Username, Args2),
  535. try
  536. F(C)
  537. after
  538. pgsql:close(C)
  539. end,
  540. flush().
  541. with_rollback(F) ->
  542. with_connection(
  543. fun(C) ->
  544. try
  545. pgsql:squery(C, "begin"),
  546. F(C)
  547. after
  548. pgsql:squery(C, "rollback")
  549. end
  550. end).
  551. with_min_version(Min, F, Args) ->
  552. with_connection(
  553. fun(C) ->
  554. {ok, Bin} = pgsql:get_parameter(C, <<"server_version">>),
  555. {ok, [{float, 1, Ver} | _], _} = erl_scan:string(binary_to_list(Bin)),
  556. case Ver >= Min of
  557. true -> F(C);
  558. false -> ?debugFmt("skipping test requiring PostgreSQL >= ~.2f~n", [Min])
  559. end
  560. end,
  561. Args).
  562. check_type(Type, In, Out, Values) ->
  563. Column = "c_" ++ atom_to_list(Type),
  564. check_type(Type, In, Out, Values, Column).
  565. check_type(Type, In, Out, Values, Column) ->
  566. with_connection(
  567. fun(C) ->
  568. Select = io_lib:format("select ~s::~w", [In, Type]),
  569. {ok, [#column{type = Type}], [{Out}]} = pgsql:equery(C, Select),
  570. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  571. {ok, #statement{columns = [#column{type = Type}]} = S} = pgsql:parse(C, Sql),
  572. Insert = fun(V) ->
  573. pgsql:bind(C, S, [V]),
  574. {ok, 1, [{V2}]} = pgsql:execute(C, S),
  575. case compare(Type, V, V2) of
  576. true -> ok;
  577. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  578. end,
  579. ok = pgsql:sync(C)
  580. end,
  581. lists:foreach(Insert, [null | Values])
  582. end).
  583. compare(_Type, null, null) -> true;
  584. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  585. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  586. compare(_Type, V1, V2) -> V1 =:= V2.
  587. %% flush mailbox
  588. flush() ->
  589. ?assertEqual([], flush([])).
  590. flush(Acc) ->
  591. receive
  592. {'EXIT', _Pid, normal} -> flush(Acc);
  593. M -> flush([M | Acc])
  594. after
  595. 0 -> lists:reverse(Acc)
  596. end.