pgsql_tests.erl 22 KB

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