mysql_tests.erl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. basic_queries(Pid) ->
  53. %% warning count
  54. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  55. ?assertEqual(1, mysql:warning_count(Pid)),
  56. %% SQL parse error
  57. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  58. mysql:query(Pid, <<"FOO">>)),
  59. %% Simple resultset with various types
  60. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  61. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  62. ok.
  63. text_protocol(Pid) ->
  64. ok = mysql:query(Pid, ?create_table_t),
  65. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  66. " VALUES ('blob', 3.14, 3.14, '00:22:11',"
  67. " '2014-11-03 00:22:24', '2014-11-03',"
  68. " NULL)">>),
  69. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  70. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  71. ?assertEqual(1, mysql:affected_rows(Pid)),
  72. %% select
  73. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  74. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  75. <<"ts">>, <<"da">>, <<"c">>], Columns),
  76. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>, {time, {0, 22, 11}},
  77. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  78. Rows),
  79. %% TODO:
  80. %% * More types: BIT, SET, ENUM, GEOMETRY
  81. %% * TIME with negative hours
  82. %% * TIME with more than 2 digits in hour.
  83. %% * TIME with microseconds
  84. %% * Negative TIME
  85. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  86. binary_protocol(Pid) ->
  87. ok = mysql:query(Pid, ?create_table_t),
  88. %% The same queries as in the text protocol. Expect the same results.
  89. {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  90. " VALUES (?, ?, ?, ?, ?, ?, ?)">>),
  91. ok = mysql:execute(Pid, Ins, [<<"blob">>, 3.14, <<"3.14">>,
  92. {time, {0, 22, 11}},
  93. {{2014, 11, 03}, {0, 22, 24}},
  94. {2014, 11, 03}, null]),
  95. %% TODO: Put the expected result in a macro to make sure they are identical
  96. %% for the text and the binary protocol tests.
  97. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t WHERE id=?">>),
  98. {ok, Columns, Rows} = mysql:execute(Pid, Stmt, [1]),
  99. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  100. <<"ts">>, <<"da">>, <<"c">>], Columns),
  101. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>,
  102. {time, {0, 22, 11}},
  103. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  104. Rows),
  105. %% TODO: Both send and receive the following values:
  106. %% * Values for all types
  107. %% * Negative numbers for all integer types
  108. %% * Integer overflow
  109. %% * TIME with more than 2 digits in hour.
  110. %% * TIME with microseconds
  111. %% * Negative TIME
  112. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  113. float_rounding(Pid) ->
  114. %% This is to make sure we get the same values for 32-bit FLOATs in the text
  115. %% and binary protocols for ordinary queries and prepared statements
  116. %% respectively.
  117. %%
  118. %% MySQL rounds to 6 significant digits when "printing" floats over the
  119. %% text protocol. When we receive a float on the binary protocol, we round
  120. %% it in the same way to match what MySQL does on the text protocol. This
  121. %% way we should to get the same values regardless of which protocol is
  122. %% used.
  123. %% Table for testing floats
  124. ok = mysql:query(Pid, "CREATE TABLE f (f FLOAT)"),
  125. %% Prepared statements
  126. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO f (f) VALUES (?)"),
  127. {ok, Select} = mysql:prepare(Pid, "SELECT f FROM f"),
  128. %% [{Input, Expected}]
  129. TestData = [{1.0, 1.0}, {3.14, 3.14}, {0.2, 0.2},
  130. {0.20082111, 0.200821}, {0.000123456789, 0.000123457},
  131. {33.3333333, 33.3333}, {-33.2233443322, -33.2233},
  132. {400.0123, 400.012}, {1000.1234, 1000.12},
  133. {999.00009, 999.0},
  134. {1234.5678, 1234.57}, {68888.8888, 68888.9},
  135. {123456.789, 123457.0}, {7654321.0, 7654320.0},
  136. {80001111.1, 80001100.0}, {987654321.0, 987654000.0},
  137. {-123456789.0, -123457000.0},
  138. {2.12345111e-23, 2.12345e-23}, {-2.12345111e-23, -2.12345e-23},
  139. {2.12345111e23, 2.12345e23}, {-2.12345111e23, -2.12345e23}],
  140. lists:foreach(fun ({Input, Expected}) ->
  141. %% Insert using binary protocol (sending it as a double)
  142. ok = mysql:execute(Pid, Insert, [Input]),
  143. %% Text (plain query)
  144. {ok, _, [[Value]]} = mysql:query(Pid, "SELECT f FROM f"),
  145. ?assertEqual(Expected, Value),
  146. %% Binary (prepared statement)
  147. {ok, _, [[BinValue]]} = mysql:execute(Pid, Select, []),
  148. ?assertEqual(Expected, BinValue),
  149. %% cleanup before the next test
  150. ok = mysql:query(Pid, "DELETE FROM f")
  151. end,
  152. TestData),
  153. ok = mysql:query(Pid, "DROP TABLE f").