pgsql_tests.erl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. decode_binary_format_test() ->
  292. with_connection(
  293. fun(C) ->
  294. {ok, [#column{type = unknown}], [{null}]} = pgsql:equery(C, "select null"),
  295. {ok, [#column{type = bool}], [{true}]} = pgsql:equery(C, "select true"),
  296. {ok, [#column{type = bool}], [{false}]} = pgsql:equery(C, "select false"),
  297. {ok, [#column{type = bpchar}], [{$A}]} = pgsql:equery(C, "select 'A'::char"),
  298. {ok, [#column{type = int2}], [{1}]} = pgsql:equery(C, "select 1::int2"),
  299. {ok, [#column{type = int2}], [{-1}]} = pgsql:equery(C, "select -1::int2"),
  300. {ok, [#column{type = int4}], [{1}]} = pgsql:equery(C, "select 1::int4"),
  301. {ok, [#column{type = int4}], [{-1}]} = pgsql:equery(C, "select -1::int4"),
  302. {ok, [#column{type = int8}], [{1}]} = pgsql:equery(C, "select 1::int8"),
  303. {ok, [#column{type = int8}], [{-1}]} = pgsql:equery(C, "select -1::int8"),
  304. {ok, [#column{type = float4}], [{1.0}]} = pgsql:equery(C, "select 1.0::float4"),
  305. {ok, [#column{type = float4}], [{-1.0}]} = pgsql:equery(C, "select -1.0::float4"),
  306. {ok, [#column{type = float8}], [{1.0}]} = pgsql:equery(C, "select 1.0::float8"),
  307. {ok, [#column{type = float8}], [{-1.0}]} = pgsql:equery(C, "select -1.0::float8"),
  308. {ok, [#column{type = bytea}], [{<<1, 2>>}]} = pgsql:equery(C, "select E'\001\002'::bytea"),
  309. {ok, [#column{type = text}], [{<<"hi">>}]} = pgsql:equery(C, "select 'hi'::text"),
  310. {ok, [#column{type = varchar}], [{<<"hi">>}]} = pgsql:equery(C, "select 'hi'::varchar"),
  311. {ok, [#column{type = record}], [{{1, null, <<"hi">>}}]} = pgsql:equery(C, "select (1, null, 'hi')")
  312. end).
  313. encode_binary_format_test() ->
  314. with_connection(
  315. fun(C) ->
  316. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bool) values ($1)", [null]),
  317. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bool) values ($1)", [true]),
  318. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bool) values ($1)", [false]),
  319. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_char) values ($1)", [$A]),
  320. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int2) values ($1)", [1]),
  321. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int2) values ($1)", [-1]),
  322. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int4) values ($1)", [1]),
  323. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int4) values ($1)", [-1]),
  324. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int8) values ($1)", [1]),
  325. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int8) values ($1)", [-1]),
  326. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float4) values ($1)", [1.0]),
  327. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float4) values ($1)", [-1.0]),
  328. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float8) values ($1)", [1.0]),
  329. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float8) values ($1)", [-1.0]),
  330. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bytea) values ($1)", [<<1, 2>>]),
  331. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bytea) values ($1)", [[1, 2]]),
  332. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_text) values ($1)", [<<"hi">>]),
  333. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_text) values ($1)", ["hi"]),
  334. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_varchar) values ($1)", [<<"hi">>]),
  335. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_varchar) values ($1)", ["hi"])
  336. end).
  337. text_format_test() ->
  338. with_connection(
  339. fun(C) ->
  340. Select = fun(Type, V) ->
  341. V2 = list_to_binary(V),
  342. Query = "select $1::" ++ Type,
  343. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
  344. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
  345. end,
  346. Select("timestamp", "2000-01-02 03:04:05"),
  347. Select("date", "2000-01-02"),
  348. Select("time", "03:04:05"),
  349. Select("numeric", "123456")
  350. end).
  351. %% -- run all tests --
  352. run_tests() ->
  353. Files = filelib:wildcard("test_ebin/*tests.beam"),
  354. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  355. eunit:test(Mods, []).
  356. %% -- internal functions --
  357. connect_only(Args) ->
  358. {ok, C} = apply(pgsql, connect, [?host | Args]),
  359. pgsql:close(C),
  360. flush().
  361. with_connection(F) ->
  362. {ok, C} = pgsql:connect(?host, "epgsql_test1", [{database, "epgsql_test_db1"}]),
  363. try
  364. F(C)
  365. after
  366. pgsql:close(C)
  367. end,
  368. flush().
  369. with_rollback(F) ->
  370. with_connection(
  371. fun(C) ->
  372. try
  373. pgsql:squery(C, "begin"),
  374. F(C)
  375. after
  376. pgsql:squery(C, "rollback")
  377. end
  378. end).
  379. %% flush mailbox
  380. flush() ->
  381. ?assertEqual([], flush([])).
  382. flush(Acc) ->
  383. receive
  384. {'EXIT', _Pid, normal} -> flush(Acc);
  385. M -> flush([M | Acc])
  386. after
  387. 0 -> lists:reverse(Acc)
  388. end.