pgsql_tests.erl 16 KB

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