pgsql_tests.erl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. Select = fun(Type, V) ->
  376. Query = "select $1::" ++ Type,
  377. {ok, _Cols, [{V}]} = pgsql:equery(C, Query, [V])
  378. end,
  379. Select("int2[]", []),
  380. Select("int2[]", [1, 2, 3, 4]),
  381. Select("int2[]", [[1], [2], [3], [4]]),
  382. Select("int2[]", [[[[[[1, 2]]]]]]),
  383. Select("bool[]", [true]),
  384. Select("char[]", [$a, $b, $c]),
  385. Select("int4[]", [[1, 2]]),
  386. Select("int8[]", [[[[1, 2]], [[3, 4]]]]),
  387. Select("text[]", [<<"one">>, <<"two>">>])
  388. end).
  389. text_format_test() ->
  390. with_connection(
  391. fun(C) ->
  392. Select = fun(Type, V) ->
  393. V2 = list_to_binary(V),
  394. Query = "select $1::" ++ Type,
  395. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
  396. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
  397. end,
  398. Select("inet", "127.0.0.1"),
  399. Select("numeric", "123456")
  400. end).
  401. connect_timeout_test() ->
  402. {error, timeout} = pgsql:connect(?host, [{port, ?port}, {timeout, 0}]).
  403. query_timeout_test() ->
  404. with_connection(
  405. fun(C) ->
  406. {error, timeout} = pgsql:squery(C, "select pg_sleep(1)"),
  407. {error, timeout} = pgsql:equery(C, "select pg_sleep(2)"),
  408. {ok, _Cols, [{1}]} = pgsql:equery(C, "select 1")
  409. end,
  410. [{timeout, 10}]).
  411. execute_timeout_test() ->
  412. with_connection(
  413. fun(C) ->
  414. {ok, S} = pgsql:parse(C, "select pg_sleep($1)"),
  415. ok = pgsql:bind(C, S, [2]),
  416. {error, timeout} = pgsql:execute(C, S, 0),
  417. ok = pgsql:bind(C, S, [0]),
  418. {ok, [{<<>>}]} = pgsql:execute(C, S, 0),
  419. ok = pgsql:close(C, S),
  420. ok = pgsql:sync(C)
  421. end,
  422. [{timeout, 10}]).
  423. connection_closed_test() ->
  424. P = self(),
  425. F = fun() ->
  426. process_flag(trap_exit, true),
  427. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  428. P ! {connected, C},
  429. receive
  430. Any -> P ! Any
  431. end
  432. end,
  433. spawn_link(F),
  434. receive
  435. {connected, C} ->
  436. timer:sleep(100),
  437. pgsql:close(C),
  438. {'EXIT', C, _} = receive R -> R end
  439. end,
  440. flush().
  441. active_connection_closed_test() ->
  442. P = self(),
  443. F = fun() ->
  444. process_flag(trap_exit, true),
  445. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  446. P ! {connected, C},
  447. R = pgsql:squery(C, "select pg_sleep(10)"),
  448. P ! R
  449. end,
  450. spawn_link(F),
  451. receive
  452. {connected, C} ->
  453. timer:sleep(100),
  454. pgsql:close(C),
  455. {error, closed} = receive R -> R end
  456. end,
  457. flush().
  458. warning_notice_test() ->
  459. with_connection(
  460. fun(C) ->
  461. Q = "create function pg_temp.raise() returns void as $$
  462. begin
  463. raise warning 'oops';
  464. end;
  465. $$ language plpgsql;
  466. select pg_temp.raise()",
  467. [{ok, _, _}, _] = pgsql:squery(C, Q),
  468. receive
  469. {pgsql, C, {notice, #error{message = <<"oops">>}}} -> ok
  470. after
  471. 100 -> erlang:error(didnt_receive_notice)
  472. end
  473. end,
  474. [{async, self()}]).
  475. listen_notify_test() ->
  476. with_connection(
  477. fun(C) ->
  478. {ok, [], []} = pgsql:squery(C, "listen epgsql_test"),
  479. {ok, _, [{Pid}]} = pgsql:equery(C, "select pg_backend_pid()"),
  480. {ok, [], []} = pgsql:squery(C, "notify epgsql_test"),
  481. receive
  482. {pgsql, C, {notification, <<"epgsql_test">>, Pid, <<>>}} -> ok
  483. after
  484. 100 -> erlang:error(didnt_receive_notification)
  485. end
  486. end,
  487. [{async, self()}]).
  488. listen_notify_payload_test() ->
  489. with_min_version(
  490. 9.0,
  491. fun(C) ->
  492. {ok, [], []} = pgsql:squery(C, "listen epgsql_test"),
  493. {ok, _, [{Pid}]} = pgsql:equery(C, "select pg_backend_pid()"),
  494. {ok, [], []} = pgsql:squery(C, "notify epgsql_test, 'test!'"),
  495. receive
  496. {pgsql, C, {notification, <<"epgsql_test">>, Pid, <<"test!">>}} -> ok
  497. after
  498. 100 -> erlang:error(didnt_receive_notification)
  499. end
  500. end,
  501. [{async, self()}]).
  502. application_test() ->
  503. lists:foreach(fun application:start/1, ?ssl_apps),
  504. ok = application:start(epgsql).
  505. %% -- run all tests --
  506. run_tests() ->
  507. Files = filelib:wildcard("test_ebin/*tests.beam"),
  508. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  509. eunit:test(Mods, []).
  510. %% -- internal functions --
  511. connect_only(Args) ->
  512. TestOpts = [{port, ?port}],
  513. case Args of
  514. [User, Opts] -> Args2 = [User, TestOpts ++ Opts];
  515. [User, Pass, Opts] -> Args2 = [User, Pass, TestOpts ++ Opts];
  516. Opts -> Args2 = [TestOpts ++ Opts]
  517. end,
  518. {ok, C} = apply(pgsql, connect, [?host | Args2]),
  519. pgsql:close(C),
  520. flush().
  521. with_connection(F) ->
  522. with_connection(F, "epgsql_test", []).
  523. with_connection(F, Args) ->
  524. with_connection(F, "epgsql_test", Args).
  525. with_connection(F, Username, Args) ->
  526. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  527. {ok, C} = pgsql:connect(?host, Username, Args2),
  528. try
  529. F(C)
  530. after
  531. pgsql:close(C)
  532. end,
  533. flush().
  534. with_rollback(F) ->
  535. with_connection(
  536. fun(C) ->
  537. try
  538. pgsql:squery(C, "begin"),
  539. F(C)
  540. after
  541. pgsql:squery(C, "rollback")
  542. end
  543. end).
  544. with_min_version(Min, F, Args) ->
  545. with_connection(
  546. fun(C) ->
  547. {ok, Bin} = pgsql:get_parameter(C, <<"server_version">>),
  548. {ok, [{float, 1, Ver} | _], _} = erl_scan:string(binary_to_list(Bin)),
  549. case Ver >= Min of
  550. true -> F(C);
  551. false -> ?debugFmt("skipping test requiring PostgreSQL >= ~.2f~n", [Min])
  552. end
  553. end,
  554. Args).
  555. check_type(Type, In, Out, Values) ->
  556. Column = "c_" ++ atom_to_list(Type),
  557. check_type(Type, In, Out, Values, Column).
  558. check_type(Type, In, Out, Values, Column) ->
  559. with_connection(
  560. fun(C) ->
  561. Select = io_lib:format("select ~s::~w", [In, Type]),
  562. {ok, [#column{type = Type}], [{Out}]} = pgsql:equery(C, Select),
  563. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  564. {ok, #statement{columns = [#column{type = Type}]} = S} = pgsql:parse(C, Sql),
  565. Insert = fun(V) ->
  566. pgsql:bind(C, S, [V]),
  567. {ok, 1, [{V2}]} = pgsql:execute(C, S),
  568. case compare(Type, V, V2) of
  569. true -> ok;
  570. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  571. end,
  572. ok = pgsql:sync(C)
  573. end,
  574. lists:foreach(Insert, [null | Values])
  575. end).
  576. compare(_Type, null, null) -> true;
  577. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  578. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  579. compare(_Type, V1, V2) -> V1 =:= V2.
  580. %% flush mailbox
  581. flush() ->
  582. ?assertEqual([], flush([])).
  583. flush(Acc) ->
  584. receive
  585. {'EXIT', _Pid, normal} -> flush(Acc);
  586. M -> flush([M | Acc])
  587. after
  588. 0 -> lists:reverse(Acc)
  589. end.