mysql_tests.erl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. connect_test() ->
  22. {ok, Pid} = mysql:connect([{user, ?user}, {password, ?password}]),
  23. %% A query without a result set
  24. ?assertEqual(ok, mysql:query(Pid, <<"USE otptest">>)),
  25. ?assertEqual(ok, mysql:disconnect(Pid)).
  26. query_test_() ->
  27. {setup,
  28. fun () ->
  29. {ok, Pid} = mysql:connect([{user, ?user}, {password, ?password}]),
  30. %ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  31. %ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  32. ok = mysql:query(Pid, <<"USE otptest">>),
  33. Pid
  34. end,
  35. fun (Pid) ->
  36. mysql:disconnect(Pid)
  37. end,
  38. {with, [fun basic_queries/1]}}.
  39. basic_queries(Pid) ->
  40. %% warning count
  41. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  42. ?assertEqual(1, mysql:warning_count(Pid)),
  43. %% SQL parse error
  44. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  45. mysql:query(Pid, <<"FOO">>)),
  46. %% Simple resultset with various types
  47. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  48. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  49. %{ok, Fields, Rows} = mysql:query(Pid, <<"SELECT * FROM settest">>),
  50. ok.