mysql_tests.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. %% MySQL/OTP – a MySQL driver for Erlang/OTP
  2. %% Copyright (C) 2014 Viktor Söderqvist
  3. %%
  4. %% This program is free software: you can redistribute it and/or modify
  5. %% it under the terms of the GNU General Public License as published by
  6. %% the Free Software Foundation, either version 3 of the License, or
  7. %% (at your option) any later version.
  8. %%
  9. %% This program is distributed in the hope that it will be useful,
  10. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. %% GNU General Public License for more details.
  13. %%
  14. %% You should have received a copy of the GNU General Public License
  15. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. %% @doc This module performs test to an actual database.
  17. -module(mysql_tests).
  18. -include_lib("eunit/include/eunit.hrl").
  19. -define(user, "otptest").
  20. -define(password, "otptest").
  21. -define(create_table_t, <<"CREATE TABLE t ("
  22. " id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,"
  23. " bl BLOB,"
  24. " tx TEXT NOT NULL," %% No default value
  25. " f FLOAT,"
  26. " dc DECIMAL(5,3),"
  27. " ti TIME,"
  28. " ts TIMESTAMP,"
  29. " da DATE,"
  30. " c CHAR(2)"
  31. ") ENGINE=InnoDB">>).
  32. connect_test() ->
  33. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
  34. exit(Pid, normal).
  35. query_test_() ->
  36. {setup,
  37. fun () ->
  38. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
  39. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  40. ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  41. ok = mysql:query(Pid, <<"USE otptest">>),
  42. Pid
  43. end,
  44. fun (Pid) ->
  45. ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
  46. exit(Pid, normal)
  47. end,
  48. {with, [fun basic_queries/1,
  49. fun text_protocol/1,
  50. fun binary_protocol/1,
  51. fun float_rounding/1,
  52. fun time/1,
  53. fun microseconds/1]}}.
  54. basic_queries(Pid) ->
  55. %% warning count
  56. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  57. ?assertEqual(1, mysql:warning_count(Pid)),
  58. %% SQL parse error
  59. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  60. mysql:query(Pid, <<"FOO">>)),
  61. %% Simple resultset with various types
  62. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  63. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  64. ok.
  65. text_protocol(Pid) ->
  66. ok = mysql:query(Pid, ?create_table_t),
  67. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  68. " VALUES ('blob', 3.14, 3.14, '00:22:11',"
  69. " '2014-11-03 00:22:24', '2014-11-03',"
  70. " NULL)">>),
  71. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  72. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  73. ?assertEqual(1, mysql:affected_rows(Pid)),
  74. %% select
  75. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  76. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  77. <<"ts">>, <<"da">>, <<"c">>], Columns),
  78. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>, {0, {0, 22, 11}},
  79. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  80. Rows),
  81. %% TODO:
  82. %% * More types: BIT, SET, ENUM, GEOMETRY
  83. %% * TIME with negative hours
  84. %% * TIME with more than 2 digits in hour.
  85. %% * TIME with microseconds
  86. %% * Negative TIME
  87. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  88. binary_protocol(Pid) ->
  89. ok = mysql:query(Pid, ?create_table_t),
  90. %% The same queries as in the text protocol. Expect the same results.
  91. {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  92. " VALUES (?, ?, ?, ?, ?, ?, ?)">>),
  93. ok = mysql:execute(Pid, Ins, [<<"blob">>, 3.14, <<"3.14">>,
  94. {0, {0, 22, 11}},
  95. {{2014, 11, 03}, {0, 22, 24}},
  96. {2014, 11, 03}, null]),
  97. %% TODO: Put the expected result in a macro to make sure they are identical
  98. %% for the text and the binary protocol tests.
  99. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t WHERE id=?">>),
  100. {ok, Columns, Rows} = mysql:execute(Pid, Stmt, [1]),
  101. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  102. <<"ts">>, <<"da">>, <<"c">>], Columns),
  103. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>,
  104. {0, {0, 22, 11}},
  105. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  106. Rows),
  107. %% TODO: Both send and receive the following values:
  108. %% * Values for all types
  109. %% * Negative numbers for all integer types
  110. %% * Integer overflow
  111. %% * TIME with more than 2 digits in hour.
  112. %% * TIME with microseconds
  113. %% * Negative TIME
  114. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  115. float_rounding(Pid) ->
  116. %% This is to make sure we get the same values for 32-bit FLOATs in the text
  117. %% and binary protocols for ordinary queries and prepared statements
  118. %% respectively.
  119. %%
  120. %% MySQL rounds to 6 significant digits when "printing" floats over the
  121. %% text protocol. When we receive a float on the binary protocol, we round
  122. %% it in the same way to match what MySQL does on the text protocol. This
  123. %% way we should to get the same values regardless of which protocol is
  124. %% used.
  125. %% Table for testing floats
  126. ok = mysql:query(Pid, "CREATE TABLE f (f FLOAT)"),
  127. %% Prepared statements
  128. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO f (f) VALUES (?)"),
  129. {ok, Select} = mysql:prepare(Pid, "SELECT f FROM f"),
  130. %% [{Input, Expected}]
  131. TestData = [{1.0, 1.0}, {3.14, 3.14}, {0.2, 0.2},
  132. {0.20082111, 0.200821}, {0.000123456789, 0.000123457},
  133. {33.3333333, 33.3333}, {-33.2233443322, -33.2233},
  134. {400.0123, 400.012}, {1000.1234, 1000.12},
  135. {999.00009, 999.0},
  136. {1234.5678, 1234.57}, {68888.8888, 68888.9},
  137. {123456.789, 123457.0}, {7654321.0, 7654320.0},
  138. {80001111.1, 80001100.0}, {987654321.0, 987654000.0},
  139. {-123456789.0, -123457000.0},
  140. {2.12345111e-23, 2.12345e-23}, {-2.12345111e-23, -2.12345e-23},
  141. {2.12345111e23, 2.12345e23}, {-2.12345111e23, -2.12345e23}],
  142. lists:foreach(fun ({Input, Expected}) ->
  143. %% Insert using binary protocol (sending it as a double)
  144. ok = mysql:execute(Pid, Insert, [Input]),
  145. %% Text (plain query)
  146. {ok, _, [[Value]]} = mysql:query(Pid, "SELECT f FROM f"),
  147. ?assertEqual(Expected, Value),
  148. %% Binary (prepared statement)
  149. {ok, _, [[BinValue]]} = mysql:execute(Pid, Select, []),
  150. ?assertEqual(Expected, BinValue),
  151. %% cleanup before the next test
  152. ok = mysql:query(Pid, "DELETE FROM f")
  153. end,
  154. TestData),
  155. ok = mysql:query(Pid, "DROP TABLE f").
  156. %% Test TIME value representation. There are a few things to check.
  157. time(Pid) ->
  158. ok = mysql:query(Pid, "CREATE TABLE tm (tm TIME)"),
  159. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO tm VALUES (?)"),
  160. {ok, Select} = mysql:prepare(Pid, "SELECT tm FROM tm"),
  161. lists:foreach(
  162. fun ({Value, Text}) ->
  163. %% --- Insert using text query ---
  164. ok = mysql:query(Pid, ["INSERT INTO tm VALUES ('", Text, "')"]),
  165. %% Select using prepared statement
  166. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  167. mysql:execute(Pid, Select, [])),
  168. %% Select using plain query
  169. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  170. mysql:query(Pid, "SELECT tm FROM tm")),
  171. %% Empty table
  172. ok = mysql:query(Pid, "DELETE FROM tm"),
  173. %% --- Insert using prepared statement ---
  174. ok = mysql:execute(Pid, Insert, [Value]),
  175. %% Select using prepared statement
  176. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  177. mysql:execute(Pid, Select, [])),
  178. %% Select using plain query
  179. ?assertEqual({ok, [<<"tm">>], [[Value]]},
  180. mysql:query(Pid, "SELECT tm FROM tm")),
  181. %% Empty table
  182. ok = mysql:query(Pid, "DELETE FROM tm"),
  183. ok
  184. end,
  185. [{{0, {10, 11, 12}}, "10:11:12"},
  186. {{5, {0, 0, 1}}, "120:00:01"},
  187. {{-1, {23, 59, 59}}, "-00:00:01"},
  188. {{-1, {23, 59, 0}}, "-00:01:00"},
  189. {{-1, {23, 0, 0}}, "-01:00:00"},
  190. {{-1, {0, 0, 0}}, "-24:00:00"},
  191. {{-5, {10, 0, 0}}, "-110:00:00"}]
  192. ),
  193. ok = mysql:query(Pid, "DROP TABLE tm").
  194. microseconds(Pid) ->
  195. %% Check whether we have the required version for this testcase.
  196. {ok, _, [[VersionBin]]} = mysql:query(Pid, <<"SELECT @@version">>),
  197. Version = lists:map(fun binary_to_integer/1,
  198. binary:split(VersionBin, <<".">>, [global])),
  199. if
  200. Version >= [5, 6, 4] ->
  201. ok = mysql:query(Pid, "CREATE TABLE m (t TIME(6))"),
  202. SelectTime = "SELECT t FROM m",
  203. {ok, SelectStmt} = mysql:prepare(Pid, SelectTime),
  204. {ok, InsertStmt} = mysql:prepare(Pid, "INSERT INTO m VALUES (?)"),
  205. %% Positive time, insert using plain query
  206. E1 = {0, {23, 59, 57.654321}},
  207. ok = mysql:query(Pid,
  208. <<"INSERT INTO m VALUES ('23:59:57.654321')">>),
  209. ?assertEqual({ok, [<<"t">>], [[E1]]},
  210. mysql:query(Pid, SelectTime)),
  211. ?assertEqual({ok, [<<"t">>], [[E1]]},
  212. mysql:execute(Pid, SelectStmt, [])),
  213. ok = mysql:query(Pid, "DELETE FROM m"),
  214. %% The same, but insert using prepared stmt
  215. ok = mysql:execute(Pid, InsertStmt, [E1]),
  216. ?assertEqual({ok, [<<"t">>], [[E1]]},
  217. mysql:query(Pid, SelectTime)),
  218. ?assertEqual({ok, [<<"t">>], [[E1]]},
  219. mysql:execute(Pid, SelectStmt, [])),
  220. ok = mysql:query(Pid, "DELETE FROM m"),
  221. %% Negative time
  222. E2 = {-1, {23, 59, 57.654321}},
  223. ok = mysql:query(Pid,
  224. <<"INSERT INTO m VALUES ('-00:00:02.345679')">>),
  225. ?assertEqual({ok, [<<"t">>], [[E2]]},
  226. mysql:query(Pid, SelectTime)),
  227. ?assertEqual({ok, [<<"t">>], [[E2]]},
  228. mysql:execute(Pid, SelectStmt, [])),
  229. ok = mysql:query(Pid, "DELETE FROM m"),
  230. %% The same, but insert using prepared stmt
  231. ok = mysql:execute(Pid, InsertStmt, [E2]),
  232. ?assertEqual({ok, [<<"t">>], [[E2]]},
  233. mysql:query(Pid, SelectTime)),
  234. ?assertEqual({ok, [<<"t">>], [[E2]]},
  235. mysql:execute(Pid, SelectStmt, [])),
  236. ok = mysql:query(Pid, "DROP TABLE m"),
  237. %% Datetime
  238. Q3 = <<"SELECT TIMESTAMP '2014-11-23 23:59:57.654321' AS t">>,
  239. E3 = [[{{2014, 11, 23}, {23, 59, 57.654321}}]],
  240. ?assertEqual({ok, [<<"t">>], E3}, mysql:query(Pid, Q3)),
  241. {ok, S3} = mysql:prepare(Pid, Q3),
  242. ?assertEqual({ok, [<<"t">>], E3}, mysql:execute(Pid, S3, [])),
  243. ok;
  244. true ->
  245. error_logger:info_msg("Skipping microseconds test. Microseconds are"
  246. " not available on MySQL version ~s. Required"
  247. " version is >= 5.6.4.",
  248. [VersionBin])
  249. end.