pgsql_tests.erl 24 KB

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