pgsql_tests.erl 23 KB

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