mysql_tests.erl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2014 Viktor Söderqvist
  3. %%
  4. %% This file is part of MySQL/OTP.
  5. %%
  6. %% MySQL/OTP is free software: you can redistribute it and/or modify it under
  7. %% the terms of the GNU Lesser General Public License as published by the Free
  8. %% Software Foundation, either version 3 of the License, or (at your option)
  9. %% any later version.
  10. %%
  11. %% This program is distributed in the hope that it will be useful, but WITHOUT
  12. %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. %% more details.
  15. %%
  16. %% You should have received a copy of the GNU Lesser General Public License
  17. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. %% @doc This module performs test to an actual database.
  19. -module(mysql_tests).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -define(user, "otptest").
  22. -define(password, "otptest").
  23. -define(create_table_t, <<"CREATE TABLE t ("
  24. " id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,"
  25. " bl BLOB,"
  26. " tx TEXT NOT NULL," %% No default value
  27. " f FLOAT,"
  28. " dc DECIMAL(5,3),"
  29. " ti TIME,"
  30. " ts TIMESTAMP,"
  31. " da DATE,"
  32. " c CHAR(2)"
  33. ") ENGINE=InnoDB">>).
  34. connect_test() ->
  35. %% A connection with a registered name
  36. Options = [{name, {local, tardis}}, {user, ?user}, {password, ?password}],
  37. {ok, Pid} = mysql:start_link(Options),
  38. %% Test some gen_server callbacks not tested elsewhere
  39. State = get_state(Pid),
  40. ?assertMatch({ok, State}, mysql:code_change("0.1.0", State, [])),
  41. ?assertMatch({error, _}, mysql:code_change("2.0.0", unknown_state, [])),
  42. exit(whereis(tardis), normal).
  43. %% For R16B where sys:get_state/1 is not available.
  44. get_state(Process) ->
  45. {status,_,_,[_,_,_,_,Misc]} = sys:get_status(Process),
  46. hd([State || {data,[{"State", State}]} <- Misc]).
  47. query_test_() ->
  48. {setup,
  49. fun () ->
  50. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
  51. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  52. ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  53. ok = mysql:query(Pid, <<"USE otptest">>),
  54. ok = mysql:query(Pid, <<"SET autocommit = 1">>),
  55. Pid
  56. end,
  57. fun (Pid) ->
  58. ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
  59. exit(Pid, normal)
  60. end,
  61. {with, [fun connect_with_db/1,
  62. fun autocommit/1,
  63. fun basic_queries/1,
  64. fun text_protocol/1,
  65. fun binary_protocol/1,
  66. fun float_rounding/1,
  67. fun decimal/1,
  68. fun int/1,
  69. fun bit/1,
  70. fun time/1,
  71. fun microseconds/1]}}.
  72. connect_with_db(_Pid) ->
  73. %% Make another connection and set the db in the handshake phase
  74. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password},
  75. {database, "otptest"}]),
  76. ?assertMatch({ok, _, [[<<"otptest">>]]},
  77. mysql:query(Pid, "SELECT DATABASE()")),
  78. exit(Pid, normal).
  79. autocommit(Pid) ->
  80. ?assert(mysql:autocommit(Pid)),
  81. ok = mysql:query(Pid, <<"SET autocommit = 0">>),
  82. ?assertNot(mysql:autocommit(Pid)),
  83. ok = mysql:query(Pid, <<"SET autocommit = 1">>),
  84. ?assert(mysql:autocommit(Pid)).
  85. basic_queries(Pid) ->
  86. %% warning count
  87. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  88. ?assertEqual(1, mysql:warning_count(Pid)),
  89. %% SQL parse error
  90. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  91. mysql:query(Pid, <<"FOO">>)),
  92. %% Simple resultset with various types
  93. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  94. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  95. ok.
  96. text_protocol(Pid) ->
  97. ok = mysql:query(Pid, ?create_table_t),
  98. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  99. " VALUES ('blob', 3.14, 3.14, '00:22:11',"
  100. " '2014-11-03 00:22:24', '2014-11-03',"
  101. " NULL)">>),
  102. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  103. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  104. ?assertEqual(1, mysql:affected_rows(Pid)),
  105. %% select
  106. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  107. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  108. <<"ts">>, <<"da">>, <<"c">>], Columns),
  109. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, 3.14, {0, {0, 22, 11}},
  110. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  111. Rows),
  112. %% TODO:
  113. %% * More types: BIT, SET, ENUM, GEOMETRY
  114. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  115. binary_protocol(Pid) ->
  116. ok = mysql:query(Pid, ?create_table_t),
  117. %% The same queries as in the text protocol. Expect the same results.
  118. {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  119. " VALUES (?, ?, ?, ?, ?, ?, ?)">>),
  120. ok = mysql:execute(Pid, Ins, [<<"blob">>, 3.14, 3.14,
  121. {0, {0, 22, 11}},
  122. {{2014, 11, 03}, {0, 22, 24}},
  123. {2014, 11, 03}, null]),
  124. %% TODO: Put the expected result in a macro to make sure they are identical
  125. %% for the text and the binary protocol tests.
  126. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t WHERE id=?">>),
  127. {ok, Columns, Rows} = mysql:execute(Pid, Stmt, [1]),
  128. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  129. <<"ts">>, <<"da">>, <<"c">>], Columns),
  130. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, 3.14,
  131. {0, {0, 22, 11}},
  132. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  133. Rows),
  134. %% TODO: Both send and receive the following values:
  135. %% * Values for all types
  136. %% * Negative numbers for all integer types
  137. %% * Integer overflow
  138. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  139. float_rounding(Pid) ->
  140. %% This is to make sure we get the same values for 32-bit FLOATs in the text
  141. %% and binary protocols for ordinary queries and prepared statements
  142. %% respectively.
  143. %%
  144. %% MySQL rounds to 6 significant digits when "printing" floats over the
  145. %% text protocol. When we receive a float on the binary protocol, we round
  146. %% it in the same way to match what MySQL does on the text protocol. This
  147. %% way we should to get the same values regardless of which protocol is
  148. %% used.
  149. %% Table for testing floats
  150. ok = mysql:query(Pid, "CREATE TABLE f (f FLOAT)"),
  151. %% Prepared statements
  152. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO f (f) VALUES (?)"),
  153. {ok, Select} = mysql:prepare(Pid, "SELECT f FROM f"),
  154. %% [{Input, Expected}]
  155. TestData = [{1.0, 1.0}, {3.14, 3.14}, {0.2, 0.2},
  156. {0.20082111, 0.200821}, {0.000123456789, 0.000123457},
  157. {33.3333333, 33.3333}, {-33.2233443322, -33.2233},
  158. {400.0123, 400.012}, {1000.1234, 1000.12},
  159. {999.00009, 999.0},
  160. {1234.5678, 1234.57}, {68888.8888, 68888.9},
  161. {123456.789, 123457.0}, {7654321.0, 7654320.0},
  162. {80001111.1, 80001100.0}, {987654321.0, 987654000.0},
  163. {-123456789.0, -123457000.0},
  164. {2.12345111e-23, 2.12345e-23}, {-2.12345111e-23, -2.12345e-23},
  165. {2.12345111e23, 2.12345e23}, {-2.12345111e23, -2.12345e23}],
  166. lists:foreach(fun ({Input, Expected}) ->
  167. %% Insert using binary protocol (sending it as a double)
  168. ok = mysql:execute(Pid, Insert, [Input]),
  169. %% Text (plain query)
  170. {ok, _, [[Value]]} = mysql:query(Pid, "SELECT f FROM f"),
  171. ?assertEqual(Expected, Value),
  172. %% Binary (prepared statement)
  173. {ok, _, [[BinValue]]} = mysql:execute(Pid, Select, []),
  174. ?assertEqual(Expected, BinValue),
  175. %% cleanup before the next test
  176. ok = mysql:query(Pid, "DELETE FROM f")
  177. end,
  178. TestData),
  179. ok = mysql:query(Pid, "DROP TABLE f").
  180. decimal(Pid) ->
  181. %% As integer when S == 0
  182. ok = mysql:query(Pid, "CREATE TABLE dec0 (d DECIMAL(50, 0))"),
  183. write_read_text_binary(
  184. Pid, 14159265358979323846264338327950288419716939937510,
  185. <<"14159265358979323846264338327950288419716939937510">>,
  186. <<"dec0">>, <<"d">>
  187. ),
  188. write_read_text_binary(
  189. Pid, -14159265358979323846264338327950288419716939937510,
  190. <<"-14159265358979323846264338327950288419716939937510">>,
  191. <<"dec0">>, <<"d">>
  192. ),
  193. ok = mysql:query(Pid, "DROP TABLE dec0"),
  194. %% As float when P =< 15, S > 0
  195. ok = mysql:query(Pid, "CREATE TABLE dec15 (d DECIMAL(15, 14))"),
  196. write_read_text_binary(Pid, 3.14159265358979, <<"3.14159265358979">>,
  197. <<"dec15">>, <<"d">>),
  198. write_read_text_binary(Pid, -3.14159265358979, <<"-3.14159265358979">>,
  199. <<"dec15">>, <<"d">>),
  200. write_read_text_binary(Pid, 3.0, <<"3">>, <<"dec15">>, <<"d">>),
  201. ok = mysql:query(Pid, "DROP TABLE dec15"),
  202. %% As binary when P >= 16, S > 0
  203. ok = mysql:query(Pid, "CREATE TABLE dec16 (d DECIMAL(16, 15))"),
  204. write_read_text_binary(Pid, <<"3.141592653589793">>,
  205. <<"3.141592653589793">>, <<"dec16">>, <<"d">>),
  206. write_read_text_binary(Pid, <<"-3.141592653589793">>,
  207. <<"-3.141592653589793">>, <<"dec16">>, <<"d">>),
  208. write_read_text_binary(Pid, <<"3.000000000000000">>, <<"3">>,
  209. <<"dec16">>, <<"d">>),
  210. ok = mysql:query(Pid, "DROP TABLE dec16").
  211. int(Pid) ->
  212. ok = mysql:query(Pid, "CREATE TABLE ints (i INT)"),
  213. write_read_text_binary(Pid, 42, <<"42">>, <<"ints">>, <<"i">>),
  214. write_read_text_binary(Pid, -42, <<"-42">>, <<"ints">>, <<"i">>),
  215. write_read_text_binary(Pid, 987654321, <<"987654321">>, <<"ints">>,
  216. <<"i">>),
  217. write_read_text_binary(Pid, -987654321, <<"-987654321">>,
  218. <<"ints">>, <<"i">>),
  219. ok = mysql:query(Pid, "DROP TABLE ints"),
  220. %% Overflow with TINYINT
  221. ok = mysql:query(Pid, "CREATE TABLE tint (i TINYINT)"),
  222. write_read_text_binary(Pid, 127, <<"1000">>, <<"tint">>, <<"i">>),
  223. write_read_text_binary(Pid, -128, <<"-1000">>, <<"tint">>, <<"i">>),
  224. ok = mysql:query(Pid, "DROP TABLE tint").
  225. %% The BIT(N) datatype in MySQL 5.0.3 and later: the equivallent to bitstring()
  226. bit(Pid) ->
  227. ok = mysql:query(Pid, "CREATE TABLE bits (b BIT(11))"),
  228. write_read_text_binary(Pid, <<16#ff, 0:3>>, <<"b'11111111000'">>,
  229. <<"bits">>, <<"b">>),
  230. write_read_text_binary(Pid, <<16#7f, 6:3>>, <<"b'01111111110'">>,
  231. <<"bits">>, <<"b">>),
  232. ok = mysql:query(Pid, "DROP TABLE bits").
  233. %% Test TIME value representation. There are a few things to check.
  234. time(Pid) ->
  235. ok = mysql:query(Pid, "CREATE TABLE tm (tm TIME)"),
  236. lists:foreach(
  237. fun ({Value, SqlLiteral}) ->
  238. write_read_text_binary(Pid, Value, SqlLiteral, <<"tm">>, <<"tm">>)
  239. end,
  240. [{{0, {10, 11, 12}}, <<"'10:11:12'">>},
  241. {{5, {0, 0, 1}}, <<"'120:00:01'">>},
  242. {{-1, {23, 59, 59}}, <<"'-00:00:01'">>},
  243. {{-1, {23, 59, 0}}, <<"'-00:01:00'">>},
  244. {{-1, {23, 0, 0}}, <<"'-01:00:00'">>},
  245. {{-1, {0, 0, 0}}, <<"'-24:00:00'">>},
  246. {{-5, {10, 0, 0}}, <<"'-110:00:00'">>}]
  247. ),
  248. ok = mysql:query(Pid, "DROP TABLE tm").
  249. microseconds(Pid) ->
  250. %% Check whether we have the required version for this testcase.
  251. {ok, _, [[Version]]} = mysql:query(Pid, <<"SELECT @@version">>),
  252. try
  253. %% Remove stuff after dash for e.g. "5.5.40-0ubuntu0.12.04.1-log"
  254. [Version1 | _] = binary:split(Version, <<"-">>),
  255. Version2 = lists:map(fun binary_to_integer/1,
  256. binary:split(Version1, <<".">>, [global])),
  257. Version2 >= [5, 6, 4] orelse throw(nope)
  258. of _ ->
  259. test_time_microseconds(Pid),
  260. test_datetime_microseconds(Pid)
  261. catch _:_ ->
  262. error_logger:info_msg("Skipping microseconds test. Current MySQL"
  263. " version is ~s. Required version is >= 5.6.4.~n",
  264. [Version])
  265. end.
  266. test_time_microseconds(Pid) ->
  267. ok = mysql:query(Pid, "CREATE TABLE m (t TIME(6))"),
  268. %% Positive time
  269. write_read_text_binary(Pid, {0, {23, 59, 57.654321}},
  270. <<"'23:59:57.654321'">>, <<"m">>, <<"t">>),
  271. %% Negative time
  272. write_read_text_binary(Pid, {-1, {23, 59, 57.654321}},
  273. <<"'-00:00:02.345679'">>, <<"m">>, <<"t">>),
  274. ok = mysql:query(Pid, "DROP TABLE m").
  275. test_datetime_microseconds(Pid) ->
  276. ok = mysql:query(Pid, "CREATE TABLE dt (dt DATETIME(6))"),
  277. write_read_text_binary(Pid, {{2014, 11, 23}, {23, 59, 57.654321}},
  278. <<"'2014-11-23 23:59:57.654321'">>, <<"dt">>,
  279. <<"dt">>),
  280. ok = mysql:query(Pid, "DROP TABLE dt").
  281. %% @doc Tests write and read in text and the binary protocol, all combinations.
  282. %% This helper function assumes an empty table with a single column.
  283. write_read_text_binary(Conn, Term, SqlLiteral, Table, Column) ->
  284. SelectQuery = <<"SELECT ", Column/binary, " FROM ", Table/binary>>,
  285. {ok, SelectStmt} = mysql:prepare(Conn, SelectQuery),
  286. %% Insert as text, read text and binary, delete
  287. InsertQuery = <<"INSERT INTO ", Table/binary, " (", Column/binary, ")"
  288. " VALUES (", SqlLiteral/binary, ")">>,
  289. ok = mysql:query(Conn, InsertQuery),
  290. ?assertEqual({ok, [Column], [[Term]]}, mysql:query(Conn, SelectQuery)),
  291. ?assertEqual({ok, [Column], [[Term]]}, mysql:execute(Conn, SelectStmt, [])),
  292. mysql:query(Conn, <<"DELETE FROM ", Table/binary>>),
  293. %% Insert as binary, read text and binary, delete
  294. InsertQ = <<"INSERT INTO ", Table/binary, " (", Column/binary, ")",
  295. " VALUES (?)">>,
  296. {ok, InsertStmt} = mysql:prepare(Conn, InsertQ),
  297. ok = mysql:execute(Conn, InsertStmt, [Term]),
  298. ok = mysql:unprepare(Conn, InsertStmt),
  299. ?assertEqual({ok, [Column], [[Term]]}, mysql:query(Conn, SelectQuery)),
  300. ?assertEqual({ok, [Column], [[Term]]}, mysql:execute(Conn, SelectStmt, [])),
  301. mysql:query(Conn, <<"DELETE FROM ", Table/binary>>),
  302. %% Cleanup
  303. ok = mysql:unprepare(Conn, SelectStmt).
  304. %% --------------------------------------------------------------------------
  305. timeout_test_() ->
  306. {setup,
  307. fun () ->
  308. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
  309. Pid
  310. end,
  311. fun (Pid) ->
  312. exit(Pid, normal)
  313. end,
  314. {with, [fun (Pid) ->
  315. %% SLEEP was added in MySQL 5.0.12
  316. ?assertEqual({ok, [<<"SLEEP(5)">>], [[1]]},
  317. mysql:query(Pid, <<"SELECT SLEEP(5)">>, 40)),
  318. %% A query after an interrupted query shouldn't get a timeout.
  319. ?assertMatch({ok,[<<"42">>], [[42]]},
  320. mysql:query(Pid, <<"SELECT 42">>)),
  321. %% Parametrized query
  322. ?assertEqual({ok, [<<"SLEEP(?)">>], [[1]]},
  323. mysql:query(Pid, <<"SELECT SLEEP(?)">>, [5], 40)),
  324. %% Prepared statement
  325. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT SLEEP(?)">>),
  326. ?assertEqual({ok, [<<"SLEEP(?)">>], [[1]]},
  327. mysql:execute(Pid, Stmt, [5], 40)),
  328. ok = mysql:unprepare(Pid, Stmt)
  329. end]}}.
  330. %% --------------------------------------------------------------------------
  331. %% Prepared statements and transactions
  332. with_table_foo_test_() ->
  333. {setup,
  334. fun () ->
  335. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password},
  336. {query_cache_time, 50}]),
  337. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  338. ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  339. ok = mysql:query(Pid, <<"USE otptest">>),
  340. ok = mysql:query(Pid, <<"CREATE TABLE foo (bar INT) engine=InnoDB">>),
  341. Pid
  342. end,
  343. fun (Pid) ->
  344. ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
  345. exit(Pid, normal)
  346. end,
  347. {with, [fun prepared_statements/1,
  348. fun parameterized_query/1,
  349. fun transaction_simple_success/1,
  350. fun transaction_simple_aborted/1]}}.
  351. prepared_statements(Pid) ->
  352. %% Unnamed
  353. ?assertEqual({error,{1146, <<"42S02">>,
  354. <<"Table 'otptest.tab' doesn't exist">>}},
  355. mysql:prepare(Pid, "SELECT * FROM tab WHERE id = ?")),
  356. {ok, StmtId} = mysql:prepare(Pid, "SELECT * FROM foo WHERE bar = ?"),
  357. ?assert(is_integer(StmtId)),
  358. ?assertEqual(ok, mysql:unprepare(Pid, StmtId)),
  359. ?assertEqual({error, not_prepared}, mysql:unprepare(Pid, StmtId)),
  360. %% Named
  361. ?assertEqual({error,{1146, <<"42S02">>,
  362. <<"Table 'otptest.tab' doesn't exist">>}},
  363. mysql:prepare(Pid, tab, "SELECT * FROM tab WHERE id = ?")),
  364. ?assertEqual({ok, foo},
  365. mysql:prepare(Pid, foo, "SELECT * FROM foo WHERE bar = ?")),
  366. %% Prepare again unprepares the old stmt associated with this name.
  367. ?assertEqual({ok, foo},
  368. mysql:prepare(Pid, foo, "SELECT bar FROM foo WHERE bar = ?")),
  369. ?assertEqual(ok, mysql:unprepare(Pid, foo)),
  370. ?assertEqual({error, not_prepared}, mysql:unprepare(Pid, foo)),
  371. %% Execute when not prepared
  372. ?assertEqual({error, not_prepared}, mysql:execute(Pid, not_a_stmt, [])),
  373. ok.
  374. parameterized_query(Conn) ->
  375. %% To see that cache eviction works as expected, look at the code coverage.
  376. {ok, _, []} = mysql:query(Conn, "SELECT * FROM foo WHERE bar = ?", [1]),
  377. {ok, _, []} = mysql:query(Conn, "SELECT * FROM foo WHERE bar = ?", [2]),
  378. receive after 150 -> ok end, %% Now the query cache should emptied
  379. {ok, _, []} = mysql:query(Conn, "SELECT * FROM foo WHERE bar = ?", [3]).
  380. transaction_simple_success(Pid) ->
  381. ?assertNot(mysql:in_transaction(Pid)),
  382. Result = mysql:transaction(Pid, fun () ->
  383. ok = mysql:query(Pid, "INSERT INTO foo VALUES (42)"),
  384. ?assert(mysql:in_transaction(Pid)),
  385. hello
  386. end),
  387. ?assertEqual({atomic, hello}, Result),
  388. ?assertNot(mysql:in_transaction(Pid)),
  389. ok = mysql:query(Pid, "DELETE FROM foo").
  390. transaction_simple_aborted(Pid) ->
  391. ok = mysql:query(Pid, "INSERT INTO foo VALUES (9)"),
  392. ?assertEqual({ok, [<<"bar">>], [[9]]},
  393. mysql:query(Pid, "SELECT bar FROM foo")),
  394. Result = mysql:transaction(Pid, fun () ->
  395. ok = mysql:query(Pid, "INSERT INTO foo VALUES (42)"),
  396. ?assertMatch({ok, _, [[2]]},
  397. mysql:query(Pid, "SELECT COUNT(*) FROM foo")),
  398. error(hello)
  399. end),
  400. ?assertMatch({aborted, {hello, Stacktrace}} when is_list(Stacktrace),
  401. Result),
  402. ?assertEqual({ok, [<<"bar">>], [[9]]},
  403. mysql:query(Pid, "SELECT bar FROM foo")),
  404. ok = mysql:query(Pid, "DELETE FROM foo"),
  405. %% Also check the abort Reason for throw and exit.
  406. ?assertEqual({aborted, {throw, foo}},
  407. mysql:transaction(Pid, fun () -> throw(foo) end)),
  408. ?assertEqual({aborted, foo},
  409. mysql:transaction(Pid, fun () -> exit(foo) end)).
  410. %% --- simple gen_server callbacks ---
  411. gen_server_coverage_test() ->
  412. {noreply, state} = mysql:handle_cast(foo, state),
  413. {noreply, state} = mysql:handle_info(foo, state),
  414. ok = mysql:terminate(kill, state).