Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Jun 11, 2024
1 parent 8a98bf2 commit 7825afc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
17 changes: 11 additions & 6 deletions test/myxql/protocol/values_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule MyXQL.Protocol.ValueTest do
use ExUnit.Case, async: true
import Bitwise
import TestHelper, only: [assert_killed: 1]

@default_sql_mode "STRICT_TRANS_TABLES"

Expand Down Expand Up @@ -115,15 +116,19 @@ defmodule MyXQL.Protocol.ValueTest do

if @protocol == :binary do
test "MYSQL_TYPE_TIME - negative time", c do
assert_raise ArgumentError, ~r"cannot decode \"-01:00:00\" as time", fn ->
query!(c, "SELECT TIME(SUBTIME('00:00:00', '01:00:00'))")
end
assert assert_killed(fn ->
assert_raise ArgumentError, ~r"cannot decode \"-01:00:00\" as time", fn ->
query!(c, "SELECT TIME(SUBTIME('00:00:00', '01:00:00'))")
end
end) =~ "stopped: ** (ArgumentError) cannot decode \"-01:00:00\" as time"
end

test "MYSQL_TYPE_TIME - more than 24h", c do
assert_raise ArgumentError, ~r"cannot decode \"1d 01:00:00\" as time", fn ->
query!(c, "SELECT TIME(ADDTIME('23:00:00', '02:00:00'))")
end
assert assert_killed(fn ->
assert_raise ArgumentError, ~r"cannot decode \"1d 01:00:00\" as time", fn ->
query!(c, "SELECT TIME(ADDTIME('23:00:00', '02:00:00'))")
end
end) =~ "stopped: ** (ArgumentError) cannot decode \"1d 01:00:00\" as time"
end
end

Expand Down
65 changes: 29 additions & 36 deletions test/myxql_test.exs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
defmodule MyXQLTest do
use ExUnit.Case, async: true
import ExUnit.CaptureLog
import TestHelper, only: [assert_killed: 1]

@opts TestHelper.opts()
@opts_with_ssl TestHelper.opts_with_ssl()

describe "connect" do
@tag ssl: true
test "connect with bad SSL opts" do
assert capture_log(fn ->
opts = put_in(@opts_with_ssl[:ssl][:ciphers], [:bad])
assert_start_and_killed(opts)
opts = put_in(@opts_with_ssl[:ssl][:ciphers], [:bad])

assert assert_killed(fn ->
{:ok, _} = MyXQL.start_link(opts)
end) =~
"** (DBConnection.ConnectionError) (127.0.0.1:3306) Invalid TLS option: {ciphers,[bad]}"
end

test "connect with host down" do
assert capture_log(fn ->
opts = [port: 9999] ++ @opts
assert_start_and_killed(opts)
opts = [port: 9999] ++ @opts

assert assert_killed(fn ->
{:ok, _} = MyXQL.start_link(opts)
end) =~
"(DBConnection.ConnectionError) (127.0.0.1:9999) connection refused - :econnrefused"
end
Expand Down Expand Up @@ -57,8 +59,8 @@ defmodule MyXQLTest do
|> Keyword.delete(:port)
|> Keyword.merge(socket: "/bad")

assert capture_log(fn ->
assert_start_and_killed(opts)
assert assert_killed(fn ->
{:ok, _} = MyXQL.start_link(opts)
end) =~
"** (DBConnection.ConnectionError) (/bad) no such file or directory - :enoent"
end
Expand Down Expand Up @@ -94,8 +96,8 @@ defmodule MyXQLTest do

opts = [port: port, handshake_timeout: 5] ++ @opts

assert capture_log(fn ->
assert_start_and_killed(opts)
assert assert_killed(fn ->
{:ok, _} = MyXQL.start_link(opts)
end) =~ "timed out because it was handshaking for longer than 5ms"
end
end
Expand Down Expand Up @@ -414,13 +416,11 @@ defmodule MyXQLTest do
end

test "disconnect on errors" do
Process.flag(:trap_exit, true)
{:ok, pid} = MyXQL.start_link([disconnect_on_error_codes: [:ER_DUP_ENTRY]] ++ @opts)

assert capture_log(fn ->
assert assert_killed(fn ->
MyXQL.transaction(pid, fn conn ->
MyXQL.query(conn, "INSERT INTO uniques VALUES (1), (1)")
assert_receive {:EXIT, ^pid, :killed}, 500
end)
end) =~ "disconnected: ** (MyXQL.Error) (1062) "
end
Expand Down Expand Up @@ -713,22 +713,26 @@ defmodule MyXQLTest do
test "stream procedure with multiple results", c do
statement = "CALL multi_procedure()"

assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
Enum.to_list(stream)
end)
end
assert assert_killed(fn ->
assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
MyXQL.transaction(c.conn, fn conn ->
stream = MyXQL.stream(conn, statement, [], max_rows: 2)
Enum.to_list(stream)
end)
end
end) =~ "stopped: ** (RuntimeError) returning multiple results is not supported"
end
end

describe "multiple results" do
setup :connect

test "using query/4 with a multiple result query", c do
assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
MyXQL.query(c.conn, "SELECT 1; SELECT 2;", [], query_type: :text)
end
assert assert_killed(fn ->
assert_raise RuntimeError, ~r"returning multiple results is not supported", fn ->
MyXQL.query(c.conn, "SELECT 1; SELECT 2;", [], query_type: :text)
end
end) =~ "stopped: ** (RuntimeError) returning multiple results is not supported"
end

test "using prepare/4 with a multiple result query", c do
Expand Down Expand Up @@ -804,12 +808,10 @@ defmodule MyXQLTest do
end

test "socket receive timeout" do
Process.flag(:trap_exit, true)
opts = [backoff_type: :stop, idle_interval: 1, ping_timeout: 0] ++ @opts
{:ok, pid} = MyXQL.start_link(opts)

assert capture_log(fn ->
assert_receive {:EXIT, ^pid, :killed}, 500
assert assert_killed(fn ->
{:ok, _} = MyXQL.start_link(opts)
end) =~ "disconnected: ** (DBConnection.ConnectionError) timeout"
end
end
Expand All @@ -823,15 +825,6 @@ defmodule MyXQLTest do
assert %MyXQL.Result{num_warnings: 1, rows: [[nil]]} = MyXQL.query!(conn, "SELECT 1/0")
end

defp assert_start_and_killed(opts) do
Process.flag(:trap_exit, true)

case MyXQL.start_link(opts) do
{:ok, pid} -> assert_receive {:EXIT, ^pid, :killed}, 500
{:error, :killed} -> :ok
end
end

defp connect(c) do
{:ok, conn} = MyXQL.start_link(@opts)
Map.put(c, :conn, conn)
Expand Down
34 changes: 22 additions & 12 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ defmodule TestHelper do
]
end

def setup_server() do
def setup_server do
configure_server()
create_test_database()
create_test_users()
create_test_tables()
end

def configure_server() do
def configure_server do
mysql!("""
-- set packet size to 100mb
SET GLOBAL max_allowed_packet=#{100_000_000};
""")
end

def create_test_database() do
def create_test_database do
mysql!("""
DROP DATABASE IF EXISTS myxql_test;
CREATE DATABASE myxql_test;
""")
end

def create_test_users() do
def create_test_users do
create_user("default_auth", nil, "secret")
create_user("nopassword", nil, nil)
create_user("mysql_native", "mysql_native_password", "secret")
Expand Down Expand Up @@ -76,7 +76,7 @@ defmodule TestHelper do
end
end

def create_test_tables() do
def create_test_tables do
timestamps_with_precision = """
my_time3 TIME(3),
my_time6 TIME(6),
Expand Down Expand Up @@ -189,7 +189,7 @@ defmodule TestHelper do
""")
end

def available_auth_plugins() do
def available_auth_plugins do
sql =
"SELECT plugin_name FROM information_schema.plugins WHERE plugin_type = 'authentication'"

Expand All @@ -198,16 +198,16 @@ defmodule TestHelper do
end
end

def supports_ssl?() do
def supports_ssl? do
mysql!("SELECT @@have_ssl") == [%{"@@have_ssl" => "YES"}]
end

def supports_public_key_exchange?() do
def supports_public_key_exchange? do
result = mysql!("SHOW STATUS LIKE 'Rsa_public_key'")
match?([%{"Value" => "-----BEGIN PUBLIC KEY-----" <> _}], result)
end

def supports_json?() do
def supports_json? do
sql =
"CREATE TEMPORARY TABLE myxql_test.test_json (json json); SHOW COLUMNS IN myxql_test.test_json"

Expand All @@ -221,13 +221,13 @@ defmodule TestHelper do
end
end

def supports_geometry?() do
def supports_geometry? do
# mysql 5.5 does not have ST_GeomFromText (it has GeomFromText) so we're excluding it
# (even though we could test against it) to keep the test suite simpler
match?({:ok, _}, mysql("SELECT ST_GeomFromText('POINT(0 0)')"))
end

def supports_timestamp_precision?() do
def supports_timestamp_precision? do
case mysql("CREATE TEMPORARY TABLE myxql_test.timestamp_precision (time time(3));") do
{:ok, _} -> true
{:error, _} -> false
Expand Down Expand Up @@ -276,7 +276,7 @@ defmodule TestHelper do
end
end

def excludes() do
def excludes do
supported_auth_plugins = [:mysql_native_password, :sha256_password, :caching_sha2_password]
available_auth_plugins = available_auth_plugins()

Expand All @@ -294,6 +294,16 @@ defmodule TestHelper do
exclude = [{:timestamp_precision, not supports_timestamp_precision?()} | exclude]
exclude
end

def assert_killed(fun) do
Process.flag(:trap_exit, true)

ExUnit.CaptureLog.capture_log(fn ->
fun.()
import ExUnit.Assertions
assert_receive {:EXIT, _, :killed}, 500
end)
end
end

TestHelper.setup_server()
Expand Down

0 comments on commit 7825afc

Please sign in to comment.