mysql_tests.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
  36. exit(Pid, normal).
  37. query_test_() ->
  38. {setup,
  39. fun () ->
  40. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
  41. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  42. ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  43. ok = mysql:query(Pid, <<"USE otptest">>),
  44. Pid
  45. end,
  46. fun (Pid) ->
  47. ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
  48. exit(Pid, normal)
  49. end,
  50. {with, [fun basic_queries/1,
  51. fun text_protocol/1,
  52. fun binary_protocol/1,
  53. fun float_rounding/1,
  54. fun time/1,
  55. fun microseconds/1]}}.
  56. basic_queries(Pid) ->
  57. %% warning count
  58. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  59. ?assertEqual(1, mysql:warning_count(Pid)),
  60. %% SQL parse error
  61. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  62. mysql:query(Pid, <<"FOO">>)),
  63. %% Simple resultset with various types
  64. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  65. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  66. ok.
  67. text_protocol(Pid) ->
  68. ok = mysql:query(Pid, ?create_table_t),
  69. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  70. " VALUES ('blob', 3.14, 3.14, '00:22:11',"
  71. " '2014-11-03 00:22:24', '2014-11-03',"
  72. " NULL)">>),
  73. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  74. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  75. ?assertEqual(1, mysql:affected_rows(Pid)),
  76. %% select
  77. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  78. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  79. <<"ts">>, <<"da">>, <<"c">>], Columns),
  80. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>, {0, {0, 22, 11}},
  81. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  82. Rows),
  83. %% TODO:
  84. %% * More types: BIT, SET, ENUM, GEOMETRY
  85. %% * TIME with negative hours
  86. %% * TIME with more than 2 digits in hour.
  87. %% * TIME with microseconds
  88. %% * Negative TIME
  89. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  90. binary_protocol(Pid) ->
  91. ok = mysql:query(Pid, ?create_table_t),
  92. %% The same queries as in the text protocol. Expect the same results.
  93. {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  94. " VALUES (?, ?, ?, ?, ?, ?, ?)">>),
  95. ok = mysql:execute(Pid, Ins, [<<"blob">>, 3.14, <<"3.14">>,
  96. {0, {0, 22, 11}},
  97. {{2014, 11, 03}, {0, 22, 24}},
  98. {2014, 11, 03}, null]),
  99. %% TODO: Put the expected result in a macro to make sure they are identical
  100. %% for the text and the binary protocol tests.
  101. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t WHERE id=?">>),
  102. {ok, Columns, Rows} = mysql:execute(Pid, Stmt, [1]),
  103. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  104. <<"ts">>, <<"da">>, <<"c">>], Columns),
  105. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>,
  106. {0, {0, 22, 11}},
  107. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  108. Rows),
  109. %% TODO: Both send and receive the following values:
  110. %% * Values for all types
  111. %% * Negative numbers for all integer types
  112. %% * Integer overflow
  113. %% * TIME with more than 2 digits in hour.
  114. %% * TIME with microseconds
  115. %% * Negative TIME
  116. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  117. float_rounding(Pid) ->
  118. %% This is to make sure we get the same values for 32-bit FLOATs in the text
  119. %% and binary protocols for ordinary queries and prepared statements
  120. %% respectively.
  121. %%
  122. %% MySQL rounds to 6 significant digits when "printing" floats over the
  123. %% text protocol. When we receive a float on the binary protocol, we round
  124. %% it in the same way to match what MySQL does on the text protocol. This
  125. %% way we should to get the same values regardless of which protocol is
  126. %% used.
  127. %% Table for testing floats
  128. ok = mysql:query(Pid, "CREATE TABLE f (f FLOAT)"),
  129. %% Prepared statements
  130. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO f (f) VALUES (?)"),
  131. {ok, Select} = mysql:prepare(Pid, "SELECT f FROM f"),
  132. %% [{Input, Expected}]
  133. TestData = [{1.0, 1.0}, {3.14, 3.14}, {0.2, 0.2},
  134. {0.20082111, 0.200821}, {0.000123456789, 0.000123457},
  135. {33.3333333, 33.3333}, {-33.2233443322, -33.2233},
  136. {400.0123, 400.012}, {1000.1234, 1000.12},
  137. {999.00009, 999.0},
  138. {1234.5678, 1234.57}, {68888.8888, 68888.9},
  139. {123456.789, 123457.0}, {7654321.0, 7654320.0},
  140. {80001111.1, 80001100.0}, {987654321.0, 987654000.0},
  141. {-123456789.0, -123457000.0},
  142. {2.12345111e-23, 2.12345e-23}, {-2.12345111e-23, -2.12345e-23},
  143. {2.12345111e23, 2.12345e23}, {-2.12345111e23, -2.12345e23}],
  144. lists:foreach(fun ({Input, Expected}) ->
  145. %% Insert using binary protocol (sending it as a double)
  146. ok = mysql:execute(Pid, Insert, [Input]),
  147. %% Text (plain query)
  148. {ok, _, [[Value]]} = mysql:query(Pid, "SELECT f FROM f"),
  149. ?assertEqual(Expected, Value),
  150. %% Binary (prepared statement)
  151. {ok, _, [[BinValue]]} = mysql:execute(Pid, Select, []),
  152. ?assertEqual(Expected, BinValue),
  153. %% cleanup before the next test
  154. ok = mysql:query(Pid, "DELETE FROM f")
  155. end,
  156. TestData),
  157. ok = mysql:query(Pid, "DROP TABLE f").
  158. %% Test TIME value representation. There are a few things to check.
  159. time(Pid) ->
  160. ok = mysql:query(Pid, "CREATE TABLE tm (tm TIME)"),
  161. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO tm VALUES (?)"),
  162. {ok, Select} = mysql:prepare(Pid, "SELECT tm FROM tm"),
  163. lists:foreach(
  164. fun ({Value, Text}) ->
  165. %% --- Insert using text query ---
  166. ok = mysql:query(Pid, ["INSERT INTO tm VALUES ('", Text, "')"]),
  167. %% Select using prepared statement
  168. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  169. mysql:execute(Pid, Select, [])),
  170. %% Select using plain query
  171. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  172. mysql:query(Pid, "SELECT tm FROM tm")),
  173. %% Empty table
  174. ok = mysql:query(Pid, "DELETE FROM tm"),
  175. %% --- Insert using prepared statement ---
  176. ok = mysql:execute(Pid, Insert, [Value]),
  177. %% Select using prepared statement
  178. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  179. mysql:execute(Pid, Select, [])),
  180. %% Select using plain query
  181. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  182. mysql:query(Pid, "SELECT tm FROM tm")),
  183. %% Empty table
  184. ok = mysql:query(Pid, "DELETE FROM tm"),
  185. ok
  186. end,
  187. [{{0, {10, 11, 12}}, "10:11:12"},
  188. {{5, {0, 0, 1}}, "120:00:01"},
  189. {{-1, {23, 59, 59}}, "-00:00:01"},
  190. {{-1, {23, 59, 0}}, "-00:01:00"},
  191. {{-1, {23, 0, 0}}, "-01:00:00"},
  192. {{-1, {0, 0, 0}}, "-24:00:00"},
  193. {{-5, {10, 0, 0}}, "-110:00:00"}]
  194. ),
  195. ok = mysql:query(Pid, "DROP TABLE tm").
  196. microseconds(Pid) ->
  197. %% Check whether we have the required version for this testcase.
  198. {ok, _, [[Version]]} = mysql:query(Pid, <<"SELECT @@version">>),
  199. try
  200. %% Remove stuff after dash for e.g. "5.5.40-0ubuntu0.12.04.1-log"
  201. [Version1 | _] = binary:split(Version, <<"-">>),
  202. Version2 = lists:map(fun binary_to_integer/1,
  203. binary:split(Version1, <<".">>, [global])),
  204. Version2 >= [5, 6, 4] orelse throw(nope)
  205. of _ ->
  206. run_test_microseconds(Pid)
  207. catch _:_ ->
  208. error_logger:info_msg("Skipping microseconds test. Current MySQL"
  209. " version is ~s. Required version is >= 5.6.4.~n",
  210. [Version])
  211. end.
  212. run_test_microseconds(Pid) ->
  213. ok = mysql:query(Pid, "CREATE TABLE m (t TIME(6))"),
  214. SelectTime = "SELECT t FROM m",
  215. {ok, SelectStmt} = mysql:prepare(Pid, SelectTime),
  216. {ok, InsertStmt} = mysql:prepare(Pid, "INSERT INTO m VALUES (?)"),
  217. %% Positive time, insert using plain query
  218. E1 = {0, {23, 59, 57.654321}},
  219. ok = mysql:query(Pid, <<"INSERT INTO m VALUES ('23:59:57.654321')">>),
  220. ?assertEqual({ok, [<<"t">>], [[E1]]}, mysql:query(Pid, SelectTime)),
  221. ?assertEqual({ok, [<<"t">>], [[E1]]}, mysql:execute(Pid, SelectStmt, [])),
  222. ok = mysql:query(Pid, "DELETE FROM m"),
  223. %% The same, but insert using prepared stmt
  224. ok = mysql:execute(Pid, InsertStmt, [E1]),
  225. ?assertEqual({ok, [<<"t">>], [[E1]]}, mysql:query(Pid, SelectTime)),
  226. ?assertEqual({ok, [<<"t">>], [[E1]]}, mysql:execute(Pid, SelectStmt, [])),
  227. ok = mysql:query(Pid, "DELETE FROM m"),
  228. %% Negative time
  229. E2 = {-1, {23, 59, 57.654321}},
  230. ok = mysql:query(Pid, <<"INSERT INTO m VALUES ('-00:00:02.345679')">>),
  231. ?assertEqual({ok, [<<"t">>], [[E2]]}, mysql:query(Pid, SelectTime)),
  232. ?assertEqual({ok, [<<"t">>], [[E2]]}, mysql:execute(Pid, SelectStmt, [])),
  233. ok = mysql:query(Pid, "DELETE FROM m"),
  234. %% The same, but insert using prepared stmt
  235. ok = mysql:execute(Pid, InsertStmt, [E2]),
  236. ?assertEqual({ok, [<<"t">>], [[E2]]}, mysql:query(Pid, SelectTime)),
  237. ?assertEqual({ok, [<<"t">>], [[E2]]}, mysql:execute(Pid, SelectStmt, [])),
  238. ok = mysql:query(Pid, "DROP TABLE m"),
  239. %% Datetime
  240. Q3 = <<"SELECT TIMESTAMP '2014-11-23 23:59:57.654321' AS t">>,
  241. E3 = [[{{2014, 11, 23}, {23, 59, 57.654321}}]],
  242. ?assertEqual({ok, [<<"t">>], E3}, mysql:query(Pid, Q3)),
  243. {ok, S3} = mysql:prepare(Pid, Q3),
  244. ?assertEqual({ok, [<<"t">>], E3}, mysql:execute(Pid, S3, [])),
  245. ok.