mysql_tests.erl 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ok.
  77. binary_protocol(Pid) ->
  78. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t">>),
  79. {ok, Cols, Rows} = mysql:query(Pid, Stmt, []),
  80. io:format("Cols: ~p~nRows: ~p~n", [Cols, Rows]),
  81. todo.