pgsql_tests.erl 23 KB

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