pgsql_tests.erl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. delete_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, "delete from test_table1 where id > 2"),
  45. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1")
  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. multiple_result_test() ->
  56. with_connection(
  57. fun(C) ->
  58. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = pgsql:squery(C, "select 1; select 2"),
  59. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = pgsql:squery(C, "select 1; select foo;")
  60. end).
  61. extended_select_test() ->
  62. with_connection(
  63. fun(C) ->
  64. {ok, Cols, Rows} = pgsql:equery(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. extended_sync_ok_test() ->
  70. with_connection(
  71. fun(C) ->
  72. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1]),
  73. {ok, _Cols, [{<<"two">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [2])
  74. end).
  75. extended_sync_error_test() ->
  76. with_connection(
  77. fun(C) ->
  78. {error, #error{}} = pgsql:equery(C, "select _alue from test_table1 where id = $1", [1]),
  79. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1])
  80. end).
  81. parse_test() ->
  82. with_connection(
  83. fun(C) ->
  84. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  85. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  86. ok = pgsql:close(C, S),
  87. ok = pgsql:sync(C)
  88. end).
  89. parse_column_format_test() ->
  90. with_connection(
  91. fun(C) ->
  92. {ok, S} = pgsql:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  93. [#column{type = int4},
  94. #column{type = bool},
  95. #column{type = float4}] = S#statement.columns,
  96. ok = pgsql:bind(C, S, []),
  97. {ok, [{1, false, 2.0}]} = pgsql:execute(C, S, 0),
  98. ok = pgsql:close(C, S),
  99. ok = pgsql:sync(C)
  100. end).
  101. parse_error_test() ->
  102. with_connection(
  103. fun(C) ->
  104. {error, #error{}} = pgsql:parse(C, "select _ from test_table1"),
  105. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  106. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  107. ok = pgsql:close(C, S),
  108. ok = pgsql:sync(C)
  109. end).
  110. parse_and_close_test() ->
  111. with_connection(
  112. fun(C) ->
  113. Parse = fun() -> pgsql:parse(C, "test", "select * from test_table1", []) end,
  114. {ok, S} = Parse(),
  115. {error, #error{code = <<"42P05">>}} = Parse(),
  116. pgsql:close(C, S),
  117. {ok, S} = Parse(),
  118. ok = pgsql:sync(C)
  119. end).
  120. bind_test() ->
  121. with_connection(
  122. fun(C) ->
  123. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  124. ok = pgsql:bind(C, S, [1]),
  125. ok = pgsql:close(C, S),
  126. ok = pgsql:sync(C)
  127. end).
  128. bind_parameter_format_test() ->
  129. with_connection(
  130. fun(C) ->
  131. {ok, S} = pgsql:parse(C, "select $1, $2, $3", [int2, text, bool]),
  132. [int2, text, bool] = S#statement.types,
  133. ok = pgsql:bind(C, S, [1, "hi", true]),
  134. {ok, [{1, <<"hi">>, true}]} = pgsql:execute(C, S, 0),
  135. ok = pgsql:close(C, S),
  136. ok = pgsql:sync(C)
  137. end).
  138. bind_error_test() ->
  139. with_connection(
  140. fun(C) ->
  141. {ok, S} = pgsql:parse(C, "select $1::char"),
  142. {error, #error{}} = pgsql:bind(C, S, [0]),
  143. ok = pgsql:bind(C, S, [$A]),
  144. ok = pgsql:close(C, S),
  145. ok = pgsql:sync(C)
  146. end).
  147. bind_and_close_test() ->
  148. with_connection(
  149. fun(C) ->
  150. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  151. ok = pgsql:bind(C, S, "one", []),
  152. {error, #error{code = <<"42P03">>}} = pgsql:bind(C, S, "one", []),
  153. ok = pgsql:close(C, portal, "one"),
  154. ok = pgsql:bind(C, S, "one", []),
  155. ok = pgsql:sync(C)
  156. end).
  157. describe_test() ->
  158. with_connection(
  159. fun(C) ->
  160. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  161. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  162. {ok, S} = pgsql:describe(C, S),
  163. ok = pgsql:close(C, S),
  164. ok = pgsql:sync(C)
  165. end).
  166. describe_with_param_test() ->
  167. with_connection(
  168. fun(C) ->
  169. {ok, S} = pgsql:parse(C, "select id from test_table1 where id = $1"),
  170. [int4] = S#statement.types,
  171. [#column{name = <<"id">>}] = S#statement.columns,
  172. {ok, S} = pgsql:describe(C, S),
  173. ok = pgsql:close(C, S),
  174. ok = pgsql:sync(C)
  175. end).
  176. describe_named_test() ->
  177. with_connection(
  178. fun(C) ->
  179. {ok, S} = pgsql:parse(C, "name", "select * from test_table1", []),
  180. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  181. {ok, S} = pgsql:describe(C, S),
  182. ok = pgsql:close(C, S),
  183. ok = pgsql:sync(C)
  184. end).
  185. describe_error_test() ->
  186. with_connection(
  187. fun(C) ->
  188. {error, #error{}} = pgsql:describe(C, statement, ""),
  189. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  190. {ok, S} = pgsql:describe(C, statement, ""),
  191. ok = pgsql:sync(C)
  192. end).
  193. portal_test() ->
  194. with_connection(
  195. fun(C) ->
  196. {ok, S} = pgsql:parse(C, "select value from test_table1"),
  197. ok = pgsql:bind(C, S, []),
  198. {partial, [{<<"one">>}]} = pgsql:execute(C, S, 1),
  199. {partial, [{<<"two">>}]} = pgsql:execute(C, S, 1),
  200. {ok, []} = pgsql:execute(C, S,1),
  201. ok = pgsql:close(C, S),
  202. ok = pgsql:sync(C)
  203. end).
  204. multiple_statement_test() ->
  205. with_connection(
  206. fun(C) ->
  207. {ok, S1} = pgsql:parse(C, "one", "select value from test_table1 where id = 1", []),
  208. ok = pgsql:bind(C, S1, []),
  209. {partial, [{<<"one">>}]} = pgsql:execute(C, S1, 1),
  210. {ok, S2} = pgsql:parse(C, "two", "select value from test_table1 where id = 2", []),
  211. ok = pgsql:bind(C, S2, []),
  212. {partial, [{<<"two">>}]} = pgsql:execute(C, S2, 1),
  213. {ok, []} = pgsql:execute(C, S1, 1),
  214. {ok, []} = pgsql:execute(C, S2, 1),
  215. ok = pgsql:close(C, S1),
  216. ok = pgsql:close(C, S2),
  217. ok = pgsql:sync(C)
  218. end).
  219. multiple_portal_test() ->
  220. with_connection(
  221. fun(C) ->
  222. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  223. ok = pgsql:bind(C, S, "one", [1]),
  224. ok = pgsql:bind(C, S, "two", [2]),
  225. {ok, [{<<"one">>}]} = pgsql:execute(C, S, "one", 0),
  226. {ok, [{<<"two">>}]} = pgsql:execute(C, S, "two", 0),
  227. ok = pgsql:close(C, S),
  228. ok = pgsql:sync(C)
  229. end).
  230. execute_function_test() ->
  231. with_rollback(
  232. fun(C) ->
  233. {ok, _Cols1, [{3}]} = pgsql:equery(C, "select insert_test1(3, 'three')"),
  234. {ok, _Cols2, [{<<>>}]} = pgsql:equery(C, "select do_nothing()")
  235. end).
  236. parameter_get_test() ->
  237. with_connection(
  238. fun(C) ->
  239. {ok, <<"off">>} = pgsql:get_parameter(C, "integer_datetimes")
  240. end).
  241. parameter_set_test() ->
  242. with_connection(
  243. fun(C) ->
  244. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'ISO, MDY'"),
  245. {ok, <<"ISO, MDY">>} = pgsql:get_parameter(C, "DateStyle"),
  246. {ok, _Cols, [{<<"2000-01-02">>}]} = pgsql:squery(C, "select '2000-01-02'::date"),
  247. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'German'"),
  248. {ok, <<"German, DMY">>} = pgsql:get_parameter(C, "DateStyle"),
  249. {ok, _Cols, [{<<"02.01.2000">>}]} = pgsql:squery(C, "select '2000-01-02'::date")
  250. end).
  251. decode_binary_format_test() ->
  252. with_connection(
  253. fun(C) ->
  254. {ok, [#column{type = unknown}], [{null}]} = pgsql:equery(C, "select null"),
  255. {ok, [#column{type = bool}], [{true}]} = pgsql:equery(C, "select true"),
  256. {ok, [#column{type = bool}], [{false}]} = pgsql:equery(C, "select false"),
  257. {ok, [#column{type = bpchar}], [{$A}]} = pgsql:equery(C, "select 'A'::char"),
  258. {ok, [#column{type = int2}], [{1}]} = pgsql:equery(C, "select 1::int2"),
  259. {ok, [#column{type = int2}], [{-1}]} = pgsql:equery(C, "select -1::int2"),
  260. {ok, [#column{type = int4}], [{1}]} = pgsql:equery(C, "select 1::int4"),
  261. {ok, [#column{type = int4}], [{-1}]} = pgsql:equery(C, "select -1::int4"),
  262. {ok, [#column{type = int8}], [{1}]} = pgsql:equery(C, "select 1::int8"),
  263. {ok, [#column{type = int8}], [{-1}]} = pgsql:equery(C, "select -1::int8"),
  264. {ok, [#column{type = float4}], [{1.0}]} = pgsql:equery(C, "select 1.0::float4"),
  265. {ok, [#column{type = float4}], [{-1.0}]} = pgsql:equery(C, "select -1.0::float4"),
  266. {ok, [#column{type = float8}], [{1.0}]} = pgsql:equery(C, "select 1.0::float8"),
  267. {ok, [#column{type = float8}], [{-1.0}]} = pgsql:equery(C, "select -1.0::float8"),
  268. {ok, [#column{type = bytea}], [{<<1, 2>>}]} = pgsql:equery(C, "select E'\001\002'::bytea"),
  269. {ok, [#column{type = text}], [{<<"hi">>}]} = pgsql:equery(C, "select 'hi'::text"),
  270. {ok, [#column{type = varchar}], [{<<"hi">>}]} = pgsql:equery(C, "select 'hi'::varchar"),
  271. {ok, [#column{type = record}], [{{1, null, <<"hi">>}}]} = pgsql:equery(C, "select (1, null, 'hi')")
  272. end).
  273. encode_binary_format_test() ->
  274. with_connection(
  275. fun(C) ->
  276. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bool) values ($1)", [null]),
  277. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bool) values ($1)", [true]),
  278. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bool) values ($1)", [false]),
  279. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_char) values ($1)", [$A]),
  280. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int2) values ($1)", [1]),
  281. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int2) values ($1)", [-1]),
  282. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int4) values ($1)", [1]),
  283. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int4) values ($1)", [-1]),
  284. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int8) values ($1)", [1]),
  285. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_int8) values ($1)", [-1]),
  286. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float4) values ($1)", [1.0]),
  287. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float4) values ($1)", [-1.0]),
  288. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float8) values ($1)", [1.0]),
  289. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_float8) values ($1)", [-1.0]),
  290. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bytea) values ($1)", [<<1, 2>>]),
  291. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_bytea) values ($1)", [[1, 2]]),
  292. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_text) values ($1)", [<<"hi">>]),
  293. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_text) values ($1)", ["hi"]),
  294. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_varchar) values ($1)", [<<"hi">>]),
  295. {ok, 1} = pgsql:equery(C, "insert into test_table2 (c_varchar) values ($1)", ["hi"])
  296. end).
  297. %% -- run all tests --
  298. run_tests() ->
  299. crypto:start(),
  300. Files = filelib:wildcard("test_ebin/*tests.beam"),
  301. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  302. eunit:test(Mods, []).
  303. %% -- internal functions --
  304. connect_only(Args) ->
  305. {ok, C} = apply(pgsql, connect, [?host | Args]),
  306. pgsql:close(C),
  307. flush().
  308. with_connection(F) ->
  309. {ok, C} = pgsql:connect(?host, "epgsql_test1", [{database, "epgsql_test_db1"}]),
  310. try
  311. F(C)
  312. after
  313. pgsql:close(C)
  314. end,
  315. flush().
  316. with_rollback(F) ->
  317. with_connection(
  318. fun(C) ->
  319. try
  320. pgsql:squery(C, "begin"),
  321. F(C)
  322. after
  323. pgsql:squery(C, "rollback")
  324. end
  325. end).
  326. %% flush mailbox
  327. flush() ->
  328. ?assertEqual([], flush([])).
  329. flush(Acc) ->
  330. receive
  331. {'EXIT', _Pid, normal} -> flush(Acc);
  332. M -> flush([M | Acc])
  333. after
  334. 0 -> lists:reverse(Acc)
  335. end.