mysql_tests.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ok = mysql:query(Pid, ?create_table_t),
  43. Pid
  44. end,
  45. fun (Pid) ->
  46. ok = mysql:query(Pid, "DROP TABLE t;"),
  47. mysql:disconnect(Pid)
  48. end,
  49. {with, [fun basic_queries/1, fun text_protocol/1, fun binary_protocol/1]}}.
  50. basic_queries(Pid) ->
  51. %% warning count
  52. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  53. ?assertEqual(1, mysql:warning_count(Pid)),
  54. %% SQL parse error
  55. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  56. mysql:query(Pid, <<"FOO">>)),
  57. %% Simple resultset with various types
  58. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  59. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  60. ok.
  61. text_protocol(Pid) ->
  62. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, dc, ti, ts, da, c)"
  63. " VALUES ('blob', 3.14, 3.14, '00:22:11',"
  64. " '2014-11-03 00:22:24', '2014-11-03',"
  65. " NULL)">>),
  66. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  67. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  68. ?assertEqual(1, mysql:affected_rows(Pid)),
  69. %% select
  70. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  71. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  72. <<"ts">>, <<"da">>, <<"c">>], Columns),
  73. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>, {0, 22, 11},
  74. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  75. Rows),
  76. %% TODO:
  77. %% * More types: BIT, SET, ENUM, GEOMETRY
  78. %% * TIME with negative hours
  79. %% * TIME with more than 2 digits in hour.
  80. %% * TIME with microseconds
  81. ok.
  82. binary_protocol(Pid) ->
  83. %% The same query as in the text protocol. Expect the same result.
  84. %% TODO: Put the expected result in a macro to make sure they are identical.
  85. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t">>),
  86. {ok, Columns, Rows} = mysql:query(Pid, Stmt, []),
  87. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"dc">>, <<"ti">>,
  88. <<"ts">>, <<"da">>, <<"c">>], Columns),
  89. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, <<"3.140">>, {0, 22, 11},
  90. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  91. Rows),
  92. %% TODO: Both send and receive the following values:
  93. %% * Values for all types
  94. %% * Negative numbers for all integer types (even the unsigned ones)
  95. %% * Integer overflow
  96. %% * TIME with negative hours
  97. %% * TIME with more than 2 digits in hour.
  98. %% * TIME with microseconds
  99. todo.