pgsql_tests.erl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. -module(pgsql_tests).
  2. -export([run_tests/0]).
  3. -include_lib("eunit/include/eunit.hrl").
  4. -include("pgsql.hrl").
  5. -define(host, "localhost").
  6. -define(port, 5432).
  7. connect_test() ->
  8. connect_only([[]]).
  9. connect_to_db_test() ->
  10. connect_only([[{database, "epgsql_test_db1"}]]).
  11. connect_as_test() ->
  12. connect_only(["epgsql_test", [{database, "epgsql_test_db1"}]]).
  13. connect_with_cleartext_test() ->
  14. connect_only(["epgsql_test_cleartext",
  15. "epgsql_test_cleartext",
  16. [{database, "epgsql_test_db1"}]]).
  17. connect_with_md5_test() ->
  18. connect_only(["epgsql_test_md5",
  19. "epgsql_test_md5",
  20. [{database, "epgsql_test_db1"}]]).
  21. connect_with_invalid_password_test() ->
  22. {error, invalid_authorization_specification} =
  23. pgsql:connect(?host,
  24. "epgsql_test_md5",
  25. "epgsql_test_sha1",
  26. [{port, ?port}, {database, "epgsql_test_db1"}]).
  27. connect_with_ssl_test() ->
  28. lists:foreach(fun application:start/1, [crypto, ssl]),
  29. with_connection(
  30. fun(C) ->
  31. {ok, _Cols, [{true}]} = pgsql:equery(C, "select ssl_is_used()")
  32. end,
  33. [{ssl, true}]).
  34. select_test() ->
  35. with_connection(
  36. fun(C) ->
  37. {ok, Cols, Rows} = pgsql:squery(C, "select * from test_table1"),
  38. [#column{name = <<"id">>, type = int4, size = 4},
  39. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  40. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  41. end).
  42. insert_test() ->
  43. with_rollback(
  44. fun(C) ->
  45. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  46. end).
  47. update_test() ->
  48. with_rollback(
  49. fun(C) ->
  50. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  51. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  52. {ok, 2} = pgsql:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  53. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1 where value = 'foo'")
  54. end).
  55. delete_test() ->
  56. with_rollback(
  57. fun(C) ->
  58. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  59. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  60. {ok, 2} = pgsql:squery(C, "delete from test_table1 where id > 2"),
  61. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1")
  62. end).
  63. create_and_drop_table_test() ->
  64. with_rollback(
  65. fun(C) ->
  66. {ok, [], []} = pgsql:squery(C, "create table test_table3 (id int4)"),
  67. {ok, [#column{type = int4}], []} = pgsql:squery(C, "select * from test_table3"),
  68. {ok, [], []} = pgsql:squery(C, "drop table test_table3")
  69. end).
  70. cursor_test() ->
  71. with_connection(
  72. fun(C) ->
  73. {ok, [], []} = pgsql:squery(C, "begin"),
  74. {ok, [], []} = pgsql:squery(C, "declare c cursor for select id from test_table1"),
  75. {ok, 2} = pgsql:squery(C, "move forward 2 from c"),
  76. {ok, 1} = pgsql:squery(C, "move backward 1 from c"),
  77. {ok, 1, _Cols, [{<<"2">>}]} = pgsql:squery(C, "fetch next from c"),
  78. {ok, [], []} = pgsql:squery(C, "close c")
  79. end).
  80. multiple_result_test() ->
  81. with_connection(
  82. fun(C) ->
  83. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = pgsql:squery(C, "select 1; select 2"),
  84. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = pgsql:squery(C, "select 1; select foo;")
  85. end).
  86. extended_select_test() ->
  87. with_connection(
  88. fun(C) ->
  89. {ok, Cols, Rows} = pgsql:equery(C, "select * from test_table1", []),
  90. [#column{name = <<"id">>, type = int4, size = 4},
  91. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  92. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  93. end).
  94. extended_sync_ok_test() ->
  95. with_connection(
  96. fun(C) ->
  97. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1]),
  98. {ok, _Cols, [{<<"two">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [2])
  99. end).
  100. extended_sync_error_test() ->
  101. with_connection(
  102. fun(C) ->
  103. {error, #error{}} = pgsql:equery(C, "select _alue from test_table1 where id = $1", [1]),
  104. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1])
  105. end).
  106. returning_from_insert_test() ->
  107. with_rollback(
  108. fun(C) ->
  109. {ok, 1, _Cols, [{3}]} = pgsql:equery(C, "insert into test_table1 (id) values (3) returning id")
  110. end).
  111. returning_from_update_test() ->
  112. with_rollback(
  113. fun(C) ->
  114. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "update test_table1 set value = 'hi' returning id")
  115. end).
  116. returning_from_delete_test() ->
  117. with_rollback(
  118. fun(C) ->
  119. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "delete from test_table1 returning id")
  120. end).
  121. parse_test() ->
  122. with_connection(
  123. fun(C) ->
  124. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  125. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  126. ok = pgsql:close(C, S),
  127. ok = pgsql:sync(C)
  128. end).
  129. parse_column_format_test() ->
  130. with_connection(
  131. fun(C) ->
  132. {ok, S} = pgsql:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  133. [#column{type = int4},
  134. #column{type = bool},
  135. #column{type = float4}] = S#statement.columns,
  136. ok = pgsql:bind(C, S, []),
  137. {ok, [{1, false, 2.0}]} = pgsql:execute(C, S, 0),
  138. ok = pgsql:close(C, S),
  139. ok = pgsql:sync(C)
  140. end).
  141. parse_error_test() ->
  142. with_connection(
  143. fun(C) ->
  144. {error, #error{}} = pgsql:parse(C, "select _ from test_table1"),
  145. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  146. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  147. ok = pgsql:close(C, S),
  148. ok = pgsql:sync(C)
  149. end).
  150. parse_and_close_test() ->
  151. with_connection(
  152. fun(C) ->
  153. Parse = fun() -> pgsql:parse(C, "test", "select * from test_table1", []) end,
  154. {ok, S} = Parse(),
  155. {error, #error{code = <<"42P05">>}} = Parse(),
  156. pgsql:close(C, S),
  157. {ok, S} = Parse(),
  158. ok = pgsql:sync(C)
  159. end).
  160. bind_test() ->
  161. with_connection(
  162. fun(C) ->
  163. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  164. ok = pgsql:bind(C, S, [1]),
  165. ok = pgsql:close(C, S),
  166. ok = pgsql:sync(C)
  167. end).
  168. bind_parameter_format_test() ->
  169. with_connection(
  170. fun(C) ->
  171. {ok, S} = pgsql:parse(C, "select $1, $2, $3", [int2, text, bool]),
  172. [int2, text, bool] = S#statement.types,
  173. ok = pgsql:bind(C, S, [1, "hi", true]),
  174. {ok, [{1, <<"hi">>, true}]} = pgsql:execute(C, S, 0),
  175. ok = pgsql:close(C, S),
  176. ok = pgsql:sync(C)
  177. end).
  178. bind_error_test() ->
  179. with_connection(
  180. fun(C) ->
  181. {ok, S} = pgsql:parse(C, "select $1::char"),
  182. {error, #error{}} = pgsql:bind(C, S, [0]),
  183. ok = pgsql:bind(C, S, [$A]),
  184. ok = pgsql:close(C, S),
  185. ok = pgsql:sync(C)
  186. end).
  187. bind_and_close_test() ->
  188. with_connection(
  189. fun(C) ->
  190. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  191. ok = pgsql:bind(C, S, "one", []),
  192. {error, #error{code = <<"42P03">>}} = pgsql:bind(C, S, "one", []),
  193. ok = pgsql:close(C, portal, "one"),
  194. ok = pgsql:bind(C, S, "one", []),
  195. ok = pgsql:sync(C)
  196. end).
  197. describe_test() ->
  198. with_connection(
  199. fun(C) ->
  200. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  201. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  202. {ok, S} = pgsql:describe(C, S),
  203. ok = pgsql:close(C, S),
  204. ok = pgsql:sync(C)
  205. end).
  206. describe_with_param_test() ->
  207. with_connection(
  208. fun(C) ->
  209. {ok, S} = pgsql:parse(C, "select id from test_table1 where id = $1"),
  210. [int4] = S#statement.types,
  211. [#column{name = <<"id">>}] = S#statement.columns,
  212. {ok, S} = pgsql:describe(C, S),
  213. ok = pgsql:close(C, S),
  214. ok = pgsql:sync(C)
  215. end).
  216. describe_named_test() ->
  217. with_connection(
  218. fun(C) ->
  219. {ok, S} = pgsql:parse(C, "name", "select * from test_table1", []),
  220. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  221. {ok, S} = pgsql:describe(C, S),
  222. ok = pgsql:close(C, S),
  223. ok = pgsql:sync(C)
  224. end).
  225. describe_error_test() ->
  226. with_connection(
  227. fun(C) ->
  228. {error, #error{}} = pgsql:describe(C, statement, ""),
  229. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  230. {ok, S} = pgsql:describe(C, statement, ""),
  231. ok = pgsql:sync(C)
  232. end).
  233. portal_test() ->
  234. with_connection(
  235. fun(C) ->
  236. {ok, S} = pgsql:parse(C, "select value from test_table1"),
  237. ok = pgsql:bind(C, S, []),
  238. {partial, [{<<"one">>}]} = pgsql:execute(C, S, 1),
  239. {partial, [{<<"two">>}]} = pgsql:execute(C, S, 1),
  240. {ok, []} = pgsql:execute(C, S,1),
  241. ok = pgsql:close(C, S),
  242. ok = pgsql:sync(C)
  243. end).
  244. returning_test() ->
  245. with_rollback(
  246. fun(C) ->
  247. {ok, S} = pgsql:parse(C, "update test_table1 set value = $1 returning id"),
  248. ok = pgsql:bind(C, S, ["foo"]),
  249. {ok, 2, [{1}, {2}]} = pgsql:execute(C, S),
  250. ok = pgsql:sync(C)
  251. end).
  252. multiple_statement_test() ->
  253. with_connection(
  254. fun(C) ->
  255. {ok, S1} = pgsql:parse(C, "one", "select value from test_table1 where id = 1", []),
  256. ok = pgsql:bind(C, S1, []),
  257. {partial, [{<<"one">>}]} = pgsql:execute(C, S1, 1),
  258. {ok, S2} = pgsql:parse(C, "two", "select value from test_table1 where id = 2", []),
  259. ok = pgsql:bind(C, S2, []),
  260. {partial, [{<<"two">>}]} = pgsql:execute(C, S2, 1),
  261. {ok, []} = pgsql:execute(C, S1, 1),
  262. {ok, []} = pgsql:execute(C, S2, 1),
  263. ok = pgsql:close(C, S1),
  264. ok = pgsql:close(C, S2),
  265. ok = pgsql:sync(C)
  266. end).
  267. multiple_portal_test() ->
  268. with_connection(
  269. fun(C) ->
  270. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  271. ok = pgsql:bind(C, S, "one", [1]),
  272. ok = pgsql:bind(C, S, "two", [2]),
  273. {ok, [{<<"one">>}]} = pgsql:execute(C, S, "one", 0),
  274. {ok, [{<<"two">>}]} = pgsql:execute(C, S, "two", 0),
  275. ok = pgsql:close(C, S),
  276. ok = pgsql:sync(C)
  277. end).
  278. execute_function_test() ->
  279. with_rollback(
  280. fun(C) ->
  281. {ok, _Cols1, [{3}]} = pgsql:equery(C, "select insert_test1(3, 'three')"),
  282. {ok, _Cols2, [{<<>>}]} = pgsql:equery(C, "select do_nothing()")
  283. end).
  284. parameter_get_test() ->
  285. with_connection(
  286. fun(C) ->
  287. {ok, <<"off">>} = pgsql:get_parameter(C, "is_superuser")
  288. end).
  289. parameter_set_test() ->
  290. with_connection(
  291. fun(C) ->
  292. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'ISO, MDY'"),
  293. {ok, <<"ISO, MDY">>} = pgsql:get_parameter(C, "DateStyle"),
  294. {ok, _Cols, [{<<"2000-01-02">>}]} = pgsql:squery(C, "select '2000-01-02'::date"),
  295. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'German'"),
  296. {ok, <<"German, DMY">>} = pgsql:get_parameter(C, "DateStyle"),
  297. {ok, _Cols, [{<<"02.01.2000">>}]} = pgsql:squery(C, "select '2000-01-02'::date")
  298. end).
  299. numeric_type_test() ->
  300. check_type(int2, "1", 1, [0, 256, -32768, +32767]),
  301. check_type(int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  302. check_type(int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  303. check_type(float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  304. check_type(float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]).
  305. character_type_test() ->
  306. check_type(bpchar, "'A'", $A, [1, $1, 255], "c_char"),
  307. check_type(text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  308. check_type(varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]).
  309. date_time_type_test() ->
  310. with_connection(
  311. fun(C) ->
  312. case pgsql:get_parameter(C, "integer_datetimes") of
  313. {ok, <<"on">>} -> MaxTsDate = 294276;
  314. {ok, <<"off">>} -> MaxTsDate = 5874897
  315. end,
  316. check_type(date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  317. check_type(time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  318. check_type(timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  319. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  320. check_type(timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  321. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}]),
  322. check_type(interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  323. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  324. end).
  325. misc_type_test() ->
  326. check_type(bool, "true", true, [true, false]),
  327. check_type(bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  328. text_format_test() ->
  329. with_connection(
  330. fun(C) ->
  331. Select = fun(Type, V) ->
  332. V2 = list_to_binary(V),
  333. Query = "select $1::" ++ Type,
  334. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
  335. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
  336. end,
  337. Select("inet", "127.0.0.1"),
  338. Select("numeric", "123456")
  339. end).
  340. %% -- run all tests --
  341. run_tests() ->
  342. Files = filelib:wildcard("test_ebin/*tests.beam"),
  343. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  344. eunit:test(Mods, []).
  345. %% -- internal functions --
  346. connect_only(Args) ->
  347. {ok, C} = apply(pgsql, connect, [?host, [{port, ?port} | Args]]),
  348. pgsql:close(C),
  349. flush().
  350. with_connection(F) ->
  351. with_connection(F, []).
  352. with_connection(F, Args) ->
  353. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  354. {ok, C} = pgsql:connect(?host, "epgsql_test", Args2),
  355. try
  356. F(C)
  357. after
  358. pgsql:close(C)
  359. end,
  360. flush().
  361. with_rollback(F) ->
  362. with_connection(
  363. fun(C) ->
  364. try
  365. pgsql:squery(C, "begin"),
  366. F(C)
  367. after
  368. pgsql:squery(C, "rollback")
  369. end
  370. end).
  371. check_type(Type, In, Out, Values) ->
  372. Column = "c_" ++ atom_to_list(Type),
  373. check_type(Type, In, Out, Values, Column).
  374. check_type(Type, In, Out, Values, Column) ->
  375. with_connection(
  376. fun(C) ->
  377. Select = io_lib:format("select ~s::~w", [In, Type]),
  378. {ok, [#column{type = Type}], [{Out}]} = pgsql:equery(C, Select),
  379. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  380. {ok, #statement{columns = [#column{type = Type}]} = S} = pgsql:parse(C, Sql),
  381. Insert = fun(V) ->
  382. pgsql:bind(C, S, [V]),
  383. {ok, 1, [{V2}]} = pgsql:execute(C, S),
  384. case compare(Type, V, V2) of
  385. true -> ok;
  386. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  387. end,
  388. ok = pgsql:sync(C)
  389. end,
  390. lists:foreach(Insert, [null | Values])
  391. end).
  392. compare(_Type, null, null) -> true;
  393. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  394. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  395. compare(_Type, V1, V2) -> V1 =:= V2.
  396. %% flush mailbox
  397. flush() ->
  398. ?assertEqual([], flush([])).
  399. flush(Acc) ->
  400. receive
  401. {'EXIT', _Pid, normal} -> flush(Acc);
  402. M -> flush([M | Acc])
  403. after
  404. 0 -> lists:reverse(Acc)
  405. end.