pgsql_tests.erl 24 KB

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