pgsql_tests.erl 23 KB

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