pgsql_tests.erl 16 KB

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