pgsql_tests.erl 23 KB

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