mysql_tests.erl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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:connect([{user, ?user}, {password, ?password}]),
  34. ?assertEqual(ok, mysql:disconnect(Pid)).
  35. query_test_() ->
  36. {setup,
  37. fun () ->
  38. {ok, Pid} = mysql:connect([{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. mysql:disconnect(Pid)
  47. end,
  48. {with, [fun basic_queries/1,
  49. fun text_protocol/1,
  50. fun binary_protocol/1]}}.
  51. basic_queries(Pid) ->
  52. %% warning count
  53. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  54. ?assertEqual(1, mysql:warning_count(Pid)),
  55. %% SQL parse error
  56. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  57. mysql:query(Pid, <<"FOO">>)),
  58. %% Simple resultset with various types
  59. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  60. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  61. ok.
  62. text_protocol(Pid) ->
  63. ok = mysql:query(Pid, ?create_table_t),
  64. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  65. " VALUES ('blob', 3.14, 3.14, '00:22:11',"
  66. " '2014-11-03 00:22:24', '2014-11-03',"
  67. " NULL)">>),
  68. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  69. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  70. ?assertEqual(1, mysql:affected_rows(Pid)),
  71. %% select
  72. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  73. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  74. <<"ts">>, <<"da">>, <<"c">>], Columns),
  75. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>, {time, {0, 22, 11}},
  76. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  77. Rows),
  78. %% TODO:
  79. %% * More types: BIT, SET, ENUM, GEOMETRY
  80. %% * TIME with negative hours
  81. %% * TIME with more than 2 digits in hour.
  82. %% * TIME with microseconds
  83. %% * Negative TIME
  84. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  85. binary_protocol(Pid) ->
  86. ok = mysql:query(Pid, ?create_table_t),
  87. %% The same queries as in the text protocol. Expect the same results.
  88. {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  89. " VALUES (?, ?, ?, ?, ?, ?, ?)">>),
  90. ok = mysql:query(Pid, Ins, [<<"blob">>, 3.14, <<"3.14">>,
  91. {time, {0, 22, 11}},
  92. {{2014, 11, 03}, {0, 22, 24}},
  93. {2014, 11, 03}, null]),
  94. %% TODO: Put the expected result in a macro to make sure they are identical
  95. %% for the text and the binary protocol tests.
  96. %% One thing that are not identical in the text and binary protocols are
  97. %% floats. We get the float as a 32-bit float, so the value is not exactly
  98. %% what we inserted.
  99. <<ExpectedFloat:32/native-float>> = <<3.14:32/native-float>>,
  100. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t WHERE id=?">>),
  101. {ok, Columns, Rows} = mysql:query(Pid, Stmt, [1]),
  102. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  103. <<"ts">>, <<"da">>, <<"c">>], Columns),
  104. ?assertEqual([[1, <<"blob">>, <<>>, ExpectedFloat, <<"3.140">>,
  105. {time, {0, 22, 11}},
  106. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  107. Rows),
  108. %% TODO: Both send and receive the following values:
  109. %% * Values for all types
  110. %% * Negative numbers for all integer types
  111. %% * Integer overflow
  112. %% * TIME with more than 2 digits in hour.
  113. %% * TIME with microseconds
  114. %% * Negative TIME
  115. ok = mysql:query(Pid, <<"DROP TABLE t">>).